|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 #include "stdafx.h" |
|
23 #include "CommandLine.h" |
|
24 |
|
25 CommandLine::CommandLine() |
|
26 : iCount(0), pCmdLine(NULL) |
|
27 { |
|
28 char *pTmpCmdLine = GetCommandLine(); |
|
29 |
|
30 // see if we have one |
|
31 if (pTmpCmdLine && *pTmpCmdLine) |
|
32 { |
|
33 char *pStart, *ptr; |
|
34 unsigned int len = strlen(pTmpCmdLine); |
|
35 |
|
36 // save a copy |
|
37 pCmdLine = new char[len + 1]; |
|
38 strcpy(pCmdLine, pTmpCmdLine); |
|
39 |
|
40 // get a working copy |
|
41 pTmpCmdLine = new char[len + 1]; |
|
42 strcpy(pTmpCmdLine, pCmdLine); |
|
43 ptr = pTmpCmdLine; |
|
44 |
|
45 // reset the array |
|
46 for (int i=0;i<256;i++) |
|
47 pArguments[i] = NULL; |
|
48 |
|
49 // limit of 256 arguments |
|
50 for (i=0;i<256;i++) |
|
51 { |
|
52 pStart = ptr; |
|
53 |
|
54 // step to start of argument |
|
55 while (*pStart && *pStart == ' ') |
|
56 pStart++; |
|
57 |
|
58 // step to end of argument |
|
59 ptr = pStart; |
|
60 while (*ptr && *ptr != ' ') |
|
61 { |
|
62 // if path is enclosed in quotes, we skip any spaces that might exist within them |
|
63 // NOTE: if a quote exists inside the path, we will lose the first part of the argument! |
|
64 if (*ptr == '"') |
|
65 { |
|
66 ptr++; |
|
67 pStart = ptr; // don't want first quote in the returned argument |
|
68 while (*ptr && *ptr != '"') |
|
69 ptr++; |
|
70 |
|
71 if (*ptr == '"') |
|
72 { |
|
73 *ptr = (char)0; // don't want last quote in the returned argument |
|
74 } |
|
75 } |
|
76 ptr++; |
|
77 } |
|
78 |
|
79 // not at the end so separate arguments |
|
80 if (*ptr) |
|
81 { |
|
82 (*ptr) = (char)0; |
|
83 ptr++; |
|
84 } |
|
85 |
|
86 if (*pStart) |
|
87 { |
|
88 // store argument |
|
89 pArguments[i] = new char[strlen(pStart) + 1]; |
|
90 strcpy(pArguments[i], pStart); |
|
91 iCount++; |
|
92 } |
|
93 else |
|
94 break; |
|
95 } |
|
96 |
|
97 delete [] pTmpCmdLine; |
|
98 } |
|
99 } |
|
100 |
|
101 |
|
102 // return whole command line less the first arg (app name) |
|
103 char * CommandLine::GetArgumentString() |
|
104 { |
|
105 if (pCmdLine && *pCmdLine) |
|
106 return pCmdLine; |
|
107 |
|
108 return NULL; |
|
109 } |
|
110 |
|
111 |
|
112 // return an argument match |
|
113 bool CommandLine::FindArgument(const char *pArg) |
|
114 { |
|
115 bool valid = false; |
|
116 |
|
117 for (unsigned int i=0;i<iCount;i++) |
|
118 { |
|
119 if (strcmp(pArg, pArguments[i]) == 0) |
|
120 { |
|
121 valid = true; |
|
122 break; |
|
123 } |
|
124 } |
|
125 |
|
126 return valid; |
|
127 } |
|
128 |
|
129 |
|
130 // return the argument according to the supplied index |
|
131 char * CommandLine::GetProgramArgument(const unsigned int iIndex) |
|
132 { |
|
133 if (iIndex < iCount) |
|
134 { |
|
135 return pArguments[iIndex]; |
|
136 } |
|
137 |
|
138 return NULL; |
|
139 } |
|
140 |
|
141 |
|
142 // find the argument that begins with the supplied 'switch' and return the value less the 'switch' |
|
143 char * CommandLine::GetArgumentBySwitch(const char *pSwitch, const bool bIncludeSwitch) |
|
144 { |
|
145 unsigned int len = strlen(pSwitch); |
|
146 |
|
147 for (unsigned int i=0;i<iCount;i++) |
|
148 { |
|
149 if (strncmp(pArguments[i], pSwitch, len) == 0) |
|
150 { |
|
151 if (bIncludeSwitch) |
|
152 return pArguments[i]; |
|
153 else |
|
154 return pArguments[i] + len; |
|
155 } |
|
156 } |
|
157 |
|
158 return NULL; |
|
159 } |
|
160 |
|
161 |