⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mkheader.py

📁 convert C programs for use in C++ compiler environment
💻 PY
📖 第 1 页 / 共 2 页
字号:
      else:
        vlist.append(line)
      for vardef in vlist:
        j = string.find(vardef, '=')
        k = string.find(vardef, ';')
        if k == -1: k = len(vardef)
        if j < k:
          vardef = vardef[:j] + ';'
        var = lexer.addextern(vardef)
        varlist.append(var)
        #print "added var", var
        defcount = defcount + 1
        #print line

 # end while

 infile.close()
 print linecount, "lines in", cfile


 # Pass 2 - Processing the header file
 # -----------------------------------
 # I compare it with declarations extracted from the C source,
 # I replace changes from source into the header file.
 # I append missing declarations.
 # I join multi-lines declarations.
 # And I rewrite the header file.


 # Reading the file and removing end of file code if exists

 header = []

 if os.path.exists(headername):
   hfile = open(headername, "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]


 tmpheader = []     # For rebuilding the file
 defheader = {}     # Define declarations inside header file
 rcount = 0         # Replacements
 kcount = 0         # Fully identical declaration
 matches = 0        # Declarations with same names
 COMMENT = FALSE    # This flag is true inside a multiline comment
 INSDEF = FALSE     # This one inside a define declaration
 INSBLK = FALSE     # This one inside a compound block
 oldline = ""       # Save line for multi-lines declarations


 for o in header:                # Scanning the header file
   line = wstring.chop(o)        # Remove annoying line separators
   if wstring.strip(line) == "": # Empty line, keep it
     tmpheader.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
         tmpheader.append(line)
         continue
   # Inside comment, this is a multi-line comment
   if COMMENT:                             # Comment opened
     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)
       tmpheader.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:
         tmpheader.append(line[i:])
         line = line[:i]
       else:
         tmpheader.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 INSDEF: INSDEF = lexer.opendef(line)
   if INSDEF:
     INSDEF = lexer.closedef(line)
     tmpheader.append(line)
     continue

   # If we are inside some block (struct, typedef), skip it
   if not INSBLK: INSBLK = lexer.openblock(line)
   if INSBLK:
     INSBLK = lexer.closeblock(line)
     tmpheader.append(line)
     continue

   # join declarations in splitted lines

   if (line[ -1 ] == ","):
     oldline = oldline + line
     continue
   else:
     if (oldline != ""): line = oldline + line

   oldline = ""
  
   # Define statements 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

   # Searching for var declarations in header file
   elif lexer.isvardecl(line):
     #print "var in header", line
     for n in varlist[:]:
       #print "in varlist", n
       #test = wstring.replace(line, "global", "static", FALSE)
       if lexer.samevar(line, n):       # Same name
         #print "samevars", line, n
         matches = matches + 1
         if lexer.vchanged(line, n):
           #print "vchanged"
           lexer.replace(line, n)       # Replace, keep comments
           #print "replaced=", line
           line = lexer.shortened       # old decl. in line replaced
           if DEBUG: print "replacing", n
           rcount = rcount + 1
         else:
           kcount = kcount + 1
         varlist.remove(n)
         break     # exit for loop
     line = lexer.addextern(line)          # adding "extern" if missing
     #print "new dec", line
   # Searching for function declaration
   elif lexer.isprototype(line):
     for n in funclist[:]:
       #print line, n
       if lexer.samefunction(line, n):   # Same name
         matches = matches + 1
         if lexer.fchanged(line, n):     # Different returns or arguments
           lexer.replace(line, n)
           line = lexer.shortened
           if DEBUG: print "replacing", line
           rcount = rcount + 1
         else:
           kcount = kcount + 1
         funclist.remove(n)
         break

   # In all case, adding the line, either changed or not
   tmpheader.append(line)

 

 # Rewriting and completing the header file

 added = 0

 k = defflag.keys()
 for d in k:
  #print "next key", d, defflag[d]
  if defflag[d] == TRUE:
   #print "defheader", defheader
   if not defheader.has_key(d):
     tmpheader.append(defline[d])
     if DEBUG: print "adding", defline[d]

 for line in varlist:
   if lexer.isstatic(line): continue
   tmpheader.append(line)
   if DEBUG: print "adding", line
   added = added + 1

 for line in funclist:
   tmpheader.append(line)
   if DEBUG: print "adding", line
   added = added + 1

 # All done

 pos = headername.find(".")
 if (pos != -1): 
    headername = headername[ : pos]
 headername += ".hpp"
 hfile = open(headername, "wb")
 for o in tmpheader: hfile.write(o + "\n")
 hfile.close()

 dummy, hname = os.path.split(headername)
 print "***", defcount, "declaration" + wstring.plural(defcount)+',' , matches, "in", hname + "," , kcount, "identical", rcount , "replaced", added, "added"
 print ""

 return



# Main function

def main():
 global extension

# Processing command line arguments

 argnum = len(sys.argv)
 if (argnum != 2) & (argnum != 3):
  print "Make Header - C to C++ tools - Scriptet.com"
  print "This tool generates or updates header files from .c files"
  print "(Don't use it with C++ file please)"
  print "Usage:  python mkheader.py csource"
  print "   or:  python mkheader.py pattern"
  print "   or:  python mkheader.py @prjfile"
  print "   or as above plus new header file extension (default .hpp)"
  print "   ex:  python mkheader.py filename.c h  (to update a .h file"
  print "pattern uses * as wildcard code to match a set of files"
  sys.exit(0)

 cfile = sys.argv[1]

 # use either default extension of given one

 extension = lexer.hpp
 if argnum == 3:
   extension = sys.argv[2]
   print extension

 # if the argument starts with @, this is a list of file
 # otherwise this is a main file

 if cfile[0] == '@':
  # Processing a project file (a simple list of C source files)
  project = cfile[1:]
  print "Processing project file", project
  flist = lexer.readproject(project)
  for f in flist:
     processfile(f)
 else:
  # Processing a file or a list matching a pattern
  path, name = os.path.split(cfile)
  if path == "": path = os.getcwd()
  #print path, name
  liste = os.listdir(path)

  # scanning the list of sources and processing each one

  for fname in liste:
   if not os.path.isfile(fname): continue
   if pattern.matching(name, fname, FALSE) == TRUE:
    f = os.path.join(path, fname)
    processfile(f)
 return 0

main()










⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -