KSquare Utilities
CmdLineExpander.cpp
Go to the documentation of this file.
1 /* CmdLineExpander.cpp -- PreProcess Command Line parameters.
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #include "FirstIncludes.h"
6 
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <string>
11 #include <iomanip>
12 #include <fstream>
13 #include <iostream>
14 #include <fstream>
15 #include <vector>
16 
17 #include "MemoryDebug.h"
18 
19 #ifdef WIN32
20 #include <io.h>
21 #include <windows.h>
22 #else
23 //#include <sys/loadavg.h>
24 #include <unistd.h>
25 #endif
26 
27 #include <sys/types.h>
28 
29 using namespace std;
30 
31 #include "CmdLineExpander.h"
32 #include "OSservices.h"
33 #include "KKStr.h"
34 using namespace KKB;
35 
36 
37 
38 CmdLineExpander::CmdLineExpander (const KKStr& _applicationName,
39  RunLog& _log,
40  kkint32 argc,
41  char** argv
42  ):
43 
44  applicationName (_applicationName),
45  log (_log)
46 
47 {
48  ExpandCmdLine (argc, argv);
49 }
50 
51 
52 
53 
54 CmdLineExpander::CmdLineExpander (const KKStr& _applicationName,
55  RunLog& _log,
56  const KKStr& _cmdLine
57  ):
58  applicationName (_applicationName),
59  log (_log)
60 
61 {
62  VectorKKStr initialParameters;
63 
64  KKStr cmdLine (_cmdLine);
65 
66  cmdLine.TrimLeft ();
67  while (!cmdLine.Empty ())
68  {
69  KKStr nextField = cmdLine.ExtractQuotedStr ("\n\r\t ", false); // false = Do not decode escape characters
70  nextField.TrimRight ();
71  if (!nextField.Empty ())
72  initialParameters.push_back (nextField);
73  cmdLine.TrimLeft ();
74  }
75 
76  BuildCmdLineParameters (initialParameters);
77  BuildExpandedParameterPairs ();
78 }
79 
80 
81 
82 
83 
85 {
86 }
87 
88 
89 
90 
91 
93  char** argv
94  )
95 {
96  parmsGood = true;
97 
98  VectorKKStr initialParameters;
99  {
100  kkint32 y;
101  for (y = 1; y < argc; y++)
102  initialParameters.push_back (argv[y]);
103  }
104 
105  BuildCmdLineParameters (initialParameters);
106  BuildExpandedParameterPairs ();
107 
108  return;
109 } /* ExpandCmdLine */
110 
111 
112 
113 
114 bool FileInStack (const KKStr& cmdFileName,
115  const VectorKKStr& cmdFileStack
116  )
117 {
118  VectorKKStr::const_iterator idx;
119  for (idx = cmdFileStack.begin (); idx != cmdFileStack.end (); idx++)
120  {
121  if (*idx == cmdFileName)
122  return true;
123  }
124  return false;
125 
126 } /* FileInStack */
127 
128 
129 
130 
131 void CmdLineExpander::BuildCmdLineParameters (const VectorKKStr& argv)
132 {
133  kkuint32 x = 0;
134 
135  while (x < argv.size ())
136  {
137  KKStr s = argv[x];
138  x++;
139 
140  KKStr sUpper = s.ToUpper();
141  if ((sUpper == "-L") || (sUpper == "-LOGFILE"))
142  {
143  if (x < argv.size ())
144  {
145  if (argv[x][(kkint16)0] != '-')
146  {
147  logFileName = argv[x];
148  if (!logFileName.Empty ())
149  log.AttachFile (logFileName);
150  x++;
151  }
152  }
153 
154  if (logFileName.Empty ())
155  {
156  log.Level (-1) << std::endl << std::endl;
157  log.Level (-1) << applicationName << " - Invalid Log File Parameter (-L)." << endl;
158  log.Level (-1) << " Name of log file required." << endl;
159  log.Level (-1) << endl;
160  parmsGood = false;
161  }
162 
163  }
164 
165  else if (sUpper == "-CMDFILE")
166  {
167  KKStr cmdFileName = "";
168 
169  if (x < argv.size ())
170  {
171  if (argv[x][(kkint16)0] != '-')
172  {
173  cmdFileName = argv[x];
174  x++;
175  }
176  }
177 
178  if (cmdFileName.Empty ())
179  {
180  log.Level (-1) << endl << endl << endl
181  << applicationName << " " << "BuildCmdLineParameters *** ERROR ***" << endl << endl
182  << "-CMDFILE option did not define a file name." << endl
183  << endl;
184 
185  parmsGood = false;
186  }
187 
188  else
189  {
190  if (FileInStack (cmdFileName, cmdFileStack))
191  {
192  log.Level (-1) << endl << endl << endl
193  << applicationName << " BuildCmdLineParameters *** ERROR ***" << endl
194  << endl
195  << "-CMDFILE [" << cmdFileName << "] is being called recursively." << endl
196  << endl;
197 
198  parmsGood = false;
199  }
200  else
201  {
202  bool validFile = true;
203  cmdFileStack.push_back (cmdFileName);
204  VectorKKStr cmdFileParameters;
205  ExtractParametersFromFile (cmdFileName, cmdFileParameters, validFile);
206  BuildCmdLineParameters (cmdFileParameters);
207  cmdFileStack.pop_back ();
208  if (!validFile)
209  parmsGood = false;
210  }
211  }
212  }
213 
214  else
215  {
216  expandedParameters.push_back (s);
217  }
218  }
219 } /* BuildCmdLineParameters */
220 
221 
222 
223 
224 void CmdLineExpander::BuildExpandedParameterPairs ()
225 {
226  kkuint32 x = 0;
227  expandedParameterPairs.clear ();
228 
229  while (x < expandedParameters.size ())
230  {
231  const KKStr& nextField = expandedParameters[x];
232  x++;
233 
234  KKStr parmSwitch = "";
235  KKStr parmValue = "";
236 
237  if (!ParameterIsASwitch (nextField))
238  {
239  parmSwitch = "";
240  parmValue = nextField;
241  }
242  else
243  {
244  parmSwitch = nextField;
245  if (x < expandedParameters.size ())
246  {
247  if (!ParameterIsASwitch (expandedParameters[x]))
248  {
249  parmValue = expandedParameters[x];
250  x++;
251  }
252  }
253  }
254 
255  expandedParameterPairs.push_back (KKStrPair (parmSwitch, parmValue));
256  }
257 
258 } /* BuildExpandedParameterPairs */
259 
260 
261 
262 
263 bool CmdLineExpander::ParameterIsASwitch (const KKStr& parm)
264 {
265  if (parm.Len () < 1)
266  return false;
267 
268  if (parm[(kkint16)0] != '-')
269  return false;
270 
271  if (parm.Len () == 1)
272  return true;
273 
274  double parmValue = 0.0;
275  if (parm.ValidNum (parmValue))
276  return false;
277 
278  return true;
279 } /* ParameterIsASwitch */
280 
281 
282 
283 
284 void CmdLineExpander::ExtractParametersFromFile (const KKStr& cmdFileName,
285  VectorKKStr& cmdFileParameters,
286  bool& validFile
287  )
288 {
289  FILE* in = osFOPEN (cmdFileName.Str (), "r");
290  if (!in)
291  {
292  log.Level (-1) << endl << endl << endl
293  << "ExtractParametersFromFile *** EROR ***" << endl
294  << endl
295  << " Invalid CmdFile[" << cmdFileName << "]" << endl
296  << endl;
297  validFile = false;
298  return;
299  }
300 
301  KKStr token;
302  bool eof = false;
303 
304  token = osReadNextQuotedStr (in, " \n\r", eof);
305  while (!eof)
306  {
307  cmdFileParameters.push_back (token);
308  token = osReadNextQuotedStr (in, " \n\r", eof);
309  }
310 
311  fclose (in);
312 } /* ExtractParametersFromFile */
CmdLineExpander(const KKStr &_applicationName, RunLog &_log, const KKStr &_cmdLine)
Pass in the command line as a single string. Will parse first before processing.
char operator[](kkint16 i) const
Definition: KKStr.cpp:3379
KKB::KKStr osReadNextQuotedStr(FILE *in, const char *whiteSpaceCharacters, bool &eof)
Read the next Quoted String from the file.
__int32 kkint32
Definition: KKBaseTypes.h:88
CmdLineExpander(const KKStr &_applicationName, RunLog &_log, kkint32 argc, char **argv)
Constructor using the parameters passed through on the command line into main().
KKStr & TrimRight(const char *whiteSpaceChars="\n\r\t ")
Definition: KKStr.cpp:1695
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
bool ValidNum(double &value) const
Definition: KKStr.cpp:2653
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
KKTHread * KKTHreadPtr
Expands command line parameters, by parsing for special parameters and expanding them to their full v...
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
bool FileInStack(const KKStr &cmdFileName, const VectorKKStr &cmdFileStack)
void TrimLeft(const char *whiteSpaceChars="\n\r\t ")
Definition: KKStr.cpp:1745
bool Empty() const
Definition: KKStr.h:241
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
KKStr ExtractQuotedStr(const char *delChars, bool decodeEscapeCharacters)
Definition: KKStr.cpp:3282
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
FILE * osFOPEN(const char *fileName, const char *mode)
Definition: OSservices.cpp:74
Used for logging messages.
Definition: RunLog.h:49
void ExpandCmdLine(kkint32 argc, char **argv)