-
Notifications
You must be signed in to change notification settings - Fork 1
/
cppFriendsFile.rb
110 lines (93 loc) · 2.71 KB
/
cppFriendsFile.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/ruby
# coding: utf-8
# Boost Filesystem で同一ファイルかどうか調べるテストを準備する
# 第一引数 = 動作(create = 生成, delete = 削除)
# 第二引数 = 生成するC++ヘッダファイル名
HELP_MESSAGE = "usage: cppFriendsFile.rb {create outputfile.hpp|delete} "
# 最初に作るファイル
FILENAME_NODE = "__generated_text_file_.txt"
VARNAME_NODE = "g_cppFriendsNodeFilename"
# 上記へのシンボリックリンク
FILENAME_SYMLINK = "__generated_symlink__.txt"
VARNAME_SYMLINK = "g_cppFriendsSymlinkFilename"
# 上記へのハードリンク
FILENAME_HARDLINK = "__generated_hardlink__.txt"
VARNAME_HARDLINK = "g_cppFriendsHardlinkFilename"
# 生成した全ファイル
FILENAME_SET = [FILENAME_NODE, FILENAME_SYMLINK, FILENAME_HARDLINK]
class AppInvalidParameter < StandardError
end
class FileCreater
def initialize(cppFilename)
generateFiles(cppFilename)
end
def generateFiles(cppFilename)
guard = cppFilename.upcase.gsub(/[^[:alnum:]]/,"_").gsub(/_+/,"_")
guard += "_ALREADY_INCLUDED"
File.open(cppFilename, "w") { |outStream|
["#ifndef #{guard}", "#define #{guard}", "namespace {",
generateNode(), generateSymlink(), generateHardlink(),
"}", "#endif"].each { |str| outStream.puts str}
}
end
def generateNode
filename = FILENAME_NODE
File.open(filename, "w") { |stream|
stream.puts "Genereted"
}
getString(VARNAME_NODE, filename)
end
def generateSymlink
filename = ""
begin
if File.symlink(FILENAME_NODE, FILENAME_SYMLINK) == 0
filename = FILENAME_SYMLINK
end
rescue
# シンボリックリンクを作れなければファイル名を空文字列にする
end
getString(VARNAME_SYMLINK, filename)
end
def generateHardlink
filename = ""
begin
if File.link(FILENAME_NODE, FILENAME_HARDLINK) == 0
filename = FILENAME_HARDLINK
end
rescue
# ハードリンクを作れなければファイル名を空文字列にする
end
getString(VARNAME_HARDLINK, filename)
end
def getString(varname, filename)
' const char* ' + varname + ' = "' + filename + '";'
end
end
class FileDeleter
def initialize
FILENAME_SET.each do |filename|
begin
File.delete(filename)
rescue
end
end
end
end
class Launcher
def initialize(argv)
raise AppInvalidParameter, HELP_MESSAGE if argv.size < 1
op = argv[0].strip.downcase
case op
when "create"
raise AppInvalidParameter, HELP_MESSAGE if argv.size < 2
FileDeleter.new
FileCreater.new(argv[1].strip)
when "delete"
FileDeleter.new
else
raise AppInvalidParameter
end
end
end
Launcher.new(ARGV)
exit(0)