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