problème: ca ne compile pas lorsque les fonctions ont le mot clef "static" au début:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# version 0.2
#
############
## TODO ##
############
#
# [X] Suivre les #include
# [ ] Déclarations des variables
# [ ] Suprimer la valeur après le "="
# [ ] Fonctions
# [ ] permettre la mise en forme des fonctions ne possédant pas de bloc {}
# [ ] C++
# [ ] Voir si ca fonctionne
# [X] Bugs
# [X] Lorsque du code contenant "}" est commenté. Solution: rajouter "-fc1" à indent
# [X] Ca ne compile pas lorsque le mot "static" apparait au début des lignes
#
import os, os.path, sys, string, re;
SHORT_CONST=1
if 1<len(sys.argv): input_name=sys.argv[1]
else:
print "Please select a file to create the header"
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 -fc1 < '+input_name+' 2>/dev/null > '+output_name);
def add_file(filename, list=[]):
if not os.access(filename, os.F_OK):
list.append("/* Can't find file '"+filename+"' */\n")
return list;
buffer = open(filename, 'r')
while 1:
line=buffer.readline();
if len(line) == 0: break; # EOF
match=re.match('#[\\s]*include[\\s]+"([^"]+)"', line)
if match:
list.append("/* "+string.strip(line)+" */\n");
list = add_file(os.path.dirname(filename)+'/'+match.group(1), list)
list.append("/* END "+string.strip(line)+" */\n");
else: list.append(line);
buffer.close()
return list
lines = add_file(output_name)
def make_line(line):
if line[0:6]=="static": line = line[7:]
return line+"\n";
buffer = open(output_name, 'w')
buffer.write("#ifndef "+CONST+"\n#define "+CONST+"\n\n");
i=0
len_lines=len(lines)
while i < len_lines:
line=string.rstrip(lines[i]); i+=1
nudeline=string.rstrip(line.split('//')[0])
if len(nudeline) and nudeline[-1]==")" and i<len_lines and len(lines[i]) and lines[i][0]=="{":
buffer.write(make_line(line+";"))
# Entering function block code
i+=1
while i < len_lines:
line=string.rstrip(lines[i]); i+=1
if len(line) and line[0]=="}": break
elif len(line):
buffer.write(make_line(line))
buffer.write("\n#endif\n");
buffer.close();
[^] # Re: Bug
Posté par Mildred (site web personnel) . En réponse au journal [Script Python] Création automatique des fichiers headers en C. Évalué à 1.
#! /usr/bin/env python # -*- coding: utf-8 -*- # # version 0.2 # ############ ## TODO ## ############ # # [X] Suivre les #include # [ ] Déclarations des variables # [ ] Suprimer la valeur après le "=" # [ ] Fonctions # [ ] permettre la mise en forme des fonctions ne possédant pas de bloc {} # [ ] C++ # [ ] Voir si ca fonctionne # [X] Bugs # [X] Lorsque du code contenant "}" est commenté. Solution: rajouter "-fc1" à indent # [X] Ca ne compile pas lorsque le mot "static" apparait au début des lignes # import os, os.path, sys, string, re; SHORT_CONST=1 if 1<len(sys.argv): input_name=sys.argv[1] else: print "Please select a file to create the header" 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 -fc1 < '+input_name+' 2>/dev/null > '+output_name); def add_file(filename, list=[]): if not os.access(filename, os.F_OK): list.append("/* Can't find file '"+filename+"' */\n") return list; buffer = open(filename, 'r') while 1: line=buffer.readline(); if len(line) == 0: break; # EOF match=re.match('#[\\s]*include[\\s]+"([^"]+)"', line) if match: list.append("/* "+string.strip(line)+" */\n"); list = add_file(os.path.dirname(filename)+'/'+match.group(1), list) list.append("/* END "+string.strip(line)+" */\n"); else: list.append(line); buffer.close() return list lines = add_file(output_name) def make_line(line): if line[0:6]=="static": line = line[7:] return line+"\n"; buffer = open(output_name, 'w') buffer.write("#ifndef "+CONST+"\n#define "+CONST+"\n\n"); i=0 len_lines=len(lines) while i < len_lines: line=string.rstrip(lines[i]); i+=1 nudeline=string.rstrip(line.split('//')[0]) if len(nudeline) and nudeline[-1]==")" and i<len_lines and len(lines[i]) and lines[i][0]=="{": buffer.write(make_line(line+";")) # Entering function block code i+=1 while i < len_lines: line=string.rstrip(lines[i]); i+=1 if len(line) and line[0]=="}": break elif len(line): buffer.write(make_line(line)) buffer.write("\n#endif\n"); buffer.close();