summaryrefslogtreecommitdiff
path: root/fileunit.pas
blob: f949c626cdb43ae012320c647fb512645e70e467 (plain)
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
unit fileunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Process;

type
  tInputThread = class (tThread)
    fertig: boolean;
    inhalt: pointer;
    len:    longint;
    proc:   tProcess;
    constructor create(p: tProcess; inh: pointer; l: longint);
    destructor destroy; override;
    procedure execute; override;
  end;

procedure saveToGeneric(name: string; pt: pointer; len: longint); inline;
procedure saveToFile(name: string; pt: pointer; len: longint); inline;
procedure saveToGz(name: string; pt: pointer; len: longint); inline;
procedure saveToPgz(name: string; pt: pointer; len: longint); inline;
procedure saveViaProcess(name: string; pr: tProcess; pt: pointer; len: longint); inline;
procedure loadFromGeneric(name: string; out pt: pointer; out len: longint); inline;
procedure loadFromFile(name: string; out pt: pointer; out len: longint); inline;
procedure loadFromGz(name: string; out pt: pointer; out len: longint); inline;
procedure loadFromPgz(name: string; out pt: pointer; out len: longint); inline;
procedure loadFromProcess(pr: tProcess; out pt: pointer; out len: longint); inline;

implementation

uses systemunit, math;

const
  minBufLen = 1024*1024;

procedure saveToGeneric(name: string; pt: pointer; len: longint);
begin
  if rightStr(name,3)='.gz' then
    saveToGz(name,pt,len)
  else if rightStr(name,4)='.pgz' then
    saveToPgz(name,pt,len)
  else
    saveToFile(name,pt,len);
end;

procedure saveToFile(name: string; pt: pointer; len: longint);
var
  f: file;
  n: string;
begin
  if fileExists(name) then
    n:=mkTemp(name+'.XXXXXX')
  else
    n:=name;
  assignFile(f,n);
  rewrite(f,1);
  blockWrite(f,pt^,len);
  closeFile(f);
  if name<>n then begin
    deleteFile(name);
    rename(f,name);
  end;
end;

procedure saveToGz(name: string; pt: pointer; len: longint);
var
  pr: tProcess;
begin
  pr:=tProcess.create(nil);
  pr.executable:='gzip';
  pr.parameters.add('--best');
  pr.parameters.add('-c');
  saveViaProcess(name,pr,pt,len);
end;

procedure saveToPgz(name: string; pt: pointer; len: longint);
var
  pr: tProcess;
begin
  pr:=tProcess.create(nil);
  pr.executable:='pigz';
  pr.parameters.add('--best');
  pr.parameters.add('-c');
  saveViaProcess(name,pr,pt,len);
end;

procedure saveViaProcess(name: string; pr: tProcess; pt: pointer; len: longint);
var
  buf:    array of byte;
  f:      file;
  rb:     longint;
  it:     tInputThread;
  datNam: string;
begin
  if fileExists(name) then
    datNam:=mkTemp(name+'.XXXXXX')
  else
    datNam:=name;

  pr.options:=pr.options + [poUsePipes];
  pr.execute;
  setLength(buf,minBufLen);
  fillchar(buf[0],length(buf)*sizeOf(buf[0]),$0);
  it:=tInputThread.create(pr,pt,len);
  assignFile(f,datNam);
  rewrite(f,1);
  while pr.running or (not it.fertig) or (pr.output.numBytesAvailable>0) do begin
    rb:=min(length(buf),pr.output.numBytesAvailable);
    if rb>0 then begin
      rb:=pr.output.read(buf[0],rb);
      blockWrite(f,buf[0],rb);
    end
    else
      sleep(1); // nix zu Schreiben, nix zu Lesen, also warten wir
  end;
  pr.free;
  it.free;
  closeFile(f);
  if name<>datNam then begin
    deleteFile(name);
    rename(f,name);
  end;
end;

procedure loadFromGeneric(name: string; out pt: pointer; out len: longint);
begin
  if rightStr(name,3)='.gz' then
    loadFromGz(name,pt,len)
  else if rightStr(name,4)='.pgz' then
    loadFromPgz(name,pt,len)
  else
    loadFromFile(name,pt,len);
end;

procedure loadFromFile(name: string; out pt: pointer; out len: longint);
var
  f: file;
begin
  assign(f,name);
  reset(f,1);
  len:=fileSize(f);
  getMem(pt,len);
  blockRead(f,pt^,len);
  close(f);
end;

procedure loadFromGz(name: string; out pt: pointer; out len: longint);
var
  pr: tProcess;
begin
  pr:=tProcess.create(nil);
  pr.executable:='zcat';
  pr.parameters.add(name);
  loadFromProcess(pr,pt,len);
end;

procedure loadFromPgz(name: string; out pt: pointer; out len: longint);
var
  pr: tProcess;
begin
  pr:=tProcess.create(nil);
  pr.executable:='pigz';
  pr.parameters.add('-d');
  pr.parameters.add('-c');
  pr.parameters.add(name);
  loadFromProcess(pr,pt,len);
end;

procedure loadFromProcess(pr: tProcess; out pt: pointer; out len: longint); inline;
var
  pTmp:       pByte;
  pLen,rb,br: longint;
begin
  pLen:=minBufLen;
  getMem(pTmp,pLen);
  br:=0;
  pr.options:=pr.options + [poUsePipes];
  pr.execute;
  while pr.running or (pr.output.numBytesAvailable>0) do begin
    rb:=pr.output.numBytesAvailable;
    if rb>0 then begin
      if pLen<br+rb then begin
        pt:=pTmp;
        len:=pLen;
        pLen:=br+rb+minBufLen;
        getMem(pTmp,pLen);
        move(pt^,pTmp^,len);
        freeMem(pt);
      end;
      rb:=pr.output.read((pTmp+br)^,rb);
      br:=br+rb;
    end
    else sleep(1);
  end;
  len:=br;
  getMem(pt,len);
  move(pTmp^,pt^,len);
  freeMem(pTmp);
  pr.free;
end;

// tInputThread ****************************************************************

constructor tInputThread.create(p: tProcess; inh: pointer; l: longint);
begin
 inherited create(true);
 fertig:=false;
 inhalt:=inh;
 len:=l;
 proc:=p;
 suspended:=false;
end;

destructor tInputThread.destroy;
begin
 inhalt:=nil;
 proc:=nil;
 inherited destroy;
end;

procedure tInputThread.execute;
var
  wb,cwb: longint;
begin
 wb:=0;
 while wb<len do begin
   cwb:=proc.input.write((inhalt+wb)^,len-wb);
   if cwb=0 then
     sleep(1)
   else
     wb:=wb+cwb;
 end;
 proc.closeInput;
 fertig:=true;
end;

end.