1 |
/* Getopt for Microsoft C |
2 |
This code is a modification of the Free Software Foundation, Inc. |
3 |
Getopt library for parsing command line argument the purpose was |
4 |
to provide a Microsoft Visual C friendly derivative. This code |
5 |
provides functionality for both Unicode and Multibyte builds. |
6 |
|
7 |
Date: 02/03/2011 - Ludvik Jerabek - Initial Release |
8 |
Version: 1.0 |
9 |
Comment: Supports getopt, getopt_long, and getopt_long_only |
10 |
and POSIXLY_CORRECT environment flag |
11 |
License: LGPL |
12 |
|
13 |
Revisions: |
14 |
|
15 |
02/03/2011 - Ludvik Jerabek - Initial Release |
16 |
02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4 |
17 |
07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs |
18 |
08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception |
19 |
08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB |
20 |
02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file |
21 |
08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi |
22 |
|
23 |
**DISCLAIMER** |
24 |
THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
25 |
EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE |
26 |
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
27 |
PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE |
28 |
EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT |
29 |
APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY |
30 |
DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY |
31 |
USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST |
32 |
PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON |
33 |
YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE |
34 |
EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
35 |
*/ |
36 |
#ifndef __GETOPT_H_ |
37 |
#define __GETOPT_H_ |
38 |
|
39 |
#ifdef _GETOPT_API |
40 |
#undef _GETOPT_API |
41 |
#endif |
42 |
|
43 |
#if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT) |
44 |
#error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually" |
45 |
#elif defined(STATIC_GETOPT) |
46 |
#pragma message("Warning static builds of getopt violate the Lesser GNU Public License") |
47 |
#define _GETOPT_API |
48 |
#elif defined(EXPORTS_GETOPT) |
49 |
#pragma message("Exporting getopt library") |
50 |
#define _GETOPT_API __declspec(dllexport) |
51 |
#else |
52 |
#pragma message("Importing getopt library") |
53 |
#define _GETOPT_API __declspec(dllimport) |
54 |
#endif |
55 |
|
56 |
|
57 |
#include <string.h> |
58 |
#include <wchar.h> |
59 |
|
60 |
// Standard GNU options |
61 |
#define null_argument 0 /*Argument Null*/ |
62 |
#define no_argument 0 /*Argument Switch Only*/ |
63 |
#define required_argument 1 /*Argument Required*/ |
64 |
#define optional_argument 2 /*Argument Optional*/ |
65 |
|
66 |
// Shorter Versions of options |
67 |
#define ARG_NULL 0 /*Argument Null*/ |
68 |
#define ARG_NONE 0 /*Argument Switch Only*/ |
69 |
#define ARG_REQ 1 /*Argument Required*/ |
70 |
#define ARG_OPT 2 /*Argument Optional*/ |
71 |
|
72 |
// Change behavior for C\C++ |
73 |
#ifdef __cplusplus |
74 |
#define _BEGIN_EXTERN_C extern "C" { |
75 |
#define _END_EXTERN_C } |
76 |
#define _GETOPT_THROW throw() |
77 |
#else |
78 |
#define _BEGIN_EXTERN_C |
79 |
#define _END_EXTERN_C |
80 |
#define _GETOPT_THROW |
81 |
#endif |
82 |
|
83 |
_BEGIN_EXTERN_C |
84 |
|
85 |
extern _GETOPT_API char *optarg_a; |
86 |
extern _GETOPT_API wchar_t *optarg_w; |
87 |
extern _GETOPT_API int optind; |
88 |
extern _GETOPT_API int opterr; |
89 |
extern _GETOPT_API int optopt; |
90 |
|
91 |
// Ansi |
92 |
struct option_a |
93 |
{ |
94 |
const char* name; |
95 |
int has_arg; |
96 |
int *flag; |
97 |
char val; |
98 |
}; |
99 |
|
100 |
// Unicode |
101 |
struct option_w |
102 |
{ |
103 |
const wchar_t* name; |
104 |
int has_arg; |
105 |
int *flag; |
106 |
wchar_t val; |
107 |
}; |
108 |
|
109 |
// Ansi |
110 |
extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW; |
111 |
extern _GETOPT_API int getopt_long_a(int ___argc, char *const *___argv, const char *__shortopts, const struct option_a *__longopts, int *__longind) _GETOPT_THROW; |
112 |
extern _GETOPT_API int getopt_long_only_a(int ___argc, char *const *___argv, const char *__shortopts, const struct option_a *__longopts, int *__longind) _GETOPT_THROW; |
113 |
|
114 |
// Unicode |
115 |
extern _GETOPT_API int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW; |
116 |
extern _GETOPT_API int getopt_long_w(int ___argc, wchar_t *const *___argv, const wchar_t *__shortopts, const struct option_w *__longopts, int *__longind) _GETOPT_THROW; |
117 |
extern _GETOPT_API int getopt_long_only_w(int ___argc, wchar_t *const *___argv, const wchar_t *__shortopts, const struct option_w *__longopts, int *__longind) _GETOPT_THROW; |
118 |
|
119 |
#ifdef _UNICODE |
120 |
#define getopt getopt_w |
121 |
#define getopt_long getopt_long_w |
122 |
#define getopt_long_only getopt_long_only_w |
123 |
#define option option_w |
124 |
#define optarg optarg_w |
125 |
#else |
126 |
#define getopt getopt_a |
127 |
#define getopt_long getopt_long_a |
128 |
#define getopt_long_only getopt_long_only_a |
129 |
#define option option_a |
130 |
#define optarg optarg_a |
131 |
#endif |
132 |
|
133 |
_END_EXTERN_C |
134 |
|
135 |
// Undefine so the macros are not included |
136 |
#undef _BEGIN_EXTERN_C |
137 |
#undef _END_EXTERN_C |
138 |
#undef _GETOPT_THROW |
139 |
#undef _GETOPT_API |
140 |
|
141 |
#endif // __GETOPT_H_ |