forked from MobyGamer/TPLibs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOMPRESS.PAS
101 lines (89 loc) · 3.55 KB
/
COMPRESS.PAS
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
{$B-} { Use fast boolean evaluation. }
program Compress;
{ Program to demonstrate use of TLZWFilter }
{$i stdefine.inc}
uses
{$ifdef windows} wincrt, {$endif}
{$ifdef wobjects} wobjects, {$else} objects, {$endif}
streams;
procedure SyntaxExit(s:string);
begin
writeln;
writeln(s);
writeln;
writeln('Usage: COMPRESS Sourcefile Destfile [/X]');
writeln(' will compress the source file to the destination');
writeln(' file, or if /X flag is used, will expand source to destination.');
halt(99);
end;
var
Source : PStream; { We don't know in advance which will be compressed }
Dest : PStream;
filename : string;
begin
Case ParamCount of
2 : begin
{$ifdef windows}
Filename := Paramstr(1);
Filename[length(filename)+1] := #0;
Source := New(PBufStream, init(@filename[1], stOpenRead, 2048));
Filename := Paramstr(2);
Filename[length(filename)+1] := #0;
Dest := New(PLZWFilter, init(New(PBufStream,
init(@filename[1],
stCreate, 2048)),
stOpenWrite));
{$else}
Source := New(PBufStream, init(Paramstr(1), stOpenRead, 2048));
Dest := New(PLZWFilter, init(New(PBufStream,
init(Paramstr(2),
stCreate, 2048)),
stOpenWrite));
{$endif windows}
Write('Compressing ',Paramstr(1),' (',Source^.GetSize,
' bytes) to ',Paramstr(2));
end;
3 : begin
if (Paramstr(3) <> '/X') and (Paramstr(3) <> '/x') then
SyntaxExit('Unrecognized option '+Paramstr(3));
{$ifdef windows}
Filename := Paramstr(2);
Filename[length(filename)+1] := #0;
Source := New(PLZWFilter, init(New(PBufStream,
init(@filename[1],
stOpenRead, 2048)),
stOpenRead));
Filename := Paramstr(2);
Filename[length(filename)+1] := #0;
Dest := New(PBufStream, init(@filename[1], stCreate, 2048));
{$else}
Source := New(PLZWFilter, init(New(PBufStream,
init(Paramstr(1),
stOpenRead, 2048)),
stOpenRead));
Dest := New(PBufStream, init(Paramstr(2), stCreate, 2048));
{$endif windows}
Write('Expanding ',Paramstr(1),' (',
PLZWFilter(Source)^.Base^.GetSize,' bytes) to ',
Paramstr(2));
end;
else
SyntaxExit('Two or three parameters required.');
end;
if (Source = nil) or (Source^.status <> stOk) then
SyntaxExit('Unable to open file '+ParamStr(1)+' for reading.');
if (Dest = nil) or (Dest^.status <> stOk) then
SyntaxExit('Unable to create file '+Paramstr(2)+'.');
FastCopy(Source^,Dest^, Source^.GetSize);
if Dest^.status <> stOK then
SyntaxExit('File error during compression/expansion.');
Case ParamCount of
2 : begin
Dest^.Flush;
Writeln(' (',PLZWFilter(Dest)^.Base^.GetSize,' bytes).');
end;
3 : Writeln(' (',Dest^.GetSize,' bytes).');
end;
Dispose(Source, done);
Dispose(Dest, done);
end.