cmStringCommand.cxx 11 KB

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