summaryrefslogtreecommitdiff
path: root/fileunit.pas
blob: 838556c93541e1312c4fe7d60f2a9439f94ea780 (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
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];
    s:=erstesArgument(s,'#');
    if length(s)=0 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 startetMit('!überspringe:',s) then begin
      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 startetMit('!Schleife:',s) then begin
      l:=i;
      zeilen.delete(i);
      t:=erstesArgument(s,':');
      if (length(t)=0) or (t[1]<>'$') then exit;

      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 length(s)>0 do begin
        u:=erstesArgument(s);
        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 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));
          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 startetMit('!überspringe:',s) then begin
      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.