This repository was archived by the owner on Oct 11, 2022. It is now read-only.
forked from nickelsworth/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0072.pas
More file actions
1535 lines (1343 loc) · 55.3 KB
/
Copy path0072.pas
File metadata and controls
1535 lines (1343 loc) · 55.3 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
I am posting these because I feel they have been "optimized' beyond my
abilities. If you find a way to further optimize it, by speed, memory
requirements, and other things, please SEND ME THE VERSION!
I have a favour to ask all pascalians. These routines seem to lock up
sometimes during the Retrieve_Function when I'm in a tight memory situation.
I say tight as I have less then 500k free in one of my programs. If
someone could rewrite the part which copies (ie. BufSize parts), I would
gladly appreciate it. Thanks!
}
UNIT DATAIO;
{ DATA Input/Output Routines
Given to the People as FreeWare
Includable into SWAG and
made expecialy for SWAG :)
AUTHOR: BOJAN LANDEKIC
SUBJECT: FILE DATA STORAGE (DATAIO)
These routines allow you to take any number of files (max 255 as I used BYTE
but you can change the limit to 65535 by using WORD instead). As I said, it
allows you to take that many files (or less) and include them into a single
file (ie. ALLFILES.DAT). Then you can retrieve/add/delete/view this file.
I am testing out DATAIO v2.0 with encryption and compression routines, and
that will be released into the Freeware as well.
The three sub-units I use are STRIO (string handlers), FILEIO (file in/out
routines) and VARS (a global declaration unit that is included everywhere).
Each routine is a FUNCTION and returns an error code (0 if okay). The
error codes are examplained under the name of each of the functions.
Even though this is made freeware I BEG everybody not to make changes and
distribute them as their own work <grin>. If you make changes, LET ME KNOW
as I plan to make a compression program competitive to ZIP/ARJ and others.
The routines which use the constant BufSize are taken from either FILES.SWG,
COPYMOVE.SWG, or DOS.SWG from SWAG archives. I cannot remember who the
original author is, but I will check and when I find out, you will be
credited.
}
INTERFACE
Uses Vars,
StrIo,
FileIO,
Crt,
Dos;
FUNCTION Retrieve_File(DataFilename, Filename: String; Display: Boolean): Byte;
FUNCTION Add_File(DataFilename, Filename: String; Display: Boolean): Byte;
FUNCTION Remove_File(DataFilename, Filename: String; Display: Boolean): Byte;
FUNCTION Show_File(DataFilename, Filename: String): Byte;
IMPLEMENTATION
FUNCTION Retrieve_File(DataFilename, Filename: String; Display: Boolean): Byte;
{
This function returns the following:
0 - [filename] has been retrieved successfully from [DataFilename]
1 - [DataFilename] was not found/does not exist/was not specified
2 - Header is incorrect (wrong file maybe?)
3 - [Filename] was not found in [datafilename]
4 - Not enough memory for FileBuf (decrese FileBuf)
5 - Not enough disk space for the to-be-extracted file
Datafile is formed like this
XXXXXXXXXX - The header
---------- - Individual file header #1 (information)
CCCCCCCCCC - File #1 itself (data/code segment)
CCCCCCCCCC
CCCCCCCCCC
---------- - Individual file header #2 (information)
CCCCCCCCCC - file #2 itself (data/code segment)
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
... - ... you get the general idea
}
Const
BufSize = 16384;
{for the copy part}
Type
FBuf = array[1..BufSize] Of Char;
Fbf = ^FBuf;
Var
y, {date function}
m,
d,
dow,
h, {time function}
min,
s,
hund : Word;
CurrentFile : Byte; {for searching through files}
DataFile,
ExtractFile : File; {file that's to be extracted}
Difference : Longint; {could be a WORD: diff betwen now-real}
OldPos, {used for updating the ORIGINAL header}
ExtractPos : LongInt; {current size of extractfile}
Bread, {for fast/error-free copying}
Bwrite : word;
FileBuf : ^fbf;
OldX,
OldY : Byte; {for display purposes only}
Begin
{Check for enough available memory}
If (MemAvail > BufSize) then
New(FileBuf)
Else
begin
Retrieve_File := 4;
Exit;
End;
{check if file exists, or if a filename has been specified}
If (DataFilename = '') OR
(Filename = '') OR
NOT FileExists(DataFilename) Then
Begin
Retrieve_File := 1;
Dispose(FileBuf);
Exit;
End;
{open the file}
Assign(DataFile, DataFilename);
Filemode := 2;
Reset(DataFile, 1);
{open the file to be extracted/made}
Assign(ExtractFile, Filename);
Filemode := 2;
Rewrite(ExtractFile, 1);
{check for the header id}
BlockRead(DataFile, Header, SizeOf(Header));
If NOT (Header.Identification = Id_Check) Then
Begin
{if the header not the same then it's not one of ours}
Retrieve_File := 2;
Dispose(FileBuf);
Exit;
End;
{Go to the beginning of the first individual file header}
Seek(DataFile, SizeOf(Header));
If Display Then
Begin
Write('Searching...');
End;
{loop through all the entries until [filename] is found}
For CurrentFile := 1 To Header.NumberOfFiles Do
Begin
{read the header}
FillChar(FileHeader, SizeOf(FileHeader), #0);
BlockRead(DataFile, FileHeader, SizeOf(FileHeader));
{so the user doesn't think we're lazy :)}
{Writeln('Processing...');
Writeln('Filename : ', FileHeader.Filename);
Writeln('Size : ', FileHeader.RealSize);}
{compare this file to the one the user wants}
If (FileHeader.Filename = Filename) Then
Begin
{A-Ha, it is the file, extract it!}
{check for disk space}
If (DiskFree(0) < FileHeader.RealSize) Then
Begin
Retrieve_File := 5;
Dispose(FileBuf);
Close(DataFile);
Close(ExtractFile);
Exit;
End;
ExtractPos := 0;
If Display Then
Begin
TextBackground(0);
TextColor(7);
GotoXY(1, WhereY);
ClrEol;
Write('Extracting ' + Filename + ': ');
OldX := WhereY;
OldY := WhereY;
End;
{make sure we update the header, since the
file is being "updated" as you might see}
OldPos := FilePos(DataFile);
GetDate(y, m, d, dow);
GetTime(h, min, s, hund);
Header.UpdatedOn := Leading_Zero(ITOA(m), 2) + '-' +
Leading_Zero(ITOA(d), 2) + '-' +
Leading_Zero(ITOA(y), 4) +
Leading_Zero(ITOA(h), 2) + ':' +
Leading_Zero(ITOA(min), 2);
Seek(DataFile, 0);
BlockWrite(DataFile, Header, SizeOf(Header));
Seek(DataFile, OldPos);
Repeat
BlockRead(DataFile, FileBuf^, BufSize, Bread);
BlockWrite(ExtractFile, FileBuf^, Bread, Bwrite);
Inc(ExtractPos, Bread);
If Display Then
Begin
GotoXY(OldX, OldY);
If (ExtractPos <= FileHeader.RealSize) Then
Write(StatusBar(FileHeader.RealSize, ExtractPos, 42))
Else
Write(StatusBar(1, 1, 42)); {100% effect :)}
End;
Until (Bread = 0) OR (Bread <> Bwrite) OR
(ExtractPos > FileHeader.RealSize);
{To compensate for the over-write}
If (ExtractPos > FileHeader.RealSize) Then
Begin
Difference := (ExtractPos - FileHeader.RealSize);
{Seek to the part where THIS file is supposed to end}
Seek(ExtractFile, FilePos(ExtractFile) - Difference);
{Erase the extra garbage}
Truncate(Extractfile);
{Unneccesery, but just to be sure for multiple extractions}
Seek(DataFile, FilePos(DataFile) - Difference);
End;
{extracted, now we quit}
Retrieve_File := 0;
Dispose(FileBuf);
Close(DataFile);
Close(ExtractFile);
If Display Then
Begin
GotoXY(OldX, OldY);
ClrEol;
Writeln('Done!');
End;
Exit;
End
Else
Begin
{Go to next record, right}
Seek(DataFile, FilePos(DataFile) + FileHeader.RealSize);
End;
End;
{If we get to here, means the file was not in the datafile}
Retrieve_File := 3;
Dispose(FileBuf);
Close(DataFile);
Close(ExtractFile);
End;
FUNCTION Add_File(DataFilename, Filename: String; Display: Boolean): Byte;
{ - The part that "copyies" the file was gotten from SWAG, the original
author of the "copying" part was Floor A.C. Naaijkens
}
{
This function can possibly return the following values:
0 - [filename] has been successfully added to [datafilename]
1 - [datafilename] and/or [filename] have not be specified/don't exist
2 - Could not create/open [datafilename]
3 - [datafilename] is not one of our files, wrong file type maybe??
4 - [filename] opening error
5 - Not enough memory (on the stack, 16386 needed).. Decrease BufSize
6 - Error during copy
7 - No more files allowed (254 file limit reached
}
{for the copy part}
Const
BufSize = 16384;
{for the copy part}
Type
FBuf = array[1..BufSize] Of Char;
Fbf = ^FBuf;
Var
y,
m,
d,
dow, {for the date}
h,
min,
s,
hund : Word; {for the time}
DataFile,
AddFile : File; {file to be added}
NewFile : Boolean; {specifies wheter [datafile] is new}
Bread, {for fast/error-free copying}
Bwrite : word;
FileBuf : ^fbf;
OldX,
OldY : Byte;
StartAt : LongInt; {for display purposes only}
DirInfo : SearchRec;
Begin
{Check for enough available memory}
If (MemAvail > BufSize) then
New(FileBuf)
else
begin
Add_File := 5;
Exit
End;
{check if file exists, or if a filename has been specified}
If (DataFilename = '') OR (Filename = '') Then
Begin
Add_File := 1;
Exit;
End;
{check if the datafile exists}
Assign(DataFile, DataFilename);
IF NOT FileExists(Datafilename) Then
Begin
{$I-}
FileMode := 2;
Rewrite(DataFile, 1);
IF (IOResult <> 0) Then
Begin
Add_File := 2;
Dispose(FileBuf);
Exit;
End;
{$I+}
NewFile := True;
End
Else
Begin
FileMode := 2;
{$I-}
Reset(DataFile, 1);
{$I+}
IF (IOResult <> 0) Then
Begin
Add_File := 2;
Dispose(FileBuf);
Exit;
End;
NewFile := False;
End;
If NewFile Then
{New file initialization}
Begin
Getdate(y, m, d, dow);
GetTime(h, min, s, hund);
FillChar(Header, SizeOf(Header), #0);
Header.Identification := Id_Check;
Header.CreatedOn := Leading_Zero(ITOA(m), 2) + '-' +
Leading_Zero(ITOA(d), 2) + '-' +
Leading_Zero(ITOA(y), 4) +
Leading_Zero(ITOA(h), 2) + ':' +
Leading_Zero(ITOA(min), 2);
Header.UpdatedOn := Header.CreatedOn;
Header.NumberOfFiles := 0;
BlockWrite(DataFile, Header, SizeOf(Header));
Seek(DataFile, 0);
End;
{Already existing file initialization}
BlockRead(Datafile, Header, SizeOf(Header));
{check for the ID string}
If NOT (Header.Identification = Id_Check) Then
Begin
Add_File := 3;
Dispose(FileBuf);
Close(DataFile);
Exit;
End;
{Go to the appropriate place in the datafile where
the writing will start}
Filename := Strip_Path(UCase(Filename));
FindFirst(Filename, Archive, DirInfo);
While (DosError = 0) Do
Begin
Assign(AddFile, DirInfo.Name);
Filemode := 2;
{$I-}
Reset(AddFile, 1);
{$I+}
IF (IOResult <> 0) Then
Begin
Add_File := 4;
Close(DataFile);
Dispose(FileBuf);
Exit;
End;
If (Header.NumberOffiles > 254) Then
Begin
Add_File := 8;
Dispose(FileBuf);
Close(DataFile);
Exit;
End
Else
Inc(Header.NumberOfFiles);
Header.UpdatedOn := Leading_Zero(ITOA(m), 2) + '-' +
Leading_Zero(ITOA(d), 2) + '-' +
Leading_Zero(ITOA(y), 4) +
Leading_Zero(ITOA(h), 2) + ':' +
Leading_Zero(ITOA(min), 2);
Seek(DataFile, 0);
BlockWrite(DataFile, Header, SizeOf(Header));
Seek(DataFile, FileSize(DataFile));
{Here we set the individual file header to the appropriate
information}
FillChar(FileHeader, SizeOf(FileHeader), #0);
FileHeader.Attribute := 0;
FileHeader.Filename := Dirinfo.Name;
FileHeader.CompType := 0;
FileHeader.RealSize := FileSize(AddFile);
FileHeader.CompSize := FileHeader.RealSize;
FileHeader.Crc := 0;
{check for disk space}
If (DiskFree(0) < FileHeader.RealSize) Then
Begin
Add_File := 5;
Dispose(FileBuf);
Close(DataFile);
Exit;
End;
BlockWrite(DataFile, FileHeader, SizeOf(FileHeader));
{copy the file}
If Display Then
Begin
TextBackground(0);
TextColor(7);
Write('Adding ' + Dirinfo.Name + ': ');
OldX := WhereX;
OldY := WhereY;
End;
StartAt := FilePos(DataFile);
Repeat
BlockRead(AddFile, FileBuf^, BufSize, Bread);
BlockWrite(DataFile, FileBuf^, Bread, Bwrite);
If Display Then
Begin
GotoXY(OldX, OldY);
Write(StatusBar(FileHeader.RealSize, (FilePos(DataFile) - StartAt), 50));
End;
Until (Bread = 0) OR (Bread <> Bwrite);
Close(AddFile);
If Display Then
Begin
GotoXY(OldX, Oldy);
ClrEol;
End;
If (Bread <> Bwrite) then
Begin
If Display Then
Writeln('Error occured while adding!');
Add_File := 6
End
Else
Begin
If Display Then
Writeln('Done!');
Add_File := 0;
End;
FindNext(DirInfo);
End; {while loop}
Dispose(FileBuf);
Close(DataFile);
End;
FUNCTION Remove_File(DataFilename, Filename: String; Display: Boolean): Byte;
{ This function returns the following:
0 - [filename] has been succcessfully deleted from Datafilename
1 - [filename] or [datafilename] are empty or [datafilename] does not exist
2 - Not enough disk space (minimum = file size of [datafilename])
3 - [datafilename] is not of our type. Maybe not the right format? Hmm..:)
}
Const
tFilename : String[12] = 'DATA.!!!'; {temporary file}
Var
OldX,
OldY, {for display}
TotalFiles, {just for the heck of it}
CurrentFile : Byte; {the for-end loop}
eFileHeader : tFileHeader; {Empty file header}
tDataFile, {only used by the Rename function}
DataFile : File; {file being worked on}
OldPos : Longint; {to be sure pointer is always there}
Cur_File, {for multiple file additions}
Search_File : String[8];
Cur_Ext,
Search_Ext : String[3];
Begin
Assign(DataFile, DataFilename);
Assign(tDataFile, tFilename);
{check if file exists, or if a filename has been specified}
If (DataFilename = '') OR
(Filename = '') OR
(NOT FileExists(DataFilename)) Then
Begin
Remove_File := 1;
Exit;
End
Else
Reset(DataFile, 1);
{check for disk space}
If (DiskFree(0) < FileSize(DataFile)) Then
Begin
Remove_File := 2;
Close(DataFile);
Exit;
End;
{check for the header id}
BlockRead(DataFile, Header, SizeOf(Header));
If NOT (Header.Identification = Id_Check) Then
Begin
{if the header is not the same then it's not one of ours}
Remove_File := 3;
Exit;
End;
{Go to the beginning of the first individual file header}
Seek(DataFile, SizeOf(Header));
Filename := UCase(Filename);
TotalFiles := Header.NumberOfFiles;
If Display Then
Begin
Writeln;
Write('Removing: ' + Filename);
OldX := WhereX + 1;
OldY := WhereY;
End;
{loop through all the entries until [filename] is found}
{BUG! Header.NumberOfFiles seems to change for some reason here!!}
Search_File := Copy(Filename, 1, Pos('.', Filename) - 1);
Search_Ext := Copy(Filename, Pos('.', Filename) + 1, Length(Filename));
For CurrentFile := 1 To TotalFiles Do
Begin
{read the header}
FillChar(eFileHeader, SizeOf(eFileHeader), #0);
BlockRead(DataFile, eFileHeader, SizeOf(eFileHeader));
OldPos := FilePos(DataFile);
If Display Then
Begin
GotoXy(OldX, OldY);
Write(StatusBar(TotalFiles, CurrentFile, 48));
End;
{compare this file to the one the user wants}
Cur_File := Copy(eFileHeader.Filename, 1, Pos('.', eFileHeader.Filename) - 1);
Cur_Ext:=Copy(eFileHeader.Filename, Pos('.', eFileHeader.Filename) + 1, Length(eFileHeader.Filename));
If (NOT Compare_Filenames(Search_File, Cur_File)) OR
(NOT Compare_Filenames(Search_Ext, Cur_Ext)) Then
Begin
{remove it from the original archive}
Retrieve_File(DataFilename, eFileHeader.Filename, False);
{add it to the temporary archive}
Add_File(tFilename, eFileHeader.Filename, False);
{go to the next file}
End;
Seek(DataFile, OldPos + eFileHeader.RealSize);
End;
Close(DataFile);
Erase(DataFile);
Rename(tDataFile, DataFilename);
End;
FUNCTION Show_File(DataFilename, Filename: String): Byte;
{ This functions returns the following:
0 - Displayed
1 - [datafilename] is blank or does not exist!
2 - File is of wrong type, meaning it's not one made by this program.
}
Var
OldY : Byte;
DataFile : File;
CurrentFile : Byte;
Cur_File, {current file name without extension}
Search_File : String[8]; {file name without the extension}
Cur_Ext, {current file extension only, no name}
Search_Ext : String[3]; {file extension only, no name}
TotalFiles : Byte; {counter for displayed files}
TotalBytes : Longint; {counter for displayed bytes}
Begin
{check if file exists, or if a filename has been specified}
If (DataFilename = '') OR
{(Filename = '') OR} {not implemented yet}
NOT FileExists(DataFilename) Then
Begin
Show_File := 1;
Exit;
End;
{open the file}
Assign(DataFile, DataFilename);
Reset(DataFile, 1);
{check for the header id}
BlockRead(DataFile, Header, SizeOf(Header));
If NOT (Header.Identification = Id_Check) Then
Begin
{if the header is not the same then it's not one of ours}
Show_File := 2;
Exit;
End;
{Go to the beginning of the first individual file header!
This is done already by BlockRead, but just to be on the
safe side :)}
Seek(DataFile, SizeOf(Header));
{loop through all the entries until [filename] is found}
Writeln;
Writeln;
Write('Listing of ' + DataFilename);
GotoXY(26, WhereY);
Write(FileSize(DataFile));
Write(' (');
Write(FileSize(DataFile) DIV 1024);
Write('k)');
Writeln;
GotoXY(1, WhereY);
Write('Created On: ');
Write(Copy(Header.CreatedOn, 1, 10));
Write(' at ');
Write(Copy(Header.CreatedOn, 11, 5));
GotoXY(35, WhereY);
Write('Last updated On: ');
Write(Copy(Header.UpdatedOn, 1, 10));
Write(' at ');
Write(Copy(Header.UpdatedOn, 11, 5));
GotoXY(71, WhereY);
Write(' Files: ');
Write(Header.NumberOffiles);
Writeln;
Writeln;
Writeln('FILENAME.EXT SIZE ');
Writeln('------------ --------------------');
TotalBytes := 0;
TotalFiles := 0;
Search_File := Copy(Filename, 1, Pos('.', Filename) - 1);
Search_Ext := Copy(Filename, Pos('.', Filename) + 1, Length(Filename));
For CurrentFile := 1 To Header.NumberOfFiles Do
Begin
{read the header}
FillChar(FileHeader, SizeOf(FileHeader), #0);
BlockRead(DataFile, FileHeader, SizeOf(FileHeader));
{so the user doesn't think we're lazy :)}
Cur_File := Copy(FileHeader.Filename, 1, Pos('.', FileHeader.Filename) - 1);
Cur_Ext := Copy(FileHeader.Filename, Pos('.', FileHeader.Filename) + 1, Length(FileHeader.Filename));
If Compare_Filenames(Search_File, Cur_File) Then
If Compare_Filenames(Search_Ext, Cur_Ext) Then
Begin
OldY := WhereY;
Write(FileHeader.Filename);
GotoXY(24, OldY);
Write(' ' :(11 - Length(ITOA(FileHeader.RealSize))));
Write(FileHeader.RealSize);
Writeln;
Inc(TotalBytes, FileHeader.RealSize);
Inc(TotalFiles);
End;
{go to the next record}
Seek(DataFile, FilePos(DataFile) + FileHeader.RealSize);
End;
Writeln('------------ --------------------');
OldY := WhereY;
If (TotalBytes = 0) Then
Writeln('No files')
Else
If (TotalFiles = 1) Then
Write('1 file')
Else
Write(ITOA(TotalFiles), ' files');
GotoXY(24, OldY);
Write(' ' :(11 - Length(ITOA(TotalBytes))));
Write(TotalBytes);
Writeln;
{If we get to here, means everything's cool}
Close(DataFile);
Show_File := 0;
End;
BEGIN
END.
{
****************************************************************************
**** UNIT: VARS.PAS ********************************************************
****************************************************************************
}
UNIT VARS;
INTERFACE
TYPE
{You can always use these :)}
St20 = String[20];
St40 = String[40];
St60 = String[60];
St80 = String[80];
tHeader = Record
Identification: String[20]; {The id string, See ID_Check}
{CreatedOn/UpdatedOn are like this MM-DD-YYYYHH:MM}
CreatedOn : String[15]; {creation date, shouldn't change}
UpdatedOn : String[15]; {last modification date}
NumberOfFiles : Byte; {number of files in this file}
End;
tFileHeader = Record
Attribute : Byte; {Attributes:
0 - None
1 - Hidden (N/A)
2 - System (N/A)
3 - Read Only (N/A)
4 - Archive (N/A)
5 - Directory (N/A)
6 - Label (N/A)
}
Filename : String[12]; {Filename as: FILENAME.EXT}
CompType : Byte; {compression type:
0 - None/Store
1 - LZH (N/A)
}
EncrType : Byte; {encryption type:
0 - None/Store
1 - XOR (N/A)
2 - RSA (N/A)
}
RealSize : Longint; {actual size}
CompSize : Longint; {compressed size} {N/A}
Crc : Longint; {Circular Redundancy Check} {N/A}
End;
VAR
Header : tHeader; {the MAIN header}
FileHeader : tFileHeader; {each file's header}
CONST
{Please modify the ID_Check to a unique value used in your programs!
I use the below one, as there's virtually no chance of anyone using the
one below. It just makes sure that incase a .DAT file loses the ID it
can't be read! Sometimes I lower the String[20] to String[2] and make
it 'PK', <grin>}
Id_Check : String[20] = #5#255'DATAIO File'; {for checking!}
IMPLEMENTATION
BEGIN
END.
{
****************************************************************************
**** UNIT: FILEIO.PAS ******************************************************
****************************************************************************
}
UNIT FILEIO;
INTERFACE
Uses Vars,
Dos;
{This is from the Borland Pascal's HELP files. I'm not sure if it's
legel to post this one, but if it's not, people in SWAG, please
replace FileExists function with anyone of the ones you guys have in
FILES.SWG :)}
FUNCTION FileExists(FileName: String): Boolean;
{Author is from SWAG archives' FILES.SWG, whoever you are, let me know
and I will credit you}
FUNCTION Compare_FileNames(SearchStr,NameStr:string): boolean;
{Author is from SWAG archives' FILES.SWG, whoever you are, let me know
and I will credit you}
PROCEDURE WipeFile(fn: string);
IMPLEMENTATION
FUNCTION FileExists(FileName: String): Boolean;
{
*** Boolean function that returns True if the file exists;otherwise,
it returns False. Closes the file if it exists.
***
}
Var
F: file;
Begin
{$I-}
Assign(F, FileName);
FileMode := 0; { Set file access to read only }
Reset(F);
Close(F);
{$I+}
FileExists := (IOResult = 0) and (FileName <> '');
End; { FileExists }
FUNCTION Compare_FileNames(SearchStr,NameStr:string): boolean; assembler;
{
Compare SearchStr with NameStr, and allow wildcards in SearchStr.
The following wildcards are allowed:
*ABC* matches everything which contains ABC
[A-C]* matches everything that starts with either A,B or C
[ADEF-JW-Z] matches A,D,E,F,G,H,I,J,W,V,X,Y or Z
ABC? matches ABC, ABC1, ABC2, ABCA, ABCB etc.
ABC[?] matches ABC1, ABC2, ABCA, ABCB etc. (but not ABC)
ABC* matches everything starting with ABC
(for using with DOS filenames like DOS (and 4DOS), you must split the
filename in the extention and the filename, and compare them seperately)
}
var
LastW:word;
asm
cld
push ds
lds si,SearchStr
les di,NameStr
xor ah,ah
lodsb
mov cx,ax
mov al,es:[di]
inc di
mov bx,ax
or cx,cx
jnz @ChkChr
or bx,bx
jz @ChrAOk
jmp @ChrNOk
xor dh,dh
@ChkChr:
lodsb
cmp al,'*'
jne @ChkQues
dec cx
jz @ChrAOk
mov dh,1
mov LastW,cx
jmp @ChkChr
@ChkQues:
cmp al,'?'
jnz @NormChr
inc di
or bx,bx
je @ChrOk
dec bx
jmp @ChrOk
@NormChr:
or bx,bx
je @ChrNOk
{From here to @No4DosChr is used for [0-9]/[?]/[!0-9] 4DOS wildcards...}
cmp al,'['
jne @No4DosChr
cmp word ptr [si],']?'
je @SkipRange
mov ah,byte ptr es:[di]
xor dl,dl
cmp byte ptr [si],'!'
jnz @ChkRange
inc si
dec cx
jz @ChrNOk
inc dx
@ChkRange:
lodsb
dec cx
jz @ChrNOk
cmp al,']'
je @NChrNOk
cmp ah,al
je @NChrOk
cmp byte ptr [si],'-'
jne @ChkRange
inc si
dec cx
jz @ChrNOk
cmp ah,al
jae @ChkR2
inc si {Throw a-Z < away}
dec cx
jz @ChrNOk
jmp @ChkRange
@ChkR2:
lodsb
dec cx
jz @ChrNOk
cmp ah,al
ja @ChkRange {= jbe @NChrOk; jmp @ChkRange}
@NChrOk:
or dl,dl
jnz @ChrNOk
inc dx
@NChrNOk:
or dl,dl
jz @ChrNOk
@NNChrOk:
cmp al,']'
je @NNNChrOk
@SkipRange:
lodsb
cmp al,']'
loopne @SkipRange
jne @ChrNOk
@NNNChrOk:
dec bx
inc di
jmp @ChrOk
@No4DosChr:
cmp es:[di],al
jne @ChrNOk
inc di
dec bx
@ChrOk:
xor dh,dh
dec cx
jnz @ChkChr { Can't use loop, distance >128 bytes }
or bx,bx
jnz @ChrNOk
@ChrAOk:
mov al,1
jmp @EndR
@ChrNOk:
or dh,dh
jz @IChrNOk
jcxz @IChrNOk
or bx,bx
jz @IChrNOk
inc di
dec bx
jz @IChrNOk
mov ax,[LastW]
sub ax,cx
add cx,ax
sub si,ax
dec si
jmp @ChkChr
@IChrNOk:
mov al,0
@EndR:
pop ds
end;
PROCEDURE WipeFile(fn: string);
Var
size,