cmStringCommand.cxx 24 KB

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