-
Notifications
You must be signed in to change notification settings - Fork 0
/
adams.rb
executable file
·226 lines (196 loc) · 4.74 KB
/
adams.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require 'optparse'
# for safety of eval
def extern_block
binding
end
Eb = extern_block
class String
def emphasize
redColor="[31;1m"
blackColor="[0m"
redColor + self + blackColor
end
def to_f
eval(self, Eb).to_f
end
end
class ZHAdams
attr_accessor :model
attr_accessor :in, :out
attr_accessor :file, :ln
def set_opt options, ioptions={}
OptionParser.new do |opts|
opts.banner = ioptions[:usage] if ioptions.member? :usage
opts.on("-o", "--output FILENAME", "Set output file") do |f|
options[:output] = f
end
opts.on("-i", "--stdin", "Allow input from tty-stdin") do
options[:stdin] = true
end
opts.on("-D", "--define COMMAND", "Execute command") do |c|
eval(c, Eb)
end
opts.on_tail("-c", "--coordinate", "Show coordinates in euler angles") do
puts <<EOF
Euler angles, 313
X\t0,0,0
Y\t90,180,0
Z\t90,90,90
EOF
exit
end
if ioptions.member? :sample
opts.on_tail("-s", "--sample", "Show sample input") do
$stdout.puts ioptions[:sample]
exit
end
end
if ioptions.member? :version
opts.on_tail("--version", "Show version") do
$stderr.puts ioptions[:version]
exit
end
end
end
end
def stdin inputFiles, isStdin
if isStdin || inputFiles.empty?
inputFd = $stdin
@file = 'stdin'
else
@file = inputFiles[0]
begin
inputFd = File.open @file
rescue
$stderr.puts "Can't open file #{inputFile}, #{$!}"
exit
end
end
inputFd
end
def stdout outFile
if outFile
begin
outFd = File.open outFile, 'w'
rescue
$stderr.puts "Can't write file #{inputFile}, #{$!}"
exit
end
else
outFd = $stdout
end
outFd
end
def stdio inputFiles, options
@in, @out = self.stdin(inputFiles, options[:stdin]), self.stdout(options[:output])
end
def gen_tokens line
line.chomp.split(',').map{|token| token.strip}
end
def parseHeader line, headerId, block
tokens = gen_tokens line
if tokens[0] != headerId
raise Errno::EINVAL, "Input file format err."
elsif tokens[1] == nil || tokens[1].empty?
raise Errno::EINVAL, "No model name specified"
end
@model = tokens[1]
block.call tokens, @out if block
end
def parse headerId, lineBlock = nil, headerBlock = nil, &lineBlockA
header = @in.gets
unless $DEBUG
begin
self.parseHeader header, headerId, headerBlock
rescue
addr = "#{@file}:#{@ln}"
# @errors[addr] ||= 0
# @errors[addr] += 1
# if @errors[addr] == 1
$stderr.print addr.emphasize + ': '
$stderr.puts $!
$stderr.print header
# end
end
else
self.parseHeader header, headerId, headerBlock
end
@ln = 1
while (line = @in.gets)
self.ln += 1 if @line_inc
lineBlock ||= lineBlockA
self.parseContentLine line, lineBlock
end
end
def parseContentLine line, block
line.strip!
case line[0..0]
when '!'
eval(line[1..-1], Eb)
when '@'
@out.puts line[1..-1]
when '#'
else
if @hereDoc
if line == @hereDoc
@hereDoc = nil
else
if @escapeHere
@out.puts eval('"' + line + '"', Eb)
else
@out.puts line
end
end
elsif (line[0..1] == '<<')
@escapeHere = (line[2..2] == '<')
@hereDoc = (line.match /^<<<?[^A-Za-z_]*([A-Za-z_][A-Za-z_0-9]*)/)[1]
else
tokens = gen_tokens line
return if !tokens || tokens.empty?
case tokens[0][0..0]
when '%'
self.ln = tokens[0][1..-1].to_i
@file = tokens[1]
@line_inc = false
else
unless $DEBUG
begin
block.call tokens, @ln, @out
rescue
addr = "#{@file}:#{@ln}"
# @errors[addr] ||= 0
# @errors[addr] += 1
# if @errors[addr] == 1
$stderr.print addr.emphasize + ': '
$stderr.puts $!
$stderr.print line
# end
end
else
block.call tokens, @ln, @out
end
end
end
end
end
def initialize ioptions={}
options = {}
opts = self.set_opt options, ioptions
yield opts
begin
opts.parse!
if ARGV.empty? && !options[:stdin] && $stdin.isatty
raise Errno::EINVAL, "error: no input files"
end
rescue
$stderr.puts "#{$!}"
$stderr.puts opts
exit
end
# inputFiles = ARGV
self.stdio ARGV, options
@line_inc = true
@hereDoc = false
# @errors = {}
end
end