cmStringCommand.cxx 10 KB

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