diff options
author | Erich Eckner <git@eckner.net> | 2014-08-25 16:29:58 +0200 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2014-08-25 16:29:58 +0200 |
commit | bfdb140f638aefd97e385f7addc7ebf1ff80d248 (patch) | |
tree | 80e5fd5050afecf6bae6470502c09b6548a94783 /fileunit.pas | |
download | epost-bfdb140f638aefd97e385f7addc7ebf1ff80d248.tar.xz |
Initialer Commit
Diffstat (limited to 'fileunit.pas')
-rw-r--r-- | fileunit.pas | 273 |
1 files changed, 273 insertions, 0 deletions
diff --git a/fileunit.pas b/fileunit.pas new file mode 100644 index 0000000..3b5e6b1 --- /dev/null +++ b/fileunit.pas @@ -0,0 +1,273 @@ +unit fileunit; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, SysUtils, Math; + +type + tInputfile = class + private + zeile: longint; + zeilen: tStringlist; + public + procedure anAnfang; + function gibZeile(out s: string): boolean; + function einlesen(fnam: string): boolean; + function brauchenoch(bez: string): boolean; + constructor create; + destructor destroy; override; + end; + +implementation + +uses typenunit; + +procedure tInputfile.anAnfang; +begin + zeile:=0; +end; + +function tInputfile.gibZeile(out s: string): boolean; +begin + result:=false; + if (zeile<0) or (zeile>=zeilen.count) then exit; + s:=zeilen[zeile]; + result:=true; + inc(zeile); +end; + +function tInputfile.einlesen(fnam: string): boolean; +var f: textfile; + i,j,k,l,Ebene: longint; + s,t,u,v: string; + SchleifenInhalt: tStringlist; + istWahr,gefunden: boolean; +const binops: array[0..8] of string = + ('<=','>=','<>','≤','≥','=','≠','<','>'); +begin + result:=false; + assignfile(f,fnam); + reset(f); + zeilen.clear; + while not eof(f) do begin + readln(f,s); + zeilen.add(s); + end; + closefile(f); + zeile:=0; + + i:=0; + while i<zeilen.count do begin + s:=zeilen[i]; + while (length(s)>0) and (s[length(s)] in [' ',#9]) do + delete(s,length(s),1); + while (length(s)>0) and (s[1] in [' ',#9]) do + delete(s,1,1); + if (length(s)=0) or (s[1]='#') then begin + zeilen.delete(i); + continue; + end; + zeilen[i]:=s; + inc(i); + end; + + i:=0; + while i<zeilen.count do begin + s:=zeilen[i]; + if pos('!überspringe:',s)=1 then begin + delete(s,1,pos(':',s)); + while (length(s)>0) and (s[1] in [' ',#9]) do + delete(s,1,1); + for j:=0 to strtoint(s) do + zeilen.delete(i); + continue; + end; + + zeilen[i]:=s; + if s='Dateiende' then begin + inc(i); + while i<zeilen.count do + zeilen.delete(i); + continue; + end; + inc(i); + end; + + i:=0; + while i<zeilen.count do begin + s:=zeilen[i]; + if pos('!Schleife:',s)=1 then begin + l:=i; + zeilen.delete(i); + delete(s,1,pos(':',s)); + while (length(s)>0) and (s[1] in [' ',#9]) do + delete(s,1,1); + t:=copy(s,1,pos(':',s)-1); + while (length(t)>0) and (t[length(t)] in [' ',#9]) do + delete(t,length(t),1); + if (length(t)=0) or (t[1]<>'$') then exit; + delete(s,1,pos(':',s)); + while (length(s)>0) and (s[1] in [' ',#9]) do + delete(s,1,1); + s:=s+' '; + + SchleifenInhalt:=TStringlist.create; + Ebene:=0; + while (i<zeilen.Count) and ((Ebene<>0) or (zeilen[i]<>'!Schleifenende')) do begin + SchleifenInhalt.Add(zeilen[i]); + if zeilen[i]='!Schleifenende' then dec(Ebene); + if pos('!Schleife:',zeilen[i])=1 then inc(Ebene); + zeilen.delete(i); + end; + zeilen.delete(i); + + while pos(' ',s)+pos(#9,s)>0 do begin + u:=copy(s,1,pos(' ',s)+pos(#9,s)-1); + if pos(' ',u)>0 then + u:=copy(u,1,pos(' ',u)-1); + if pos(#9,u)>0 then + u:=copy(u,1,pos(#9,u)-1); + delete(s,1,length(u)); + while (length(s)>0) and (s[1] in [' ',#9]) do + delete(s,1,1); + for j:=0 to SchleifenInhalt.Count-1 do begin + v:=SchleifenInhalt[j]; + k:=length(v); + while (pos(t,v)>0) and (k>0) do begin + v:=copy(v,1,pos(t,v)-1)+u+copy(v,pos(t,v)+length(t),length(v)); + dec(k); + end; + zeilen.insert(i,v); + inc(i); + end; + end; + + SchleifenInhalt.Free; + i:=l; + continue; + end; + zeilen[i]:=s; + inc(i); + end; + + i:=0; + while i<zeilen.count do begin + s:=zeilen[i]; + if (length(s)>0) and (s[1]='?') then begin + delete(s,1,1); + while (length(s)>0) and (s[1] in [' ',#9]) do + delete(s,1,1); + t:=copy(s,1,pos(':',s)-1); + while pos(' ',t)>0 do + delete(t,pos(' ',t),1); + while pos(#9,t)>0 do + delete(t,pos(#9,t),1); + delete(s,1,pos(':',s)); + while (length(s)>0) and (s[1] in [' ',#9]) do + delete(s,1,1); + 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)); + delete(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 + gibAus('Operator '''+binops[j]+''' ist nicht implementiert!',3); + exit; + end; + end{of case}; + if not istWahr then + zeilen.delete(i); + break; + end; + if not gefunden then begin + gibAus('Ich kann keinen gültigen Operator in Bedingung '''+t+''' finden!',3); + exit; + end; + if not istWahr then + continue; + zeilen[i]:=s; + end; + inc(i); + end; + + i:=0; + while i<zeilen.count do begin + s:=zeilen[i]; + if pos('!überspringe:',s)=1 then begin + delete(s,1,pos(':',s)); + while (length(s)>0) and (s[1] in [' ',#9]) do + delete(s,1,1); + for j:=0 to strtoint(s) do + zeilen.delete(i); + continue; + end; + + zeilen[i]:=s; + if s='Dateiende' then begin + inc(i); + while i<zeilen.count do + zeilen.delete(i); + continue; + end; + inc(i); + end; + + result:=true; +end; + +function tInputfile.brauchenoch(bez: string): boolean; +var i: longint; + s,t: string; +begin + result:=false; + for i:=max(0,zeile-1) to zeilen.count-1 do begin + s:=' '+zeilen[i]+' '; + while pos(bez,s)>0 do begin + t:=copy(s,1,pos(bez,s)+length(bez)); + delete(s,1,pos(bez,s)+length(bez)-1); + delete(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; + +constructor tInputfile.create; +begin + inherited create; + zeilen:=tStringlist.create; + zeile:=0; +end; + +destructor tInputfile.destroy; +begin + zeilen.free; + inherited destroy; +end; + +end. + |