blob: 7cfef10a53ae829b42da9f1a3f9e6c0e2a100274 [file] [log] [blame]
Nicolas Jager95c526b2016-10-20 09:47:03 -04001/* Getopt for Microsoft C
2This code is a modification of the Free Software Foundation, Inc.
3Getopt library for parsing command line argument the purpose was
4to provide a Microsoft Visual C friendly derivative. This code
5provides functionality for both Unicode and Multibyte builds.
6
7Date: 02/03/2011 - Ludvik Jerabek - Initial Release
8Version: 1.0
9Comment: Supports getopt, getopt_long, and getopt_long_only
10and POSIXLY_CORRECT environment flag
11License: LGPL
12
13Revisions:
14
1502/03/2011 - Ludvik Jerabek - Initial Release
1602/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4
1707/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs
1808/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception
1908/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB
2002/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file
21
22**DISCLAIMER**
23THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
24EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE
27EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT
28APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY
29DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY
30USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST
31PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON
32YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE
33EXPRESSLY 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;
84extern _GETOPT_API int optind;
85extern _GETOPT_API int opterr;
86extern _GETOPT_API int optopt;
87
88struct option
89{
90 const TCHAR* name;
91 int has_arg;
92 int *flag;
93 TCHAR val;
94};
95
96extern _GETOPT_API int getopt(int argc, TCHAR *const *argv, const TCHAR *optstring) _GETOPT_THROW;
97extern _GETOPT_API int getopt_long(int ___argc, TCHAR *const *___argv, const TCHAR *__shortopts, const struct option *__longopts, int *__longind) _GETOPT_THROW;
98extern _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_