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 |
10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features |
23 |
|
24 |
**DISCLAIMER** |
25 |
THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
26 |
EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE |
27 |
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
28 |
PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE |
29 |
EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT |
30 |
APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY |
31 |
DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY |
32 |
USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST |
33 |
PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON |
34 |
YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE |
35 |
EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
36 |
*/ |
37 |
#ifndef __GETOPT_H_ |
38 |
#define __GETOPT_H_ |
39 |
|
40 |
#ifdef _GETOPT_API |
41 |
#undef _GETOPT_API |
42 |
#endif |
43 |
|
44 |
#if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT) |
45 |
#error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually" |
46 |
#elif defined(STATIC_GETOPT) |
47 |
#pragma message("Warning static builds of getopt violate the Lesser GNU Public License") |
48 |
#define _GETOPT_API |
49 |
#elif defined(EXPORTS_GETOPT) |
50 |
#pragma message("Exporting getopt library") |
51 |
#define _GETOPT_API __declspec(dllexport) |
52 |
#else |
53 |
#pragma message("Importing getopt library") |
54 |
#define _GETOPT_API __declspec(dllimport) |
55 |
#endif |
56 |
|
57 |
// Change behavior for C\C++ |
58 |
#ifdef __cplusplus |
59 |
#define _BEGIN_EXTERN_C extern "C" { |
60 |
#define _END_EXTERN_C } |
61 |
#define _GETOPT_THROW throw() |
62 |
#else |
63 |
#define _BEGIN_EXTERN_C |
64 |
#define _END_EXTERN_C |
65 |
#define _GETOPT_THROW |
66 |
#endif |
67 |
|
68 |
// Standard GNU options |
69 |
#define null_argument 0 /*Argument Null*/ |
70 |
#define no_argument 0 /*Argument Switch Only*/ |
71 |
#define required_argument 1 /*Argument Required*/ |
72 |
#define optional_argument 2 /*Argument Optional*/ |
73 |
|
74 |
// Shorter Options |
75 |
#define ARG_NULL 0 /*Argument Null*/ |
76 |
#define ARG_NONE 0 /*Argument Switch Only*/ |
77 |
#define ARG_REQ 1 /*Argument Required*/ |
78 |
#define ARG_OPT 2 /*Argument Optional*/ |
79 |
|
80 |
#include <string.h> |
81 |
#include <wchar.h> |
82 |
|
83 |
_BEGIN_EXTERN_C |
84 |
|
85 |
extern _GETOPT_API int optind; |
86 |
extern _GETOPT_API int opterr; |
87 |
extern _GETOPT_API int optopt; |
88 |
|
89 |
// Ansi |
90 |
struct option_a |
91 |
{ |
92 |
const char* name; |
93 |
int has_arg; |
94 |
int *flag; |
95 |
char val; |
96 |
}; |
97 |
extern _GETOPT_API char *optarg_a; |
98 |
extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW; |
99 |
extern _GETOPT_API int getopt_long_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW; |
100 |
extern _GETOPT_API int getopt_long_only_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW; |
101 |
|
102 |
// Unicode |
103 |
struct option_w |
104 |
{ |
105 |
const wchar_t* name; |
106 |
int has_arg; |
107 |
int *flag; |
108 |
wchar_t val; |
109 |
}; |
110 |
extern _GETOPT_API wchar_t *optarg_w; |
111 |
extern _GETOPT_API int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW; |
112 |
extern _GETOPT_API int getopt_long_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW; |
113 |
extern _GETOPT_API int getopt_long_only_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW; |
114 |
|
115 |
_END_EXTERN_C |
116 |
|
117 |
#undef _BEGIN_EXTERN_C |
118 |
#undef _END_EXTERN_C |
119 |
#undef _GETOPT_THROW |
120 |
#undef _GETOPT_API |
121 |
|
122 |
#ifdef _UNICODE |
123 |
#define getopt getopt_w |
124 |
#define getopt_long getopt_long_w |
125 |
#define getopt_long_only getopt_long_only_w |
126 |
#define option option_w |
127 |
#define optarg optarg_w |
128 |
#else |
129 |
#define getopt getopt_a |
130 |
#define getopt_long getopt_long_a |
131 |
#define getopt_long_only getopt_long_only_a |
132 |
#define option option_a |
133 |
#define optarg optarg_a |
134 |
#endif |
135 |
#endif // __GETOPT_H_ |