summaryrefslogtreecommitdiff
path: root/mystringlistunit.pas
blob: 398e86d677e0e532e95e493ebfc59d3bb294edb9 (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
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
unit mystringlistunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, RegExpr, Process, Math, lowlevelunit, protokollunit;

type
  tMyStringlist = class;

  tInputThread = class (tThread)
    fertig: boolean;
    inhalt: tMyStringList;
    proc:   tProcess;
    constructor create(p: tProcess; sl: tMyStringList);
    destructor destroy; override;
    procedure execute; override;
  end;

  tMyStringlist = class (tStringlist)
  private
    line:  longint;
    prot:  tProtokollant;
  public
    constructor create; overload;
    constructor create(protokollant: tProtokollant); overload;
    procedure loadFromFile(const s: ansiString); override;
    procedure loadFromGz(const s: ansiString);
    procedure saveToGz(const s: ansiString);
    function readln(out s: string): boolean;
    procedure grep(expr: string);
    function eof: boolean;
    procedure rewind;
    function stillNeed(bez: string): boolean;
    function unfoldMacros: boolean;
  end;

procedure _del(var s: string; p,c: longint); inline;  // identisch zu delete(s,p,c) -- lediglich um delete innerhalb von tMyStringlist verfügbar zu haben

implementation

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

constructor tInputThread.create(p: tProcess; sl: tMyStringList);
begin
 inherited create(true);
 fertig:=false;
 inhalt:=sl;
 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<length(inhalt.text) do begin
   cwb:=proc.input.write(inhalt.text[wb+1],length(inhalt.text)-wb);
   if cwb=0 then
     sleep(1)
   else
     wb:=wb+cwb;
 end;
 proc.CloseInput;
 fertig:=true;
end;

// tMyStringlist ***************************************************************

constructor tMyStringlist.create;
begin
 create(nil);
end;

constructor tMyStringlist.create(protokollant: tProtokollant);
begin
 inherited create;
 prot:=tProtokollant.create(protokollant,'tMyStringlist');
 line:=0;
end;

procedure tMyStringlist.loadFromFile(const s: ansiString);
var i: longint;
begin
 inherited loadFromFile(s);
 for i:=0 to count-1 do
   self[i]:=trim(self[i]);
 line:=0;
 writeln(inttostr(count)+' Zeilen eingelesen');
end;

procedure tMyStringlist.loadFromGz(const s: ansiString);
var p:     tProcess;
    buf:   ansiString;
    rb,br: longint;
begin
 p:=tProcess.create(nil);
 p.executable:='/usr/bin/zcat';
 p.parameters.add(s);
 p.options:=p.options + [poUsePipes];
 setlength(buf,0);
 br:=0;
 p.execute;
 while p.running do begin
   rb:=p.output.numBytesAvailable;
   if rb>0 then begin
     if length(buf)<br+rb then
       setlength(buf,br+rb+1048576);
     rb:=p.output.read(buf[br+1],rb);
     br:=br+rb;
   end
   else sleep(1);
 end;
 setlength(buf,br);
 rb:=p.output.numBytesAvailable;
 while rb>0 do begin
   setlength(buf,br+rb);
   rb:=p.output.read(buf[br+1],rb);
   br:=br+rb;
   rb:=p.output.numBytesAvailable;
 end;
 text:=buf;
 setlength(buf,0);
 p.free;
 for rb:=0 to count-1 do
   self[rb]:=trim(self[rb]);
 line:=0;
 writeln(inttostr(count)+' Zeilen eingelesen');
end;

procedure tMyStringlist.saveToGz(const s: ansiString);
var
  p:     tProcess;
  buf:   array of byte;
  f:     file;
  rb:    longint;
  it:    tInputThread;
const
  outBufLen = 1024*1024;
begin
 p:=tProcess.create(nil);
 p.executable:='/usr/bin/gzip';
 p.parameters.add('--best');
 p.parameters.add('-c');
 p.options:=p.options + [poUsePipes];
 p.execute;
 setlength(buf,outBufLen);
 fillchar(buf[0],length(buf)*sizeof(buf[0]),$0);
 it:=tInputThread.create(p,self);
 assignfile(f,s);
 rewrite(f,1);
 while p.running or (not it.fertig) or (p.output.numBytesAvailable>0) do begin
   rb:=min(length(buf),p.output.numBytesAvailable);
   if rb>0 then begin
     rb:=p.output.read(buf[0],rb);
     blockwrite(f,buf[0],rb);
   end
   else
     sleep(1); // nix zu Schreiben, nix zu Lesen, also warten wir
 end;
 it.free;
 closefile(f);
end;

function tMyStringlist.readln(out s: string): boolean;
begin
 result:=not eof;
 if not result then begin
   s:='';
   exit;
 end;
 s:=self[line];
 inc(line);
end;

procedure tMyStringlist.grep(expr: string);
var
  re: tRegExpr;
  i:  longint;
begin
  re:=tRegExpr.create;
  re.Expression:=expr;
  for i:=count-1 downto 0 do
    if not re.Exec(self[i]) then
      delete(i);
  re.free;
end;

function tMyStringlist.eof: boolean;
begin
 result:=line>=count;
end;

procedure tMyStringlist.rewind;
begin
 line:=0;
end;

function tMyStringlist.stillNeed(bez: string): boolean;
var i:   longint;
    s,t: string;
begin
  result:=false;
  for i:=max(0,line-1) to count-1 do begin
    s:=' '+self[i]+' ';
    while pos(bez,s)>0 do begin
      t:=copy(s,1,pos(bez,s)+length(bez));
      _del(s,1,pos(bez,s)+length(bez)-1);
      _del(t,1,length(t)-length(bez)-2);
      if (t[1] in [' ',#9,':','[']) and (t[length(t)] in [' ',#9,':',']']) and (copy(t,2,length(bez))=bez) then begin
        result:=true;
        exit;
      end;
    end;
  end;
end;

function tMyStringlist.unfoldMacros: boolean;
var i,j,k,l,Ebene:     longint;
    s,t,u,v:           string;
    SchleifenInhalt:   tMyStringlist;
    istWahr,gefunden:  boolean;
const binops: array[0..8] of string =
  ('<=','>=','<>','≤','≥','=','≠','<','>');
begin
  result:=false;

  i:=0;
  while i<count do begin  // Kommentare löschen
    s:=self[i];
    s:=erstesArgument(s,'#');
    if length(s)=0 then begin
      delete(i);
      continue;
    end;
    self[i]:=s;
    inc(i);
  end;

  i:=0;
  while i<count do begin  // Übersprünge überspringen, alles nach 'Dateiende' löschen
    s:=self[i];
    if startetMit('!überspringe:',s) then begin
      for j:=0 to strtoint(s) do
        delete(i);
      continue;
    end;

    if s='Dateiende' then begin
      inc(i);
      while i<count do
        delete(i);
      continue;
    end;
    inc(i);
  end;

  i:=0;
  while i<count do begin  // Schleifen ausrollen
    s:=self[i];
    if startetMit('!Schleife:',s) then begin
      l:=i;
      delete(i);
      t:=erstesArgument(s,':');
      if (length(t)=0) or (t[1]<>'$') then exit;

      SchleifenInhalt:=tMyStringlist.create; // Schleifenkörper merken
      Ebene:=0;
      while (i<Count) and ((Ebene<>0) or (self[i]<>'!Schleifenende')) do begin
        SchleifenInhalt.Add(self[i]);
        if self[i]='!Schleifenende' then dec(Ebene);
        if pos('!Schleife:',self[i])=1 then inc(Ebene);
        delete(i);
      end;
      delete(i);

      while length(s)>0 do begin  // Schleifenzähler laufen lassen
        u:=erstesArgument(s);
        for j:=0 to SchleifenInhalt.Count-1 do begin  // Schleifenkörper ...
          v:=SchleifenInhalt[j];
          k:=length(v);
          while (pos(t,v)>0) and (k>0) do begin       // ... nach Ersetzung ...
            v:=copy(v,1,pos(t,v)-1)+u+copy(v,pos(t,v)+length(t),length(v));
            dec(k);
          end;
          insert(i,v);                                // ... kopieren
          inc(i);
        end;
      end;

      SchleifenInhalt.free;
      i:=l;
      continue;
    end;
    self[i]:=s;
    inc(i);
  end;

  i:=0;
  while i<count do begin   // Bedingungen auswerten
    s:=self[i];
    if startetMit('?',s) then begin
      t:=erstesArgument(s,':');
      gefunden:=false;
      for j:=0 to length(binops)-1 do
        if pos(binops[j],t)>0 then begin
          gefunden:=true;
          u:=trim(copy(t,1,pos(binops[j],t)-1));
          _del(t,1,pos(binops[j],t)+length(binops[j])-1);
          t:=trim(t);
          case binops[j] of
            '≤','<=': istWahr:=strtofloat(u)<=strtofloat(t);
            '≥','>=': istWahr:=strtofloat(u)>=strtofloat(t);
            '=':
              try
                istWahr:=strtofloat(u)=strtofloat(t);
              except
                istWahr:=u=t;
              end;
            '≠','<>':
              try
                istWahr:=strtofloat(u)<>strtofloat(t);
              except
                istWahr:=u<>t;
              end;
            '<': istWahr:=strtofloat(u)<strtofloat(t);
            '>': istWahr:=strtofloat(u)>strtofloat(t);
            else begin
              if assigned(prot) then prot.schreibe('Operator '''+binops[j]+''' ist nicht implementiert!',true)
              else gibAus('Operator '''+binops[j]+''' ist nicht implementiert!',3);
              exit;
            end;
          end{of case};
          if not istWahr then
            delete(i);
          break;
        end;
      if not gefunden then begin
        if assigned(prot) then prot.schreibe('Ich kann keinen gültigen Operator in Bedingung '''+t+''' finden!',true)
        else gibAus('Ich kann keinen gültigen Operator in Bedingung '''+t+''' finden!',3);
        exit;
      end;
      if not istWahr then
        continue;
      self[i]:=s;
    end;
    inc(i);
  end;

  i:=0;
  while i<count do begin  // nochmals Übersprünge überspringen und alles nach 'Dateiende' ignorieren
    s:=self[i];
    if startetMit('!überspringe:',s) then begin
      for j:=0 to strtoint(s) do
        delete(i);
      continue;
    end;

    if s='Dateiende' then begin
      inc(i);
      while i<count do
        delete(i);
      continue;
    end;
    inc(i);
  end;

  result:=true;
end;

procedure _del(var s: string; p,c: longint);
begin
 delete(s,p,c);
end;

end.