Skip to content

Instantly share code, notes, and snippets.

@chenzx
Last active December 14, 2015 00:49
Show Gist options
  • Save chenzx/5002159 to your computer and use it in GitHub Desktop.
Save chenzx/5002159 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
#
#Usage: ruby findAndCorrectFilePathCaseConflicts.rb <hFilePathLists.txt> <sourceFilePathLists.txt>
#首先,加载全部.h头文件的清单:
hFilePaths = []
File.open(ARGV[0]).each { |line|
hFilePaths.push(line)
}
PATTERN1 = /#include[ ]*"([^"]+)"/
PATTERN2 = /#include[ ]*<([^>]+)>/
def detectIncludedHeaderFileCaseConflict(hFilePaths, includedHeaderFile)
realIncludeFileCaseSensitiveName = nil
#首先按大小写一致匹配,如果有命中,则不改!
findExactMatch = false
hFilePaths.each {|hFilepath|
hFilepath = hFilepath.strip
if hFilepath.end_with? "/"+includedHeaderFile or hFilepath==+includedHeaderFile then
findExactMatch = true
end
}
if not findExactMatch then
hFilePaths.each {|hFilepath|
hFilepath = hFilepath.strip
#先全部转为小写进行匹配,如果命中,则进行大小写敏感的再次匹配
if hFilepath.downcase.end_with? includedHeaderFile.downcase then
hFilepathMatchedPart = hFilepath[-includedHeaderFile.length .. -1]
if hFilepathMatchedPart!= includedHeaderFile then
if hFilepath.downcase == includedHeaderFile.downcase then
return hFilepathMatchedPart
else
#Check situation: .../DateMath.h should not match with math.h!!!
c = hFilepath[-includedHeaderFile.length-1 .. -includedHeaderFile.length-1]
if not /[a-zA-Z_]/.match(c) then
return hFilepathMatchedPart
end
end
end
break
end
}
end
realIncludeFileCaseSensitiveName
end
allIncludeFileNamesModifiedTargets = Hash.new(0)
#然后,遍历所有待检查的源代码文件:
listOfFileHasIncludeCaseConflict = []
File.open(ARGV[1]).each { |sourceFilePath|
findFirstIncludeLine = false
lineNo = 1
sourceFilePath = sourceFilePath.strip
correctedSourceFileContent = []
fileHasIncludeCaseConflict = false
f = File.open(sourceFilePath)
f.each { |line|
begin
m1 = PATTERN1.match(line)
rescue => detail
m1 = PATTERN1.match(line.encode("utf-8", :invalid => :replace, :undef => :replace))
end
if m1 then
if not findFirstIncludeLine then findFirstIncludeLine=true end
fileHasIncludeCaseConflict = true
includedHeaderFile = m1[1]
realIncludeFileCaseSensitiveName = detectIncludedHeaderFileCaseConflict(hFilePaths, includedHeaderFile)
if realIncludeFileCaseSensitiveName then
puts "#{sourceFilePath}:#{lineNo}: #{includedHeaderFile} => #{realIncludeFileCaseSensitiveName}"
allIncludeFileNamesModifiedTargets[realIncludeFileCaseSensitiveName] += 1
correctedSourceFileContent.push "//"+line
correctedSourceFileContent.push "#include \"#{realIncludeFileCaseSensitiveName}\"\n"
else
correctedSourceFileContent.push line
end
else
begin
m2 = PATTERN2.match(line)
rescue => detail
m2 = PATTERN2.match(line.encode("utf-8", :invalid => :replace, :undef => :replace))
end
if m2 then
if not findFirstIncludeLine then findFirstIncludeLine=true end
fileHasIncludeCaseConflict = true
includedHeaderFile = m2[1]
realIncludeFileCaseSensitiveName = detectIncludedHeaderFileCaseConflict(hFilePaths, includedHeaderFile)
if realIncludeFileCaseSensitiveName then
puts "#{sourceFilePath}:#{lineNo}: #{includedHeaderFile} => #{realIncludeFileCaseSensitiveName}"
allIncludeFileNamesModifiedTargets[realIncludeFileCaseSensitiveName] += 1
correctedSourceFileContent.push "//"+line
correctedSourceFileContent.push "#include <#{realIncludeFileCaseSensitiveName}>\n"
else
correctedSourceFileContent.push line
end
else
#没有匹配,本行代码不是#include行
correctedSourceFileContent.push line
end
end
lineNo = lineNo + 1
}
f.close #手工关闭
if fileHasIncludeCaseConflict then
listOfFileHasIncludeCaseConflict.push sourceFilePath
#Start correct the conflict file:
File.rename(sourceFilePath, sourceFilePath+".conflict-orginal");
File.new(sourceFilePath, "w").write( correctedSourceFileContent.join )
end
}
#puts "\n\n\nnumOfFileHasIncludeCaseConflict=#{listOfFileHasIncludeCaseConflict.length}"
puts "\n\n\nallIncludeFileNamesModifiedTargets:"
allIncludeFileNamesModifiedTargets.keys.each {|k|
puts k
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment