getopt.h 4.8 KB

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