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