cmStringCommand.cxx 12 KB

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