忘记了上次在哪里找到这个功能库,只有一个 .h 和 .c 文件,再次搜索的时候发现找不到了,结果只能在之前的代码中,两个文件提出使用,顾将这两个文件备份在这里。
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 25 **DISCLAIMER** 26 THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 27 EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE 28 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 29 PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE 30 EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT 31 APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY 32 DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY 33 USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST 34 PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON 35 YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE 36 EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 37 */ 38 #ifndef __GETOPT_H_ 39 #define __GETOPT_H_ 40 41 #ifdef _GETOPT_API 42 #undef _GETOPT_API 43 #endif 44 45 #if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT) 46 #error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually" 47 #elif defined(STATIC_GETOPT) 48 #pragma message("Warning static builds of getopt violate the Lesser GNU Public License") 49 #define _GETOPT_API 50 #elif defined(EXPORTS_GETOPT) 51 #pragma message("Exporting getopt library") 52 #define _GETOPT_API __declspec(dllexport) 53 #else 54 #pragma message("Importing getopt library") 55 #define _GETOPT_API __declspec(dllimport) 56 #endif 57 58 // Change behavior for CC++ 59 #ifdef __cplusplus 60 #define _BEGIN_EXTERN_C extern "C" { 61 #define _END_EXTERN_C } 62 #define _GETOPT_THROW throw() 63 #else 64 #define _BEGIN_EXTERN_C 65 #define _END_EXTERN_C 66 #define _GETOPT_THROW 67 #endif 68 69 // Standard GNU options 70 #define null_argument 0 /*Argument Null*/ 71 #define no_argument 0 /*Argument Switch Only*/ 72 #define required_argument 1 /*Argument Required*/ 73 #define optional_argument 2 /*Argument Optional*/ 74 75 // Shorter Options 76 #define ARG_NULL 0 /*Argument Null*/ 77 #define ARG_NONE 0 /*Argument Switch Only*/ 78 #define ARG_REQ 1 /*Argument Required*/ 79 #define ARG_OPT 2 /*Argument Optional*/ 80 81 #include <string.h> 82 #include <wchar.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 // Ansi 91 struct option_a 92 { 93 const char* name; 94 int has_arg; 95 int *flag; 96 int val; 97 }; 98 extern _GETOPT_API char *optarg_a; 99 extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW; 100 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; 101 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; 102 103 // Unicode 104 struct option_w 105 { 106 const wchar_t* name; 107 int has_arg; 108 int *flag; 109 int val; 110 }; 111 extern _GETOPT_API wchar_t *optarg_w; 112 extern _GETOPT_API int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW; 113 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; 114 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; 115 116 _END_EXTERN_C 117 118 #undef _BEGIN_EXTERN_C 119 #undef _END_EXTERN_C 120 #undef _GETOPT_THROW 121 #undef _GETOPT_API 122 123 #ifdef _UNICODE 124 #define getopt getopt_w 125 #define getopt_long getopt_long_w 126 #define getopt_long_only getopt_long_only_w 127 #define option option_w 128 #define optarg optarg_w 129 #else 130 #define getopt getopt_a 131 #define getopt_long getopt_long_a 132 #define getopt_long_only getopt_long_only_a 133 #define option option_a 134 #define optarg optarg_a 135 #endif 136 #endif // __GETOPT_H_
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 25 **DISCLAIMER** 26 THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 27 EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE 28 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 29 PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE 30 EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT 31 APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY 32 DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY 33 USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST 34 PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON 35 YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE 36 EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 37 */ 38 #define _CRT_SECURE_NO_WARNINGS 39 #include <stdlib.h> 40 #include <stdio.h> 41 #include <malloc.h> 42 #include "getopt.h" 43 44 #ifdef __cplusplus 45 #define _GETOPT_THROW throw() 46 #else 47 #define _GETOPT_THROW 48 #endif 49 50 int optind = 1; 51 int opterr = 1; 52 int optopt = '?'; 53 enum ENUM_ORDERING { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER }; 54 55 // 56 // 57 // Ansi structures and functions follow 58 // 59 // 60 61 static struct _getopt_data_a 62 { 63 int optind; 64 int opterr; 65 int optopt; 66 char *optarg; 67 int __initialized; 68 char *__nextchar; 69 enum ENUM_ORDERING __ordering; 70 int __posixly_correct; 71 int __first_nonopt; 72 int __last_nonopt; 73 } getopt_data_a; 74 char *optarg_a; 75 76 static void exchange_a(char **argv, struct _getopt_data_a *d) 77 { 78 int bottom = d->__first_nonopt; 79 int middle = d->__last_nonopt; 80 int top = d->optind; 81 char *tem; 82 while (top > middle && middle > bottom) 83 { 84 if (top - middle > middle - bottom) 85 { 86 int len = middle - bottom; 87 register int i; 88 for (i = 0; i < len; i++) 89 { 90 tem = argv[bottom + i]; 91 argv[bottom + i] = argv[top - (middle - bottom) + i]; 92 argv[top - (middle - bottom) + i] = tem; 93 } 94 top -= len; 95 } 96 else 97 { 98 int len = top - middle; 99 register int i; 100 for (i = 0; i < len; i++) 101 { 102 tem = argv[bottom + i]; 103 argv[bottom + i] = argv[middle + i]; 104 argv[middle + i] = tem; 105 } 106 bottom += len; 107 } 108 } 109 d->__first_nonopt += (d->optind - d->__last_nonopt); 110 d->__last_nonopt = d->optind; 111 } 112 static const char *_getopt_initialize_a (const char *optstring, struct _getopt_data_a *d, int posixly_correct) 113 { 114 d->__first_nonopt = d->__last_nonopt = d->optind; 115 d->__nextchar = NULL; 116 d->__posixly_correct = posixly_correct | !!getenv("POSIXLY_CORRECT"); 117 if (optstring[0] == '-') 118 { 119 d->__ordering = RETURN_IN_ORDER; 120 ++optstring; 121 } 122 else if (optstring[0] == '+') 123 { 124 d->__ordering = REQUIRE_ORDER; 125 ++optstring; 126 } 127 else if (d->__posixly_correct) 128 d->__ordering = REQUIRE_ORDER; 129 else 130 d->__ordering = PERMUTE; 131 return optstring; 132 } 133 int _getopt_internal_r_a (int argc, char *const *argv, const char *optstring, const struct option_a *longopts, int *longind, int long_only, struct _getopt_data_a *d, int posixly_correct) 134 { 135 int print_errors = d->opterr; 136 if (argc < 1) 137 return -1; 138 d->optarg = NULL; 139 if (d->optind == 0 || !d->__initialized) 140 { 141 if (d->optind == 0) 142 d->optind = 1; 143 optstring = _getopt_initialize_a (optstring, d, posixly_correct); 144 d->__initialized = 1; 145 } 146 else if (optstring[0] == '-' || optstring[0] == '+') 147 optstring++; 148 if (optstring[0] == ':') 149 print_errors = 0; 150 if (d->__nextchar == NULL || *d->__nextchar == '