• [^] # Re: Bug

    Posté par (site web personnel) . En réponse au journal [Script Python] Création automatique des fichiers headers en C. Évalué à 1.

    Il suffit de rajouter -fc1 à indent:
    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    # 
    # version 0.1
    # 
    ############
    ## 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
    # 
    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)
    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(line+";\n")
    		# 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(line+"\n")
    buffer.write("\n#endif\n");
    buffer.close();