unit mystringlistunit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, RegExpr, Process, Math; 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; public constructor create; 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; end; 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 wb0 then begin if length(buf)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; end.