300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python把c语言的.h文件转为c++的.cpp和.h文件

python把c语言的.h文件转为c++的.cpp和.h文件

时间:2020-12-16 17:30:57

相关推荐

python把c语言的.h文件转为c++的.cpp和.h文件

把c转为c++对象

c文件内容

typedef struct ast_value_t{ast_metadata meta;ast_value_data data;ast_value_type type;};

转为的内容

cpp文件内容

ast_value_t:: ast_value_t(){return;}ast_value_t :: ~ast_value_t(){return;}

h文件内容

class ast_value_t{public:ast_value_t();~ast_value_t();private:ast_metadata meta;ast_value_data data;ast_value_type type;};

python脚本内容

#!/usr/bin/python# -*- coding: UTF-8 -*-# 输出文件地址import sysoutPath = "/home/"# 打开一个文件(文件地址,输出的文件名称)def analysis(filePath,fileName):# 需要写入的.h文件fH = open(outPath + fileName + ".h",'w+')# 需要写入的.cpp文件fCpp = open(outPath + fileName + ".cpp",'w+')# 要读取的文件file = open(filePath, 'r')strs = file.readlines()readLine = ""# 默认状态state = 0for str in strs:# 出现{在第一行直接跳过if state == 9:if str.find("{") == 0:state = 1continue# 判断是否以struct开头和typedef struct开头if state == 0 and (str.find("struct") == 0 or str.find("typedef struct") == 0):# 判断第一行结尾是否是;如果是就结束不操作if str.find(";") != -1:state = 0readLine = ""continue# 修改状态为在写入state = 1# 分割字符串获取类名称data = str.partition("struct")# print "内容是", data[2]# 如果第一行没有以{开头说明在下一行以{开头所以给个跳过状态if str.find("{") == -1:state = 9# 开始读取类名称equation = data[2].partition("{")# 写入.h文件writeH(fH,1,equation[0].strip())# 写入.cpp文件writeCpp(fCpp,equation[0].strip())# 如果是正在写入状态,并且第一行不是}说明还在写,所以读取elif state == 1 and str.find("}") != 0:readLine += "\t\t" + str.strip()+"\n"# 如果是正在写入状态,并且第一行是}说明写完,所以状态清空并写入记载内容elif state == 1 and str.find("}") == 0:state = 0writeH(fH,2,readLine)readLine = ""print "转换成功!"# 关闭打开的文件file.close()fH.close()fCpp.close()# 写文.h文件# 1表示class,2表示private开始def writeH(file,type,content):bodyContent = ""if type == 1:bodyContent = ("\n\nclass %s\n{\n\tpublic:\n\t\t%s();\n\t\t~%s();" %(content,content,content))elif type == 2:bodyContent = ("\n\tprivate:\n%s};" %content)file.write(bodyContent)# 写文.cpp文件def writeCpp(file,content):bodyContent = ("\n%s :: %s ()\n{\n\treturn;\n}\n\n%s :: ~%s ()\n{\n\treturn;\n}\n" %(content,content,content,content))file.write(bodyContent)# 修改导出路径def editOutPath():global outPathoutPath = raw_input("请输入导出置路径:")print "修改成功!"# 修改导出路径def convertFile():path = raw_input("请输入文件路径:")name = raw_input("请输入导出后文件名称:")analysis(path,name)# 结束def close():print "再见"sys.exit()def playWrite():print "---------- 欢迎进入执行系统 ---------------"print "1. 修改导出地址\n2. 开始转换文件\n0. 退出"runLang = raw_input("请输入:")switch = {'1': editOutPath,'2': convertFile,'0': close}switch.get(runLang)()playWrite()playWrite()

博主认为网上会有对应文章,搜索很长时间都没有找到,博主只好自己动手,希望博主这篇文章可以帮助到需要的人

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。