summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Utility/NXCommandLine.cpp
blob: 5001192f83b44cbd7f6db2ffb2ca4edacbc8d0fe (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*------------------------------------------------------
   CCmdLine

   A utility for parsing command lines.

   Copyright (C) 1999 Chris Losinger, Smaller Animals Software.
   http://www.smalleranimals.com

   This software is provided 'as-is', without any express
   or implied warranty.  In no event will the authors be 
   held liable for any damages arising from the use of this software.

   Permission is granted to anyone to use this software 
   for any purpose, including commercial applications, and 
   to alter it and redistribute it freely, subject to the 
   following restrictions:

     1. The origin of this software must not be misrepresented; 
   you must not claim that you wrote the original software. 
   If you use this software in a product, an acknowledgment 
   in the product documentation would be appreciated but is not required.

     2. Altered source versions must be plainly marked as such, 
   and must not be misrepresented as being the original software.

     3. This notice may not be removed or altered from any source 
   distribution.

   See SACmds.h for more info.
------------------------------------------------------*/

// if you're using MFC, you'll need to un-comment this line
// #include "stdafx.h"

#include "Nanorex/Utility/NXCommandLine.h"
//#include "crtdbg.h"

namespace Nanorex {

/* FUNCTION: SplitLine */
int NXCommandLine::SplitLine(const char *line) {
	
	// Build atom array
	std::string atom = "";
	int index;
	int lineLength = strlen(line);
	bool inQuotedString = false;
	std::vector<std::string> atomArray;
	for (index = 0; index < lineLength; index++) {
		if (line[index] == '"') {
			inQuotedString = !inQuotedString;
			
		} else if (line[index] != ' ') {
			atom += line[index];
			
		} else { // handle space
			if (inQuotedString) {
				atom += line[index];
				
			} else {
				inQuotedString = false;
				if (atom != "") {
					atomArray.push_back(atom);
				}
				atom = "";
			}
		}
	}
	if ((lineLength > 0) && (atom != "")) {
		atomArray.push_back(atom);
	}
	
	// Build char* array
	int arrayLength = atomArray.size();
	char** argv = new char*[arrayLength];
	for (index = 0; index < arrayLength; index++) {
		char* buffer = new char[atomArray[index].size()+1];
		buffer = strcpy(buffer, atomArray[index].c_str());
		argv[index] = buffer;
	}
	
	int switchCount = 0;
	if (arrayLength != 0)
		switchCount = SplitLine(arrayLength, argv);
	
	// Free memory
	for (index = 0; index < arrayLength; index++)
		delete argv[index];
	delete argv;
	
	return switchCount;
}


/*------------------------------------------------------
  int CCmdLine::SplitLine(int argc, char **argv)

  parse the command line into switches and arguments

  returns number of switches found
------------------------------------------------------*/
int NXCommandLine::SplitLine(int argc, char **argv)
{
	clear();
	
	StringType curParam; // current argv[x]
	
   // skip the exe name (start with i = 1)
	for (int i = 1; i < argc; i++)
	{
      // if it's a switch, start a new CCmdLine
		if (IsSwitch(argv[i]))
		{
			curParam = argv[i];
			
			StringType arg;
			
         // look at next input string to see if it's a switch or an argument
			if (i + 1 < argc)
			{
				if (!IsSwitch(argv[i + 1]))
				{
               // it's an argument, not a switch
					arg = argv[i + 1];
					
               // skip to next
					i++;
				}
				else
				{
					arg = "";
				}
			}
			
         // add it
			CCmdParam cmd;
			
         // only add non-empty args
			if (arg != "")
			{
				cmd.m_strings.push_back(arg);
			}
			
         // add the CCmdParam to 'this'
			std::pair<NXCommandLine::iterator, bool> res =
				insert(NXCommandLine::value_type(curParam, cmd));
			
		}
		else
		{
         // it's not a new switch, so it must be more stuff for the last switch
			
         // ...let's add it
			NXCommandLine::iterator theIterator;
			
         // get an iterator for the current param
			theIterator = find(curParam);
			if (theIterator!=end())
			{
				(*theIterator).second.m_strings.push_back(argv[i]);
			}
			else
			{
            // ??
			}
		}
	}
	
	return size();
}

/*------------------------------------------------------

   protected member function
   test a parameter to see if it's a switch :

   switches are of the form : -x
   where 'x' is one or more characters.
   the first character of a switch must be non-numeric!

------------------------------------------------------*/

bool NXCommandLine::IsSwitch(const char *pParam)
{
	if (pParam==NULL)
		return false;
	
   // switches must non-empty
   // must have at least one character after the '-'
	int len = strlen(pParam);
	if (len <= 1)
	{
		return false;
	}
	
   // switches always start with '-'
	if (pParam[0]=='-')
	{
      // allow negative numbers as arguments.
      // ie., don't count them as switches
		return (!isdigit(pParam[1]));
	}
	else
	{
		return false;
	}
}

/*------------------------------------------------------
   bool CCmdLine::HasSwitch(const char *pSwitch)

   was the switch found on the command line ?

   ex. if the command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5

   call                          return
   ----                          ------
   cmdLine.HasSwitch("-a")       true
   cmdLine.HasSwitch("-z")       false
------------------------------------------------------*/

bool NXCommandLine::HasSwitch(const char *pSwitch)
{
	NXCommandLine::iterator theIterator;
	theIterator = find(pSwitch);
	return (theIterator!=end());
}

/*------------------------------------------------------

   StringType CCmdLine::GetSafeArgument(const char *pSwitch, int iIdx, const char *pDefault)

   fetch an argument associated with a switch . if the parameter at
   index iIdx is not found, this will return the default that you
   provide.

   example :

   command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5

   call                                      return
   ----                                      ------
   cmdLine.GetSafeArgument("-a", 0, "zz")    p1
   cmdLine.GetSafeArgument("-a", 1, "zz")    p2
   cmdLine.GetSafeArgument("-b", 0, "zz")    p4
   cmdLine.GetSafeArgument("-b", 1, "zz")    zz

------------------------------------------------------*/

StringType NXCommandLine::GetSafeArgument(const char *pSwitch, int iIdx, const char *pDefault)
{
	StringType sRet;
	
	if (pDefault!=NULL)
		sRet = pDefault;
	
	try
	{
		sRet = GetArgument(pSwitch, iIdx);
	}
	catch (...)
	{
	}
	
	return sRet;
}

/*------------------------------------------------------

   StringType CCmdLine::GetArgument(const char *pSwitch, int iIdx)

   fetch a argument associated with a switch. throws an exception 
   of (int)0, if the parameter at index iIdx is not found.

   example :

   command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5

   call                             return
   ----                             ------
   cmdLine.GetArgument("-a", 0)     p1
   cmdLine.GetArgument("-b", 1)     throws (int)0, returns an empty string

------------------------------------------------------*/

StringType NXCommandLine::GetArgument(const char *pSwitch, int iIdx)
{
	if (HasSwitch(pSwitch))
	{
		NXCommandLine::iterator theIterator;
		
		theIterator = find(pSwitch);
		if (theIterator!=end())
		{
			if ((int)(*theIterator).second.m_strings.size() > iIdx)
			{
				return (*theIterator).second.m_strings[iIdx];
			}
		}
	}
	
	throw (int)0;
	
	return "";
}

/*------------------------------------------------------
   int CCmdLine::GetArgumentCount(const char *pSwitch)

   returns the number of arguments found for a given switch.

   returns -1 if the switch was not found

------------------------------------------------------*/

int NXCommandLine::GetArgumentCount(const char *pSwitch)
{
	int iArgumentCount = -1;
	
	if (HasSwitch(pSwitch))
	{
		NXCommandLine::iterator theIterator;
		
		theIterator = find(pSwitch);
		if (theIterator!=end())
		{
			iArgumentCount = (*theIterator).second.m_strings.size();
		}
	}
	
	return iArgumentCount;
}

} // nanohive::