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

1)上代码test.py

import os
import sys
import re

# like ./stringstream_output.py ./test
SOURCE_PATH=""
FUNCTION="\tfriend TSCOMMON_EXPORT stringstream& operator<<(stringstream& ss, const "
OUTSTREAM=""
ERRNO=0
EXP_WORDS=re.compile(r'\w+')
results=[]

if (len(sys.argv) < 2):
	ERRNO=1
	exit (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):
			continue
		start = False
		NEW_TXT_STRING=""
		FUNCTIOIN_STRING=""
		source_file_name=SOURCE_PATH
		source_file_name+='//'
		source_file_name+=file
		new_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 += line
						print "========================"
						FUNCTIOIN_STRING+=FUNCTION
						FUNCTIOIN_STRING+=results[1]
						FUNCTIOIN_STRING+="& token)"
						FUNCTIOIN_STRING+="\t\n"
						continue

			
			if (line.find('{')!=-1):
				print "111111111111111111111111111111"
				NEW_TXT_STRING += line
				lineNo = 0
				start = True
			elif (line.find('}')!=-1):
				FUNCTIOIN_STRING += ";\n\t\treturn ss;\n\t}\n"
				NEW_TXT_STRING += FUNCTIOIN_STRING
				NEW_TXT_STRING += line
				FUNCTIOIN_STRING = ""
				start = False
			elif (line.find('//')!=-1):
				NEW_TXT_STRING += line
			else:
				NEW_TXT_STRING += line
				if (start == True):
					results = EXP_WORDS.findall(line)
					if (len(results) != 2):
						continue
					else:
						lineNo = lineNo + 1
						if (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 line
							print "-------------------"
							FUNCTIOIN_STRING+=":\" << token."
							FUNCTIOIN_STRING+=results[1]
		else:
			new_fd.write(NEW_TXT_STRING)
			new_fd.close()

2)源文件里的内容为:

///信息分发
struct CThostFtdcDisseminationField
{
	///序列系列号
	TThostFtdcSequenceSeriesType	SequenceSeries;
	///序列号
	TThostFtdcSequenceNoType	SequenceNo;
};

///用户登录请求
struct CThostFtdcReqUserLoginField
{
	///交易日
	TThostFtdcDateType	TradingDay;
	///经纪公司代码
	TThostFtdcBrokerIDType	BrokerID;
	///用户代码
	TThostFtdcUserIDType	UserID;
	///密码
	TThostFtdcPasswordType	Password;
	///用户端产品信息
	TThostFtdcProductInfoType	UserProductInfo;
	///接口端产品信息
	TThostFtdcProductInfoType	InterfaceProductInfo;
	///协议信息
	TThostFtdcProtocolInfoType	ProtocolInfo;
	///Mac地址
	TThostFtdcMacAddressType	MacAddress;
	///动态密码
	TThostFtdcPasswordType	OneTimePassword;
	///终端IP地址
	TThostFtdcIPAddressType	ClientIPAddress;
};

./test.py test

之后

///信息分发
struct CThostFtdcDisseminationField
{
	///序列系列号
	TThostFtdcSequenceSeriesType	SequenceSeries;
	///序列号
	TThostFtdcSequenceNoType	SequenceNo;
	friend TSCOMMON_EXPORT stringstream& operator<<(stringstream& ss, const CThostFtdcDisseminationField& token)	
	{
		ss << "SequenceSeries:" << token.SequenceSeries << "|SequenceNo:" << token.SequenceNo;
		return ss;
	}
};

///用户登录请求
struct CThostFtdcReqUserLoginField
{
	///交易日
	TThostFtdcDateType	TradingDay;
	///经纪公司代码
	TThostFtdcBrokerIDType	BrokerID;
	///用户代码
	TThostFtdcUserIDType	UserID;
	///密码
	TThostFtdcPasswordType	Password;
	///用户端产品信息
	TThostFtdcProductInfoType	UserProductInfo;
	///接口端产品信息
	TThostFtdcProductInfoType	InterfaceProductInfo;
	///协议信息
	TThostFtdcProtocolInfoType	ProtocolInfo;
	///Mac地址
	TThostFtdcMacAddressType	MacAddress;
	///动态密码
	TThostFtdcPasswordType	OneTimePassword;
	///终端IP地址
	TThostFtdcIPAddressType	ClientIPAddress;
	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)

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

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

你可能感兴趣的:(自动化写代码,python)