Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
5.9 KiB

  1. /******************************************************************************
  2. * getopt.cpp *
  3. *------------*
  4. * Based on the program by Henry Spencer posted to Usenet net.sources list
  5. * This is the equivalent to the program options parsing utility
  6. * found in most UNIX compilers.
  7. *------------------------------------------------------------------------------
  8. * Copyright (C) 1997 Entropic Research Laboratory, Inc.
  9. * Copyright (C) 1998 Entropic, Inc.
  10. * Copyright (C) 2000 Microsoft Corporation Date: 03/21/00
  11. * All Rights Reserved
  12. *
  13. ********************************************************************* PACOG ***/
  14. #include "getopt.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17. /*****************************************************************************
  18. * CGetOpt::CGetOpt *
  19. *------------------*
  20. * Description:
  21. *
  22. ******************************************************************* PACOG ***/
  23. CGetOpt::CGetOpt(bool fReportError)
  24. {
  25. m_argc = 0;
  26. m_argv = 0;
  27. m_optstring = 0;
  28. m_scan = 0;
  29. m_optind = 0;
  30. m_optarg = 0;
  31. m_opterr = fReportError;
  32. }
  33. /*****************************************************************************
  34. * CGetOpt::Init *
  35. *---------------*
  36. * Description:
  37. *
  38. ******************************************************************* PACOG ***/
  39. void CGetOpt::Init (int argc, char *argv[], char* optstring)
  40. {
  41. m_argc = argc;
  42. m_argv = argv;
  43. m_optstring = optstring;
  44. m_scan = 0;
  45. m_optind = 0;
  46. m_optarg = 0;
  47. }
  48. /*****************************************************************************
  49. * CGetOpt::NextOption *
  50. *---------------------*
  51. * Description:
  52. *
  53. ******************************************************************* PACOG ***/
  54. int CGetOpt::NextOption()
  55. {
  56. char c;
  57. char *place;
  58. m_optarg = 0;
  59. if (m_scan == 0|| *m_scan == '\0')
  60. {
  61. if (m_optind == 0)
  62. {
  63. m_optind++;
  64. }
  65. if (m_optind >= m_argc || m_argv[m_optind][0] != '-' || m_argv[m_optind][1] == '\0')
  66. {
  67. return(EOF);
  68. }
  69. if (strcmp(m_argv[m_optind], "--") == 0)
  70. {
  71. m_optind++;
  72. return(EOF);
  73. }
  74. m_scan = m_argv[m_optind]+1;
  75. m_optind++;
  76. }
  77. c = *m_scan++;
  78. place = strchr(m_optstring, c);
  79. if (place == 0 || c == ':')
  80. {
  81. if (m_opterr)
  82. {
  83. fprintf(stderr, "%s: unknown option -%c\n", m_argv[0], c);
  84. }
  85. return('?');
  86. }
  87. place++;
  88. if (*place == ':')
  89. {
  90. if (*m_scan != '\0')
  91. {
  92. m_optarg = m_scan;
  93. m_scan = 0;
  94. }
  95. else
  96. {
  97. m_optarg = m_argv[m_optind];
  98. m_optind++;
  99. }
  100. }
  101. return c;
  102. }
  103. /*****************************************************************************
  104. * CGetOpt::OptArg *
  105. *-----------------*
  106. * Description:
  107. *
  108. ******************************************************************* PACOG ***/
  109. char* CGetOpt::OptArg ()
  110. {
  111. return m_optarg;
  112. }
  113. /*****************************************************************************
  114. * CGetOpt::OptInd *
  115. *-----------------*
  116. * Description:
  117. *
  118. ******************************************************************* PACOG ***/
  119. int CGetOpt::OptInd ()
  120. {
  121. return m_optind;
  122. }
  123. /******
  124. *
  125. * WIDE CHAR VERSIONS
  126. *
  127. ******/
  128. /*****************************************************************************
  129. * CWGetOpt::CWGetOpt *
  130. *--------------------*
  131. * Description:
  132. *
  133. ******************************************************************* PACOG ***/
  134. CWGetOpt::CWGetOpt(bool fReportError)
  135. {
  136. m_argc = 0;
  137. m_argv = 0;
  138. m_optstring = 0;
  139. m_scan = 0;
  140. m_optind = 0;
  141. m_optarg = 0;
  142. m_opterr = fReportError;
  143. }
  144. /*****************************************************************************
  145. * CWGetOpt::Init *
  146. *---------------*
  147. * Description:
  148. *
  149. ******************************************************************* PACOG ***/
  150. void CWGetOpt::Init (int argc, WCHAR *argv[], WCHAR* optstring)
  151. {
  152. m_argc = argc;
  153. m_argv = argv;
  154. m_optstring = optstring;
  155. m_scan = 0;
  156. m_optind = 0;
  157. m_optarg = 0;
  158. }
  159. /*****************************************************************************
  160. * CWGetOpt::NextOption *
  161. *---------------------*
  162. * Description:
  163. *
  164. ******************************************************************* PACOG ***/
  165. int CWGetOpt::NextOption()
  166. {
  167. WCHAR c;
  168. WCHAR *place;
  169. m_optarg = 0;
  170. if (m_scan == 0|| *m_scan == L'\0')
  171. {
  172. if (m_optind == 0)
  173. {
  174. m_optind++;
  175. }
  176. if (m_optind >= m_argc || m_argv[m_optind][0] != L'-' || m_argv[m_optind][1] == L'\0')
  177. {
  178. return(EOF);
  179. }
  180. if (wcscmp(m_argv[m_optind], L"--") == 0)
  181. {
  182. m_optind++;
  183. return(EOF);
  184. }
  185. m_scan = m_argv[m_optind]+1;
  186. m_optind++;
  187. }
  188. c = *m_scan++;
  189. place = wcschr(m_optstring, c);
  190. if (place == 0 || c == L':')
  191. {
  192. if (m_opterr)
  193. {
  194. fprintf(stderr, "%s: unknown option -%c\n", m_argv[0], c);
  195. }
  196. return(L'?');
  197. }
  198. place++;
  199. if (*place == L':')
  200. {
  201. if (*m_scan != L'\0')
  202. {
  203. m_optarg = m_scan;
  204. m_scan = 0;
  205. }
  206. else
  207. {
  208. m_optarg = m_argv[m_optind];
  209. m_optind++;
  210. }
  211. }
  212. return c;
  213. }
  214. /*****************************************************************************
  215. * CWGetOpt::OptArg *
  216. *-----------------*
  217. * Description:
  218. *
  219. ******************************************************************* PACOG ***/
  220. WCHAR* CWGetOpt::OptArg ()
  221. {
  222. return m_optarg;
  223. }
  224. /*****************************************************************************
  225. * CWGetOpt::OptInd *
  226. *-----------------*
  227. * Description:
  228. *
  229. ******************************************************************* PACOG ***/
  230. int CWGetOpt::OptInd ()
  231. {
  232. return m_optind;
  233. }