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