Options.C 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: Options.C /main/1 1996/07/29 16:59:26 cde-hp $ */
  24. // Derived from comp.sources.unix/volume3/att_getopt.
  25. #ifndef Options_DEF_INCLUDED
  26. #define Options_DEF_INCLUDED 1
  27. #ifndef OPTION_CHAR
  28. #define OPTION_CHAR T('-')
  29. #endif
  30. #ifdef SP_NAMESPACE
  31. namespace SP_NAMESPACE {
  32. #endif
  33. template<class T>
  34. Options<T>::Options(int argc, T *const *argv, const T *opts)
  35. : argc_(argc), argv_(argv), opts_(opts), ind_(1), sp_(1)
  36. {
  37. }
  38. template<class T>
  39. const T *Options<T>::search(T c) const
  40. {
  41. for (const T *s = opts_; *s; s++)
  42. if (*s == c)
  43. return s;
  44. return 0;
  45. }
  46. template<class T>
  47. bool Options<T>::get(T &c)
  48. {
  49. const T *cp;
  50. if (sp_ == 1) {
  51. if (ind_ >= argc_)
  52. return false;
  53. if ((
  54. #ifdef OTHER_OPTION_CHAR
  55. argv_[ind_][0] != OTHER_OPTION_CHAR &&
  56. #endif
  57. argv_[ind_][0] != OPTION_CHAR) || argv_[ind_][1] == 0) {
  58. #ifdef REORDER_ARGS
  59. int i;
  60. for (i = ind_; i < argc_; i++)
  61. if ((
  62. #ifdef OTHER_OPTION_CHAR
  63. argv_[i][0] == OTHER_OPTION_CHAR ||
  64. #endif
  65. argv_[i][0] == OPTION_CHAR) && argv_[i][1] != 0)
  66. break;
  67. if (i < argc_) {
  68. c = argv_[i][1];
  69. if (c != T(':') && c != OPTION_CHAR
  70. && (cp = search(c)) != 0
  71. && cp[1] == T(':') && argv_[i][2] == 0 && i < argc_ - 1) {
  72. int j;
  73. T *temp1 = argv_[i];
  74. T *temp2 = argv_[i + 1];
  75. for (j = i - 1; j >= ind_; j--)
  76. argv_[j+2] = argv_[j];
  77. argv_[ind_] = temp1;
  78. argv_[ind_ + 1] = temp2;
  79. }
  80. else {
  81. int j;
  82. T *temp = argv_[i];
  83. for (j = i - 1; j >= ind_; j--)
  84. argv_[j+1] = argv_[j];
  85. argv_[ind_] = temp;
  86. }
  87. }
  88. else
  89. #endif
  90. return false;
  91. }
  92. if ((argv_[ind_][0] == OPTION_CHAR && argv_[ind_][1] == OPTION_CHAR
  93. && argv_[ind_][2] == 0)
  94. #ifdef OTHER_OPTION_CHAR
  95. || (argv_[ind_][0] == OTHER_OPTION_CHAR
  96. && argv_[ind_][1] == OTHER_OPTION_CHAR
  97. && argv_[ind_][2] == 0)
  98. #endif
  99. ) {
  100. ind_++;
  101. return false;
  102. }
  103. }
  104. opt_ = c = argv_[ind_][sp_];
  105. if (c == T(':') || (cp = search(c)) == 0) {
  106. if (argv_[ind_][++sp_] == 0) {
  107. ind_++;
  108. sp_ = 1;
  109. }
  110. c = T('?');
  111. return true;
  112. }
  113. if (*++cp == T(':')) {
  114. if (argv_[ind_][sp_ + 1] != 0)
  115. arg_ = &argv_[ind_++][sp_ + 1];
  116. else if (++ind_ >= argc_) {
  117. sp_ = 1;
  118. c = (*opts_ == T(':') ? T(':') : T('?'));
  119. return true;
  120. }
  121. else
  122. arg_ = argv_[ind_++];
  123. sp_ = 1;
  124. }
  125. else {
  126. if (argv_[ind_][++sp_] == 0) {
  127. sp_ = 1;
  128. ind_++;
  129. }
  130. arg_ = 0;
  131. }
  132. return true;
  133. }
  134. #ifdef SP_NAMESPACE
  135. }
  136. #endif
  137. #endif /* not Options_DEF_INCLUDED */