summaryrefslogtreecommitdiff
path: root/fileunit.pas
blob: 4530967125925c5072b2ea1a903321b3cca4ee2c (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
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 saveToFile(name: string; pt: pointer; len: longint); inline;
procedure saveToGz(name: string; pt: pointer; 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;

implementation

uses systemunit, math;

const
  minBufLen = 1024*1024;

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;
  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:=tProcess.create(nil);
  pr.executable:='gzip';
  pr.parameters.add('--best');
  pr.parameters.add('-c');
  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 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;
  pTmp:       pByte;
  pLen,rb,br: longint;
begin
  pr:=tProcess.create(nil);
  pr.executable:='zcat';
  pr.parameters.add(name);
  pr.options:=pr.options + [poUsePipes];
  pLen:=minBufLen;
  getmem(pTmp,pLen);
  br:=0;
  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);
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.