自动实现结构体打印--用python

来源:互联网 发布:江阴网络大专 编辑:程序博客网 时间:2024/05/01 15:58

1)上代码test.py

import osimport sysimport re# like ./stringstream_output.py ./testSOURCE_PATH=""FUNCTION="\tfriend TSCOMMON_EXPORT stringstream& operator<<(stringstream& ss, const "OUTSTREAM=""ERRNO=0EXP_WORDS=re.compile(r'\w+')results=[]if (len(sys.argv) < 2):ERRNO=1exit (ERRNO)SOURCE_PATH=sys.argv[1]print "---------------------------"for root,dirs,files in os.walk(SOURCE_PATH):for file in files:if (file.find('.new')!=-1):continuestart = FalseNEW_TXT_STRING=""FUNCTIOIN_STRING=""source_file_name=SOURCE_PATHsource_file_name+='//'source_file_name+=filenew_file_name = source_file_name + '.new'fd = open(source_file_name,"r")new_fd = open(new_file_name, "w")for line in fd.readlines():if (line.find('struct')!=-1):results = EXP_WORDS.findall(line)if (len(results) != 0):if (results[0] == 'struct'):NEW_TXT_STRING += lineprint "========================"FUNCTIOIN_STRING+=FUNCTIONFUNCTIOIN_STRING+=results[1]FUNCTIOIN_STRING+="& token)"FUNCTIOIN_STRING+="\t\n"continueif (line.find('{')!=-1):print "111111111111111111111111111111"NEW_TXT_STRING += linelineNo = 0start = Trueelif (line.find('}')!=-1):FUNCTIOIN_STRING += ";\n\t\treturn ss;\n\t}\n"NEW_TXT_STRING += FUNCTIOIN_STRINGNEW_TXT_STRING += lineFUNCTIOIN_STRING = ""start = Falseelif (line.find('//')!=-1):NEW_TXT_STRING += lineelse:NEW_TXT_STRING += lineif (start == True):results = EXP_WORDS.findall(line)if (len(results) != 2):continueelse:lineNo = lineNo + 1if (lineNo == 1):FUNCTIOIN_STRING+="\t{"FUNCTIOIN_STRING+="\n"FUNCTIOIN_STRING+="\t\t"FUNCTIOIN_STRING+="ss << \""FUNCTIOIN_STRING+=results[1]FUNCTIOIN_STRING+=":\" << token."FUNCTIOIN_STRING+=results[1]else:if (lineNo % 2 != 0):FUNCTIOIN_STRING+="\n\t\t"FUNCTIOIN_STRING+=" << \"|"FUNCTIOIN_STRING+=results[1]print "-------------------"print lineprint "-------------------"FUNCTIOIN_STRING+=":\" << token."FUNCTIOIN_STRING+=results[1]else:new_fd.write(NEW_TXT_STRING)new_fd.close()

2)源文件里的内容为:

///信息分发struct CThostFtdcDisseminationField{///序列系列号TThostFtdcSequenceSeriesTypeSequenceSeries;///序列号TThostFtdcSequenceNoTypeSequenceNo;};///用户登录请求struct CThostFtdcReqUserLoginField{///交易日TThostFtdcDateTypeTradingDay;///经纪公司代码TThostFtdcBrokerIDTypeBrokerID;///用户代码TThostFtdcUserIDTypeUserID;///密码TThostFtdcPasswordTypePassword;///用户端产品信息TThostFtdcProductInfoTypeUserProductInfo;///接口端产品信息TThostFtdcProductInfoTypeInterfaceProductInfo;///协议信息TThostFtdcProtocolInfoTypeProtocolInfo;///Mac地址TThostFtdcMacAddressTypeMacAddress;///动态密码TThostFtdcPasswordTypeOneTimePassword;///终端IP地址TThostFtdcIPAddressTypeClientIPAddress;};

./test.py test

之后

///信息分发struct CThostFtdcDisseminationField{///序列系列号TThostFtdcSequenceSeriesTypeSequenceSeries;///序列号TThostFtdcSequenceNoTypeSequenceNo;friend TSCOMMON_EXPORT stringstream& operator<<(stringstream& ss, const CThostFtdcDisseminationField& token){ss << "SequenceSeries:" << token.SequenceSeries << "|SequenceNo:" << token.SequenceNo;return ss;}};///用户登录请求struct CThostFtdcReqUserLoginField{///交易日TThostFtdcDateTypeTradingDay;///经纪公司代码TThostFtdcBrokerIDTypeBrokerID;///用户代码TThostFtdcUserIDTypeUserID;///密码TThostFtdcPasswordTypePassword;///用户端产品信息TThostFtdcProductInfoTypeUserProductInfo;///接口端产品信息TThostFtdcProductInfoTypeInterfaceProductInfo;///协议信息TThostFtdcProtocolInfoTypeProtocolInfo;///Mac地址TThostFtdcMacAddressTypeMacAddress;///动态密码TThostFtdcPasswordTypeOneTimePassword;///终端IP地址TThostFtdcIPAddressTypeClientIPAddress;friend TSCOMMON_EXPORT stringstream& operator<<(stringstream& ss, const CThostFtdcReqUserLoginField& token){ss << "TradingDay:" << token.TradingDay << "|BrokerID:" << token.BrokerID << "|UserID:" << token.UserID << "|Password:" << token.Password << "|UserProductInfo:" << token.UserProductInfo << "|InterfaceProductInfo:" << token.InterfaceProductInfo << "|ProtocolInfo:" << token.ProtocolInfo << "|MacAddress:" << token.MacAddress << "|OneTimePassword:" << token.OneTimePassword << "|ClientIPAddress:" << token.ClientIPAddress;return ss;}};

3)

程序虽小,但是当你在接第三方库的时候,你又要保存大量的信息,不要告诉我,你会没结构体重都做一次输出的函数的重写。

亲,把这个繁琐而且极有规律的事情交给计算机吧。。相信我,它比你完成的好

原创粉丝点击