📄 mkcpp.py
字号:
# print "is array", line
# One line declaration, save it and continue
i = string.find(line, ';')
if i != -1:
# one line global array with initializer or not
if lexer.isstatic(line):
line = lexer.removestatic(line)
addit(line)
continue
# attribute with initalizer, transformed
j = string.find(line, '=');
if j != -1:
ahead = line[:j] + ";"
line = setmember(line)
addit(line)
staticlist.append(ahead[:i+1])
# other attribute, removed from source file
# but added to list of attributes
else:
ahead = line
varlist.append(ahead[:i + 1])
#print "one line", line[:i + 1]
continue
# Multiline array with assignment
fulldecl = line + "\n"
initflag = FALSE
globalflag = lexer.isstatic(line)
j = string.find(line, "=")
if j != -1:
ahead = line[:j] + ";"
initflag = TRUE
else:
ahead = line
ARRAY = lexer.openarray(line)
#print "open?", ARRAY
while ARRAY == FALSE:
line = infile.readline()
#line = lexer.removecomment(line)
if wstring.clean(line) != "":
fulldecl = fulldecl + line
ARRAY = lexer.openarray(line)
ARRAY = lexer.closearray(line)
while(ARRAY):
line = infile.readline()
#line = lexer.removecomment(line)
fulldecl = fulldecl + line
ARRAY = lexer.closearray(line)
varlist.append(ahead)
if globalflag == TRUE: # a global array
fulldecl = lexer.removestatic(fulldecl)
addit(fulldecl)
continue
if initflag == TRUE:
fulldecl = setmember(fulldecl)
addit(fulldecl)
staticlist.append(ahead)
continue
# A variable definition
else:
if lexer.isvardef(line):
#print "var in source", line
globalflag = lexer.isstatic(line)
if globalflag:
line = lexer.removestatic(line)
nlist = []
if lexer.multivar(line):
nlist = lexer.splitvardef(line)
else:
nlist.append(line)
for n in nlist:
j = string.find(n, '=')
if j != -1:
ahead = n[:j] + ";"
if not globalflag:
n = setmember(n)
addit(n)
staticlist.append(ahead)
else:
if globalflag: addit(n)
ahead = n
varlist.append(ahead)
#print "append var", n
#print line
continue
# Save a compound block not previously handled
if not INSBLK: INSBLK = lexer.openblock(line)
if INSBLK:
INSBLK = lexer.closeblock(line)
#print "open block >", line
addit(line)
while INSBLK:
line = infile.readline() # Skipping the content
if not line: break
#print "block >", line
addit(line)
INSBLK = lexer.closeblock(line)
continue
# other statement
#print "other", line
addit(line)
# end main while
addit("")
# add the instance of the class
addit(instance + " " + instance + "Obj;\n")
infile.close()
# Processing the header file
# It is rebuilt
# Reading the file and removing end of file code if exists
header = []
if os.path.exists(hppname):
hfile = open(hppname, "rb")
header = hfile.readlines()
hfile.close
# Removing EOF 0x1a ctrl-z is presents
if len(header) != 0:
l = header[-1]
l = wstring.strip(l)
if l < ' ':
header = header[:-1]
hpp = [] # To rebuild the file
defheader = {} # Define declarations inside header file
rcount = 0 # Replacements
COMMENT = FALSE # This flag is true while inside a comment block
INSMAC = FALSE # This one inside a macro
INSTYP = FALSE # This one inside a typedef or struct
# Now the header file is rebuilt
for o in header: # Scanning the header file
line = wstring.chop(o) # Remove annoying line separators
if wstring.strip(line) == "": # Empty line, keep it
hpp.append(line)
continue
# If we enter a comment block, append it and process other code
if not COMMENT:
if not lexer.embeddedcomment(line):
COMMENT = lexer.opencomment(line)
else:
test = lexer.removecomment(line)
if wstring.strip(test) == "": # Simple one-line comment
hpp.append(line)
continue
# Inside comment, this is a multi-line comment
if COMMENT: # Comment opened
#print "inside comment", line
COMMENT = lexer.closecomment(line) # Always inside comment?
# End of multi-line comment
if not COMMENT: # Terminator reached
i = string.find(line, "*/")
if i == -1:
print "Error in parsing header"
sys.exit(0)
hpp.append(line[:i+2])
line = line[i + 2:] # Keeping the code is exists
# Start of multi-line comment
if COMMENT:
i = string.find(line, "/*")
if i != -1:
hpp.append(line[i:])
line = line[:i]
else:
hpp.append(line) # A line of the comment block
continue
line = wstring.strip(line)
if line == "": continue
# If we are inside a macro or pragma block, skip it
if not INSMAC: INSMAC = lexer.opendef(line)
if INSMAC:
#print "inside macro", line
INSMAC = lexer.closedef(line)
hpp.append(line)
continue
# If we are inside struct or typedef, skip it
if not INSTYP: INSTYP = lexer.openrecord(line)
if INSTYP:
INSTYP = lexer.closerecord(line)
hpp.append(line)
continue
# Define statement are memorized
if line[0:7] == "#define":
key, value = lexer.splitdefine(line)
#print "in header", line, key, value
if key != None: defheader[key] = value # Add to dictionary
# get the class name from the header
if string.find(line, "class") != -1:
classname = lexer.getclassname(line)
if lexer.isprototype(line):
if lexer.isglobal(line):
line = lexer.removeglobal(line)
hpp.append(line)
continue
if lexer.isvardecl(line):
# print "var in header", line
if not lexer.isextern(line):
for n in staticlist[:]:
#print "var in source", n
if lexer.samevar(line, n): # Same name
line = lexer.addstatic(line)
staticlist.remove(n)
break # exit for loop
# In all case, adding the line, either changed or not
hpp.append(line)
# Adding one instance of this class
# it must to be defined in the cpp source also
# as: classname classnameObj;
obj = classname + "Obj"
hpp.append("extern " + classname + " " + obj + ';')
# Rewriting the updated header file
hfile = open(hppname, "wb")
for o in hpp:
hfile.write(o + "\n")
hfile.close()
# For each include directive
# I get header path, and with it the class name
# and I test if the class is in the list of classes used by this source
# if not, I add the missing header in which this class is declared
for hname in included: # get the path from an include directive
if allheaders.has_key(hname): # if this path in allheaders?
clname = allheaders[hname] # get the class name the header contains
else:
dummy, fname = os.path.split(hname) # get the filename
if withoutpath.has_key(fname): # perhaps path missing in directive?
clname = withoutpath[fname] # get the class from the second list
else:
print "*", fname, "not in list of headers"
continue
if clname in omitted: # is class in the list of used classes?
omitted.remove(clname) # class already included, remove it
#print "already included", clname
continue
# now adding all omitted include statements
for cname in omitted: # get a class name
if allclasses.has_key(cname): # is it in the dict
hname = allclasses[cname] # get the header filename
s = "#include \"" + hname + "\"" # build an include statement
cpp.insert(lastinclude + 1, s) # insert it in last position
print "added ", s
# Creating the C++ code file
node, ext = os.path.splitext(cfile)
cppname = node + ".cpp"
cppfile = open(cppname, "wb")
for o in cpp:
cppfile.write(o + "\n")
cppfile.close()
dummy, cppname = os.path.split(cppname)
#dummy, hppname = os.path.split(hppname)
print cppname, "created,", "and", hppname, "updated"
print ""
return
# Main
def main():
global hpplist # List of .hpp files
global clist # List of c files (project)
global hpp # New .hpp header
global cpp # New .cpp source file
global dclass # Dictionnary of classes and members/methods
global fclass
global counter
global classfile
param = sys.argv
lenparam = len(param)
if (lenparam < 2) | (lenparam > 3):
print """
Make Cpp - C to C++ tools - Scriptet.com
Converts global variables to static attributes.
Transforms function calls to method calls.
usage: mkcpp srcfile.c headerfile.hpp
or: mkcpp srcfile.c
or: mkcpp @srclist @headerlist
srclist is the file created by the -l option or by mklist.py
headerlist is the file created by the -a optio or by allhead.py
previous .cpp and .hpp files will be overwritten
"""
sys.exit()
hpp = []
newhpp = []
ccode = []
cppcode = []
counter = 0
cfile = param[1] # c source or list
if lenparam == 3:
hfile = param[2] # header or list
else:
node, dummy = os.path.splitext(cfile)
hfile = node + ".hpp"
clist = []
hlist = []
if cfile[0] == '@':
# Processing a project file (a simple list of C source files)
project = cfile[1:]
clist = lexer.readproject(project)
else:
# Processing a file or a list matching a pattern
clist.append(cfile)
if hfile[0] == '@':
# Processing a header list
project = hfile[1:]
hlist = lexer.readproject(project)
else:
# Processing a file or a list matching a pattern
hlist.append(hfile)
# Building the directory of each class with members and methods
print "mkcpp - building the directory of classes"
for classfile in hlist:
if not os.path.exists(classfile):
print classfile, "not found"
continue
#print "Adding", classfile
build(classfile)
# the main function is not member of a class
# this string allows to pass through further processing
fclass["main"] = "$$$"
dclass["main"] = "$$$"
# Moving var. declarations and changing functions into methods
for c in clist:
if not os.path.exists(c):
print c, "not found"
continue
transform(c)
return 0
main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -