CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/Utilities/ReleaseScripts/python/commentSkipper/cppCommentSkipper.py

Go to the documentation of this file.
00001 __author__="Aurelija"
00002 __date__ ="$2010-07-13 12.17.20$"
00003 
00004 import string
00005 
00006 def filterFiles(fileList):
00007     files = []
00008 
00009     for file in fileList:
00010         files.append((file, filterFile(file)))
00011     return files
00012 
00013 def filterFile(file): #ifstream& input)
00014 
00015     lines = open(file).readlines()
00016     commentStage = False
00017 
00018     for i in range(len(lines)):
00019         #for j in range(len(lines[i])):
00020         j = 0
00021         
00022         while lines[i][j] != '\n':
00023             
00024             char = lines[i][j]
00025             #char /
00026             if char == '/':
00027                 #comment /*
00028                 if lines[i][j+1] == '*' and not commentStage: #why !commentStage? because it could be /*...../*.....*/
00029                     commentStage = True
00030                     commentStartLine, commentStartColumn = i, j
00031                     j += 1
00032                 #comment //  why !commentStage? because, can be a variant of this example: /*....//.....*/
00033                 elif not commentStage and (lines[i][j+1] == '/'):
00034                     lines[i] = string.replace(lines[i], lines[i][j:],'\n', 1)
00035                     break
00036             #char "
00037             elif char == '"':
00038                 if not commentStage:
00039                     next = string.find(lines[i][j+1:], '"') #next "
00040                     lines[i] = string.replace(lines[i], lines[i][j:j+next+2], '', 1) # clear string in ""
00041             #char '
00042             elif char == '\'':
00043                 if not commentStage:
00044                     next = string.find(lines[i][j+1:], '\'') #next '
00045                     lines[i] = string.replace(lines[i], lines[i][j:j+next+2], '', 1) # clear string in ''
00046             #char *
00047             elif char == '*':
00048                 if (commentStage and (lines[i][j+1] == '/')):
00049                     commentStage = False;
00050                     if commentStartLine != i:
00051                         lines[i] = string.replace(lines[i], lines[i][:j+2],'', 1) # comment */ [..code]
00052                         j = -1 #because of j+=1 at the end
00053                     else:
00054                         lines[i] = string.replace(lines[i], lines[i][commentStartColumn:j+2], '', 1) # [code..] /*comment*/ [.. code]
00055                         j = commentStartColumn - 1 #because of j+=1 at the ends
00056             if j != len(lines[i]) - 1:
00057                 j += 1
00058             else:
00059                 j = 0
00060                 break
00061         if commentStage:
00062             if i == commentStartLine: lines[i] = lines[i].replace(lines[i][commentStartColumn:],'\n', 1)
00063             else: lines[i] = lines[i].replace(lines[i][:], '\n')
00064     return lines