首页 > 代码库 > Python自動生成.c .h 文件模板
Python自動生成.c .h 文件模板
# Auto File Creator for C.
# Author:J.Hu
# Date: 2014/10/13
#! /usr/bin/python
# import file
import os
import string
import time
#from time import gmtime,strftime
## Common Header Comment
str_head = "/*****************************************************";
str_author = "** Author: J.Hu";
str_date = "** Date: "+strftime("%Y-%m-%d", gmtime());
str_copyright="** Copyright(c) All Rights Reserved.";
str_head_end= "*****************************************************/";
str_headtext = (str_head, str_author, str_date, str_copyright, str_head_end);
## Source FIle
strc_include = "/***** Include files ********************************/";
strc_const = "/***** Define constant ******************************/";
strc_struct = "/***** Define structure *****************************/";
strc_protype = "/***** Define Prototype of functions ****************/";
strc_global = "/***** Define global ********************************/";
str_cfile = (strc_include, strc_const, strc_struct, strc_protype, strc_global);
## Header File
strh_include = "/***** INCLUDES *************************************/";
strh_typedef = "/***** TYPEDEFS *************************************/";
strh_def = "/***** DEFINES **************************************/";
strh_ex_var = "/***** EXTERNAL VARIABLES ***************************/";
strh_ex_fun = "/***** EXTERNAL FUNCTIONS ***************************/";
strh_var = "/***** VARIABLES ************************************/";
strh_fun = "/***** PROTOTYPES ***********************************/";
strh_const = "/***** CONSTANTS ************************************/";
str_hfile = (strh_include, strh_typedef, strh_def, strh_ex_var, strh_ex_fun, strh_var, strh_fun, strh_const);
# Creat C File.
def file_init_c(src):
fp = open(src,‘w‘);
for text in str_headtext:
fp.write(text+"\n");
for text in str_cfile:
fp.write(text+"\r\n");
fp.close();
# Creat H File
def file_init_h(src, file_name):
fp = open(src,‘w‘);
for text in str_headtext:
fp.write(text+"\n");
text = "#ifndef "+"_"+file_name+"_H_"+"\r"
fp.write(text);
text = "#define "+"_"+file_name+"_H_"+"\r"
fp.write(text);
for text in str_hfile:
fp.write(text+"\r\n");
text = "#endif /* "+"_"+file_name+"_H_"+" */\r\n";
fp.write(text);
fp.close();
def make_file(src):
#is Exist:
if os.path.exists(src):
print "path:[%s] is exist" %src;
return (False,0);
#Get File Path and File
file_path,file_namefull = os.path.split(src);
#get file Name and file type
file_name,file_type = os.path.splitext(file_namefull);
file_type = file_type.lower();
if file_type == ‘.c‘:
return (True, file_init_c(src));
elif file_type == ‘.h‘:
file_name = file_name.upper();
return (True, file_init_h(src,file_name));
else:
print "not object";
return (False,0);
if __name__ == ‘__main__‘:
# main program
file_name = raw_input("Please Input File Name:");
ret, func = make_file(file_name);
if ret:
func;
Python自動生成.c .h 文件模板