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.

315 lines
8.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2006, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifdef WIN32
  31. #include "talk/base/win32.h"
  32. #include <shellapi.h>
  33. #endif
  34. #include "talk/base/flags.h"
  35. // -----------------------------------------------------------------------------
  36. // Implementation of Flag
  37. Flag::Flag(const char* file, const char* name, const char* comment,
  38. Type type, void* variable, FlagValue default__)
  39. : file_(file),
  40. name_(name),
  41. comment_(comment),
  42. type_(type),
  43. variable_(reinterpret_cast<FlagValue*>(variable)),
  44. default_(default__) {
  45. FlagList::Register(this);
  46. }
  47. void Flag::SetToDefault() {
  48. // Note that we cannot simply do '*variable_ = default_;' since
  49. // flag variables are not really of type FlagValue and thus may
  50. // be smaller! The FlagValue union is simply 'overlayed' on top
  51. // of a flag variable for convenient access. Since union members
  52. // are guarantee to be aligned at the beginning, this works.
  53. switch (type_) {
  54. case Flag::BOOL:
  55. variable_->b = default_.b;
  56. return;
  57. case Flag::INT:
  58. variable_->i = default_.i;
  59. return;
  60. case Flag::FLOAT:
  61. variable_->f = default_.f;
  62. return;
  63. case Flag::STRING:
  64. variable_->s = default_.s;
  65. return;
  66. }
  67. UNREACHABLE();
  68. }
  69. static const char* Type2String(Flag::Type type) {
  70. switch (type) {
  71. case Flag::BOOL: return "bool";
  72. case Flag::INT: return "int";
  73. case Flag::FLOAT: return "float";
  74. case Flag::STRING: return "string";
  75. }
  76. UNREACHABLE();
  77. return NULL;
  78. }
  79. static void PrintFlagValue(Flag::Type type, FlagValue* p) {
  80. switch (type) {
  81. case Flag::BOOL:
  82. printf("%s", (p->b ? "true" : "false"));
  83. return;
  84. case Flag::INT:
  85. printf("%d", p->i);
  86. return;
  87. case Flag::FLOAT:
  88. printf("%f", p->f);
  89. return;
  90. case Flag::STRING:
  91. printf("%s", p->s);
  92. return;
  93. }
  94. UNREACHABLE();
  95. }
  96. void Flag::Print(bool print_current_value) {
  97. printf(" --%s (%s) type: %s default: ", name_, comment_,
  98. Type2String(type_));
  99. PrintFlagValue(type_, &default_);
  100. if (print_current_value) {
  101. printf(" current value: ");
  102. PrintFlagValue(type_, variable_);
  103. }
  104. printf("\n");
  105. }
  106. // -----------------------------------------------------------------------------
  107. // Implementation of FlagList
  108. Flag* FlagList::list_ = NULL;
  109. FlagList::FlagList() {
  110. list_ = NULL;
  111. }
  112. void FlagList::Print(const char* file, bool print_current_value) {
  113. // Since flag registration is likely by file (= C++ file),
  114. // we don't need to sort by file and still get grouped output.
  115. const char* current = NULL;
  116. for (Flag* f = list_; f != NULL; f = f->next()) {
  117. if (file == NULL || file == f->file()) {
  118. if (current != f->file()) {
  119. printf("Flags from %s:\n", f->file());
  120. current = f->file();
  121. }
  122. f->Print(print_current_value);
  123. }
  124. }
  125. }
  126. Flag* FlagList::Lookup(const char* name) {
  127. Flag* f = list_;
  128. while (f != NULL && strcmp(name, f->name()) != 0)
  129. f = f->next();
  130. return f;
  131. }
  132. void FlagList::SplitArgument(const char* arg,
  133. char* buffer, int buffer_size,
  134. const char** name, const char** value,
  135. bool* is_bool) {
  136. *name = NULL;
  137. *value = NULL;
  138. *is_bool = false;
  139. if (*arg == '-') {
  140. // find the begin of the flag name
  141. arg++; // remove 1st '-'
  142. if (*arg == '-')
  143. arg++; // remove 2nd '-'
  144. if (arg[0] == 'n' && arg[1] == 'o') {
  145. arg += 2; // remove "no"
  146. *is_bool = true;
  147. }
  148. *name = arg;
  149. // find the end of the flag name
  150. while (*arg != '\0' && *arg != '=')
  151. arg++;
  152. // get the value if any
  153. if (*arg == '=') {
  154. // make a copy so we can NUL-terminate flag name
  155. int n = static_cast<int>(arg - *name);
  156. if (n >= buffer_size)
  157. Fatal(__FILE__, __LINE__, "CHECK(%s) failed", "n < buffer_size");
  158. memcpy(buffer, *name, n * sizeof(char));
  159. buffer[n] = '\0';
  160. *name = buffer;
  161. // get the value
  162. *value = arg + 1;
  163. }
  164. }
  165. }
  166. int FlagList::SetFlagsFromCommandLine(int* argc, const char** argv,
  167. bool remove_flags) {
  168. // parse arguments
  169. for (int i = 1; i < *argc; /* see below */) {
  170. int j = i; // j > 0
  171. const char* arg = argv[i++];
  172. // split arg into flag components
  173. char buffer[1024];
  174. const char* name;
  175. const char* value;
  176. bool is_bool;
  177. SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
  178. if (name != NULL) {
  179. // lookup the flag
  180. Flag* flag = Lookup(name);
  181. if (flag == NULL) {
  182. fprintf(stderr, "Error: unrecognized flag %s\n", arg);
  183. return j;
  184. }
  185. // if we still need a flag value, use the next argument if available
  186. if (flag->type() != Flag::BOOL && value == NULL) {
  187. if (i < *argc) {
  188. value = argv[i++];
  189. } else {
  190. fprintf(stderr, "Error: missing value for flag %s of type %s\n",
  191. arg, Type2String(flag->type()));
  192. return j;
  193. }
  194. }
  195. // set the flag
  196. char empty[] = { '\0' };
  197. char* endp = empty;
  198. switch (flag->type()) {
  199. case Flag::BOOL:
  200. *flag->bool_variable() = !is_bool;
  201. break;
  202. case Flag::INT:
  203. *flag->int_variable() = strtol(value, &endp, 10);
  204. break;
  205. case Flag::FLOAT:
  206. *flag->float_variable() = strtod(value, &endp);
  207. break;
  208. case Flag::STRING:
  209. *flag->string_variable() = value;
  210. break;
  211. }
  212. // handle errors
  213. if ((flag->type() == Flag::BOOL && value != NULL) ||
  214. (flag->type() != Flag::BOOL && is_bool) ||
  215. *endp != '\0') {
  216. fprintf(stderr, "Error: illegal value for flag %s of type %s\n",
  217. arg, Type2String(flag->type()));
  218. return j;
  219. }
  220. // remove the flag & value from the command
  221. if (remove_flags)
  222. while (j < i)
  223. argv[j++] = NULL;
  224. }
  225. }
  226. // shrink the argument list
  227. if (remove_flags) {
  228. int j = 1;
  229. for (int i = 1; i < *argc; i++) {
  230. if (argv[i] != NULL)
  231. argv[j++] = argv[i];
  232. }
  233. *argc = j;
  234. }
  235. // parsed all flags successfully
  236. return 0;
  237. }
  238. void FlagList::Register(Flag* flag) {
  239. assert(flag != NULL && strlen(flag->name()) > 0);
  240. if (Lookup(flag->name()) != NULL)
  241. Fatal(flag->file(), 0, "flag %s declared twice", flag->name());
  242. flag->next_ = list_;
  243. list_ = flag;
  244. }
  245. #ifdef WIN32
  246. WindowsCommandLineArguments::WindowsCommandLineArguments() {
  247. // start by getting the command line.
  248. LPTSTR command_line = ::GetCommandLine();
  249. // now, convert it to a list of wide char strings.
  250. LPWSTR *wide_argv = ::CommandLineToArgvW(command_line, &argc_);
  251. // now allocate an array big enough to hold that many string pointers.
  252. argv_ = new char*[argc_];
  253. // iterate over the returned wide strings;
  254. for(int i = 0; i < argc_; ++i) {
  255. std::string s = talk_base::ToUtf8(wide_argv[i], wcslen(wide_argv[i]));
  256. char *buffer = new char[s.length() + 1];
  257. talk_base::strcpyn(buffer, s.length() + 1, s.c_str());
  258. // make sure the argv array has the right string at this point.
  259. argv_[i] = buffer;
  260. }
  261. LocalFree(wide_argv);
  262. }
  263. WindowsCommandLineArguments::~WindowsCommandLineArguments() {
  264. // need to free each string in the array, and then the array.
  265. for(int i = 0; i < argc_; i++) {
  266. delete[] argv_[i];
  267. }
  268. delete[] argv_;
  269. }
  270. #endif // WIN32