日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/9/19 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 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 = ""# 默認狀態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()

博主認為網上會有對應文章,搜索很長時間都沒有找到,博主只好自己動手,希望博主這篇文章可以幫助到需要的人

總結

以上是生活随笔為你收集整理的python把c语言的.h文件转为c++的.cpp和.h文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。