-
Notifications
You must be signed in to change notification settings - Fork 0
/
aaae.rb
227 lines (173 loc) · 3.69 KB
/
aaae.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
227
#!/usr/bin/env ruby -w
#
# apple ascii art encoder.
#
require 'optparse'
#
# read stdin
# --trim -> trim trailing whitespace
# --dle -> compress > 2 spaces into a dle
# --indent=# -> indent it by # spaces
# --width=# -> console width (default = 80)
# --center -> center block assuming width
# --target=x -> target language (c, orca, mpw, etc)
#
# todo -- expand tabs? (--ts=8)
# todo -- left/right/center slign block?
#
#
# output char name[] = { 0x01, 0x02, ... };
#
def output_bytes(name, data)
tmp = data.join('')
index = 0
if name
puts "char #{name}[] = {"
else
puts "{"
end
tmp.each_byte {|b|
print " " if index == 0
printf("0x%02x, ", b)
index = index + 1
if index == 8
print "\n"
index = 0
end
}
puts "0x00\n"
#print "\n" if index > 0
puts "};"
end
#
# output char name[] = { "bleh\r","bleh\r" };
#
def output_strings(name, data)
map = {
0x0d => "\\r",
0x0a => "\\n",
}
if name
puts "char #{name}[] = "
end
data.each {|x|
# escape \
x = x.gsub(/([\\"])/) {|m| '\\' + m }
# escape "
#x = x.gsub(/"/, '\"')
# escape chars...
# unfortunately, \x doesn't have a length limit so
# it can't be used. octal sucks but it's limited
# to 3 characters.
x = x.gsub(/([\x00-\x1f\x7f])/) {|m|
c = m.getbyte(0)
next map[c] if map[c]
sprintf("\\%03o", c)
}
printf(" \"%s\"\n", x)
}
puts ";"
end
data = []
options = {
:trim => true,
:dle => false,
:indent => 0,
:width => 80,
:center => false,
:dry_run => false,
:name => nil,
:format => :string
}
OptionParser.new do |opts|
opts.banner = "Usage: aaae.rb [options] [input file]"
opts.on("--[no-]trim", "Trim trailing white space from input.") do |x|
options[:trim] = x
end
opts.on("--[no-]dle", "Use DLE to compress spaces.") do |x|
options[:dle] = x
end
opts.on("--width N", Integer, "Set screen width. Default = 80.") do |x|
options[:width] = x
end
opts.on("--indent N", Integer, "Indent N spaces.") do |x|
options[:indent] = x
end
opts.on("--name N", String, "Set variable name.") do |x|
if x.match(/^[A-Za-z_][A-Za-z0-9_]*$/)
options[:name] = x
else
$stderr.puts "Invalid name -- #{x}"
end
end
opts.on("-f", "--format FORMAT", [:string, :bytes], "Set output format (string, bytes)") do |x|
options[:format] = x
end
opts.on("--[no-]center", "Center output.") do |x|
options[:center] = x
end
opts.on("--dry-run", "Simulated output.") do
options[:dry_run] = true
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts "aaae.rb 0.1"
exit
end
end.parse!
ARGF.each_line {|x|
x.chomp!
x.rstrip! if options[:trim]
data.push(x)
}
# option to strip leading space?
if (options[:trim])
end
# get the width of the block.
bw = data.reduce(0) {|akku, x|
next x.length if x.length > akku
next akku
}
if options[:indent] > 0
space = ' ' * options[:indent]
data = data.map {|x| space + x }
end
if options[:center]
indent = (options[:width] - bw) / 2
space = ' ' * indent
data = data.map {|x| space + x }
end
# todo -- check if any lines > screen width...
if options[:dry_run]
width = options[:width]
print "+", "-" * width, "+\n"
#print "|", " " * width, "|\n"
data.each {|x|
printf("|%-*s|\n", width, x)
}
#print "|", " " * width, "|\n"
print "+", "-" * width, "+\n"
exit 0
end
# dle!
if options[:dle]
data = data.map {|x|
x.gsub(/( {3,})/) {|m|
size = m.length
next "\x10" + (size + 32).chr
}
}
end
# now add r/n
data = data.map {|x| x + "\r" }
# now output
# should have flag for text vs bytes, asm, etc.
case options[:format]
when :string
output_strings(options[:name], data)
when :bytes
output_bytes(options[:name], data)
end