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; if not directoryexists(optionArgument(oOutput,0)) then begin writeln('Output directory '''+optionArgument(oOutput,0)+''' does not exist'); halt(1); 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].writeCombined(values[i,true],optionArgument(oOutput,0)+'/analyse_'+inttostr(i)+'.dat'); 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 is[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'') 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