📄 filehandle.py
字号:
#!/usr/local/bin/python
import os
import os.path, fnmatch
def listFiles(root, patterns='*', recurse=1, return_folders=0):
# Expand patterns from semicolon-separated string to list
pattern_list = patterns.split(';')
# Collect input and output arguments into one bunch
class Bunch:
def __init__(self, **kwds): self.__dict__.update(kwds)
arg = Bunch(recurse=recurse, pattern_list=pattern_list,
return_folders=return_folders, results=[])
def visit(arg, dirname, files):
# Append to arg.results all relevant files (and perhaps folders)
for name in files:
fullname = os.path.normpath(os.path.join(dirname, name))
if arg.return_folders or os.path.isfile(fullname):
for pattern in arg.pattern_list:
if fnmatch.fnmatch(name, pattern):
arg.results.append(fullname)
break
# Block recursion if recursion was disallowed
if not arg.recurse: files[:]=[]
os.path.walk(root, visit, arg)
return arg.results
class FileHandle:
def __init__(self, name):
self._name = name
self._new_name = name + "d"
def find_new_delete(self):
line_no = 0
for line in self._lines:
if line.find(" new ") != -1:
return line_no
elif line.find(" delete ") != -1:
return line_no
elif line.find(" delete[] ") != -1:
return line_no
line_no = line_no + 1
return -1
def find_last_include_before(self, end):
line_no = end
while line_no > 0:
line = self._lines[line_no]
if line.find("#include ") != -1:
break
line_no = line_no - 1;
return line_no
def level(self, no):
level = 0
line_no = no - 1
while line_no > 0:
line = self._lines[line_no]
if line.find("#if") != -1:
level = level - 1
if level < 0:
break
elif line.find("#endif") != -1:
level = level + 1
line_no = line_no - 1
return level
def insert_my_include_file(self, no):
self._lines.insert(no + 1, "#include \"dbg_heap.h\"\n");
def write(self):
ofile = open(self._new_name, 'w')
ofile.writelines(self._lines)
ofile.close()
def do(self):
self._lines = open(self._name).readlines()
new_pos = self.find_new_delete()
if new_pos >= 0:
inc_pos = self.find_last_include_before(new_pos)
while inc_pos >= 0:
if self.level(inc_pos) == 0:
break
inc_pos = self.find_last_include_before(inc_pos - 1)
if inc_pos >= 0:
self.insert_my_include_file(inc_pos)
self.write()
def need_todo(self):
if os.path.isfile(self._new_name) == False:
return True
ostat = os.stat(self._name)
nstat = os.stat(self._new_name)
return ostat.st_mtime > nstat.st_mtime
if __name__ == "__main__":
files = listFiles('.', '*.cpp')
for file in files:
handler = FileHandle(file)
if handler.need_todo():
print "handle " + file
handler.do()
else:
print file + " need not update"
# handler = FileHandle("MSGCTR_ComposerEngineBase.cpp")
# if handler.need_todo():
# print "do handle"
# handler.do()
# else:
# print "need not"
# new_pos = handler.find_new_delete()
# include_pos = handler.find_last_include_before(new_pos)
# while include_pos >= 0:
# if handler.is_top_level(include_pos) == 0:
# break
# include_pos = handler.find_last_include_before(include_pos - 1)
# if include_pos >= 0:
# handler.insert_my_include_file(include_pos)
# handler.write()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -