source: oup/releases/0.28a/Unit2_functions.pas@ 1060

Last change on this file since 1060 was 27, checked in by alloc, 18 years ago
File size: 21.0 KB
Line 
1UNIT Unit2_functions;
2INTERFACE
3USES Classes, Dialogs, SysUtils, StrUtils, Math,
4 Unit3_data, ABSDecUtil, ABSMain, DB;
5
6TYPE
7 TExportSet=SET OF (DO_dat,DO_raw,DO_convert,DO_toone);
8
9FUNCTION GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringList;
10FUNCTION GetExtensionsList:TStringList;
11
12FUNCTION HexToLong(hex:String):LongWord;
13FUNCTION Decode_Int(buffer:Tdata):LongWord;
14FUNCTION Encode_Int(input:LongWord):Tdata;
15FUNCTION Decode_Float(buffer:Tdata):Single;
16FUNCTION Encode_Float(input:Single):Tdata;
17FUNCTION DataToBin(data:Tdata):String;
18FUNCTION BinToInt(bin:String):Byte;
19
20FUNCTION GetFileInfo(fileid:LongWord):TFileInfo;
21FUNCTION LoadDatInfos(filename:String):Boolean;
22PROCEDURE OpenDatabase(FileName:String);
23PROCEDURE CloseDatabase;
24
25FUNCTION LoadDatFile(fileid:LongWord):Tdata;
26PROCEDURE UpdateDatFile(fileid:LongWord; data:Tdata);
27FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
28FUNCTION UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
29
30FUNCTION LoadRawFile(fileid,dat_offset,raw_addr,size:LongWord; loc_sep:Boolean; target:Pointer):Boolean;
31FUNCTION LoadRawFileByIDOffset(fileid,dat_offset:LongWord; target:Pointer):Boolean;
32FUNCTION UpdateRawFile(rawinfo:TRawInfo; target:Pointer):Boolean;
33
34FUNCTION ExportFile(fileid:LongWord; filename:String; settings:TExportSet; path:String):Integer;
35
36FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
37FUNCTION FormatFileSize(size:LongWord):String;
38FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
39FUNCTION GetWinFileName(name:String):String;
40FUNCTION GetExtractPath:String;
41
42FUNCTION Explode(_string:String; delimiter:Char):TStringList;
43
44
45IMPLEMENTATION
46USES Unit4_Exporters, Unit9_data_structures;
47
48VAR
49 Database:TABSDatabase;
50 Query:TABSQuery;
51
52TYPE
53 TValueSwitcher=Record
54 CASE IsFloat: Boolean OF
55 True: (ValueFloat:Single);
56 False: (ValueInt:LongWord);
57 END;
58
59FUNCTION NormalizeHexString(VAR hex:String):Boolean;
60 VAR
61 i:Byte;
62 BEGIN
63 IF hex[1]='$' THEN BEGIN
64 FOR i:=1 TO Length(hex)-1 DO BEGIN
65 hex[i]:=hex[i+1];
66 END;
67 SetLength(hex, Length(hex)-1);
68 END;
69 IF (hex[1]='0') AND (UpCase(hex[2])='X') THEN BEGIN
70 FOR i:=1 TO Length(hex)-2 DO BEGIN
71 hex[i]:=hex[i+2];
72 END;
73 SetLength(hex, Length(hex)-2);
74 END;
75 IF Length(hex)=0 THEN
76 Result:=False
77 ELSE
78 Result:=True;
79 END;
80
81FUNCTION HexToLong(hex:String):LongWord;
82 VAR
83 i:Byte;
84 BEGIN
85 IF NormalizeHexString(hex) THEN BEGIN
86 hex:=UpperCase(hex);
87 Result:=0;
88 FOR i:=1 TO Length(hex) DO BEGIN
89 Result:=Result SHL 4;
90 CASE hex[i] OF
91 '0'..'9': Result:=Result+Ord(hex[i])-48;
92 'A'..'F': Result:=Result+Ord(hex[i])-55;
93 ELSE
94 Result:=0;
95 Exit;
96 END;
97 END;
98 END ELSE BEGIN
99 Result:=0;
100 END;
101 END;
102
103FUNCTION Decode_Int(buffer:Tdata):LongWord;
104 BEGIN
105 Result:=buffer[0]+buffer[1]*256+buffer[2]*256*256+buffer[3]*256*256*256;
106 END;
107FUNCTION Encode_Int(input:LongWord):Tdata;
108 BEGIN
109 SetLength(Result,4);
110 Result[0]:=input MOD 256;
111 input:=input DIV 256;
112 Result[1]:=input MOD 256;
113 input:=input DIV 256;
114 Result[2]:=input MOD 256;
115 input:=input DIV 256;
116 Result[3]:=input MOD 256;
117 END;
118FUNCTION Decode_Float(buffer:Tdata):Single;
119 VAR _valueswitcher:TValueSwitcher;
120 BEGIN
121 _valueswitcher.ValueInt:=Decode_Int(buffer);
122 Result:=_valueswitcher.ValueFloat;
123 IF IsNAN(Result) THEN Result:=0.0;
124 END;
125FUNCTION Encode_Float(input:Single):Tdata;
126 VAR _valueswitcher:TValueSwitcher;
127 BEGIN
128 _valueswitcher.ValueFloat:=input;
129 Result:=Encode_Int(_valueswitcher.ValueInt);
130 END;
131
132FUNCTION DataToBin(data:Tdata):String;
133 VAR
134 i,j:Byte;
135 singlebyte:Byte;
136 bytepart:String;
137 BEGIN
138 SetLength(bytepart,8);
139 Result:='';
140 FOR i:=0 TO High(data) DO BEGIN
141 singlebyte:=data[i];
142 FOR j:=7 DOWNTO 0 DO BEGIN
143 bytepart[j+1]:=Char((singlebyte AND $01)+48);
144 singlebyte:=singlebyte SHR 1;
145 END;
146 Result:=Result+bytepart+' ';
147 END;
148 END;
149FUNCTION BinToInt(bin:String):Byte;
150 VAR
151 Add: Integer;
152 i: Byte;
153 BEGIN
154 Result:=0;
155 IF Length(bin)<>8 THEN Exit;
156 Add:=1;
157 FOR i:=8 DOWNTO 1 DO BEGIN
158 IF NOT (bin[i] IN ['0','1']) THEN Exit;
159 IF bin[i] = '1' THEN Inc(Result,Add);
160 Add:=Add SHL 1;
161 END;
162 END;
163
164FUNCTION GetFileInfo(fileid:LongWord):TFileInfo;
165 BEGIN
166 IF opened_state=opened_dat THEN BEGIN
167 Result:=dat_files[fileid];
168 END ELSE BEGIN
169 Query.SQL.Text:='SELECT * FROM datfiles WHERE id='+IntToStr(fileid)+' ORDER BY id ASC;';
170 Query.Open;
171 IF Query.RecordCount=1 THEN BEGIN
172 Query.First;
173 Result.ID:=Query.FieldByName('id').AsInteger;
174 Result.Name:=Query.FieldByName('name').AsString;
175 Result.Extension:=Query.FieldByName('extension').AsString;
176 Result.FileName:=FormatNumber(Result.ID,5,'0')+'-'+Result.Name+'.'+Result.Extension;
177 Result.Size:=Query.FieldByName('size').AsInteger;
178 Result.FileType:=Query.FieldByName('contenttype').AsInteger;
179 Result.DatAddr:=0;
180 Result.opened:=False;
181 END;
182 Query.Close;
183 END;
184 END;
185
186
187FUNCTION GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringList;
188 VAR
189 i:LongWord;
190 where:String;
191 where_ext:String;
192 BEGIN
193 SetLength(Result,0);
194 IF opened_state=opened_dat THEN BEGIN
195 FOR i:=0 TO dat_header.Files-1 DO BEGIN
196 IF ( (Length(ext)=0) OR (Pos(dat_files[i].Extension,ext)>0) ) AND
197 ( (Length(pattern)=0) OR (Pos(UpperCase(pattern),UpperCase(dat_files[i].Name))>0) ) THEN BEGIN
198 IF NoEmptyFiles THEN BEGIN
199 IF (dat_files[i].FileType AND $02)=0 THEN BEGIN
200 SetLength(Result,Length(Result)+1);
201 Result[High(Result)]:=dat_files[i].FileName;
202 END;
203 END ELSE BEGIN
204 SetLength(Result,Length(Result)+1);
205 Result[High(Result)]:=dat_files[i].FileName;
206 END;
207 END;
208 END;
209 END ELSE BEGIN
210 where:='';
211 IF Length(ext)>0 THEN BEGIN
212 IF Length(where)>0 THEN where:=where+' AND ';
213 IF Pos(',',ext)>0 THEN BEGIN
214 i:=1;
215 where_ext:='';
216 WHILE i<Length(ext) DO BEGIN
217 IF Length(where_ext)>0 THEN where_ext:=where_ext+' OR ';
218 where_ext:=where_ext+'(extension="'+MidStr(ext,i,4)+'")';
219 i:=i+5;
220 END;
221 where:=where+'('+where_ext+')';
222 END ELSE BEGIN
223 where:=where+'(extension="'+ext+'")';
224 END;
225 END;
226 IF Length(pattern)>0 THEN BEGIN
227 IF Length(where)>0 THEN where:=where+' AND ';
228 where:=where+'(name LIKE "%'+pattern+'%")';
229 END;
230 IF NoEmptyFiles THEN BEGIN
231 IF Length(where)>0 THEN where:=where+' AND ';
232 where:=where+'(contenttype<>2)';
233 END;
234 IF Length(where)>0 THEN where:=' WHERE '+where;
235 Query.SQL.Text:='SELECT id,name,extension FROM datfiles'+where+' ORDER BY id ASC;';
236 Query.Open;
237 IF Query.RecordCount>0 THEN BEGIN
238 Query.First;
239 SetLength(Result,Query.RecordCount);
240 i:=0;
241 REPEAT
242 Result[i]:=FormatNumber(Query.FieldByName('id').AsInteger,5,'0')+'-'+Query.FieldByName('name').AsString+'.'+Query.FieldByName('extension').AsString;
243 Inc(i);
244 Query.Next;
245 UNTIL Query.EOF;
246 END;
247 Query.Close;
248 END;
249 END;
250
251FUNCTION GetExtensionsList:TStringList;
252 VAR
253 i:LongWord;
254 BEGIN
255 SetLength(Result,0);
256 IF opened_state=opened_dat THEN BEGIN
257 FOR i:=0 TO dat_header.Extensions-1 DO BEGIN
258 SetLength(Result,Length(Result)+1);
259 WITH dat_extensionsmap[i] DO BEGIN
260 Result[High(Result)]:=Extension[3]+Extension[2]+Extension[1]+Extension[0]+' ('+IntToStr(ExtCount)+')';
261 END;
262 END;
263 END ELSE BEGIN
264 Query.SQL.Text:='SELECT extension,count(extension) AS x FROM datfiles GROUP BY extension ORDER BY extension ASC;';
265 Query.Open;
266 IF Query.RecordCount>0 THEN BEGIN
267 SetLength(Result,Query.RecordCount);
268 i:=0;
269 REPEAT
270 Result[i]:=Query.FieldByName('extension').AsString+' ('+IntToStr(Query.FieldByName('x').AsInteger)+')';
271 Inc(i);
272 Query.Next;
273 UNTIL Query.EOF;
274 END;
275 Query.Close;
276 END;
277 END;
278
279FUNCTION LoadDatInfos(filename:String):Boolean;
280 VAR i:LongWord;
281 dat_file:TFileStream;
282 header_pc,header_mac:Boolean;
283 BEGIN
284 Result:=True;
285 opened_state:=opened_dat;
286 dat_filename:=filename;
287 raw_filename:=MidStr(filename,1,Length(filename)-3)+'raw';
288 dat_file:=TFileStream.Create(filename, fmOpenRead);
289 dat_file.Read(dat_header,SizeOf(dat_header));
290 header_pc:=True;
291 header_mac:=True;
292 FOR i:=0 TO High(dat_header.Ident) DO BEGIN
293 IF dat_header.Ident[i]<>header_ident1_pc[i] THEN BEGIN
294 header_pc:=False;
295 END;
296 IF dat_header.Ident[i]<>header_ident1_mac[i] THEN BEGIN
297 header_mac:=False;
298 END;
299 END;
300 IF NOT (header_pc OR header_mac) THEN BEGIN
301 Result:=False;
302 Exit;
303 END ELSE BEGIN
304 IF (header_pc AND NOT header_mac) THEN
305 dat_os_mac:=False
306 ELSE
307 IF (NOT header_pc AND header_mac) THEN
308 dat_os_mac:=True
309 ELSE BEGIN
310 Result:=False;
311 Exit;
312 END;
313 END;
314 SetLength(dat_filesmap,dat_header.Files);
315 SetLength(dat_files,dat_header.Files);
316 FOR i:=0 TO dat_header.Files-1 DO dat_file.Read(dat_filesmap[i],SizeOf(dat_filesmap[i]));
317 FOR i:=0 TO dat_header.Files-1 DO BEGIN
318 dat_files[i].Extension:=dat_filesmap[i].Extension;
319 dat_files[i].Extension:=ReverseString(dat_files[i].Extension);
320 dat_files[i].Size:=dat_filesmap[i].FileSize;
321 dat_files[i].FileType:=dat_filesmap[i].FileType;
322 dat_files[i].DatAddr:=dat_filesmap[i].DataAddr-8+dat_header.DataAddr;
323 IF (dat_filesmap[i].FileType AND $01)=0 THEN BEGIN
324 dat_file.Seek(dat_filesmap[i].NameAddr+dat_header.NamesAddr,soFromBeginning);
325 SetLength(dat_files[i].Name,100);
326 dat_file.Read(dat_files[i].Name[1],100);
327 dat_files[i].Name:=MidStr(dat_files[i].Name,1+4,Pos(#0,dat_files[i].Name)-1-4);
328 END ELSE BEGIN
329 dat_files[i].Name:='';
330 END;
331 dat_files[i].FileName:=FormatNumber(i,5,'0')+'-'+dat_files[i].Name+'.'+dat_files[i].Extension;
332 END;
333 dat_file.Seek($40+dat_header.Files*$14,soFromBeginning);
334 SetLength(dat_namedfilesmap,dat_header.NamedFiles);
335 FOR i:=0 TO dat_header.NamedFiles-1 DO dat_file.Read(dat_namedfilesmap[i],SizeOf(dat_namedfilesmap[i]));
336
337 dat_file.Seek($40+dat_header.Files*$14+dat_header.NamedFiles*$8,soFromBeginning);
338 SetLength(dat_extensionsmap,dat_header.Extensions);
339 FOR i:=0 TO dat_header.Extensions-1 DO dat_file.Read(dat_extensionsmap[i],SizeOf(dat_extensionsmap[i]));
340
341 dat_file.Free;
342 END;
343
344
345FUNCTION LoadDatFile(fileid:LongWord):Tdata;
346 VAR
347 dat_file:TFileStream;
348 mem:TStream;
349 BEGIN
350 IF opened_state=opened_dat THEN BEGIN
351 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
352 dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
353 SetLength(Result,dat_files[fileid].Size);
354 dat_file.Read(Result[0],dat_files[fileid].Size);
355 dat_file.Free;
356 END ELSE BEGIN
357 Query.SQL.Text:='SELECT data FROM datfiles WHERE id='+IntToStr(fileid)+';';
358 Query.Open;
359 IF Query.RecordCount>0 THEN BEGIN
360 mem:=Query.CreateBlobStream(Query.FieldByName('data'),bmRead);
361 SetLength(Result,mem.Size);
362 mem.Seek(0,soFromBeginning);
363 mem.Read(Result[0],mem.Size);
364 mem.Free;
365 END;
366 Query.Close;
367 END;
368 END;
369PROCEDURE UpdateDatFile(fileid:LongWord; data:Tdata);
370 VAR
371 dat_file:TFileStream;
372 BEGIN
373 IF opened_state=opened_dat THEN BEGIN
374 dat_file:=TFileStream.Create(dat_filename, fmOpenReadWrite);
375 dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
376 dat_file.Write(data[0],Length(data));
377 dat_file.Free;
378 END ELSE BEGIN
379 END;
380 END;
381
382FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
383 VAR
384 dat_file:TFileStream;
385 mem:TStream;
386 BEGIN
387 Result:=False;
388 IF opened_state=opened_dat THEN BEGIN
389 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
390 Result:=True;
391 dat_file.Seek(dat_files[fileid].DatAddr+offset,soFromBeginning);
392 dat_file.Read(target^,size);
393 dat_file.Free;
394 END ELSE BEGIN
395 Query.SQL.Text:='SELECT data FROM datfiles WHERE id='+IntToStr(fileid)+';';
396 Query.Open;
397 IF Query.RecordCount>0 THEN BEGIN
398 mem:=Query.CreateBlobStream(Query.FieldByName('data'),bmRead);
399 mem.Seek(offset,soFromBeginning);
400 mem.Read(target^,size);
401 mem.Free;
402 END;
403 Query.Close;
404 END;
405 END;
406FUNCTION UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
407 VAR
408 dat_file:TFileStream;
409 BEGIN
410 Result:=False;
411 IF opened_state=opened_dat THEN BEGIN
412 dat_file:=TFileStream.Create(dat_filename, fmOpenReadWrite);
413 Result:=True;
414 dat_file.Seek(dat_files[fileid].DatAddr+offset,soFromBeginning);
415 dat_file.Write(target^,size);
416 dat_file.Free;
417 END ELSE BEGIN
418 END;
419 END;
420
421FUNCTION LoadRawFile(fileid,dat_offset,raw_addr,size:LongWord; loc_sep:Boolean; target:Pointer):Boolean;
422 VAR
423 filestream:TFileStream;
424 mem:TStream;
425 BEGIN
426 Result:=False;
427 IF opened_state=opened_dat THEN BEGIN
428 Result:=True;
429 IF NOT loc_sep THEN
430 filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.raw'),fmOpenRead)
431 ELSE
432 filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.sep'),fmOpenRead);
433 filestream.Seek(raw_addr,soFromBeginning);
434 filestream.Read(target^,size);
435 filestream.Free;
436 END ELSE BEGIN
437 Query.SQL.Text:='SELECT data FROM rawmap WHERE (src_id='+IntToStr(fileid)+') AND (src_link_offset='+IntToStr(dat_offset)+');';
438 Query.Open;
439 IF Query.RecordCount>0 THEN BEGIN
440 Result:=True;
441 mem:=Query.CreateBlobStream(Query.FieldByName('data'),bmRead);
442 mem.Seek(0,soFromBeginning);
443 mem.Read(target^,size);
444 mem.Free;
445 END;
446 Query.Close;
447 END;
448 END;
449FUNCTION LoadRawFileByIDOffset(fileid,dat_offset:LongWord; target:Pointer):Boolean;
450 VAR
451 i:Byte;
452 raw_info:TRawInfo;
453 mem:TStream;
454 BEGIN
455 Result:=False;
456 IF opened_state=opened_dat THEN BEGIN
457 Result:=True;
458 raw_info:=GetRawInfo(fileid,dat_offset);
459 LoadRawFile(fileid,dat_offset,raw_info.raw_addr,raw_info.raw_size,raw_info.loc_sep,target);
460 END ELSE BEGIN
461 Query.SQL.Text:='SELECT data FROM rawmap WHERE (src_id='+IntToStr(fileid)+') AND (src_link_offset='+IntToStr(dat_offset)+');';
462 Query.Open;
463 IF Query.RecordCount>0 THEN BEGIN
464 Result:=True;
465 mem:=Query.CreateBlobStream(Query.FieldByName('data'),bmRead);
466 mem.Seek(0,soFromBeginning);
467 mem.Read(target^,mem.size);
468 mem.Free;
469 END;
470 Query.Close;
471 END;
472 END;
473FUNCTION UpdateRawFile(rawinfo:TRawInfo; target:Pointer):Boolean;
474 VAR
475 filestream:TFileStream;
476 BEGIN
477 Result:=False;
478 IF opened_state=opened_dat THEN BEGIN
479 Result:=True;
480 IF NOT rawinfo.loc_sep THEN
481 filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.raw'),fmOpenReadWrite)
482 ELSE
483 filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.sep'),fmOpenReadWrite);
484 filestream.Seek(rawinfo.raw_addr,soFromBeginning);
485 filestream.Write(target^,rawinfo.raw_size);
486 filestream.Free;
487 END ELSE BEGIN
488 END;
489 END;
490
491
492FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
493 BEGIN
494 Result:=AnsiReplaceStr(Format('%'+IntToStr(width)+'u',[value]),' ',leadingzeros);
495 END;
496
497FUNCTION FormatFileSize(size:LongWord):String;
498 BEGIN
499 IF size>=1000*1024*1024 THEN BEGIN
500 Result:=FloatToStrF(size/1024/1024/1024,ffFixed,5,1)+' GB';
501 END ELSE BEGIN
502 IF size>=1000*1024 THEN BEGIN
503 Result:=FloatToStrF(size/1024/1024,ffFixed,5,1)+' MB';
504 END ELSE BEGIN
505 IF size>=1000 THEN BEGIN
506 Result:=FloatToStrF(size/1024,ffFixed,5,1)+' KB';
507 END ELSE BEGIN
508 Result:=IntToStr(size)+' B';
509 END;
510 END;
511 END;
512 END;
513
514FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
515 VAR
516 string_build,ascii_version:String;
517 i:LongWord;
518 BEGIN
519 string_build:='';
520 ascii_version:='';
521 FOR i:=0 TO High(data) DO BEGIN
522 IF NOT HexOnly THEN
523 IF (i MOD 16)=0 THEN
524 string_build:=string_build+'0x'+IntToHex(i,6)+' ';
525 string_build:=string_build+IntToHex(data[i],2);
526 IF NOT HexOnly THEN BEGIN
527 IF data[i]>=32 THEN ascii_version:=ascii_version+Chr(data[i])
528 ELSE ascii_version:=ascii_version+'.';
529 IF ((i+1) MOD 2)=0 THEN string_build:=string_build+#32;
530 IF ((i+1) MOD 16)=0 THEN BEGIN
531 string_build:=string_build+#32+ascii_version+CrLf;
532 ascii_version:='';
533 END;
534 END;
535 END;
536 Result:=string_build;
537 END;
538
539
540FUNCTION ExportFile(fileid:LongWord; filename:String; settings:TExportSet; path:String):Integer;
541 VAR
542 i:Byte;
543 extension:String;
544 rawlist:TRawList;
545 BEGIN
546 Result:=export_noerror;
547 extension:=RightStr(filename,4);
548 IF DO_toone IN settings THEN BEGIN
549 ExportDatFile(fileid,path+'\'+GetWinFileName(filename));
550 END ELSE BEGIN
551 IF DO_dat IN settings THEN ExportDatFile(fileid,path+'\'+GetWinFileName(filename));
552 IF DO_raw IN settings THEN BEGIN
553 rawlist:=GetRawList(fileid);
554 IF Length(rawlist)>0 THEN BEGIN
555 FOR i:=0 TO High(rawlist) DO BEGIN
556 ExportRawFile(fileid,rawlist[i].src_offset,path+'\'+GetWinFileName(filename));
557 END;
558 END;
559 END;
560 END;
561 END;
562
563FUNCTION Explode(_string:String; delimiter:Char):TStringList;
564 VAR
565 start,len:Word;
566 BEGIN
567 SetLength(Result, 0);
568 start:=1;
569 WHILE PosEx(delimiter,_string,start)>0 DO BEGIN
570 len:=PosEx(delimiter,_string,start)-start;
571 SetLength(Result, Length(Result)+1);
572 Result[High(Result)]:=MidStr(_string,start,len);
573 start:=start+len+1;
574 END;
575 SetLength(Result, Length(Result)+1);
576 Result[High(Result)]:=MidStr(_string,start,Length(_string)-start+1);
577 END;
578
579FUNCTION GetWinFileName(name:String):String;
580 BEGIN
581 Result:=name;
582 Result:=AnsiReplaceStr(Result,'\','__');
583 Result:=AnsiReplaceStr(Result,'/','__');
584 Result:=AnsiReplaceStr(Result,'>','__');
585 Result:=AnsiReplaceStr(Result,'<','__');
586 END;
587
588FUNCTION GetExtractPath:String;
589 BEGIN
590 Result:=ExtractFilePath(dat_filename)+'\extracted_'+ExtractFileName(dat_filename);
591 END;
592
593
594PROCEDURE OpenDatabase(FileName:String);
595 VAR
596 i:Byte;
597 temps:String;
598 BEGIN
599 IF NOT FileExists(FileName) THEN BEGIN
600 ShowMessage('File doesn''t exist!!!');
601 Exit;
602 END;
603 Database:=TABSDatabase.Create(NIL);
604 Database.DatabaseName:='OLDBcon';
605 Database.DatabaseFileName:=FileName;
606 Database.Open;
607 Query:=TABSQuery.Create(Database);
608 Query.DatabaseName:='OLDBcon';
609 Query.SQL.Text:='SELECT [name],[value] FROM globals ORDER BY [name] ASC';
610 Query.Open;
611 Query.First;
612 REPEAT
613 IF Query.FieldByName('name').AsString='dbversion' THEN BEGIN
614 IF Query.FieldByName('value').AsString<>DBversion THEN BEGIN
615 ShowMessage('Database-file '+CrLf+'"'+FileName+'"'+CrLf+'has wrong version. (Required: '+DBversion+'; found: '+Query.FieldByName('value').AsString+')');
616 Query.Close;
617 CloseDatabase;
618 Exit;
619 END;
620 END;
621 IF Query.FieldByName('name').AsString='lvl' THEN BEGIN
622 database_level:=StrToInt(Query.FieldByName('value').AsString);
623 END;
624 IF Query.FieldByName('name').AsString='ident' THEN BEGIN
625 temps:=Query.FieldByName('value').AsString;
626 FOR i:=0 TO High(database_ident) DO BEGIN
627 CASE temps[(i*2)+1+0] OF
628 '0'..'9': database_ident[i]:=Ord(temps[(i*2)+1+0])-48;
629 'A'..'F': database_ident[i]:=Ord(temps[(i*2)+1+0])-55;
630 END;
631 database_ident[i]:=database_ident[i]*16;
632 CASE temps[(i*2)+1+1] OF
633 '0'..'9': database_ident[i]:=database_ident[i]+Ord(temps[(i*2)+1+0])-48;
634 'A'..'F': database_ident[i]:=database_ident[i]+Ord(temps[(i*2)+1+0])-55;
635 END;
636 END;
637 END;
638 Query.Next;
639 UNTIL Query.Eof;
640 Query.Close;
641 opened_state:=opened_db;
642 END;
643PROCEDURE CloseDatabase;
644 BEGIN
645 Database.Close;
646 Database.Free;
647 opened_state:=opened_nothing;
648 END;
649
650
651
652END.
Note: See TracBrowser for help on using the repository browser.