1: get_file.sh

#!/bin/sh

code_path="/root/utHash"
files_name="file_path.cfg"
cfg_file_path="./${files_name}"
if [ -e $cfg_file_path ]
then
	rm $cfg_file_path
fi

find $code_path -depth -name "*.c" > $cfg_file_path
find $code_path -depth -name "*.h" >> $cfg_file_path

2:

import re
import os

struct_vals = []
cfg_file_name = 'file_path.cfg'
struct_file_name = 'struct.c'
rex = '(struct\s+(\w+)\s+\{(?:\s+(?:struct\s+)?\w+\s+\w+;)+\s+\})'
#rex = 'struct\s+(\w+)\s+{(\s+\w+\s+\w+;\s+)+\s+}'

def output_c_file(struct_val_list):
	with open(struct_file_name, 'w') as fd_struct:
		for i in struct_val_list:
			fd_struct.write('/* struct ' + i[1] + ' */\n')
			fd_struct.write(i[0])
			fd_struct.write('\n')

with open(cfg_file_name, 'r') as fd_path:
    file_path = fd_path.readline()
    while file_path <> "":
        with open(file_path.replace("\n", ""), 'r') as fd_file:
            buf = fd_file.read()
            match_result = re.findall(rex, buf)
            if match_result <> []:
                print(match_result)
                for i in match_result:
                    print(i)
                    if i not in struct_vals:
                        struct_vals.append(i)
        file_path = fd_path.readline()
output_c_file(struct_vals)
		
print(struct_vals)

转载于:https://my.oschina.net/u/2990965/blog/1832487

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐