Rev 197 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
195 | f9daq | 1 | // XGetopt.cpp Version 1.2 |
2 | // |
||
3 | // Author: Hans Dietrich |
||
4 | // hdietrich2@hotmail.com |
||
5 | // |
||
6 | // Description: |
||
7 | // XGetopt.cpp implements getopt(), a function to parse command lines. |
||
8 | // |
||
9 | // History |
||
10 | // Version 1.2 - 2003 May 17 |
||
11 | // - Added Unicode support |
||
12 | // |
||
13 | // Version 1.1 - 2002 March 10 |
||
14 | // - Added example to XGetopt.cpp module header |
||
15 | // |
||
16 | // This software is released into the public domain. |
||
17 | // You are free to use it in any way you like. |
||
18 | // |
||
19 | // This software is provided "as is" with no expressed |
||
20 | // or implied warranty. I accept no liability for any |
||
21 | // damage or loss of business that this software may cause. |
||
22 | // |
||
23 | /////////////////////////////////////////////////////////////////////////////// |
||
24 | |||
25 | |||
26 | /////////////////////////////////////////////////////////////////////////////// |
||
27 | // if you are using precompiled headers then include this line: |
||
28 | //#include "stdafx.h" |
||
29 | /////////////////////////////////////////////////////////////////////////////// |
||
30 | |||
31 | |||
32 | /////////////////////////////////////////////////////////////////////////////// |
||
33 | // if you are not using precompiled headers then include these lines: |
||
34 | #include <windows.h> |
||
35 | #include <stdio.h> |
||
36 | #include <tchar.h> |
||
37 | /////////////////////////////////////////////////////////////////////////////// |
||
38 | |||
39 | |||
40 | #include "XGetopt.h" |
||
41 | |||
42 | |||
43 | /////////////////////////////////////////////////////////////////////////////// |
||
44 | // |
||
45 | // X G e t o p t . c p p |
||
46 | // |
||
47 | // |
||
48 | // NAME |
||
49 | // getopt -- parse command line options |
||
50 | // |
||
51 | // SYNOPSIS |
||
52 | // int getopt(int argc, TCHAR *argv[], TCHAR *optstring) |
||
53 | // |
||
54 | // extern TCHAR *optarg; |
||
55 | // extern int optind; |
||
56 | // |
||
57 | // DESCRIPTION |
||
58 | // The getopt() function parses the command line arguments. Its |
||
59 | // arguments argc and argv are the argument count and array as |
||
60 | // passed into the application on program invocation. In the case |
||
61 | // of Visual C++ programs, argc and argv are available via the |
||
62 | // variables __argc and __argv (double underscores), respectively. |
||
63 | // getopt returns the next option letter in argv that matches a |
||
64 | // letter in optstring. (Note: Unicode programs should use |
||
65 | // __targv instead of __argv. Also, all character and string |
||
66 | // literals should be enclosed in _T( ) ). |
||
67 | // |
||
68 | // optstring is a string of recognized option letters; if a letter |
||
69 | // is followed by a colon, the option is expected to have an argument |
||
70 | // that may or may not be separated from it by white space. optarg |
||
71 | // is set to point to the start of the option argument on return from |
||
72 | // getopt. |
||
73 | // |
||
74 | // Option letters may be combined, e.g., "-ab" is equivalent to |
||
75 | // "-a -b". Option letters are case sensitive. |
||
76 | // |
||
77 | // getopt places in the external variable optind the argv index |
||
78 | // of the next argument to be processed. optind is initialized |
||
79 | // to 0 before the first call to getopt. |
||
80 | // |
||
81 | // When all options have been processed (i.e., up to the first |
||
82 | // non-option argument), getopt returns EOF, optarg will point |
||
83 | // to the argument, and optind will be set to the argv index of |
||
84 | // the argument. If there are no non-option arguments, optarg |
||
85 | // will be set to NULL. |
||
86 | // |
||
87 | // The special option "--" may be used to delimit the end of the |
||
88 | // options; EOF will be returned, and "--" (and everything after it) |
||
89 | // will be skipped. |
||
90 | // |
||
91 | // RETURN VALUE |
||
92 | // For option letters contained in the string optstring, getopt |
||
93 | // will return the option letter. getopt returns a question mark (?) |
||
94 | // when it encounters an option letter not included in optstring. |
||
95 | // EOF is returned when processing is finished. |
||
96 | // |
||
97 | // BUGS |
||
98 | // 1) Long options are not supported. |
||
99 | // 2) The GNU double-colon extension is not supported. |
||
100 | // 3) The environment variable POSIXLY_CORRECT is not supported. |
||
101 | // 4) The + syntax is not supported. |
||
102 | // 5) The automatic permutation of arguments is not supported. |
||
103 | // 6) This implementation of getopt() returns EOF if an error is |
||
104 | // encountered, instead of -1 as the latest standard requires. |
||
105 | // |
||
106 | // EXAMPLE |
||
107 | // BOOL CMyApp::ProcessCommandLine(int argc, TCHAR *argv[]) |
||
108 | // { |
||
109 | // int c; |
||
110 | // |
||
111 | // while ((c = getopt(argc, argv, _T("aBn:"))) != EOF) |
||
112 | // { |
||
113 | // switch (c) |
||
114 | // { |
||
115 | // case _T('a'): |
||
116 | // TRACE(_T("option a\n")); |
||
117 | // // |
||
118 | // // set some flag here |
||
119 | // // |
||
120 | // break; |
||
121 | // |
||
122 | // case _T('B'): |
||
123 | // TRACE( _T("option B\n")); |
||
124 | // // |
||
125 | // // set some other flag here |
||
126 | // // |
||
127 | // break; |
||
128 | // |
||
129 | // case _T('n'): |
||
130 | // TRACE(_T("option n: value=%d\n"), atoi(optarg)); |
||
131 | // // |
||
132 | // // do something with value here |
||
133 | // // |
||
134 | // break; |
||
135 | // |
||
136 | // case _T('?'): |
||
137 | // TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]); |
||
138 | // return FALSE; |
||
139 | // break; |
||
140 | // |
||
141 | // default: |
||
142 | // TRACE(_T("WARNING: no handler for option %c\n"), c); |
||
143 | // return FALSE; |
||
144 | // break; |
||
145 | // } |
||
146 | // } |
||
147 | // // |
||
148 | // // check for non-option args here |
||
149 | // // |
||
150 | // return TRUE; |
||
151 | // } |
||
152 | // |
||
153 | /////////////////////////////////////////////////////////////////////////////// |
||
154 | |||
155 | //TCHAR *optarg; // global argument pointer |
||
156 | char *optarg; // global argument pointer |
||
157 | int optind = 0; // global argv index |
||
158 | |||
159 | //int getopt(int argc, TCHAR *argv[], TCHAR *optstring) |
||
160 | int getopt(int argc, char *argv[], char *optstring) |
||
161 | { |
||
162 | //static TCHAR *next = NULL; |
||
163 | static char *next = NULL; |
||
164 | if (optind == 0) |
||
165 | next = NULL; |
||
166 | |||
167 | optarg = NULL; |
||
168 | |||
169 | if (next == NULL || *next == _T('\0')) |
||
170 | { |
||
171 | if (optind == 0) |
||
172 | optind++; |
||
173 | |||
174 | if (optind >= argc || argv[optind][0] != _T('-') || argv[optind][1] == _T('\0')) |
||
175 | { |
||
176 | optarg = NULL; |
||
177 | if (optind < argc) |
||
178 | optarg = argv[optind]; |
||
179 | return EOF; |
||
180 | } |
||
181 | |||
182 | if (strcmp( argv[optind], _T("--")) == 0) |
||
183 | { |
||
184 | optind++; |
||
185 | optarg = NULL; |
||
186 | if (optind < argc) |
||
187 | optarg = argv[optind]; |
||
188 | return EOF; |
||
189 | } |
||
190 | |||
191 | next = argv[optind]; |
||
192 | next++; // skip past - |
||
193 | optind++; |
||
194 | } |
||
195 | |||
196 | //TCHAR c = *next++; |
||
197 | //TCHAR *cp = _tcschr(optstring, c); |
||
198 | char c = *next++; |
||
199 | //char *cp = _tcschr(optstring, c); |
||
200 | char *cp =strchr(optstring, c); |
||
201 | |||
202 | if (cp == NULL || c == _T(':')) |
||
203 | return _T('?'); |
||
204 | |||
205 | cp++; |
||
206 | if (*cp == _T(':')) |
||
207 | { |
||
208 | if (*next != _T('\0')) |
||
209 | { |
||
210 | optarg = next; |
||
211 | next = NULL; |
||
212 | } |
||
213 | else if (optind < argc) |
||
214 | { |
||
215 | optarg = argv[optind]; |
||
216 | optind++; |
||
217 | } |
||
218 | else |
||
219 | { |
||
220 | return _T('?'); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | return c; |
||
225 | } |