Browse Subversion Repository
Contents of /script/addExtraIncludeGuard.rb
Parent Directory
| Revision Log
Revision 191 -
( show annotations)
( download)
Fri Jan 4 02:49:36 2008 UTC
(16 years, 4 months ago)
by satofumi
File size: 1911 byte(s)
using namespace beego
| 1 |
#!/usr/bin/ruby |
| 2 |
|
| 3 |
# 冗長インクルードガードの適用スクリプト |
| 4 |
|
| 5 |
require "fileutils" |
| 6 |
|
| 7 |
# 引数にファイル指定がなければ終了 |
| 8 |
(print "usage:\n\t" + __FILE__ + " <header files>\n\n"; exit) if ARGV.size == 0 |
| 9 |
|
| 10 |
|
| 11 |
# 冗長インクルードガードの追加 |
| 12 |
def addIncludeGuard(fname, pre_line) |
| 13 |
|
| 14 |
# 大文字の箇所で _ を追加する |
| 15 |
macro_name = '' |
| 16 |
|
| 17 |
# !!! いずれ、それっぽいイテレータに置き換える |
| 18 |
# !!! pack() とかを使うべきか? |
| 19 |
n = fname.size |
| 20 |
for i in 0 .. n-1 |
| 21 |
ch = fname[i].chr |
| 22 |
if ch =~ /\./ |
| 23 |
macro_name += '_' |
| 24 |
elsif ch =~ /[A-Z]/ && !macro_name.empty? |
| 25 |
macro_name += '_' + ch |
| 26 |
else |
| 27 |
# !!! 大文字にする方法を調べて置き換える |
| 28 |
if ch =~ /[A-Z0-9_]/ |
| 29 |
macro_name += ch |
| 30 |
else |
| 31 |
macro_name += (fname[i] + 'A'[0] - 'a'[0]).chr |
| 32 |
end |
| 33 |
end |
| 34 |
end |
| 35 |
|
| 36 |
# 内容の更新 |
| 37 |
if pre_line.match(macro_name) |
| 38 |
return false |
| 39 |
end |
| 40 |
lines = '' |
| 41 |
lines += "#ifndef " + macro_name + "\n" |
| 42 |
lines += "#include \"" + fname + "\"\n" |
| 43 |
lines += "#endif\n" |
| 44 |
end |
| 45 |
|
| 46 |
# ファイル毎に冗長インクルードガードの適用を行う |
| 47 |
ARGV.each { |file| |
| 48 |
print file + ' ... ' |
| 49 |
|
| 50 |
# 「#include "(.+?)"」に対して、前後に「#ifndef」「#endif」を追加する |
| 51 |
updated_lines = '' #!< 変更後のファイル内容 |
| 52 |
is_updated = false #!< 変更の必要があれば true |
| 53 |
|
| 54 |
open (file) { |io| |
| 55 |
pre_line = '' |
| 56 |
while line = io.gets |
| 57 |
case (line.chomp()) |
| 58 |
when /\#include "(.+?)"/ |
| 59 |
header_file = $1 |
| 60 |
ret = addIncludeGuard(header_file, pre_line) |
| 61 |
if ret != false |
| 62 |
updated_lines += ret |
| 63 |
is_updated = true |
| 64 |
else |
| 65 |
updated_lines += line |
| 66 |
end |
| 67 |
else |
| 68 |
updated_lines += line |
| 69 |
end |
| 70 |
pre_line = line |
| 71 |
end |
| 72 |
} |
| 73 |
if is_updated |
| 74 |
# ファイルの置き換え |
| 75 |
replace_file = file + '.bak' |
| 76 |
FileUtils.mv(file, replace_file) |
| 77 |
File.open(file, "a") { |io| |
| 78 |
io.write(updated_lines) |
| 79 |
} |
| 80 |
print "update!\n" |
| 81 |
|
| 82 |
else |
| 83 |
print "O.K.\n" |
| 84 |
end |
| 85 |
} |
|