URL: https://linuxfr.org/users/mildred/journaux/script-python-cr%C3%A9ation-automatique-des-fichiers-headers-en-c Title: [Script Python] Création automatique des fichiers headers en C Authors: Mildred Date: 2004年12月27日T22:58:58+01:00 Tags: Score: 0 Voici un peutut script python qui est la réponse à ma question: [http://linuxfr.org/forums/19/5740.html(...)](http://linuxfr.org/forums/19/5740.html) #! /usr/bin/env python # -*- coding: utf-8 -*- # # version 0.1 # ############ ## TODO ## ############ # # [X] Suivre les #include # [ ] Déclarations des variables # [ ] Supprimer la valeur après le "=" # [ ] Fonctions # [ ] permettre la mise en forme des fonctions ne possédant pas de bloc {} # [ ] C++ # [ ] Voir si ca fonctionne # import os, os.path, sys, string, re; SHORT_CONST=1 if 1<len(sys.argv): input_name=sys.argv[1] else: £spaces£ £/spaces£print "Please select a file to create the header" £spaces£ £/spaces£sys.exit(1); if 2<len(sys.argv): output_name=sys.argv[2] else: output_name=string.replace(file+'.h', '.c.h', '.h'); if 3<len(sys.argv): CONST=sys.argv[3] elif SHORT_CONST: CONST="_"+re.sub('[^A-Z]', '_', string.upper(os.path.basename(output_name))) else: CONST="_"+re.sub('[^A-Z]', '_', string.upper(output_name)) os.system('indent -l0 -npsl < '+input_name+' 2>/dev/null > '+output_name); def add_file(filename, list=[]): £spaces£ £/spaces£if not os.access(filename, os.F_OK): £spaces£ £/spaces£list.append("/* Can't find file '"+filename+"' */\n") £spaces£ £/spaces£return list; £spaces£ £/spaces£buffer = open(filename, 'r') £spaces£ £/spaces£while 1: £spaces£ £/spaces£line=buffer.readline(); £spaces£ £/spaces£if len(line) == 0: break; # EOF £spaces£ £/spaces£match=re.match('#[\\s]*include[\\s]+"([^"]+)"', line) £spaces£ £/spaces£if match: £spaces£ £/spaces£list.append("/* "+string.strip(line)+" */\n"); £spaces£ £/spaces£list = add_file(os.path.dirname(filename)+'/'+match.group(1), list) £spaces£ £/spaces£list.append("/* END "+string.strip(line)+" */\n"); £spaces£ £/spaces£else: list.append(line); £spaces£ £/spaces£buffer.close() £spaces£ £/spaces£return list lines = add_file(output_name) buffer = open(output_name, 'w') buffer.write("#ifndef "+CONST+"\n#define "+CONST+"\n\n"); # for line in lines: print string.rstrip(line); i=0 # line='//' len_lines=len(lines) while i < len_lines: £spaces£ £/spaces£line=string.rstrip(lines[i]); i+=1 £spaces£ £/spaces£nudeline=string.rstrip(line.split('//')[0]) £spaces£ £/spaces£if len(nudeline) and nudeline[-1]==")" and i<len_lines and len(lines[i]) and lines[i][0]=="{": £spaces£ £/spaces£buffer.write(line+";\n") £spaces£ £/spaces£# Entering function block code £spaces£ £/spaces£i+=1 £spaces£ £/spaces£while i < len_lines: £spaces£ £/spaces£line=string.rstrip(lines[i]); i+=1 £spaces£ £/spaces£if len(line) and line[0]=="}": break £spaces£ £/spaces£elif len(line): £spaces£ £/spaces£buffer.write(line+"\n") buffer.write("\n#endif\n"); buffer.close();