-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathjwbtab.dpr
More file actions
373 lines (322 loc) · 8.97 KB
/
Copy pathjwbtab.dpr
File metadata and controls
373 lines (322 loc) · 8.97 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
program jwbtab;
{
Wakan TTextTable mgr tool.
}
{$APPTYPE CONSOLE}
uses
SysUtils,
Classes,
Windows,
TextTable,
JWBIO,
TextTableBrowser in 'Modules\Package\TextTableBrowser.pas' {fTextTableBrowser};
type
EBadUsage = class(Exception);
procedure BadUsage(msg: string); forward;
procedure ShowUsage(const errmsg: string = ''); forward;
procedure BadUsage(msg: string);
begin
raise EBadUsage.Create(msg);
end;
procedure ShowUsage(const errmsg: string);
var s: string;
begin
s := 'Usage: '+ExtractFilename(Paramstr(0))+' <table> <command> [/options option params]'#13#10
+''#13#10
+'<table> is a path and prefix to table files, i.e.'#13#10
+' .\some\path\Table'#13#10
+''#13#10
+'Supported commands:'#13#10
+' no command: display information'#13#10
+' export-text|dump -- export to text file / console'#13#10
+' import-text -- import from text file / keyboard'#13#10
+' dump-index <index-id> [/signatures] -- prints index contents'#13#10
+' check-index [index-id] -- checks all indices'#13#10
+' browse -- open a window with table contents'#13#10;
if errmsg<>'' then
s := errmsg + #13#10#13#10 + s;
writeln(s);
end;
var
Command: string;
TablePath: string; //common to many commands
IndexName: string;
DumpIndexParams: record
DumpSignatures: boolean;
end;
procedure ParseCommandLine();
var i: integer;
s: string;
begin
//Set to default
Command := '';
TablePath := '';
IndexName := '';
//No params at all => nothing to parse
if ParamCount<1 then exit;
TablePath := ParamStr(1);
i := 2;
while i<=ParamCount() do begin
s := AnsiLowerCase(ParamStr(i));
if Length(s)<=0 then continue;
//Options
if s[1]='/' then begin
//Common options
//none
//Command-related options
if Command='export-text' then begin
//No options
BadUsage('Invalid option: '+s);
end else
//Command-related options
if Command='import-text' then begin
//No options
BadUsage('Invalid option: '+s);
end else
if Command='dump-index' then begin
if s='/signatures' then
DumpIndexParams.DumpSignatures := true
else
BadUsage('Invalid option: '+s);
end else
if Command='check-index' then begin
//No options
BadUsage('Invalid option: '+s);
end else
if Command='browse' then begin
//No options
BadUsage('Invalid option: '+s);
end else
BadUsage('Invalid option: '+s);
end else
//Command
if Command='' then begin
Command := s;
//Aliases
if Command='dump' then
Command := 'export-text';
//Normal parsing
if Command='export-text' then begin
//Nothing to initialize
end else
if Command='import-text' then begin
//Nothing to initialize
end else
if Command='dump-index' then begin
FillChar(DumpIndexParams, SizeOf(DumpIndexParams), 0);
end else
if Command='check-index' then begin
//Nothing to initialize
end else
if Command='browse' then begin
//Nothing to initialize
end else
BadUsage('Invalid command or file: "'+s+'"');
end else
//Non-command non-option params (filename list etc)
begin
//Command-related options
if Command='export-text' then begin
//No options
BadUsage('Invalid param: '+s);
end else
if Command='import-text' then begin
//No options
BadUsage('Invalid param: '+s);
end else
if Command='dump-index' then begin
if IndexName='' then
IndexName := s
else
//No options
BadUsage('Invalid param: '+s);
end else
if Command='check-index' then begin
if IndexName='' then
IndexName := s
else
//No options
BadUsage('Invalid param: '+s);
end else
if Command='browse' then begin
//No options
BadUsage('Invalid param: '+s);
end else
BadUsage('Invalid param: "'+s+'"');
end;
Inc(i);
end; //of ParamStr enumeration
//Check that post-parsing conditions are met (non-conflicting options etc)
if TablePath='' then
BadUsage('Invalid table path.');
if Command='dump-index' then
if IndexName='' then
BadUsage('dump-index needs index id or name');
end;
function DataTypeToStr(const dt: char): string;
begin
case dt of
'b': Result := 'byte';
'w': Result := 'word';
'i': Result := 'integer';
'l': Result := 'bool';
's': Result := 'AnsiString';
'x': Result := 'WideString';
else
Result := '?';
end;
end;
procedure RunInfo();
var tt: TTextTable;
i: integer;
begin
tt := TTextTable.Create(nil, TablePath, true, false);
writeln('Table: '+TablePath);
writeln('Records: '+IntToStr(tt.RecordCount));
writeln('');
writeln('Fields:');
for i := 0 to Length(tt.fields) - 1 do
writeln(' '+tt.Fields[i].Name+': '+DataTypeToStr(tt.Fields[i].DataType) + ' ['+tt.Fields[i].DataType+']');
writeln('');
writeln('Seeks:');
for i := 0 to tt.seeks.Count - 1 do
writeln(' '+tt.Seeks[i].Name+': '+tt.Seeks[i].Declaration);
writeln('');
writeln('Orders:');
for i := 0 to tt.Orders.Count - 1 do
if i<tt.Seeks.Count-1 then
writeln(' '+tt.Orders[i]+' -> '+tt.Seeks[i+1].Name)
else
writeln(' '+tt.Orders[i]);
FreeAndNil(tt);
end;
procedure RunExportText();
var tt: TTextTable;
wri: TStreamEncoder;
begin
tt := TTextTable.Create(nil, TablePath, true, false);
SetConsoleOutputCP(CP_UTF8);
wri := ConsoleWriter(TUTF8Encoding.Create);
tt.ExportToText(wri, '');
FreeAndNil(wri);
FreeAndNil(tt);
end;
procedure RunImportText();
var tt: TTextTable;
rea: TStreamDecoder;
begin
tt := TTextTable.Create(nil, TablePath, true, false);
rea := ConsoleReader();
tt.ImportFromText(rea);
FreeAndNil(rea);
FreeAndNil(tt);
end;
type
TTextTableEx = class(TTextTable)
public
procedure DumpIndex(wri: TStreamEncoder; IndexId: integer; DumpSignatures: boolean);
end;
procedure TTextTableEx.DumpIndex(wri: TStreamEncoder; IndexId: integer; DumpSignatures: boolean);
var i: integer;
RecId: integer;
sign: string;
begin
for i := 0 to Self.RecordCount - 1 do begin
RecId := Self.TransOrder(i,IndexId);
if DumpSignatures then begin
sign := Self.SortRec(RecId, IndexId+1);
wri.Writeln(Format('%.8d = %.8d = %s', [i, RecId, sign]));
end else
wri.Writeln(Format('%.8d = %.8d', [i, RecId]));
end;
end;
function IndexParamToId(tt: TTextTable; const param: string): integer;
begin
if TryStrToInt(param, Result) then begin
if Result>=tt.Orders.Count then
raise Exception.Create('No index with id='+IntToStr(Result));
end else begin
Result := tt.Orders.IndexOf(param);
if Result < 0 then
Result := tt.Seeks.IndexOf(param);
if Result < 0 then
raise Exception.Create('Index not found: '+param);
end;
end;
procedure RunDumpIndex();
var tt: TTextTableEx;
wri: TStreamEncoder;
IndexId: integer;
begin
SetConsoleOutputCP(CP_UTF8);
tt := TTextTableEx.Create(nil, TablePath, true, false);
IndexId := IndexParamToId(tt,IndexName);
wri := ConsoleWriter(TUTF8Encoding.Create);
tt.DumpIndex(wri, IndexId, DumpIndexParams.DumpSignatures);
FreeAndNil(wri);
FreeAndNil(tt);
end;
procedure RunCheckIndex();
var tt: TTextTable;
IndexId, BadId: integer;
begin
tt := TTextTable.Create(nil, TablePath, true, false);
if IndexName<>'' then
IndexId := IndexParamToId(tt,IndexName)
else
IndexId := -1;
if IndexId>0 then begin
if tt.CheckIndex(IndexId) then
writeln('Index OK.')
else
writeln('Index invalid.')
end else begin
BadId := tt.CheckIndices;
if BadId < 0 then
writeln('All indices OK.')
else
writeln('Invalid index '+IntToStr(BadId));
end;
FreeAndNil(tt);
end;
procedure RunBrowse();
var tt: TTextTable;
fBrowse: TfTextTableBrowser;
begin
tt := TTextTable.Create(nil, TablePath, true, false);
fBrowse := TfTextTableBrowser.Create(nil);
fBrowse.SetTable(tt);
fBrowse.ShowModal;
FreeAndNil(fBrowse);
FreeAndNil(tt);
end;
begin
try
ParseCommandLine();
if TablePath='' then
ShowUsage()
else
if Command='' then
RunInfo()
else
if Command='export-text' then
RunExportText()
else
if Command='import-text' then
RunImportText()
else
if Command='dump-index' then
RunDumpIndex()
else
if Command='check-index' then
RunCheckIndex()
else
if Command='browse' then
RunBrowse()
else
BadUsage('Invalid command: '+Command);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.