1
0

cmStringCommand.cxx 8.8 KB

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