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 
22 **DISCLAIMER**
23 THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
24 EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26 PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE
27 EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT
28 APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY
29 DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY
30 USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST
31 PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON
32 YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE
33 EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 */
35 #ifndef __GETOPT_H_
36 #define __GETOPT_H_
37 
38 #ifdef _GETOPT_API
39 #undef _GETOPT_API
40 #endif
41 
42 #if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT)
43 #error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually"
44 #elif defined(STATIC_GETOPT)
45 #pragma message("Warning static builds of getopt violate the Lesser GNU Public License")
46 #define _GETOPT_API
47 #elif defined(EXPORTS_GETOPT)
48 #pragma message("Exporting getopt library")
49 #define _GETOPT_API __declspec(dllexport)
50 #else
51 #pragma message("Importing getopt library")
52 #define _GETOPT_API __declspec(dllimport)
53 #endif
54 
55 
56 #include <tchar.h>
57 
58 // Standard GNU options
59 #define	null_argument		0 /*Argument Null*/
60 #define	no_argument		0 /*Argument Switch Only*/
61 #define required_argument	1 /*Argument Required*/
62 #define optional_argument	2 /*Argument Optional*/
63 
64 // Shorter Versions of options
65 #define ARG_NULL 0 /*Argument Null*/
66 #define ARG_NONE 0 /*Argument Switch Only*/
67 #define ARG_REQ 1  /*Argument Required*/
68 #define ARG_OPT 2  /*Argument Optional*/
69 
70 // Change behavior for C\C++
71 #ifdef __cplusplus
72 #define _BEGIN_EXTERN_C extern "C" {
73 #define _END_EXTERN_C }
74 #define _GETOPT_THROW throw()
75 #else
76 #define _BEGIN_EXTERN_C
77 #define _END_EXTERN_C
78 #define _GETOPT_THROW
79 #endif
80 
81 _BEGIN_EXTERN_C
82 
83 	extern _GETOPT_API TCHAR *optarg;
84 extern _GETOPT_API int optind;
85 extern _GETOPT_API int opterr;
86 extern _GETOPT_API int optopt;
87 
88 struct option
89 {
90 	const TCHAR* name;
91 	int has_arg;
92 	int *flag;
93 	TCHAR val;
94 };
95 
96 extern _GETOPT_API int getopt(int argc, TCHAR *const *argv, const TCHAR *optstring) _GETOPT_THROW;
97 extern _GETOPT_API int getopt_long(int ___argc, TCHAR *const *___argv, const TCHAR *__shortopts, const struct option *__longopts, int *__longind) _GETOPT_THROW;
98 extern _GETOPT_API int getopt_long_only(int ___argc, TCHAR *const *___argv, const TCHAR *__shortopts, const struct option *__longopts, int *__longind) _GETOPT_THROW;
99 _END_EXTERN_C
100 
101 // Undefine so the macros are not included
102 #undef _BEGIN_EXTERN_C
103 #undef _END_EXTERN_C
104 #undef _GETOPT_THROW
105 #undef _GETOPT_API
106 
107 #endif  // __GETOPT_H_
108