CMS 3D CMS Logo

cppCommentSkipper.py
Go to the documentation of this file.
1 from builtins import range
2 __author__="Aurelija"
3 __date__ ="$2010-07-13 12.17.20$"
4 
5 import string
6 
7 def filterFiles(fileList):
8  files = []
9 
10  for file in fileList:
11  files.append((file, filterFile(file)))
12  return files
13 
14 def filterFile(file): #ifstream& input)
15  try:
16  lines = open(file).readlines()
17  except UnicodeDecodeError as e:
18  print("CppCommentSkipper: WARNING: Invalid UTF-8 sequence in {0}".format(file))
19  lines = open(file, errors='replace').readlines()
20  commentStage = False
21 
22  for i in range(len(lines)):
23  #for j in range(len(lines[i])):
24  j = 0
25 
26  while lines[i][j] != '\n':
27 
28  char = lines[i][j]
29  #char /
30  if char == '/':
31  #comment /*
32  if lines[i][j+1] == '*' and not commentStage: #why !commentStage? because it could be /*...../*.....*/
33  commentStage = True
34  commentStartLine, commentStartColumn = i, j
35  j += 1
36  #comment // why !commentStage? because, can be a variant of this example: /*....//.....*/
37  elif not commentStage and (lines[i][j+1] == '/'):
38  lines[i] = lines[i].replace(lines[i][j:],'\n', 1)
39  break
40  #char "
41  elif char == '"':
42  if not commentStage:
43  next = lines[i][j+1:].find('"') #next "
44  lines[i] = lines[i].replace(lines[i][j:j+next+2], '', 1) # clear string in ""
45  #char '
46  elif char == '\'':
47  if not commentStage:
48  next = lines[i][j+1:].find('\'') #next '
49  lines[i] = lines[i].replace(lines[i][j:j+next+2], '', 1) # clear string in ''
50  #char *
51  elif char == '*':
52  if (commentStage and (lines[i][j+1] == '/')):
53  commentStage = False;
54  if commentStartLine != i:
55  lines[i] = lines[i].replace(lines[i][:j+2],'', 1) # comment */ [..code]
56  j = -1 #because of j+=1 at the end
57  else:
58  lines[i] = lines[i].replace(lines[i][commentStartColumn:j+2], '', 1) # [code..] /*comment*/ [.. code]
59  j = commentStartColumn - 1 #because of j+=1 at the ends
60  if j != len(lines[i]) - 1:
61  j += 1
62  else:
63  j = 0
64  break
65  if commentStage:
66  if i == commentStartLine: lines[i] = lines[i].replace(lines[i][commentStartColumn:],'\n', 1)
67  else: lines[i] = lines[i].replace(lines[i][:], '\n')
68  return lines
def replace(string, replacements)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47