-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchutil_app.rbbas
298 lines (248 loc) · 7.2 KB
/
archutil_app.rbbas
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#tag Class
Protected Class archutil_app
Inherits ConsoleApplication
#tag Event
Function Run(args() as String) As Integer
dim i as integer
dim errors(-1) as string
dim t as string
dim template_text as string
input_encoding = encodings.utf8
output_encoding = encodings.utf8
// OPTIONS
for i = 3 to args.ubound
LoadOption( args(i) )
next
ShowVersion
// SHOW DOCS and quit if appropriate
ShowDocs args
// SOURCE
src = GetFileOrQuit( args(1), "Source" )
auto_encoding = sniffEncoding( src )
if auto_encoding <> "" then
print "Input Encoding Detected: " + auto_encoding
input_encoding = EncodingByName( auto_encoding )
end if
Assert args.ubound > 1, "Missing argument, <template> path."
template = GetFileOrQuit( args(2), "Template" )
// DESTINATION
if args.ubound < 3 or args(3).left(1) = "-" then
dest = GetFolderItem("")
else
dest = GetFileOrQuit( args(3), "Destination", true )
end if
print "Input Encoding: " + input_encoding.internetName
print "Output Encoding: " + output_encoding.internetName
if overwrite then
print "Overwrite is set to TRUE"
else
print "Overwrite is set to FALSE"
end if
if verbose then
print "Verbose is set to TRUE"
else
print "Verbose is set to FALSE"
end if
print "File name suffix: " + file_name_suffix
select case newline
case UnixLinefeeds
print "Newline format: UNIX (ASCII 10)"
case MacClassicLinefeeds
print "Newline format: Mac Class (ASCII 13)"
case DOSLineFeeds
print "Newline format: DOS (ASCII 13 + ASCII 10)"
end select
template_text = template.LoadAllText
print "Template (" + template.name + ") loaded."
ss = new Spreadsheet( src, input_encoding )
print "Source (" + src.name + ") loaded."
for i = 1 to ss.colcount
t = SpreadsheetAndTemplateToXML( ss, template_text, i, errors, dest, file_name_field, file_name_suffix, overwrite )
next
if verbose and errors.ubound >= 0 then
print " "
print "Errors"
print "------"
for i = 0 to errors.ubound
print errors(i)
next
end if
End Function
#tag EndEvent
#tag Event
Sub UnhandledException(error As RuntimeException)
print error.Message
Quit
End Sub
#tag EndEvent
#tag Method, Flags = &h0
Function GetFileOrQuit(path as String, name as string = "", is_directory as boolean = false) As folderItem
dim f as folderItem
f = GetShellFolderItem( path )
Assert f <> nil and f.exists, path + " does not exist.", true
if is_directory then
Assert f.directory, path + " is not a directory.", true
else
Assert not f.directory, path + " is a directory.", true
end if
if name <> "" and verbose then
print name + ": " + f.ShellPath
end if
return f
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub LoadOption(s as String)
if s.left(1) = "-" then
select case s.mid(2,1)
case "c"
file_name_field = s.mid(3)
case "e"
input_encoding = EncodingByName( s.mid(3) )
case "f"
output_encoding = EncodingByName( s.mid(3) )
case "o"
overwrite = true
case "v"
verbose = true
case "s"
file_name_suffix = s.mid(3)
case "n"
select case s.mid(3)
case "m"
newline = MacClassicLinefeeds
case "d"
newline = DOSLineFeeds
end select
end select
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub ShowDocs(args() as string)
dim i as integer
if args.ubound > 2 then
return
end if
if args.ubound > 1 then
select case args(1)
case "-elist"
for i = 0 to encodings.Count - 1
print encodings.Item(i).internetName
next
quit
case "help"
case "-help"
case "?"
case "man"
case else
return
end select
end if
print "usage: archutil <source> <template> [<dest>] [options]"
print " <source> source file path"
print " <template> template file path"
print " <dest> destination folder path (default is app location)"
print " [options]:"
print " -e<encoding_string> set input encoding (default auto)"
print " archutil -elist to list all encodings"
print " -f<encoding_string> set output encoding (default UTF8)"
print " -c<filename_column> set the filename (default Filename)"
print " -o overwrite existing files (default FALSE)"
print " -v verbose output (default FALSE)"
print " -s<suffix> sets output file name suffix (default .xml)"
print " -n(u|m|d) sets output newline to ASCII 10, 13, or 13+10"
print " (default ASCII 10)"
quit
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub ShowVersion()
if verbose then
print "================================================"
print " archutil v" + kVersion + " ©2009 University of Alabama"
print " written by Tonio Loewald <[email protected]>"
print " build " + str(kBuildNumber)
print "================================================"
else
print "archutil v" + kVersion + " | ©2009 | build " + str(kBuildNumber)
end if
End Sub
#tag EndMethod
#tag Property, Flags = &h0
auto_encoding As string
#tag EndProperty
#tag Property, Flags = &h0
dest As folderitem
#tag EndProperty
#tag Property, Flags = &h0
file_name_field As String = "Filename"
#tag EndProperty
#tag Property, Flags = &h0
file_name_suffix As String = ".xml"
#tag EndProperty
#tag Property, Flags = &h0
input_encoding As TextEncoding
#tag EndProperty
#tag Property, Flags = &h0
newline As Integer = unixlinefeeds
#tag EndProperty
#tag Property, Flags = &h0
output_encoding As TextEncoding
#tag EndProperty
#tag Property, Flags = &h0
overwrite As boolean
#tag EndProperty
#tag Property, Flags = &h0
src As folderitem
#tag EndProperty
#tag Property, Flags = &h0
ss As spreadsheet
#tag EndProperty
#tag Property, Flags = &h0
template As folderitem
#tag EndProperty
#tag Property, Flags = &h0
verbose As boolean
#tag EndProperty
#tag Constant, Name = kBuildNumber, Type = Double, Dynamic = False, Default = \"3", Scope = Public
#tag EndConstant
#tag Constant, Name = kVersion, Type = String, Dynamic = False, Default = \"0.2", Scope = Public
#tag EndConstant
#tag ViewBehavior
#tag ViewProperty
Name="auto_encoding"
Group="Behavior"
Type="string"
#tag EndViewProperty
#tag ViewProperty
Name="file_name_field"
Group="Behavior"
InitialValue="Filename"
Type="String"
#tag EndViewProperty
#tag ViewProperty
Name="file_name_suffix"
Group="Behavior"
InitialValue=".xml"
Type="String"
#tag EndViewProperty
#tag ViewProperty
Name="newline"
Group="Behavior"
InitialValue="unixlinefeeds"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="overwrite"
Group="Behavior"
Type="boolean"
#tag EndViewProperty
#tag ViewProperty
Name="verbose"
Group="Behavior"
Type="boolean"
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass