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

📄 mkcpp.py

📁 convert C programs for use in C++ compiler environment
💻 PY
📖 第 1 页 / 共 3 页
字号:


# Process a block for C to C++ references replacements

def processblock(blocklist, classname):
 newblock = []
 COMMENT = FALSE      # This flag is true inside a comment
 INSBLK  = FALSE      # This one inside a function's body
 INSMAC  = FALSE
 linenum = 0

 for line in blocklist:
   linenum = linenum + 1
   line = wstring.chop(line)       # Removing line separators
   subline = wstring.strip(line)
   if len(subline) == 0: continue  # Empty line ignored

   # Processing first line
   if linenum == 0:
     i = string.find(subline, '{')
     if i == -1:
        print "Error in compound processing"
        sys.exit(0)
     subline = subline[i:]

   # Processing last line
   if linenum == len(blocklist):
     i = string.find(subline, '}')
     if i == -1:
       print "Error in compound processing"
       sys.exit(0)
     subline = subline[0: i+1]

   # Skip comment block, append it and

   if not COMMENT:
     if not lexer.embeddedcomment(subline):
       COMMENT = lexer.opencomment(subline)
     else:
       test = lexer.removecomment(subline)
       if wstring.strip(test) == "":       # Simple one-line comment
         newblock.append(line)
         continue
   # Inside a comment block if multi-lines
   if COMMENT:                             # Comment opened
     COMMENT = lexer.closecomment(subline)   # Always inside comment?
     # End of multi-line comment
     if not COMMENT:                       # Terminator reached
       i = string.find(subline, "*/")
       if i == -1:
         print "Error in parsing header"
         sys.exit(0)
       subline = subline[i + 2:]           # Keeping the code is exists

     # Start of multi-line comment
     if COMMENT:
       i = string.find(subline, "/*")
       if i != -1: subline = subline[:i]
       else:
         newblock.append(line)
         continue

   if subline is None: continue
   subline = wstring.strip(subline)
   if subline == "":
      newblock.append(line)
      continue

   # If we are inside a macro or pragma block, skip it
   if not INSMAC: INSMAC = lexer.opendef(subline)
   if INSMAC:
     INSMAC = lexer.closedef(line)
     newblock.append(line)
     continue

   # Process ident replacement in other cases

   line = cppreference(line, subline, classname)
   newblock.append(line)
   continue

   # end of the main for loop
 return newblock


# Processing a C source and then header file
# Copy var declarations from source to header

def transform(cfile):
 global cpp
 global hpp
 global locals
 global linenumber
 global lastinclude
 global included
 global omitted
 global allclasses
 global allheaders
 global withoutpath

 # I guess the header file name
 node, ext = os.path.splitext(cfile)
 hppname = node + ".hpp"

 # I guess the class name to create an instance
 dummy, instance = os.path.split(node)
 instance = allheaders[hppname]

 print "mkcpp - converting", cfile, "with" , hppname

 infile  = open(cfile, "rb")
 if infile is None:
   print "Enable to open", cfile
   sys.exit(0)

 cpp = []
 hpp = []
 varlist = []
 funclist = []
 staticlist = []
 included = []
 omitted = []
 linenumber = 0

 # Internal function: add a line to cpp file

 def addit(line):
   global linenumber
   line = wstring.chop(line)
   cpp.append(line)
   linenumber += 1
   return

 # Processing the C file

 COMMENT = FALSE      # This flag is true inside a comment
 INSBLK  = FALSE      # This one inside a function's body
 INSMAC  = FALSE
 NOCASE = (os.name == "nt") | (os.name == "dos")

 while(1):
   line = infile.readline()
   if not line: break

   line = wstring.chop(line)    # Removing line separators
   if len(wstring.strip(line)) == 0:
      addit("")
      continue                  # Empty line ignored

   # Updating include statements

   if len(line) > 11:                       # min is #include "a"
     if line[:9] == "#include ":
       # get last include linenumber
       lastinclude = linenumber

       i = string.find(line, '\"')          # locate first "
       if i == -1:
         #print "no valid"
         addit(line)
         continue
       j = string.find(line, '\"', i + 1)        # locate second "
       if j == -1:
         print "Error, quotes not closed"
         print line
         sys.exit(0)

       hpath = line[i + 1:j]
       if len(hpath) > 2:
         node, ext = os.path.splitext(hpath)
         hppath = node + ".hpp"
         if NOCASE: hppath = string.lower(hppath)
         # add this path to the list of included header files
         if hppath not in included:
           included.append(hppath)
         line = string.replace(line, hpath, hppath)
         #print "replaced", line
         addit(line)
         continue

   # If we enter a comment block, append it and
   # process other code on the same line

   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
         addit(line)
         continue
   # Inside a comment block if multi-lines
   if COMMENT:                             # Comment opened
     #print "inside c 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)
       addit(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:
         addit(line[i:])
         line = line[:i]
       else:
         addit(line)              # A line of the comment block
         continue

   if line is None: continue
   if wstring.strip(line) == "":
     addit(line)
     continue

   # If we are inside a macro or pragma block, skip it
   if not INSMAC: INSMAC = lexer.opendef(line)
   if INSMAC:
     INSMAC = lexer.closedef(line)
     addit(line)
     continue

   # Comment unterminated are removed
   if lexer.unterminated(line):
     line = lexer.removecomment(line)

   # Simple keywords of type on the line
   if lexer.typeonly(line):
     #print "type only", line
     while(1):
      next = infile.readline()
      if not next: break
      next = wstring.clean(next)
      if next == "": continue
      line = line + ' ' + next
      break
     #print "type only now", line


   # A function's prototype: remove it
   if lexer.isprototype(line):
     continue

   # A local one line typedef
   if lexer.linetypedef(line):
     addit(line)
     continue

   if lexer.linestruct(line):
     addit(line)
     continue


   # A function's definition
   if lexer.isfunction(line) == TRUE:
     #print "is function ", line

     # The heading may use several lines
     while string.find(line, ')') == -1:
      next = infile.readline()
      if next == None:
         print "Error, function uncomplete"
         exit()
      next = wstring.chop(next)
      next = wstring.strip(next)
      line = line + next

     # Handling the old format with declarations on next lines
     nextline = line

     if lexer.oldformat(line):
       #print "old format", line
       tlist = []
       counter = lexer.argscount(line)
       while(counter):
        l = infile.readline()
        if not l: break
        l = wstring.clean(l)
        l = lexer.getdecl(l)
        if lexer.isvardecl(l):
          tlist.append(l)
          counter = counter - 1
        else:
          break
        if '{' in l:
          i = string.find(l, '{')
          nextline = l[i:]
          break

       # Rebuilding the heading according to the new format
       line = lexer.argreplace(line, tlist)
       #print "args replace", line, "with list", tlist

     # With the class prefix, a function becomes a method
     # but for the main function

     funcname = lexer.getident(line)
     if funcname is None:
       print "Error, no ident in", line
       sys.exit(0)
     funcname = string.lower(funcname)
     # add the class prefix, but for the main function
     # for wich classname (used further) must be defined
     if funcname != "main":
       line, classname = setmethod(line)
     else:
       classname = getclass("main")

     line = lexer.removestatic(line)
     addit(line)

     #print "Added method", line

     # Now making a list from the block,
     # for scanning variables inside local scope

     blocklist = []

     # Adding the arguments in the list of local variables
     plist, pi, pj = lexer.getargs(line)
     arglist = []
     if not plist is None:
       for p in plist:
         arglist.append(lexer.addsemicolon(p))

     locals = []
     processlocals(arglist)

     # Handling already skiped lines by giving the last line
     # read from the file, for following tests

     line = nextline

     if not INSBLK: INSBLK = '{' in line
     # reading next lines, skipping empty or comment lines
     while not INSBLK:
       line = infile.readline()
       if not line:
         print "Error, end of file in block"
         print line
         sys.exit(0)
       blocklist.append(line)
       INSBLK = lexer.openblock(line)
     while(INSBLK):
       INSBLK = lexer.closeblock(line)
       if INSBLK:
         #print "inside c block", line
         line = infile.readline()      # Skipping the content
         if not line: INSBLK = FALSE
         else:
           blocklist.append(line)

     # Making a list of local variables into the "locals" list

     processlocals(blocklist)

     # Now performing replacements

     newblock = processblock(blocklist, classname)
     for line in newblock:
       addit(line)

     continue     # Continue in main loop

   # An external declaration should be ignored
   # since all vars now have declaration in headers
   # this works if the declaration is a single line

   elif lexer.isextern(line):  continue

   # An array declaration
   # - static: remain global, static removed
   # - with initializer: become static attribute definition
   # - without initializer: declared in class, removed here

   elif lexer.isarray(line):

⌨️ 快捷键说明

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