1 (*
2  * The contents of this file are subject to the Mozilla Public License
3  * Version 1.1 (the "License"); you may not use this file except in
4  * compliance with the License. You may obtain a copy of the License at
5  * http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9  * License for the specific language governing rights and limitations
10  * under the License.
11  *
12  * The Initial Developer of this code is John Hansen.
13  * Portions created by John Hansen are Copyright (C) 2009 John Hansen.
14  * All Rights Reserved.
15  *
16  *)
17 unit ParamUtils;
18 
19 interface
20 
21 uses
22   Classes;
23 
ParamSwitchnull24 function ParamSwitch(S: String; bIgnoreCase : Boolean = true; const cmdLine : string = ''): Boolean;
ParamValuenull25 function ParamValue(S: String; bIgnoreCase : Boolean = true; const cmdLine : string = ''): String;
ParamIntValuenull26 function ParamIntValue(S: String; def : Integer = -1; bIgnoreCase : Boolean = true; const cmdLine : string = ''): Integer;
JCHParamStrnull27 function JCHParamStr(Index: Integer; const cmdLine : string = ''): string;
JCHParamCountnull28 function JCHParamCount(const cmdLine : string = ''): Integer;
getFilenameParamnull29 function getFilenameParam(const cmdLine : string = '') : string;
30 procedure LoadParamDefinitions(aStrings : TStrings; const cmdLine : string = '');
31 
32 implementation
33 
34 uses
35   SysUtils;
36 
CharNextnull37 function CharNext(P : PChar) : PChar;
38 begin
39   Result := P;
40   Inc(Result);
41 end;
42 
GetParamStrnull43 function GetParamStr(P: PChar; var Param: string): PChar;
44 var
45   i, Len: Integer;
46   Start, S, Q: PChar;
47 begin
48   while True do
49   begin
50     while (P[0] <> #0) and (P[0] <= ' ') do
51       P := CharNext(P);
52     if (P[0] = '"') and (P[1] = '"') then Inc(P, 2) else Break;
53   end;
54   Len := 0;
55   Start := P;
56   while P[0] > ' ' do
57   begin
58     if P[0] = '"' then
59     begin
60       P := CharNext(P);
61       while (P[0] <> #0) and (P[0] <> '"') do
62       begin
63         Q := CharNext(P);
64         Inc(Len, Q - P);
65         P := Q;
66       end;
67       if P[0] <> #0 then
68         P := CharNext(P);
69     end
70     else
71     begin
72       Q := CharNext(P);
73       Inc(Len, Q - P);
74       P := Q;
75     end;
76   end;
77 
78   SetLength(Param, Len);
79 
80   P := Start;
81   S := Pointer(Param);
82   i := 0;
83   while P[0] > ' ' do
84   begin
85     if P[0] = '"' then
86     begin
87       P := CharNext(P);
88       while (P[0] <> #0) and (P[0] <> '"') do
89       begin
90         Q := CharNext(P);
91         while P < Q do
92         begin
93           S[i] := P^;
94           Inc(P);
95           Inc(i);
96         end;
97       end;
98       if P[0] <> #0 then P := CharNext(P);
99     end
100     else
101     begin
102       Q := CharNext(P);
103       while P < Q do
104       begin
105         S[i] := P^;
106         Inc(P);
107         Inc(i);
108       end;
109     end;
110   end;
111 
112   Result := P;
113 end;
114 
JCHParamStrnull115 function JCHParamStr(Index: Integer; const cmdLine : string): string;
116 var
117   P: PChar;
118 begin
119   Result := '';
120   if (Index = 0) or (cmdLine = '') then
121     Result := ParamStr(Index)
122   else
123   begin
124     P := PChar(cmdLine);
125     while True do
126     begin
127       P := GetParamStr(P, Result);
128       if (Index = 0) or (Result = '') then Break;
129       Dec(Index);
130     end;
131   end;
132 end;
133 
JCHParamCountnull134 function JCHParamCount(const cmdLine : string): Integer;
135 var
136   P: PChar;
137   S: string;
138 begin
139   Result := 0;
140   S := '';
141   if cmdLine = '' then
142     Result := ParamCount
143   else
144   begin
145     P := GetParamStr(PChar(cmdLine), S);
146     while True do
147     begin
148       P := GetParamStr(P, S);
149       if S = '' then Break;
150       Inc(Result);
151     end;
152   end;
153 end;
154 
ParamSwitchnull155 function ParamSwitch(S: String; bIgnoreCase : Boolean; const cmdLine : string): Boolean;
156 var
157   I: Integer;
158   P: String;
159 begin
160   Result := False ;
161   if bIgnoreCase then S := UpperCase(S);
162   for I := 0 to JCHParamCount(cmdLine) do begin
163     P := JCHParamStr(I, cmdLine);
164     if bIgnoreCase then P := UpperCase(P);
165     if (P=S) or (Pos(S+'=',P)=1) then begin
166       Result := True ;
167       Break;
168     end;
169   end;
170 end;
171 
ParamValuenull172 function ParamValue(S: String; bIgnoreCase : Boolean; const cmdLine : string): String;
173 var
174   I : Integer;
175   P, val: String;
176 begin
177   Result := '' ;
178   if bIgnoreCase then S := UpperCase(S);
179   for I := 0 to JCHParamCount(cmdLine) do begin
180     val := JCHParamStr(I, cmdLine);
181     if bIgnoreCase then
182       P := UpperCase(val)
183     else
184       P := val;
185     if (Pos(S+'=',P)<>0) then begin
186       Result := Copy(val,Length(S)+2,MaxInt);
187       Break;
188     end;
189   end;
190 end;
191 
ParamIntValuenull192 function ParamIntValue(S: String; def : integer; bIgnoreCase : Boolean; const cmdLine : string): Integer;
193 begin
194   Result := StrToIntDef(ParamValue(S, bIgnoreCase, cmdLine), def);
195 end;
196 
getFilenameParamnull197 function getFilenameParam(const cmdLine : string) : string;
198 var
199   i : Integer;
200   tmp : string;
201 begin
202   Result := '';
203   for i := 1 to JCHParamCount(cmdLine) do
204   begin
205     tmp := JCHParamStr(i, cmdLine);
206     if Pos('-', tmp) = 1 then Continue; // a switch
207     // the first parameter that is not a switch is the filename
208     Result := tmp;
209     Break;
210   end;
211 end;
212 
213 procedure LoadParamDefinitions(aStrings : TStrings; const cmdLine : string);
214 var
215   I : Integer;
216   P: String;
217 begin
218   for I := 0 to JCHParamCount(cmdLine) do begin
219     P := JCHParamStr(I, cmdLine);
220     if Pos('-D=', P) = 1 then begin
221       // this is a #define
222       System.Delete(P, 1, 3);
223       if Pos('=', P) = 0 then
224         P := P + '=1';
225       aStrings.Add(P);
226     end;
227   end;
228 end;
229 
230 end.
231