cmStringCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmStringCommand.h"
  14. #include <stdlib.h> // required for atoi
  15. //----------------------------------------------------------------------------
  16. bool cmStringCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() < 1)
  19. {
  20. this->SetError("must be called with at least one argument.");
  21. return false;
  22. }
  23. std::string subCommand = args[0];
  24. if(subCommand == "REGEX")
  25. {
  26. return this->HandleRegexCommand(args);
  27. }
  28. else if(subCommand == "COMPARE")
  29. {
  30. return this->HandleCompareCommand(args);
  31. }
  32. else if(subCommand == "ASCII")
  33. {
  34. return this->HandleAsciiCommand(args);
  35. }
  36. std::string e = "does not recognize sub-command "+subCommand;
  37. this->SetError(e.c_str());
  38. return false;
  39. }
  40. //----------------------------------------------------------------------------
  41. bool cmStringCommand::HandleAsciiCommand(std::vector<std::string> const& args)
  42. {
  43. if ( args.size() <= 1 )
  44. {
  45. this->SetError("No output variable specified");
  46. return false;
  47. }
  48. std::string::size_type cc;
  49. std::string outvar = args[args.size()-1];
  50. std::string output = "";
  51. for ( cc = 1; cc < args.size()-1; cc ++ )
  52. {
  53. int ch = atoi(args[cc].c_str());
  54. if ( ch > 0 && ch < 256 )
  55. {
  56. output += static_cast<char>(ch);
  57. }
  58. else
  59. {
  60. std::string error = "Character with code ";
  61. error += ch;
  62. error += " does not exist.";
  63. this->SetError(error.c_str());
  64. return false;
  65. }
  66. }
  67. // Store the output in the provided variable.
  68. m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
  69. return true;
  70. }
  71. //----------------------------------------------------------------------------
  72. bool cmStringCommand::HandleRegexCommand(std::vector<std::string> const& args)
  73. {
  74. if(args.size() < 2)
  75. {
  76. this->SetError("sub-command REGEX requires a mode to be specified.");
  77. return false;
  78. }
  79. std::string mode = args[1];
  80. if(mode == "MATCH")
  81. {
  82. if(args.size() < 5)
  83. {
  84. this->SetError("sub-command REGEX, mode MATCH needs "
  85. "at least 5 arguments total to command.");
  86. return false;
  87. }
  88. return this->RegexMatch(args);
  89. }
  90. else if(mode == "MATCHALL")
  91. {
  92. if(args.size() < 5)
  93. {
  94. this->SetError("sub-command REGEX, mode MATCHALL needs "
  95. "at least 5 arguments total to command.");
  96. return false;
  97. }
  98. return this->RegexMatchAll(args);
  99. }
  100. else if(mode == "REPLACE")
  101. {
  102. if(args.size() < 6)
  103. {
  104. this->SetError("sub-command REGEX, mode MATCH needs "
  105. "at least 6 arguments total to command.");
  106. return false;
  107. }
  108. return this->RegexReplace(args);
  109. }
  110. std::string e = "sub-command REGEX does not recognize mode "+mode;
  111. this->SetError(e.c_str());
  112. return false;
  113. }
  114. //----------------------------------------------------------------------------
  115. bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
  116. {
  117. //"STRING(REGEX MATCH <regular_expression> <output variable> <input> [<input>...])\n";
  118. std::string regex = args[2];
  119. std::string outvar = args[3];
  120. // Concatenate all the last arguments together.
  121. std::string input = args[4];
  122. for(unsigned int i=5; i < args.size(); ++i)
  123. {
  124. input += args[i];
  125. }
  126. // Compile the regular expression.
  127. cmRegularExpression re;
  128. if(!re.compile(regex.c_str()))
  129. {
  130. std::string e = "sub-command REGEX, mode MATCH failed to compile regex \""+regex+"\".";
  131. this->SetError(e.c_str());
  132. return false;
  133. }
  134. // Scan through the input for all matches.
  135. std::string output;
  136. if(re.find(input.c_str()))
  137. {
  138. std::string::size_type l = re.start();
  139. std::string::size_type r = re.end();
  140. if(r-l == 0)
  141. {
  142. std::string e = "sub-command REGEX, mode MATCH regex \""+regex+"\" matched an empty string.";
  143. this->SetError(e.c_str());
  144. return false;
  145. }
  146. output = input.substr(l, r-l);
  147. }
  148. // Store the output in the provided variable.
  149. m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
  150. return true;
  151. }
  152. //----------------------------------------------------------------------------
  153. bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
  154. {
  155. //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input> [<input>...])\n";
  156. std::string regex = args[2];
  157. std::string outvar = args[3];
  158. // Concatenate all the last arguments together.
  159. std::string input = args[4];
  160. for(unsigned int i=5; i < args.size(); ++i)
  161. {
  162. input += args[i];
  163. }
  164. // Compile the regular expression.
  165. cmRegularExpression re;
  166. if(!re.compile(regex.c_str()))
  167. {
  168. std::string e = "sub-command REGEX, mode MATCHALL failed to compile regex \""+regex+"\".";
  169. this->SetError(e.c_str());
  170. return false;
  171. }
  172. // Scan through the input for all matches.
  173. std::string output;
  174. const char* p = input.c_str();
  175. while(re.find(p))
  176. {
  177. std::string::size_type l = re.start();
  178. std::string::size_type r = re.end();
  179. if(r-l == 0)
  180. {
  181. std::string e = "sub-command REGEX, mode MATCHALL regex \""+regex+"\" matched an empty string.";
  182. this->SetError(e.c_str());
  183. return false;
  184. }
  185. if(output.length() > 0)
  186. {
  187. output += ";";
  188. }
  189. output += std::string(p+l, r-l);
  190. p += r;
  191. }
  192. // Store the output in the provided variable.
  193. m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
  194. return true;
  195. }
  196. //----------------------------------------------------------------------------
  197. bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
  198. {
  199. //"STRING(REGEX REPLACE <regular_expression> <replace_expression> <output variable> <input> [<input>...])\n"
  200. std::string regex = args[2];
  201. std::string replace = args[3];
  202. std::string outvar = args[4];
  203. // Pull apart the replace expression to find the escaped [0-9] values.
  204. std::vector<RegexReplacement> replacement;
  205. std::string::size_type l = 0;
  206. while(l < replace.length())
  207. {
  208. std::string::size_type r = replace.find("\\", l);
  209. if(r == std::string::npos)
  210. {
  211. r = replace.length();
  212. replacement.push_back(replace.substr(l, r-l));
  213. }
  214. else
  215. {
  216. if(r-l > 0)
  217. {
  218. replacement.push_back(replace.substr(l, r-l));
  219. }
  220. if(r == (replace.length()-1))
  221. {
  222. this->SetError("sub-command REGEX, mode REPLACE: "
  223. "replace-expression ends in a backslash.");
  224. return false;
  225. }
  226. if((replace[r+1] >= '0') && (replace[r+1] <= '9'))
  227. {
  228. replacement.push_back(replace[r+1]-'0');
  229. }
  230. else if(replace[r+1] == 'n')
  231. {
  232. replacement.push_back("\n");
  233. }
  234. else if(replace[r+1] == '\\')
  235. {
  236. replacement.push_back("\\");
  237. }
  238. else
  239. {
  240. std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
  241. e += replace.substr(r, 2);
  242. e += "\"in replace-expression.";
  243. this->SetError(e.c_str());
  244. return false;
  245. }
  246. r += 2;
  247. }
  248. l = r;
  249. }
  250. // Concatenate all the last arguments together.
  251. std::string input = args[5];
  252. for(unsigned int i=6; i < args.size(); ++i)
  253. {
  254. input += args[i];
  255. }
  256. // Compile the regular expression.
  257. cmRegularExpression re;
  258. if(!re.compile(regex.c_str()))
  259. {
  260. std::string e = "sub-command REGEX, mode REPLACE failed to compile regex \""+regex+"\".";
  261. this->SetError(e.c_str());
  262. return false;
  263. }
  264. // Scan through the input for all matches.
  265. std::string output;
  266. std::string::size_type base = 0;
  267. while(re.find(input.c_str()+base))
  268. {
  269. std::string::size_type l2 = re.start();
  270. std::string::size_type r = re.end();
  271. // Concatenate the part of the input that was not matched.
  272. output += input.substr(base, l2);
  273. // Make sure the match had some text.
  274. if(r-l2 == 0)
  275. {
  276. std::string e = "sub-command REGEX, mode REPLACE regex \""+regex+"\" matched an empty string.";
  277. this->SetError(e.c_str());
  278. return false;
  279. }
  280. // Concatenate the replacement for the match.
  281. for(unsigned int i=0; i < replacement.size(); ++i)
  282. {
  283. if(replacement[i].number < 0)
  284. {
  285. // This is just a plain-text part of the replacement.
  286. output += replacement[i].value;
  287. }
  288. else
  289. {
  290. // Replace with part of the match.
  291. int n = replacement[i].number;
  292. std::string::size_type start = re.start(n);
  293. std::string::size_type end = re.end(n);
  294. std::string::size_type len = input.length()-base;
  295. if((start != std::string::npos) && (end != std::string::npos) &&
  296. (start <= len) && (end <= len))
  297. {
  298. output += input.substr(base+start, end-start);
  299. }
  300. else
  301. {
  302. std::string e =
  303. "sub-command REGEX, mode REPLACE: replace expression \""+
  304. replace+"\" contains an out-of-range escape for regex \""+
  305. regex+"\".";
  306. this->SetError(e.c_str());
  307. return false;
  308. }
  309. }
  310. }
  311. // Move past the match.
  312. base += r;
  313. }
  314. // Concatenate the text after the last match.
  315. output += input.substr(base, input.length()-base);
  316. // Store the output in the provided variable.
  317. m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
  318. return true;
  319. }
  320. //----------------------------------------------------------------------------
  321. bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const& args)
  322. {
  323. if(args.size() < 2)
  324. {
  325. this->SetError("sub-command COMPARE requires a mode to be specified.");
  326. return false;
  327. }
  328. std::string mode = args[1];
  329. if((mode == "EQUAL") || (mode == "NOTEQUAL") ||
  330. (mode == "LESS") || (mode == "GREATER"))
  331. {
  332. if(args.size() < 5)
  333. {
  334. std::string e = "sub-command COMPARE, mode ";
  335. e += mode;
  336. e += " needs at least 5 arguments total to command.";
  337. this->SetError(e.c_str());
  338. return false;
  339. }
  340. const std::string& left = args[2];
  341. const std::string& right = args[3];
  342. const std::string& outvar = args[4];
  343. bool result;
  344. if(mode == "LESS")
  345. {
  346. result = (left < right);
  347. }
  348. else if(mode == "GREATER")
  349. {
  350. result = (left > right);
  351. }
  352. else if(mode == "EQUAL")
  353. {
  354. result = (left == right);
  355. }
  356. else // if(mode == "NOTEQUAL")
  357. {
  358. result = !(left == right);
  359. }
  360. if(result)
  361. {
  362. m_Makefile->AddDefinition(outvar.c_str(), "1");
  363. }
  364. else
  365. {
  366. m_Makefile->AddDefinition(outvar.c_str(), "0");
  367. }
  368. return true;
  369. }
  370. std::string e = "sub-command COMPARE does not recognize mode "+mode;
  371. this->SetError(e.c_str());
  372. return false;
  373. }