/src/OS/Args.hpp

https://github.com/mobotics/XCSoar · C++ Header · 182 lines · 144 code · 16 blank · 22 comment · 4 complexity · c341ce29ab82814934267f240641d9aa MD5 · raw file

  1. /*
  2. Copyright_License {
  3. XCSoar Glide Computer - http://www.xcsoar.org/
  4. Copyright (C) 2000-2012 The XCSoar Project
  5. A detailed list of copyright holders can be found in the file "AUTHORS".
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. }
  18. */
  19. #ifndef ARGS_HPP
  20. #define ARGS_HPP
  21. #include "Compiler.h"
  22. #include "Util/tstring.hpp"
  23. #ifdef _UNICODE
  24. #include "OS/PathName.hpp"
  25. #endif
  26. #include <list>
  27. #include <algorithm>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <assert.h>
  31. #ifdef MORE_USAGE
  32. extern void PrintMoreUsage();
  33. #endif
  34. class Args {
  35. std::list<char *> args;
  36. const char *name, *usage;
  37. #ifdef WIN32
  38. char *cmdline;
  39. #endif
  40. public:
  41. Args(int argc, char **argv, const char *_usage)
  42. :name(argv[0]), usage(_usage) {
  43. assert(name != NULL);
  44. assert(usage != NULL);
  45. std::copy(argv + 1, argv + argc, std::back_inserter(args));
  46. #ifdef WIN32
  47. cmdline = NULL;
  48. #endif
  49. }
  50. Args(const Args &other) = delete;
  51. Args(Args &&other):name(other.name), usage(other.usage) {
  52. std::swap(args, other.args);
  53. #ifdef WIN32
  54. std::swap(cmdline, other.cmdline);
  55. #endif
  56. }
  57. #ifdef WIN32
  58. Args(const TCHAR *_cmdline, const char *_usage)
  59. :usage(_usage) {
  60. ParseCommandLine(_cmdline);
  61. }
  62. ~Args() {
  63. delete[] cmdline;
  64. }
  65. void ParseCommandLine(const char *_cmdline) {
  66. const char *s = _cmdline;
  67. cmdline = new char[strlen(s) + 1];
  68. char *d = cmdline; // current position in destination buffer
  69. char *option = cmdline;
  70. name = NULL;
  71. bool in_qoute = false;
  72. do {
  73. if (*s == '"')
  74. in_qoute = !in_qoute;
  75. else if (*s == '\0' || (!in_qoute && *s == ' ')) {
  76. // collapse runs of unqouted ' 's to a single '\0'
  77. if (d > cmdline && *(d-1) != '\0') {
  78. *d++ = '\0';
  79. // remember potential start position of next option
  80. option = d;
  81. }
  82. } else {
  83. *d = *s;
  84. if (option == d) {
  85. // first quoted blank or non blank character of new option
  86. #ifndef _WIN32_WCE
  87. // program name is not included in command line on CE
  88. if (name == NULL)
  89. name = option;
  90. else
  91. #endif
  92. args.push_back(option);
  93. }
  94. d++;
  95. }
  96. } while (*s++);
  97. if (name == NULL)
  98. name = "";
  99. }
  100. #ifdef _UNICODE
  101. void ParseCommandLine(const TCHAR *_cmdline) {
  102. NarrowPathName convert(_cmdline);
  103. ParseCommandLine(convert);
  104. }
  105. #endif
  106. #endif
  107. Args &operator=(const Args &other) = delete;
  108. gcc_noreturn
  109. void UsageError() {
  110. fprintf(stderr, "Usage: %s %s\n", name, usage);
  111. #ifdef MORE_USAGE
  112. PrintMoreUsage();
  113. #endif
  114. exit(EXIT_FAILURE);
  115. }
  116. bool IsEmpty() const {
  117. return args.empty();
  118. }
  119. const char *GetNext() {
  120. assert(!IsEmpty());
  121. const char *p = args.front();
  122. args.pop_front();
  123. return p;
  124. }
  125. const char *PeekNext() const {
  126. return IsEmpty() ? NULL : args.front();
  127. }
  128. const char *ExpectNext() {
  129. if (IsEmpty())
  130. UsageError();
  131. return GetNext();
  132. }
  133. tstring ExpectNextT() {
  134. const char *p = ExpectNext();
  135. assert(p != NULL);
  136. #ifdef _UNICODE
  137. PathName convert(p);
  138. return tstring(convert);
  139. #else
  140. return tstring(p);
  141. #endif
  142. }
  143. void ExpectEnd() {
  144. if (!IsEmpty())
  145. UsageError();
  146. }
  147. };
  148. #endif