summaryrefslogtreecommitdiff
path: root/analyzer.lpr
blob: 5b6d1501670805dd0c0b431d4295d7f7c1c8306d (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
program analyzer;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  classes, sysUtils, custApp, valuesunit, lowlevelunit, dateutils;

type

  tOption = (oHelp, oAcceleration, oPosition, oOutput);

  tOptionMeta = record
    long:  string;
    short: char;
    value: byte;   // bit 0: kann Wert haben; bit 1: muss Wert haben
  end;

  tOptionContent = record
    typ:      tOption;
    hasValue: boolean;
    value:    string;
  end;

  { TAnalyzer }

  tAnalyzer = class(tCustomApplication)
  protected
    options: array of tOptionContent;
    oCnt:    array[tOption] of longint;
    procedure doRun; override;
    procedure parseOptions;
    function optionArgument(o: tOption; i: longint): string;
  public
    constructor create(theOwner: tComponent); override;
    destructor destroy; override;
    procedure writeHelp; virtual;
  end;

const
  optionsMeta: array[tOption] of tOptionMeta = (
    (long: 'help';         short: 'h'; value: $00),
    (long: 'acceleration'; short: 'a'; value: $03),
    (long: 'position';     short: 'p'; value: $03),
    (long: 'output';       short: 'o'; value: $03)
  );

{ TAnalyzer }

procedure tAnalyzer.doRun;
var
  i:      longint;
  values: array of array[boolean] of tValues;
begin
  if oCnt[oHelp]>0 then begin
    writeHelp;
    terminate;
    exit;
  end;

  if oCnt[oAcceleration]=0 then begin
    writeln('error: required option ''-a'' not given');
    writeHelp;
    halt(1);
  end;

  if oCnt[oAcceleration]<>oCnt[oPosition] then begin
    writeln('error: option ''-p'' not given equally often as option ''-a''');
    writeHelp;
    halt(1);
  end;

  if oCnt[oOutput]<>1 then begin
    writeln('error: required option ''-o'' not given exactly once');
    writeHelp;
    halt(1);
  end;

  for i:=0 to oCnt[oAcceleration]-1 do begin
    if not fileexists(optionArgument(oAcceleration,i)) then begin
      writeln('Acceleration file '''+optionArgument(oAcceleration,i)+''' does not exist');
      halt(1);
    end;
    if not fileexists(optionArgument(oPosition,i)) then begin
      writeln('Position file '''+optionArgument(oPosition,i)+''' does not exist');
      halt(1);
    end;
  end;

  { add your program here }
  setlength(values,oCnt[oAcceleration]);
  for i:=0 to length(values)-1 do begin
    values[i,false]:=tValues.create;
    values[i,false].readFromFile(optionArgument(oAcceleration,i));
    values[i,true]:=tValues.create;
    values[i,true].readFromFile(values[i,false].timeInterval,optionArgument(oPosition,i));
    values[i,false].intersect(values[i,true]);
  end;

  for i:=0 to length(values)-1 do begin
    values[i,false].free;
    values[i,true].free;
  end;
  setlength(values,0);

  // stop program loop
  terminate;
end;

procedure tAnalyzer.parseOptions;
var
  i,j,incre,jncre: longint;
  s:               string;
  oTypen:          array of byte; // 0 = argument; 1 = short option(s); 2 = long option
  o:               tOption;
begin
  for o:=low(tOption) to high(tOption) do
    oCnt[o]:=0;

  setlength(oTypen,paramcount);
  for i:=0 to length(oTypen)-1 do
    if copy(paramstr(i+1),1,2)='--' then
      oTypen[i]:=2
    else if copy(paramstr(i+1),1,1)='-' then
      oTypen[i]:=1
    else
      oTypen[i]:=0;

  setlength(options,0);
  i:=0;
  while i<length(oTypen) do begin
    incre:=1;
    s:=paramstr(i+1);
    case oTypen[i] of
      0: begin
        writeln('Spurious argument '''+s+''' at position '+inttostr(i+1));
        halt(1);
      end;
      1: begin
        delete(s,1,1);

        j:=1;
        while j<=length(s) do begin
          jncre:=1;
          setlength(options,length(options)+1);
          options[length(options)-1].hasValue:=false;
          options[length(options)-1].value:='';
          options[length(options)-1].typ:=low(optionsMeta);
          while options[length(options)-1].typ<high(optionsMeta) do begin
            if (optionsMeta[options[length(options)-1].typ].short=s[j]) then
              break;
            inc(options[length(options)-1].typ);
          end;
          if (optionsMeta[options[length(options)-1].typ].short<>s[j]) then begin
            writeln('Illegal option at position '+inttostr(i+1)+', character '+inttostr(j)+': '''+s[j]+'''');
            writeHelp;
            halt(1);
          end;

          if odd(optionsMeta[options[length(options)-1].typ].value) // may have value
            and (((i<length(oTypen)-1) and (oTypen[i+1]=0)) or (j<length(s))) then begin // still arguments left
            options[length(options)-1].hasValue:=true;
            if j<length(s) then begin
              options[length(options)-1].value:=copy(s,j+1,length(s));
              jncre:=length(s);
            end
            else begin
              options[length(options)-1].value:=paramstr(i+2);
              inc(incre);
            end;
          end
          else
            options[length(options)-1].hasValue:=false;

          if (not options[length(options)-1].hasValue) and odd(optionsMeta[options[length(options)-1].typ].value shr 1) then begin
            writeln('Option '''+optionsMeta[options[length(options)-1].typ].short+''' at position '+inttostr(i+1)+', character '+inttostr(j)+' requires an argument!');
            writeHelp;
            halt(1);
          end;
          inc(oCnt[options[length(options)-1].typ]);
          inc(j,jncre);
        end;
      end;
      2: begin
        delete(s,1,2);

        setlength(options,length(options)+1);

        if pos('=',s)>0 then begin
          options[length(options)-1].value:=copy(s,pos('=',s)+1,length(s));
          s:=copy(s,1,pos('=',s)-1);
          options[length(options)-1].hasValue:=true;
        end
        else begin
          options[length(options)-1].value:='';
          options[length(options)-1].hasValue:=false;
        end;
        options[length(options)-1].typ:=low(optionsMeta);
        while options[length(options)-1].typ<high(optionsMeta) do begin
          if (optionsMeta[options[length(options)-1].typ].long<>'') and (optionsMeta[options[length(options)-1].typ].long=s) then
            break;
          inc(options[length(options)-1].typ);
        end;
        if (optionsMeta[options[length(options)-1].typ].long='') or (optionsMeta[options[length(options)-1].typ].long<>s) then begin
          writeln('Illegal option at position '+inttostr(i+1)+': '''+s+'''');
          writeHelp;
          halt(1);
        end;

        if (not options[length(options)-1].hasValue) // no value yet
          and odd(optionsMeta[options[length(options)-1].typ].value) // may have value
          and (i<length(oTypen)-1) // still arguments left
          and (oTypen[i+1]=0) then begin // next argument is no option
          options[length(options)-1].hasValue:=true;
          options[length(options)-1].value:=paramstr(i+2);
          inc(incre);
        end;

        if options[length(options)-1].hasValue and not odd(optionsMeta[options[length(options)-1].typ].value) then begin
          writeln('Option '''+optionsMeta[options[length(options)-1].typ].long+''' at position '+inttostr(i+1)+' does not take an argument!');
          writeHelp;
          halt(1);
        end;

        if (not options[length(options)-1].hasValue) and odd(optionsMeta[options[length(options)-1].typ].value shr 1) then begin
          writeln('Option '''+optionsMeta[options[length(options)-1].typ].long+''' at position '+inttostr(i+1)+' requires an argument!');
          writeHelp;
          halt(1);
        end;
        inc(oCnt[options[length(options)-1].typ]);
      end;
    end{of case};
    inc(i,incre);
  end;
end;

function tAnalyzer.optionArgument(o: tOption; i: longint): string;
var
  j: longint;
begin
  for j:=0 to length(options)-1 do
    if (options[j].typ=o) and options[j].hasValue then begin
      dec(i);
      if i<0 then begin
        result:=options[j].value;
        exit;
      end;
    end;
  raise exception.create('Option '''+optionsMeta[o].long+''' not available '+inttostr(i+1)+' times');
end;

constructor tAnalyzer.create(theOwner: tComponent);
begin
  inherited create(theOwner);
  parseOptions;
  stopOnException:=true;
end;

destructor tAnalyzer.destroy;
begin
  inherited destroy;
end;

procedure tAnalyzer.writeHelp;
begin
  { add your help code here }
  writeln('usage: ', exeName, ' -h');
  writeln('usage: ', exeName, ' -i input1 -i input2 -o output');
end;

var
  application: tAnalyzer;
begin
  application:=tAnalyzer.create(nil);
  application.title:='analyzer';
  application.run;
  application.free;
end.