CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cppCommentSkipper.py
Go to the documentation of this file.
1 __author__="Aurelija"
2 __date__ ="$2010-07-13 12.17.20$"
3 
4 import string
5 
6 def filterFiles(fileList):
7  files = []
8 
9  for file in fileList:
10  files.append((file, filterFile(file)))
11  return files
12 
13 def filterFile(file): #ifstream& input)
14 
15  lines = open(file).readlines()
16  commentStage = False
17 
18  for i in range(len(lines)):
19  #for j in range(len(lines[i])):
20  j = 0
21 
22  while lines[i][j] != '\n':
23 
24  char = lines[i][j]
25  #char /
26  if char == '/':
27  #comment /*
28  if lines[i][j+1] == '*' and not commentStage: #why !commentStage? because it could be /*...../*.....*/
29  commentStage = True
30  commentStartLine, commentStartColumn = i, j
31  j += 1
32  #comment // why !commentStage? because, can be a variant of this example: /*....//.....*/
33  elif not commentStage and (lines[i][j+1] == '/'):
34  lines[i] = string.replace(lines[i], lines[i][j:],'\n', 1)
35  break
36  #char "
37  elif char == '"':
38  if not commentStage:
39  next = string.find(lines[i][j+1:], '"') #next "
40  lines[i] = string.replace(lines[i], lines[i][j:j+next+2], '', 1) # clear string in ""
41  #char '
42  elif char == '\'':
43  if not commentStage:
44  next = string.find(lines[i][j+1:], '\'') #next '
45  lines[i] = string.replace(lines[i], lines[i][j:j+next+2], '', 1) # clear string in ''
46  #char *
47  elif char == '*':
48  if (commentStage and (lines[i][j+1] == '/')):
49  commentStage = False;
50  if commentStartLine != i:
51  lines[i] = string.replace(lines[i], lines[i][:j+2],'', 1) # comment */ [..code]
52  j = -1 #because of j+=1 at the end
53  else:
54  lines[i] = string.replace(lines[i], lines[i][commentStartColumn:j+2], '', 1) # [code..] /*comment*/ [.. code]
55  j = commentStartColumn - 1 #because of j+=1 at the ends
56  if j != len(lines[i]) - 1:
57  j += 1
58  else:
59  j = 0
60  break
61  if commentStage:
62  if i == commentStartLine: lines[i] = lines[i].replace(lines[i][commentStartColumn:],'\n', 1)
63  else: lines[i] = lines[i].replace(lines[i][:], '\n')
64  return lines