我需要搜索路径中的所有*.c
源文件以查找对*.h
标头的引用以查找未使用的C标头。我写了一个ruby脚本,但感觉非常笨拙。使用ruby搜索路径文件中的文本
我创建一个包含所有C文件和一个包含所有H文件的数组的数组。 我遍历头文件数组。对于每个标题,我打开每个C文件并查找对标题的引用。
有没有更简单或更好的方法?
require 'ftools'
require 'find'
# add a file search
class File
def self.find(dir, filename="*.*", subdirs=true)
Dir[ subdirs ? File.join(dir.split(/\\/), "**", filename) : File.join(dir.split(/\\/), filename) ]
end
end
files = File.find(".", "*.c", true)
headers = File.find(".", "*.h", true)
headers.each do |file|
#puts "Searching for #{file}(#{File.basename(file)})"
found = 0
files.each do |cfile|
#puts "searching in #{cfile}"
if File.read(cfile).downcase.include?(File.basename(file).downcase)
found += 1
end
end
puts "#{file} used #{found} times"
end
+1为具有相同的想法,也为细节代码。 – YOU 2010-01-20 03:38:34