crn_command_line_params.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // File: crn_command_line_params.cpp
  2. // See Copyright Notice and license at the end of inc/crnlib.h
  3. #include "crn_core.h"
  4. #include "crn_command_line_params.h"
  5. #include "crn_console.h"
  6. #include "crn_cfile_stream.h"
  7. #ifdef WIN32
  8. #define CRNLIB_CMD_LINE_ALLOW_SLASH_PARAMS 1
  9. #endif
  10. #if CRNLIB_USE_WIN32_API
  11. #include "crn_winhdr.h"
  12. #endif
  13. namespace crnlib
  14. {
  15. void get_command_line_as_single_string(dynamic_string& cmd_line, int argc, char *argv[])
  16. {
  17. argc, argv;
  18. #if CRNLIB_USE_WIN32_API
  19. cmd_line.set(GetCommandLineA());
  20. #else
  21. cmd_line.clear();
  22. for (int i = 0; i < argc; i++)
  23. {
  24. dynamic_string tmp(argv[i]);
  25. if ((tmp.front() != '"') && (tmp.front() != '-') && (tmp.front() != '@'))
  26. tmp = "\"" + tmp + "\"";
  27. if (cmd_line.get_len())
  28. cmd_line += " ";
  29. cmd_line += tmp;
  30. }
  31. #endif
  32. }
  33. command_line_params::command_line_params()
  34. {
  35. }
  36. void command_line_params::clear()
  37. {
  38. m_params.clear();
  39. m_param_map.clear();
  40. }
  41. bool command_line_params::split_params(const char* p, dynamic_string_array& params)
  42. {
  43. bool within_param = false;
  44. bool within_quote = false;
  45. uint ofs = 0;
  46. dynamic_string str;
  47. while (p[ofs])
  48. {
  49. const char c = p[ofs];
  50. if (within_param)
  51. {
  52. if (within_quote)
  53. {
  54. if (c == '"')
  55. within_quote = false;
  56. str.append_char(c);
  57. }
  58. else if ((c == ' ') || (c == '\t'))
  59. {
  60. if (!str.is_empty())
  61. {
  62. params.push_back(str);
  63. str.clear();
  64. }
  65. within_param = false;
  66. }
  67. else
  68. {
  69. if (c == '"')
  70. within_quote = true;
  71. str.append_char(c);
  72. }
  73. }
  74. else if ((c != ' ') && (c != '\t'))
  75. {
  76. within_param = true;
  77. if (c == '"')
  78. within_quote = true;
  79. str.append_char(c);
  80. }
  81. ofs++;
  82. }
  83. if (within_quote)
  84. {
  85. console::error("Unmatched quote in command line \"%s\"", p);
  86. return false;
  87. }
  88. if (!str.is_empty())
  89. params.push_back(str);
  90. return true;
  91. }
  92. bool command_line_params::load_string_file(const char* pFilename, dynamic_string_array& strings)
  93. {
  94. cfile_stream in_stream;
  95. if (!in_stream.open(pFilename, cDataStreamReadable | cDataStreamSeekable))
  96. {
  97. console::error("Unable to open file \"%s\" for reading!", pFilename);
  98. return false;
  99. }
  100. dynamic_string ansi_str;
  101. for ( ; ; )
  102. {
  103. if (!in_stream.read_line(ansi_str))
  104. break;
  105. ansi_str.trim();
  106. if (ansi_str.is_empty())
  107. continue;
  108. strings.push_back(dynamic_string(ansi_str.get_ptr()));
  109. }
  110. return true;
  111. }
  112. bool command_line_params::parse(const dynamic_string_array& params, uint n, const param_desc* pParam_desc)
  113. {
  114. CRNLIB_ASSERT(n && pParam_desc);
  115. m_params = params;
  116. uint arg_index = 0;
  117. while (arg_index < params.size())
  118. {
  119. const uint cur_arg_index = arg_index;
  120. const dynamic_string& src_param = params[arg_index++];
  121. if (src_param.is_empty())
  122. continue;
  123. #if CRNLIB_CMD_LINE_ALLOW_SLASH_PARAMS
  124. if ((src_param[0] == '/') || (src_param[0] == '-'))
  125. #else
  126. if (src_param[0] == '-')
  127. #endif
  128. {
  129. if (src_param.get_len() < 2)
  130. {
  131. console::error("Invalid command line parameter: \"%s\"", src_param.get_ptr());
  132. return false;
  133. }
  134. dynamic_string key_str(src_param);
  135. key_str.right(1);
  136. int modifier = 0;
  137. char c = key_str[key_str.get_len() - 1];
  138. if (c == '+')
  139. modifier = 1;
  140. else if (c == '-')
  141. modifier = -1;
  142. if (modifier)
  143. key_str.left(key_str.get_len() - 1);
  144. uint param_index;
  145. for (param_index = 0; param_index < n; param_index++)
  146. if (key_str == pParam_desc[param_index].m_pName)
  147. break;
  148. if (param_index == n)
  149. {
  150. console::error("Unrecognized command line parameter: \"%s\"", src_param.get_ptr());
  151. return false;
  152. }
  153. const param_desc& desc = pParam_desc[param_index];
  154. const uint cMaxValues = 16;
  155. dynamic_string val_str[cMaxValues];
  156. uint num_val_strs = 0;
  157. if (desc.m_num_values)
  158. {
  159. CRNLIB_ASSERT(desc.m_num_values <= cMaxValues);
  160. if ((arg_index + desc.m_num_values) > params.size())
  161. {
  162. console::error("Expected %u value(s) after command line parameter: \"%s\"", desc.m_num_values, src_param.get_ptr());
  163. return false;
  164. }
  165. for (uint v = 0; v < desc.m_num_values; v++)
  166. val_str[num_val_strs++] = params[arg_index++];
  167. }
  168. dynamic_string_array strings;
  169. if ((desc.m_support_listing_file) && (val_str[0].get_len() >= 2) && (val_str[0][0] == '@'))
  170. {
  171. dynamic_string filename(val_str[0]);
  172. filename.right(1);
  173. filename.unquote();
  174. if (!load_string_file(filename.get_ptr(), strings))
  175. {
  176. console::error("Failed loading listing file \"%s\"!", filename.get_ptr());
  177. return false;
  178. }
  179. }
  180. else
  181. {
  182. for (uint v = 0; v < num_val_strs; v++)
  183. {
  184. val_str[v].unquote();
  185. strings.push_back(val_str[v]);
  186. }
  187. }
  188. param_value pv;
  189. pv.m_values.swap(strings);
  190. pv.m_index = cur_arg_index;
  191. pv.m_modifier = (int8)modifier;
  192. m_param_map.insert(std::make_pair(key_str, pv));
  193. }
  194. else
  195. {
  196. param_value pv;
  197. pv.m_values.push_back(src_param);
  198. pv.m_values.back().unquote();
  199. pv.m_index = cur_arg_index;
  200. m_param_map.insert(std::make_pair(g_empty_dynamic_string, pv));
  201. }
  202. }
  203. return true;
  204. }
  205. bool command_line_params::parse(const char* pCmd_line, uint n, const param_desc* pParam_desc, bool skip_first_param)
  206. {
  207. CRNLIB_ASSERT(n && pParam_desc);
  208. dynamic_string_array p;
  209. if (!split_params(pCmd_line, p))
  210. return 0;
  211. if (p.empty())
  212. return 0;
  213. if (skip_first_param)
  214. p.erase(0U);
  215. return parse(p, n, pParam_desc);
  216. }
  217. bool command_line_params::is_param(uint index) const
  218. {
  219. CRNLIB_ASSERT(index < m_params.size());
  220. if (index >= m_params.size())
  221. return false;
  222. const dynamic_string& w = m_params[index];
  223. if (w.is_empty())
  224. return false;
  225. #if CRNLIB_CMD_LINE_ALLOW_SLASH_PARAMS
  226. return (w.get_len() >= 2) && ((w[0] == '-') || (w[0] == '/'));
  227. #else
  228. return (w.get_len() >= 2) && (w[0] == '-');
  229. #endif
  230. }
  231. uint command_line_params::find(uint num_keys, const char** ppKeys, crnlib::vector<param_map_const_iterator>* pIterators, crnlib::vector<uint>* pUnmatched_indices) const
  232. {
  233. CRNLIB_ASSERT(ppKeys);
  234. if (pUnmatched_indices)
  235. {
  236. pUnmatched_indices->resize(m_params.size());
  237. for (uint i = 0; i < m_params.size(); i++)
  238. (*pUnmatched_indices)[i] = i;
  239. }
  240. uint n = 0;
  241. for (uint i = 0; i < num_keys; i++)
  242. {
  243. const char* pKey = ppKeys[i];
  244. param_map_const_iterator begin, end;
  245. find(pKey, begin, end);
  246. while (begin != end)
  247. {
  248. if (pIterators)
  249. pIterators->push_back(begin);
  250. if (pUnmatched_indices)
  251. {
  252. int k = pUnmatched_indices->find(begin->second.m_index);
  253. if (k >= 0)
  254. pUnmatched_indices->erase_unordered(k);
  255. }
  256. n++;
  257. begin++;
  258. }
  259. }
  260. return n;
  261. }
  262. void command_line_params::find(const char* pKey, param_map_const_iterator& begin, param_map_const_iterator& end) const
  263. {
  264. dynamic_string key(pKey);
  265. begin = m_param_map.lower_bound(key);
  266. end = m_param_map.upper_bound(key);
  267. }
  268. uint command_line_params::get_count(const char* pKey) const
  269. {
  270. param_map_const_iterator begin, end;
  271. find(pKey, begin, end);
  272. uint n = 0;
  273. while (begin != end)
  274. {
  275. n++;
  276. begin++;
  277. }
  278. return n;
  279. }
  280. command_line_params::param_map_const_iterator command_line_params::get_param(const char* pKey, uint index) const
  281. {
  282. param_map_const_iterator begin, end;
  283. find(pKey, begin, end);
  284. if (begin == end)
  285. return m_param_map.end();
  286. uint n = 0;
  287. while ((begin != end) && (n != index))
  288. {
  289. n++;
  290. begin++;
  291. }
  292. if (begin == end)
  293. return m_param_map.end();
  294. return begin;
  295. }
  296. bool command_line_params::has_value(const char* pKey, uint index) const
  297. {
  298. return get_num_values(pKey, index) != 0;
  299. }
  300. uint command_line_params::get_num_values(const char* pKey, uint index) const
  301. {
  302. param_map_const_iterator it = get_param(pKey, index);
  303. if (it == end())
  304. return 0;
  305. return it->second.m_values.size();
  306. }
  307. bool command_line_params::get_value_as_bool(const char* pKey, uint index, bool def) const
  308. {
  309. param_map_const_iterator it = get_param(pKey, index);
  310. if (it == end())
  311. return def;
  312. if (it->second.m_modifier)
  313. return it->second.m_modifier > 0;
  314. else
  315. return true;
  316. }
  317. int command_line_params::get_value_as_int(const char* pKey, uint index, int def, int l, int h, uint value_index) const
  318. {
  319. param_map_const_iterator it = get_param(pKey, index);
  320. if ((it == end()) || (value_index >= it->second.m_values.size()))
  321. return def;
  322. int val;
  323. const char* p = it->second.m_values[value_index].get_ptr();
  324. if (!string_to_int(p, val))
  325. {
  326. crnlib::console::warning("Invalid value specified for parameter \"%s\", using default value of %i", pKey, def);
  327. return def;
  328. }
  329. if (val < l)
  330. {
  331. crnlib::console::warning("Value %i for parameter \"%s\" is out of range, clamping to %i", val, pKey, l);
  332. val = l;
  333. }
  334. else if (val > h)
  335. {
  336. crnlib::console::warning("Value %i for parameter \"%s\" is out of range, clamping to %i", val, pKey, h);
  337. val = h;
  338. }
  339. return val;
  340. }
  341. float command_line_params::get_value_as_float(const char* pKey, uint index, float def, float l, float h, uint value_index) const
  342. {
  343. param_map_const_iterator it = get_param(pKey, index);
  344. if ((it == end()) || (value_index >= it->second.m_values.size()))
  345. return def;
  346. float val;
  347. const char* p = it->second.m_values[value_index].get_ptr();
  348. if (!string_to_float(p, val))
  349. {
  350. crnlib::console::warning("Invalid value specified for float parameter \"%s\", using default value of %f", pKey, def);
  351. return def;
  352. }
  353. if (val < l)
  354. {
  355. crnlib::console::warning("Value %f for parameter \"%s\" is out of range, clamping to %f", val, pKey, l);
  356. val = l;
  357. }
  358. else if (val > h)
  359. {
  360. crnlib::console::warning("Value %f for parameter \"%s\" is out of range, clamping to %f", val, pKey, h);
  361. val = h;
  362. }
  363. return val;
  364. }
  365. bool command_line_params::get_value_as_string(const char* pKey, uint index, dynamic_string& value, uint value_index) const
  366. {
  367. param_map_const_iterator it = get_param(pKey, index);
  368. if ((it == end()) || (value_index >= it->second.m_values.size()))
  369. {
  370. value.empty();
  371. return false;
  372. }
  373. value = it->second.m_values[value_index];
  374. return true;
  375. }
  376. const dynamic_string& command_line_params::get_value_as_string_or_empty(const char* pKey, uint index, uint value_index) const
  377. {
  378. param_map_const_iterator it = get_param(pKey, index);
  379. if ((it == end()) || (value_index >= it->second.m_values.size()))
  380. return g_empty_dynamic_string;
  381. return it->second.m_values[value_index];
  382. }
  383. } // namespace crnlib