cmStringCommand.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmStringCommand.h"
  11. #include "cmCryptoHash.h"
  12. #include <cmsys/RegularExpression.hxx>
  13. #include <cmsys/SystemTools.hxx>
  14. #include <stdlib.h> // required for atoi
  15. #include <ctype.h>
  16. #include <time.h>
  17. #include <cmTimestamp.h>
  18. //----------------------------------------------------------------------------
  19. bool cmStringCommand
  20. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  21. {
  22. if(args.size() < 1)
  23. {
  24. this->SetError("must be called with at least one argument.");
  25. return false;
  26. }
  27. const std::string &subCommand = args[0];
  28. if(subCommand == "REGEX")
  29. {
  30. return this->HandleRegexCommand(args);
  31. }
  32. else if(subCommand == "REPLACE")
  33. {
  34. return this->HandleReplaceCommand(args);
  35. }
  36. else if ( subCommand == "MD5" ||
  37. subCommand == "SHA1" ||
  38. subCommand == "SHA224" ||
  39. subCommand == "SHA256" ||
  40. subCommand == "SHA384" ||
  41. subCommand == "SHA512" )
  42. {
  43. return this->HandleHashCommand(args);
  44. }
  45. else if(subCommand == "TOLOWER")
  46. {
  47. return this->HandleToUpperLowerCommand(args, false);
  48. }
  49. else if(subCommand == "TOUPPER")
  50. {
  51. return this->HandleToUpperLowerCommand(args, true);
  52. }
  53. else if(subCommand == "COMPARE")
  54. {
  55. return this->HandleCompareCommand(args);
  56. }
  57. else if(subCommand == "ASCII")
  58. {
  59. return this->HandleAsciiCommand(args);
  60. }
  61. else if(subCommand == "CONFIGURE")
  62. {
  63. return this->HandleConfigureCommand(args);
  64. }
  65. else if(subCommand == "LENGTH")
  66. {
  67. return this->HandleLengthCommand(args);
  68. }
  69. else if(subCommand == "SUBSTRING")
  70. {
  71. return this->HandleSubstringCommand(args);
  72. }
  73. else if(subCommand == "STRIP")
  74. {
  75. return this->HandleStripCommand(args);
  76. }
  77. else if(subCommand == "RANDOM")
  78. {
  79. return this->HandleRandomCommand(args);
  80. }
  81. else if(subCommand == "FIND")
  82. {
  83. return this->HandleFindCommand(args);
  84. }
  85. else if(subCommand == "TIMESTAMP")
  86. {
  87. return this->HandleTimestampCommand(args);
  88. }
  89. else if(subCommand == "MAKE_C_IDENTIFIER")
  90. {
  91. return this->HandleMakeCIdentifierCommand(args);
  92. }
  93. std::string e = "does not recognize sub-command "+subCommand;
  94. this->SetError(e.c_str());
  95. return false;
  96. }
  97. //----------------------------------------------------------------------------
  98. bool cmStringCommand::HandleHashCommand(std::vector<std::string> const& args)
  99. {
  100. #if defined(CMAKE_BUILD_WITH_CMAKE)
  101. if(args.size() != 3)
  102. {
  103. cmOStringStream e;
  104. e << args[0] << " requires an output variable and an input string";
  105. this->SetError(e.str().c_str());
  106. return false;
  107. }
  108. cmsys::auto_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
  109. if(hash.get())
  110. {
  111. std::string out = hash->HashString(args[2].c_str());
  112. this->Makefile->AddDefinition(args[1].c_str(), out.c_str());
  113. return true;
  114. }
  115. return false;
  116. #else
  117. cmOStringStream e;
  118. e << args[0] << " not available during bootstrap";
  119. this->SetError(e.str().c_str());
  120. return false;
  121. #endif
  122. }
  123. //----------------------------------------------------------------------------
  124. bool cmStringCommand::HandleToUpperLowerCommand(
  125. std::vector<std::string> const& args, bool toUpper)
  126. {
  127. if ( args.size() < 3 )
  128. {
  129. this->SetError("no output variable specified");
  130. return false;
  131. }
  132. std::string outvar = args[2];
  133. std::string output;
  134. if (toUpper)
  135. {
  136. output = cmSystemTools::UpperCase(args[1]);
  137. }
  138. else
  139. {
  140. output = cmSystemTools::LowerCase(args[1]);
  141. }
  142. // Store the output in the provided variable.
  143. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  144. return true;
  145. }
  146. //----------------------------------------------------------------------------
  147. bool cmStringCommand::HandleAsciiCommand(std::vector<std::string> const& args)
  148. {
  149. if ( args.size() < 3 )
  150. {
  151. this->SetError("No output variable specified");
  152. return false;
  153. }
  154. std::string::size_type cc;
  155. std::string outvar = args[args.size()-1];
  156. std::string output = "";
  157. for ( cc = 1; cc < args.size()-1; cc ++ )
  158. {
  159. int ch = atoi(args[cc].c_str());
  160. if ( ch > 0 && ch < 256 )
  161. {
  162. output += static_cast<char>(ch);
  163. }
  164. else
  165. {
  166. std::string error = "Character with code ";
  167. error += args[cc];
  168. error += " does not exist.";
  169. this->SetError(error.c_str());
  170. return false;
  171. }
  172. }
  173. // Store the output in the provided variable.
  174. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  175. return true;
  176. }
  177. //----------------------------------------------------------------------------
  178. bool cmStringCommand::HandleConfigureCommand(
  179. std::vector<std::string> const& args)
  180. {
  181. if ( args.size() < 2 )
  182. {
  183. this->SetError("No input string specified.");
  184. return false;
  185. }
  186. else if ( args.size() < 3 )
  187. {
  188. this->SetError("No output variable specified.");
  189. return false;
  190. }
  191. // Parse options.
  192. bool escapeQuotes = false;
  193. bool atOnly = false;
  194. for(unsigned int i = 3; i < args.size(); ++i)
  195. {
  196. if(args[i] == "@ONLY")
  197. {
  198. atOnly = true;
  199. }
  200. else if(args[i] == "ESCAPE_QUOTES")
  201. {
  202. escapeQuotes = true;
  203. }
  204. else
  205. {
  206. cmOStringStream err;
  207. err << "Unrecognized argument \"" << args[i] << "\"";
  208. this->SetError(err.str().c_str());
  209. return false;
  210. }
  211. }
  212. // Configure the string.
  213. std::string output;
  214. this->Makefile->ConfigureString(args[1], output, atOnly, escapeQuotes);
  215. // Store the output in the provided variable.
  216. this->Makefile->AddDefinition(args[2].c_str(), output.c_str());
  217. return true;
  218. }
  219. //----------------------------------------------------------------------------
  220. bool cmStringCommand::HandleRegexCommand(std::vector<std::string> const& args)
  221. {
  222. if(args.size() < 2)
  223. {
  224. this->SetError("sub-command REGEX requires a mode to be specified.");
  225. return false;
  226. }
  227. std::string mode = args[1];
  228. if(mode == "MATCH")
  229. {
  230. if(args.size() < 5)
  231. {
  232. this->SetError("sub-command REGEX, mode MATCH needs "
  233. "at least 5 arguments total to command.");
  234. return false;
  235. }
  236. return this->RegexMatch(args);
  237. }
  238. else if(mode == "MATCHALL")
  239. {
  240. if(args.size() < 5)
  241. {
  242. this->SetError("sub-command REGEX, mode MATCHALL needs "
  243. "at least 5 arguments total to command.");
  244. return false;
  245. }
  246. return this->RegexMatchAll(args);
  247. }
  248. else if(mode == "REPLACE")
  249. {
  250. if(args.size() < 6)
  251. {
  252. this->SetError("sub-command REGEX, mode REPLACE needs "
  253. "at least 6 arguments total to command.");
  254. return false;
  255. }
  256. return this->RegexReplace(args);
  257. }
  258. std::string e = "sub-command REGEX does not recognize mode "+mode;
  259. this->SetError(e.c_str());
  260. return false;
  261. }
  262. //----------------------------------------------------------------------------
  263. bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
  264. {
  265. //"STRING(REGEX MATCH <regular_expression> <output variable>
  266. // <input> [<input>...])\n";
  267. std::string regex = args[2];
  268. std::string outvar = args[3];
  269. // Concatenate all the last arguments together.
  270. std::string input = args[4];
  271. for(unsigned int i=5; i < args.size(); ++i)
  272. {
  273. input += args[i];
  274. }
  275. this->ClearMatches(this->Makefile);
  276. // Compile the regular expression.
  277. cmsys::RegularExpression re;
  278. if(!re.compile(regex.c_str()))
  279. {
  280. std::string e =
  281. "sub-command REGEX, mode MATCH failed to compile regex \""+regex+"\".";
  282. this->SetError(e.c_str());
  283. return false;
  284. }
  285. // Scan through the input for all matches.
  286. std::string output;
  287. if(re.find(input.c_str()))
  288. {
  289. this->StoreMatches(this->Makefile, re);
  290. std::string::size_type l = re.start();
  291. std::string::size_type r = re.end();
  292. if(r-l == 0)
  293. {
  294. std::string e =
  295. "sub-command REGEX, mode MATCH regex \""+regex+
  296. "\" matched an empty string.";
  297. this->SetError(e.c_str());
  298. return false;
  299. }
  300. output = input.substr(l, r-l);
  301. }
  302. // Store the output in the provided variable.
  303. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  304. return true;
  305. }
  306. //----------------------------------------------------------------------------
  307. bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
  308. {
  309. //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input>
  310. // [<input>...])\n";
  311. std::string regex = args[2];
  312. std::string outvar = args[3];
  313. // Concatenate all the last arguments together.
  314. std::string input = args[4];
  315. for(unsigned int i=5; i < args.size(); ++i)
  316. {
  317. input += args[i];
  318. }
  319. this->ClearMatches(this->Makefile);
  320. // Compile the regular expression.
  321. cmsys::RegularExpression re;
  322. if(!re.compile(regex.c_str()))
  323. {
  324. std::string e =
  325. "sub-command REGEX, mode MATCHALL failed to compile regex \""+
  326. regex+"\".";
  327. this->SetError(e.c_str());
  328. return false;
  329. }
  330. // Scan through the input for all matches.
  331. std::string output;
  332. const char* p = input.c_str();
  333. while(re.find(p))
  334. {
  335. this->StoreMatches(this->Makefile, re);
  336. std::string::size_type l = re.start();
  337. std::string::size_type r = re.end();
  338. if(r-l == 0)
  339. {
  340. std::string e = "sub-command REGEX, mode MATCHALL regex \""+
  341. regex+"\" matched an empty string.";
  342. this->SetError(e.c_str());
  343. return false;
  344. }
  345. if(output.length() > 0)
  346. {
  347. output += ";";
  348. }
  349. output += std::string(p+l, r-l);
  350. p += r;
  351. }
  352. // Store the output in the provided variable.
  353. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  354. return true;
  355. }
  356. //----------------------------------------------------------------------------
  357. bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
  358. {
  359. //"STRING(REGEX REPLACE <regular_expression> <replace_expression>
  360. // <output variable> <input> [<input>...])\n"
  361. std::string regex = args[2];
  362. std::string replace = args[3];
  363. std::string outvar = args[4];
  364. // Pull apart the replace expression to find the escaped [0-9] values.
  365. std::vector<RegexReplacement> replacement;
  366. std::string::size_type l = 0;
  367. while(l < replace.length())
  368. {
  369. std::string::size_type r = replace.find("\\", l);
  370. if(r == std::string::npos)
  371. {
  372. r = replace.length();
  373. replacement.push_back(replace.substr(l, r-l));
  374. }
  375. else
  376. {
  377. if(r-l > 0)
  378. {
  379. replacement.push_back(replace.substr(l, r-l));
  380. }
  381. if(r == (replace.length()-1))
  382. {
  383. this->SetError("sub-command REGEX, mode REPLACE: "
  384. "replace-expression ends in a backslash.");
  385. return false;
  386. }
  387. if((replace[r+1] >= '0') && (replace[r+1] <= '9'))
  388. {
  389. replacement.push_back(replace[r+1]-'0');
  390. }
  391. else if(replace[r+1] == 'n')
  392. {
  393. replacement.push_back("\n");
  394. }
  395. else if(replace[r+1] == '\\')
  396. {
  397. replacement.push_back("\\");
  398. }
  399. else
  400. {
  401. std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
  402. e += replace.substr(r, 2);
  403. e += "\" in replace-expression.";
  404. this->SetError(e.c_str());
  405. return false;
  406. }
  407. r += 2;
  408. }
  409. l = r;
  410. }
  411. // Concatenate all the last arguments together.
  412. std::string input = args[5];
  413. for(unsigned int i=6; i < args.size(); ++i)
  414. {
  415. input += args[i];
  416. }
  417. this->ClearMatches(this->Makefile);
  418. // Compile the regular expression.
  419. cmsys::RegularExpression re;
  420. if(!re.compile(regex.c_str()))
  421. {
  422. std::string e =
  423. "sub-command REGEX, mode REPLACE failed to compile regex \""+
  424. regex+"\".";
  425. this->SetError(e.c_str());
  426. return false;
  427. }
  428. // Scan through the input for all matches.
  429. std::string output;
  430. std::string::size_type base = 0;
  431. while(re.find(input.c_str()+base))
  432. {
  433. this->StoreMatches(this->Makefile, re);
  434. std::string::size_type l2 = re.start();
  435. std::string::size_type r = re.end();
  436. // Concatenate the part of the input that was not matched.
  437. output += input.substr(base, l2);
  438. // Make sure the match had some text.
  439. if(r-l2 == 0)
  440. {
  441. std::string e = "sub-command REGEX, mode REPLACE regex \""+
  442. regex+"\" matched an empty string.";
  443. this->SetError(e.c_str());
  444. return false;
  445. }
  446. // Concatenate the replacement for the match.
  447. for(unsigned int i=0; i < replacement.size(); ++i)
  448. {
  449. if(replacement[i].number < 0)
  450. {
  451. // This is just a plain-text part of the replacement.
  452. output += replacement[i].value;
  453. }
  454. else
  455. {
  456. // Replace with part of the match.
  457. int n = replacement[i].number;
  458. std::string::size_type start = re.start(n);
  459. std::string::size_type end = re.end(n);
  460. std::string::size_type len = input.length()-base;
  461. if((start != std::string::npos) && (end != std::string::npos) &&
  462. (start <= len) && (end <= len))
  463. {
  464. output += input.substr(base+start, end-start);
  465. }
  466. else
  467. {
  468. std::string e =
  469. "sub-command REGEX, mode REPLACE: replace expression \""+
  470. replace+"\" contains an out-of-range escape for regex \""+
  471. regex+"\".";
  472. this->SetError(e.c_str());
  473. return false;
  474. }
  475. }
  476. }
  477. // Move past the match.
  478. base += r;
  479. }
  480. // Concatenate the text after the last match.
  481. output += input.substr(base, input.length()-base);
  482. // Store the output in the provided variable.
  483. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  484. return true;
  485. }
  486. //----------------------------------------------------------------------------
  487. void cmStringCommand::ClearMatches(cmMakefile* mf)
  488. {
  489. for (unsigned int i=0; i<10; i++)
  490. {
  491. char name[128];
  492. sprintf(name, "CMAKE_MATCH_%d", i);
  493. mf->AddDefinition(name, "");
  494. mf->MarkVariableAsUsed(name);
  495. }
  496. }
  497. //----------------------------------------------------------------------------
  498. void cmStringCommand::StoreMatches(cmMakefile* mf,cmsys::RegularExpression& re)
  499. {
  500. for (unsigned int i=0; i<10; i++)
  501. {
  502. char name[128];
  503. sprintf(name, "CMAKE_MATCH_%d", i);
  504. mf->AddDefinition(name, re.match(i).c_str());
  505. mf->MarkVariableAsUsed(name);
  506. }
  507. }
  508. //----------------------------------------------------------------------------
  509. bool cmStringCommand::HandleFindCommand(std::vector<std::string> const&
  510. args)
  511. {
  512. // check if all required parameters were passed
  513. if(args.size() < 4 || args.size() > 5)
  514. {
  515. this->SetError("sub-command FIND requires 3 or 4 parameters.");
  516. return false;
  517. }
  518. // check if the reverse flag was set or not
  519. bool reverseMode = false;
  520. if(args.size() == 5 && args[4] == "REVERSE")
  521. {
  522. reverseMode = true;
  523. }
  524. // if we have 5 arguments the last one must be REVERSE
  525. if(args.size() == 5 && args[4] != "REVERSE")
  526. {
  527. this->SetError("sub-command FIND: unknown last parameter");
  528. return false;
  529. }
  530. // local parameter names.
  531. const std::string& sstring = args[1];
  532. const std::string& schar = args[2];
  533. const std::string& outvar = args[3];
  534. // ensure that the user cannot accidentally specify REVERSE as a variable
  535. if(outvar == "REVERSE")
  536. {
  537. this->SetError("sub-command FIND does not allow to select REVERSE as "
  538. "the output variable. "
  539. "Maybe you missed the actual output variable?");
  540. return false;
  541. }
  542. // try to find the character and return its position
  543. size_t pos;
  544. if(!reverseMode)
  545. {
  546. pos = sstring.find(schar);
  547. }
  548. else
  549. {
  550. pos = sstring.rfind(schar);
  551. }
  552. if(std::string::npos != pos)
  553. {
  554. cmOStringStream s;
  555. s << pos;
  556. this->Makefile->AddDefinition(outvar.c_str(), s.str().c_str());
  557. return true;
  558. }
  559. // the character was not found, but this is not really an error
  560. this->Makefile->AddDefinition(outvar.c_str(), "-1");
  561. return true;
  562. }
  563. //----------------------------------------------------------------------------
  564. bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const&
  565. args)
  566. {
  567. if(args.size() < 2)
  568. {
  569. this->SetError("sub-command COMPARE requires a mode to be specified.");
  570. return false;
  571. }
  572. std::string mode = args[1];
  573. if((mode == "EQUAL") || (mode == "NOTEQUAL") ||
  574. (mode == "LESS") || (mode == "GREATER"))
  575. {
  576. if(args.size() < 5)
  577. {
  578. std::string e = "sub-command COMPARE, mode ";
  579. e += mode;
  580. e += " needs at least 5 arguments total to command.";
  581. this->SetError(e.c_str());
  582. return false;
  583. }
  584. const std::string& left = args[2];
  585. const std::string& right = args[3];
  586. const std::string& outvar = args[4];
  587. bool result;
  588. if(mode == "LESS")
  589. {
  590. result = (left < right);
  591. }
  592. else if(mode == "GREATER")
  593. {
  594. result = (left > right);
  595. }
  596. else if(mode == "EQUAL")
  597. {
  598. result = (left == right);
  599. }
  600. else // if(mode == "NOTEQUAL")
  601. {
  602. result = !(left == right);
  603. }
  604. if(result)
  605. {
  606. this->Makefile->AddDefinition(outvar.c_str(), "1");
  607. }
  608. else
  609. {
  610. this->Makefile->AddDefinition(outvar.c_str(), "0");
  611. }
  612. return true;
  613. }
  614. std::string e = "sub-command COMPARE does not recognize mode "+mode;
  615. this->SetError(e.c_str());
  616. return false;
  617. }
  618. //----------------------------------------------------------------------------
  619. bool cmStringCommand::HandleReplaceCommand(std::vector<std::string> const&
  620. args)
  621. {
  622. if(args.size() < 5)
  623. {
  624. this->SetError("sub-command REPLACE requires at least four arguments.");
  625. return false;
  626. }
  627. const std::string& matchExpression = args[1];
  628. const std::string& replaceExpression = args[2];
  629. const std::string& variableName = args[3];
  630. std::string input = args[4];
  631. for(unsigned int i=5; i < args.size(); ++i)
  632. {
  633. input += args[i];
  634. }
  635. cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
  636. replaceExpression.c_str());
  637. this->Makefile->AddDefinition(variableName.c_str(), input.c_str());
  638. return true;
  639. }
  640. //----------------------------------------------------------------------------
  641. bool cmStringCommand::HandleSubstringCommand(std::vector<std::string> const&
  642. args)
  643. {
  644. if(args.size() != 5)
  645. {
  646. this->SetError("sub-command SUBSTRING requires four arguments.");
  647. return false;
  648. }
  649. const std::string& stringValue = args[1];
  650. int begin = atoi(args[2].c_str());
  651. int end = atoi(args[3].c_str());
  652. const std::string& variableName = args[4];
  653. size_t stringLength = stringValue.size();
  654. int intStringLength = static_cast<int>(stringLength);
  655. if ( begin < 0 || begin > intStringLength )
  656. {
  657. cmOStringStream ostr;
  658. ostr << "begin index: " << begin << " is out of range 0 - "
  659. << stringLength;
  660. this->SetError(ostr.str().c_str());
  661. return false;
  662. }
  663. int leftOverLength = intStringLength - begin;
  664. if ( end < -1 || end > leftOverLength )
  665. {
  666. cmOStringStream ostr;
  667. ostr << "end index: " << end << " is out of range -1 - "
  668. << leftOverLength;
  669. this->SetError(ostr.str().c_str());
  670. return false;
  671. }
  672. this->Makefile->AddDefinition(variableName.c_str(),
  673. stringValue.substr(begin, end).c_str());
  674. return true;
  675. }
  676. //----------------------------------------------------------------------------
  677. bool cmStringCommand
  678. ::HandleLengthCommand(std::vector<std::string> const& args)
  679. {
  680. if(args.size() != 3)
  681. {
  682. this->SetError("sub-command LENGTH requires two arguments.");
  683. return false;
  684. }
  685. const std::string& stringValue = args[1];
  686. const std::string& variableName = args[2];
  687. size_t length = stringValue.size();
  688. char buffer[1024];
  689. sprintf(buffer, "%d", static_cast<int>(length));
  690. this->Makefile->AddDefinition(variableName.c_str(), buffer);
  691. return true;
  692. }
  693. //----------------------------------------------------------------------------
  694. bool cmStringCommand
  695. ::HandleMakeCIdentifierCommand(std::vector<std::string> const& args)
  696. {
  697. if(args.size() != 3)
  698. {
  699. this->SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
  700. return false;
  701. }
  702. const std::string& input = args[1];
  703. const std::string& variableName = args[2];
  704. this->Makefile->AddDefinition(variableName.c_str(),
  705. cmSystemTools::MakeCidentifier(input.c_str()).c_str());
  706. return true;
  707. }
  708. //----------------------------------------------------------------------------
  709. bool cmStringCommand::HandleStripCommand(
  710. std::vector<std::string> const& args)
  711. {
  712. if(args.size() != 3)
  713. {
  714. this->SetError("sub-command STRIP requires two arguments.");
  715. return false;
  716. }
  717. const std::string& stringValue = args[1];
  718. const std::string& variableName = args[2];
  719. size_t inStringLength = stringValue.size();
  720. size_t startPos = inStringLength + 1;
  721. size_t endPos = 0;
  722. const char* ptr = stringValue.c_str();
  723. size_t cc;
  724. for ( cc = 0; cc < inStringLength; ++ cc )
  725. {
  726. if ( !isspace(*ptr) )
  727. {
  728. if ( startPos > inStringLength )
  729. {
  730. startPos = cc;
  731. }
  732. endPos = cc;
  733. }
  734. ++ ptr;
  735. }
  736. size_t outLength = 0;
  737. // if the input string didn't contain any non-space characters, return
  738. // an empty string
  739. if (startPos > inStringLength)
  740. {
  741. outLength = 0;
  742. startPos = 0;
  743. }
  744. else
  745. {
  746. outLength=endPos - startPos + 1;
  747. }
  748. this->Makefile->AddDefinition(variableName.c_str(),
  749. stringValue.substr(startPos, outLength).c_str());
  750. return true;
  751. }
  752. //----------------------------------------------------------------------------
  753. bool cmStringCommand
  754. ::HandleRandomCommand(std::vector<std::string> const& args)
  755. {
  756. if(args.size() < 2 || args.size() == 3 || args.size() == 5)
  757. {
  758. this->SetError("sub-command RANDOM requires at least one argument.");
  759. return false;
  760. }
  761. static bool seeded = false;
  762. bool force_seed = false;
  763. unsigned int seed = 0;
  764. int length = 5;
  765. const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
  766. "QWERTYUIOPASDFGHJKLZXCVBNM"
  767. "0123456789";
  768. std::string alphabet;
  769. if ( args.size() > 3 )
  770. {
  771. size_t i = 1;
  772. size_t stopAt = args.size() - 2;
  773. for ( ; i < stopAt; ++i )
  774. {
  775. if ( args[i] == "LENGTH" )
  776. {
  777. ++i;
  778. length = atoi(args[i].c_str());
  779. }
  780. else if ( args[i] == "ALPHABET" )
  781. {
  782. ++i;
  783. alphabet = args[i];
  784. }
  785. else if ( args[i] == "RANDOM_SEED" )
  786. {
  787. ++i;
  788. seed = static_cast<unsigned int>(atoi(args[i].c_str()));
  789. force_seed = true;
  790. }
  791. }
  792. }
  793. if ( !alphabet.size() )
  794. {
  795. alphabet = cmStringCommandDefaultAlphabet;
  796. }
  797. double sizeofAlphabet = static_cast<double>(alphabet.size());
  798. if ( sizeofAlphabet < 1 )
  799. {
  800. this->SetError("sub-command RANDOM invoked with bad alphabet.");
  801. return false;
  802. }
  803. if ( length < 1 )
  804. {
  805. this->SetError("sub-command RANDOM invoked with bad length.");
  806. return false;
  807. }
  808. const std::string& variableName = args[args.size()-1];
  809. std::vector<char> result;
  810. if (!seeded || force_seed)
  811. {
  812. seeded = true;
  813. srand(force_seed? seed : cmSystemTools::RandomSeed());
  814. }
  815. const char* alphaPtr = alphabet.c_str();
  816. int cc;
  817. for ( cc = 0; cc < length; cc ++ )
  818. {
  819. int idx=(int) (sizeofAlphabet* rand()/(RAND_MAX+1.0));
  820. result.push_back(*(alphaPtr + idx));
  821. }
  822. result.push_back(0);
  823. this->Makefile->AddDefinition(variableName.c_str(), &*result.begin());
  824. return true;
  825. }
  826. //----------------------------------------------------------------------------
  827. bool cmStringCommand
  828. ::HandleTimestampCommand(std::vector<std::string> const& args)
  829. {
  830. if(args.size() < 2)
  831. {
  832. this->SetError("sub-command TIMESTAMP requires at least one argument.");
  833. return false;
  834. }
  835. else if(args.size() > 4)
  836. {
  837. this->SetError("sub-command TIMESTAMP takes at most three arguments.");
  838. return false;
  839. }
  840. unsigned int argsIndex = 1;
  841. const std::string &outputVariable = args[argsIndex++];
  842. std::string formatString;
  843. if(args.size() > argsIndex && args[argsIndex] != "UTC")
  844. {
  845. formatString = args[argsIndex++];
  846. }
  847. bool utcFlag = false;
  848. if(args.size() > argsIndex)
  849. {
  850. if(args[argsIndex] == "UTC")
  851. {
  852. utcFlag = true;
  853. }
  854. else
  855. {
  856. std::string e = " TIMESTAMP sub-command does not recognize option " +
  857. args[argsIndex] + ".";
  858. this->SetError(e.c_str());
  859. return false;
  860. }
  861. }
  862. cmTimestamp timestamp;
  863. std::string result = timestamp.CurrentTime(formatString, utcFlag);
  864. this->Makefile->AddDefinition(outputVariable.c_str(), result.c_str());
  865. return true;
  866. }