cmStringCommand.cxx 12 KB

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