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 = 0
    for str in strs:
        # 出现{在第一行直接跳过
        if state == 9:
            if str.find("{") == 0:
                state = 1
                continue
        # 判断是否以struct开头和typedef struct开头
        if state == 0 and (str.find("struct") == 0 or str.find("typedef struct") == 0):
            # 判断第一行结尾是否是;如果是就结束不操作
            if str.find(";") != -1:
                state = 0
                readLine = ""
                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 = 0
            writeH(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 outPath
    outPath = 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()

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值