Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:47

0001 from builtins import range
0002 __author__="Aurelija"
0003 __date__ ="$2010-07-13 12.17.20$"
0004 
0005 import string
0006 
0007 def filterFiles(fileList):
0008     files = []
0009 
0010     for file in fileList:
0011         files.append((file, filterFile(file)))
0012     return files
0013 
0014 def filterFile(file): #ifstream& input)
0015     try:
0016         lines = open(file).readlines()
0017     except UnicodeDecodeError as e:
0018         print("CppCommentSkipper: WARNING: Invalid UTF-8 sequence in {0}".format(file))
0019         lines = open(file, errors='replace').readlines()
0020     commentStage = False
0021 
0022     for i in range(len(lines)):
0023         #for j in range(len(lines[i])):
0024         j = 0
0025         
0026         while lines[i][j] != '\n':
0027             
0028             char = lines[i][j]
0029             #char /
0030             if char == '/':
0031                 #comment /*
0032                 if lines[i][j+1] == '*' and not commentStage: #why !commentStage? because it could be /*...../*.....*/
0033                     commentStage = True
0034                     commentStartLine, commentStartColumn = i, j
0035                     j += 1
0036                 #comment //  why !commentStage? because, can be a variant of this example: /*....//.....*/
0037                 elif not commentStage and (lines[i][j+1] == '/'):
0038                     lines[i] = lines[i].replace(lines[i][j:],'\n', 1)
0039                     break
0040             #char "
0041             elif char == '"':
0042                 if not commentStage:
0043                     next = lines[i][j+1:].find('"') #next "
0044                     lines[i] = lines[i].replace(lines[i][j:j+next+2], '', 1) # clear string in ""
0045             #char '
0046             elif char == '\'':
0047                 if not commentStage:
0048                     next = lines[i][j+1:].find('\'') #next '
0049                     lines[i] = lines[i].replace(lines[i][j:j+next+2], '', 1) # clear string in ''
0050             #char *
0051             elif char == '*':
0052                 if (commentStage and (lines[i][j+1] == '/')):
0053                     commentStage = False;
0054                     if commentStartLine != i:
0055                         lines[i] = lines[i].replace(lines[i][:j+2],'', 1) # comment */ [..code]
0056                         j = -1 #because of j+=1 at the end
0057                     else:
0058                         lines[i] = lines[i].replace(lines[i][commentStartColumn:j+2], '', 1) # [code..] /*comment*/ [.. code]
0059                         j = commentStartColumn - 1 #because of j+=1 at the ends
0060             if j < len(lines[i]) - 1:
0061                 j += 1
0062             else:
0063                 j = 0
0064                 break
0065         if commentStage:
0066             if i == commentStartLine: lines[i] = lines[i].replace(lines[i][commentStartColumn:],'\n', 1)
0067             else: lines[i] = lines[i].replace(lines[i][:], '\n')
0068     return lines