python把c语言的.h文件转为c++的.cpp和.h文件
生活随笔
收集整理的這篇文章主要介紹了
python把c语言的.h文件转为c++的.cpp和.h文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
把c轉(zhuǎn)為c++對象
c文件內(nèi)容
typedef struct ast_value_t {ast_metadata meta;ast_value_data data;ast_value_type type; };轉(zhuǎn)為的內(nèi)容
cpp文件內(nèi)容
ast_value_t:: ast_value_t() {return; }ast_value_t :: ~ast_value_t() {return; }h文件內(nèi)容
class ast_value_t {public:ast_value_t();~ast_value_t();private:ast_metadata meta;ast_value_data data;ast_value_type type; };python腳本內(nèi)容
#!/usr/bin/python # -*- coding: UTF-8 -*- # 輸出文件地址 import sys outPath = "/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 = ""# 默認狀態(tài)state = 0for str in strs:# 出現(xiàn){在第一行直接跳過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):# 判斷第一行結(jié)尾是否是;如果是就結(jié)束不操作if str.find(";") != -1:state = 0readLine = ""continue# 修改狀態(tài)為在寫入state = 1# 分割字符串獲取類名稱data = str.partition("struct")# print "內(nèi)容是", data[2]# 如果第一行沒有以{開頭說明在下一行以{開頭所以給個跳過狀態(tài)if str.find("{") == -1:state = 9# 開始讀取類名稱equation = data[2].partition("{")# 寫入.h文件writeH(fH,1,equation[0].strip())# 寫入.cpp文件writeCpp(fCpp,equation[0].strip())# 如果是正在寫入狀態(tài),并且第一行不是}說明還在寫,所以讀取elif state == 1 and str.find("}") != 0:readLine += "\t\t" + str.strip()+"\n"# 如果是正在寫入狀態(tài),并且第一行是}說明寫完,所以狀態(tài)清空并寫入記載內(nèi)容elif state == 1 and str.find("}") == 0:state = 0writeH(fH,2,readLine)readLine = ""print "轉(zhuǎn)換成功!"# 關(guān)閉打開的文件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)# 修改導(dǎo)出路徑 def editOutPath():global outPathoutPath = raw_input("請輸入導(dǎo)出置路徑:")print "修改成功!"# 修改導(dǎo)出路徑 def convertFile():path = raw_input("請輸入文件路徑:")name = raw_input("請輸入導(dǎo)出后文件名稱:")analysis(path,name)# 結(jié)束 def close():print "再見"sys.exit()def playWrite():print "---------- 歡迎進入執(zhí)行系統(tǒng) ---------------"print "1. 修改導(dǎo)出地址\n2. 開始轉(zhuǎn)換文件\n0. 退出"runLang = raw_input("請輸入:")switch = {'1': editOutPath,'2': convertFile,'0': close}switch.get(runLang)()playWrite()playWrite()博主認為網(wǎng)上會有對應(yīng)文章,搜索很長時間都沒有找到,博主只好自己動手,希望博主這篇文章可以幫助到需要的人
總結(jié)
以上是生活随笔為你收集整理的python把c语言的.h文件转为c++的.cpp和.h文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抖音是什么?抖音短视频怎么用
- 下一篇: Intel七代酷睿i7-7700K性能测