getopt.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. Date: 02/03/2011 - Ludvik Jerabek - Initial Release
  7. Version: 1.0
  8. Comment: Supports getopt, getopt_long, and getopt_long_only
  9. and POSIXLY_CORRECT environment flag
  10. License: LGPL
  11. Revisions:
  12. 02/03/2011 - Ludvik Jerabek - Initial Release
  13. 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4
  14. 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs
  15. 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception
  16. 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB
  17. 02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file
  18. 08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi
  19. 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features
  20. 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable
  21. **DISCLAIMER**
  22. THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  23. EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE
  24. IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  25. PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE
  26. EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT
  27. APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY
  28. DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY
  29. USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST
  30. PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON
  31. YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE
  32. EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  33. */
  34. #ifndef __GETOPT_H_
  35. #define __GETOPT_H_
  36. #ifdef _GETOPT_API
  37. #undef _GETOPT_API
  38. #endif
  39. #if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT)
  40. #error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually"
  41. #elif defined(STATIC_GETOPT)
  42. #pragma message("Warning static builds of getopt violate the Lesser GNU Public License")
  43. #define _GETOPT_API
  44. #elif defined(EXPORTS_GETOPT)
  45. #pragma message("Exporting getopt library")
  46. #define _GETOPT_API __declspec(dllexport)
  47. #else
  48. #pragma message("Importing getopt library")
  49. #define _GETOPT_API __declspec(dllimport)
  50. #endif
  51. // Change behavior for C\C++
  52. #ifdef __cplusplus
  53. #define _BEGIN_EXTERN_C extern "C" {
  54. #define _END_EXTERN_C }
  55. #define _GETOPT_THROW throw()
  56. #else
  57. #define _BEGIN_EXTERN_C
  58. #define _END_EXTERN_C
  59. #define _GETOPT_THROW
  60. #endif
  61. // Standard GNU options
  62. #define null_argument 0
  63. #define no_argument 0
  64. #define required_argument 1
  65. #define optional_argument 2
  66. // Shorter Options
  67. #define ARG_NULL 0
  68. #define ARG_NONE 0
  69. #define ARG_REQ 1
  70. #define ARG_OPT 2
  71. #include <string.h>
  72. #include <wchar.h>
  73. _BEGIN_EXTERN_C
  74. extern _GETOPT_API int optind;
  75. extern _GETOPT_API int opterr;
  76. extern _GETOPT_API int optopt;
  77. // Ansi
  78. struct option_a
  79. {
  80. const char* name;
  81. int has_arg;
  82. int *flag;
  83. int val;
  84. };
  85. extern _GETOPT_API char *optarg_a;
  86. extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW;
  87. 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;
  88. 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;
  89. // Unicode
  90. struct option_w
  91. {
  92. const wchar_t* name;
  93. int has_arg;
  94. int *flag;
  95. int val;
  96. };
  97. extern _GETOPT_API wchar_t *optarg_w;
  98. extern _GETOPT_API int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW;
  99. 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;
  100. 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;
  101. _END_EXTERN_C
  102. #undef _BEGIN_EXTERN_C
  103. #undef _END_EXTERN_C
  104. #undef _GETOPT_THROW
  105. #undef _GETOPT_API
  106. #ifdef _UNICODE
  107. #define getopt getopt_w
  108. #define getopt_long getopt_long_w
  109. #define getopt_long_only getopt_long_only_w
  110. #define option option_w
  111. #define optarg optarg_w
  112. #else
  113. #define getopt getopt_a
  114. #define getopt_long getopt_long_a
  115. #define getopt_long_only getopt_long_only_a
  116. #define option option_a
  117. #define optarg optarg_a
  118. #endif
  119. #endif // __GETOPT_H_