从自动生成.h的头文件集合和类声明集合到用python读写文件

Posted 后营马族子弟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从自动生成.h的头文件集合和类声明集合到用python读写文件相关的知识,希望对你有一定的参考价值。

最近在用python自动生成c++的类.因为这些类会根据需求不同产生不同的类,所以需要用python自动生成.由于会产生大量的类,而且这些类是变化的.所以如果是在某个.h中要用include来加载这些类,会累死人的.所以用python来生成这些类的头文件引用和类的类名声明

先看例子,再聊python的读写文件的代码 在聊聊我的python代码

技术分享------------------------>技术分享

好吧.上面的图就是面临的需求

下面来聊聊从网上找的读写文件的python代码吧.csdn的一个博主写的很详细.  python如何读写文件:http://blog.csdn.net/adupt/article/details/4435615

仔细阅读完博主的讲解后,有这么一段

file_object = open(‘thefile.txt‘)
技术分享try:
技术分享     all_the_text = file_object.read( )
技术分享finally:
技术分享     file_object.close( )

针对上面的这段代码我会在来个说明 关于 try except finally with 等的帖子 <python中的 try except finally with>

恩.好了,下面把我的python代码也就是实现最开始的截图的代码放这里 供自己以后类似的情况来参考

 1 #!/usr/bin/python                                   #此文件是自动生成头文件声明的python脚本
 2 # -*- encoding: utf-8 -*-
 3 import os
 4 
 5 def generateRecordsHeaderInclude(folderPath):
 6     
 7     try:
 8         filePath = os.path.join(folderPath,"misc_records.h")
 9         with open(filePath, "w") as file:
10             recordsFolderPath = os.path.join(folderPath, records) #E:\fare_uuid\misc\include\misc\records  存放生成的类的.h
11             includeNames = os.listdir(recordsFolderPath)  #获取records文件夹下的所有文件名
12             #写c++代码
13             file.write("#ifndef MISC_MISC_RECORDS_H_\n")
14             file.write("#define MISC_MISC_RECORDS_H_\n")
15             file.write("\n//Records\n")
16             
17             for includeName in includeNames:
18                 file.write(#include "misc/records/%s"\n % includeName)
19 
20             file.write("#endif /* MISC_MISC_RECORDS_H_ */") 
21     except:
22         print "create file %s\\misc_records.h error" % folderPath
23         return
24 
25     print "create file %s\\misc_records.h success!" % folderPath
26     
27     
28 
29 if __name__ == __main__:
30     homedir = r"E:\farestar_uuid\misc"   
31     folderPath = os.path.join(homedir, include, misc)
32     print folderPath =  + folderPath
33     
34     generateRecordsHeaderInclude(folderPath)  #E:\farestar_uuid\misc\include\misc

 

 1 #!/usr/bin/python                                       #此文件是自动生成c++类的类名声明的python脚本
 2 # -*- encoding: utf-8 -*-
 3 import os
 4 
 5 def getStructName(recFilePath, fwdFilePath):
 6     try:
 7         with open(recFilePath, r) as file:
 8             for line in file:
 9                 if struct in line and : public MiscObject in line:
10                     words = line.split(" ")
11                     structIndex = words.index(struct)    # 类名里面是    struct xxx : public MiscObject 所以读到这一行的时候就拆解处 类名来
12                     return words[structIndex+1]             
13     except:
14         print read %s error % filePath
15         print create %s error % fwdFilePath
16         raise RuntimeError("create %s error in genRecordsStructDeclare.py" % fwdFilePath)
17         
18     
19 def generateRecordsStructDeclare(folderPath):
20     try:
21         fwdFilePath = os.path.join(folderPath,"misc_fwd.h")  #类名声明的头文件
22         with open(fwdFilePath, "w") as file:
23             recordsFolderPath = os.path.join(folderPath, records)  #所以的类名的.h文件在records文件夹下
24             recFileNames = os.listdir(recordsFolderPath)
25 
26             
27             file.write("#ifndef MISC_MISC_FWD_H_\n")
28             file.write("#define MISC_MISC_FWD_H_\n")
29 
30             file.write(#include "misc/miscid.h"\n)
31             file.write(#include "misc/miscerrorcode.h"\n)
32             file.write(namespace misc\n)
33             file.write({\n)
34             for recFileName in recFileNames:
35                 recFilePath = os.path.join(recordsFolderPath, recFileName)  #xxxx/recors/agency.h
36                 structName = getStructName( recFilePath,  fwdFilePath)
37                 file.write(    struct %s;\n % structName)  #生成一行类名声明
38             file.write(}\n)
39             
40             #file.write(‘#include "misc/records/%s"\n‘ % includeName)
41 
42             file.write("#endif /* MISC_MISC_FWD_H_ */") 
43     except:
44         print "create file %s error" % fwdFilePath
45         return
46 
47     print "create file %s success!" % fwdFilePath
48     
49     
50 
51 if __name__ == __main__:
52     homedir = r"E:\farestar_uuid\misc"
53     folderPath = os.path.join(homedir, include, misc)
54     print folderPath =  + folderPath
55     
56     generateRecordsHeaderInclude(folderPath)

 

以上是关于从自动生成.h的头文件集合和类声明集合到用python读写文件的主要内容,如果未能解决你的问题,请参考以下文章

如何从 H.264 帧和音频帧的集合创建 mp4 文件?

使用集合视图时声明目录

JavaSE(面向对象之类和类的成员)

集合及特殊集合

Cocoa 线程和类声明连接

集合arraylist