cmSystemTools.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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 "cmSystemTools.h"
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <cmsys/RegularExpression.hxx>
  19. #include <cmsys/Directory.hxx>
  20. // support for realpath call
  21. #ifndef _WIN32
  22. #include <limits.h>
  23. #include <stdlib.h>
  24. #include <sys/param.h>
  25. #include <sys/wait.h>
  26. #endif
  27. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__))
  28. #include <string.h>
  29. #include <windows.h>
  30. #include <direct.h>
  31. #define _unlink unlink
  32. #else
  33. #include <sys/types.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #endif
  37. bool cmSystemTools::s_RunCommandHideConsole = false;
  38. bool cmSystemTools::s_DisableRunCommandOutput = false;
  39. bool cmSystemTools::s_ErrorOccured = false;
  40. bool cmSystemTools::s_FatalErrorOccured = false;
  41. bool cmSystemTools::s_DisableMessages = false;
  42. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  43. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  44. {
  45. if ( str )
  46. {
  47. cmSystemTools::s_Windows9xComspecSubstitute = str;
  48. }
  49. }
  50. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  51. {
  52. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  53. }
  54. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&, void*);
  55. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  56. // replace replace with with as many times as it shows up in source.
  57. // write the result into source.
  58. #if defined(_WIN32) && !defined(__CYGWIN__)
  59. void cmSystemTools::ExpandRegistryValues(std::string& source)
  60. {
  61. // Regular expression to match anything inside [...] that begins in HKEY.
  62. // Note that there is a special rule for regular expressions to match a
  63. // close square-bracket inside a list delimited by square brackets.
  64. // The "[^]]" part of this expression will match any character except
  65. // a close square-bracket. The ']' character must be the first in the
  66. // list of characters inside the [^...] block of the expression.
  67. cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  68. // check for black line or comment
  69. while (regEntry.find(source))
  70. {
  71. // the arguments are the second match
  72. std::string key = regEntry.match(1);
  73. std::string val;
  74. if (ReadRegistryValue(key.c_str(), val))
  75. {
  76. std::string reg = "[";
  77. reg += key + "]";
  78. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  79. }
  80. else
  81. {
  82. std::string reg = "[";
  83. reg += key + "]";
  84. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  85. }
  86. }
  87. }
  88. #else
  89. void cmSystemTools::ExpandRegistryValues(std::string&)
  90. {
  91. }
  92. #endif
  93. std::string cmSystemTools::EscapeQuotes(const char* str)
  94. {
  95. std::string result = "";
  96. for(const char* ch = str; *ch != '\0'; ++ch)
  97. {
  98. if(*ch == '"')
  99. {
  100. result += '\\';
  101. }
  102. result += *ch;
  103. }
  104. return result;
  105. }
  106. std::string cmSystemTools::EscapeSpaces(const char* str)
  107. {
  108. #if defined(_WIN32) && !defined(__CYGWIN__)
  109. std::string result;
  110. // if there are spaces
  111. std::string temp = str;
  112. if (temp.find(" ") != std::string::npos &&
  113. temp.find("\"")==std::string::npos)
  114. {
  115. result = "\"";
  116. result += str;
  117. result += "\"";
  118. return result;
  119. }
  120. return str;
  121. #else
  122. std::string result = "";
  123. for(const char* ch = str; *ch != '\0'; ++ch)
  124. {
  125. if(*ch == ' ')
  126. {
  127. result += '\\';
  128. }
  129. result += *ch;
  130. }
  131. return result;
  132. #endif
  133. }
  134. std::string cmSystemTools::RemoveEscapes(const char* s)
  135. {
  136. std::string result = "";
  137. for(const char* ch = s; *ch; ++ch)
  138. {
  139. if(*ch == '\\' && *(ch+1) != ';')
  140. {
  141. ++ch;
  142. switch (*ch)
  143. {
  144. case '\\': result.insert(result.end(), '\\'); break;
  145. case '"': result.insert(result.end(), '"'); break;
  146. case ' ': result.insert(result.end(), ' '); break;
  147. case 't': result.insert(result.end(), '\t'); break;
  148. case 'n': result.insert(result.end(), '\n'); break;
  149. case 'r': result.insert(result.end(), '\r'); break;
  150. case '0': result.insert(result.end(), '\0'); break;
  151. case '\0':
  152. {
  153. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  154. return result;
  155. }
  156. default:
  157. {
  158. std::string chStr(1, *ch);
  159. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  160. "\nin argument ", s);
  161. }
  162. }
  163. }
  164. else
  165. {
  166. result.insert(result.end(), *ch);
  167. }
  168. }
  169. return result;
  170. }
  171. void cmSystemTools::Error(const char* m1, const char* m2,
  172. const char* m3, const char* m4)
  173. {
  174. std::string message = "CMake Error: ";
  175. if(m1)
  176. {
  177. message += m1;
  178. }
  179. if(m2)
  180. {
  181. message += m2;
  182. }
  183. if(m3)
  184. {
  185. message += m3;
  186. }
  187. if(m4)
  188. {
  189. message += m4;
  190. }
  191. cmSystemTools::s_ErrorOccured = true;
  192. cmSystemTools::Message(message.c_str(),"Error");
  193. }
  194. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  195. {
  196. s_ErrorCallback = f;
  197. s_ErrorCallbackClientData = clientData;
  198. }
  199. void cmSystemTools::Message(const char* m1, const char *title)
  200. {
  201. if(s_DisableMessages)
  202. {
  203. return;
  204. }
  205. if(s_ErrorCallback)
  206. {
  207. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  208. return;
  209. }
  210. else
  211. {
  212. std::cerr << m1 << std::endl << std::flush;
  213. }
  214. }
  215. void cmSystemTools::ReportLastSystemError(const char* msg)
  216. {
  217. std::string m = msg;
  218. m += ": System Error: ";
  219. m += Superclass::GetLastSystemError();
  220. cmSystemTools::Error(m.c_str());
  221. }
  222. bool cmSystemTools::IsOn(const char* val)
  223. {
  224. if (!val)
  225. {
  226. return false;
  227. }
  228. std::basic_string<char> v = val;
  229. for(std::basic_string<char>::iterator c = v.begin();
  230. c != v.end(); c++)
  231. {
  232. *c = toupper(*c);
  233. }
  234. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  235. }
  236. bool cmSystemTools::IsNOTFOUND(const char* val)
  237. {
  238. cmsys::RegularExpression reg("-NOTFOUND$");
  239. if(reg.find(val))
  240. {
  241. return true;
  242. }
  243. return std::string("NOTFOUND") == val;
  244. }
  245. bool cmSystemTools::IsOff(const char* val)
  246. {
  247. if (!val || strlen(val) == 0)
  248. {
  249. return true;
  250. }
  251. std::basic_string<char> v = val;
  252. for(std::basic_string<char>::iterator c = v.begin();
  253. c != v.end(); c++)
  254. {
  255. *c = toupper(*c);
  256. }
  257. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  258. v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
  259. }
  260. bool cmSystemTools::RunCommand(const char* command,
  261. std::string& output,
  262. const char* dir,
  263. bool verbose,
  264. int timeout)
  265. {
  266. int dummy;
  267. return cmSystemTools::RunCommand(command, output, dummy,
  268. dir, verbose, timeout);
  269. }
  270. #if defined(WIN32) && !defined(__CYGWIN__)
  271. #include "cmWin32ProcessExecution.h"
  272. // use this for shell commands like echo and dir
  273. bool RunCommandViaWin32(const char* command,
  274. const char* dir,
  275. std::string& output,
  276. int& retVal,
  277. bool verbose,
  278. int timeout)
  279. {
  280. #if defined(__BORLANDC__)
  281. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  282. retVal,
  283. verbose, timeout,
  284. cmSystemTools::GetRunCommandHideConsole());
  285. #else // Visual studio
  286. ::SetLastError(ERROR_SUCCESS);
  287. if ( ! command )
  288. {
  289. cmSystemTools::Error("No command specified");
  290. return false;
  291. }
  292. cmWin32ProcessExecution resProc;
  293. if(cmSystemTools::GetRunCommandHideConsole())
  294. {
  295. resProc.SetHideWindows(true);
  296. }
  297. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  298. {
  299. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  300. }
  301. if ( !resProc.StartProcess(command, dir, verbose) )
  302. {
  303. return false;
  304. }
  305. resProc.Wait(timeout);
  306. output = resProc.GetOutput();
  307. retVal = resProc.GetExitValue();
  308. return true;
  309. #endif
  310. }
  311. // use this for shell commands like echo and dir
  312. bool RunCommandViaSystem(const char* command,
  313. const char* dir,
  314. std::string& output,
  315. int& retVal,
  316. bool verbose)
  317. {
  318. std::cout << "@@ " << command << std::endl;
  319. std::string commandInDir;
  320. if(dir)
  321. {
  322. commandInDir = "cd ";
  323. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  324. commandInDir += " && ";
  325. commandInDir += command;
  326. }
  327. else
  328. {
  329. commandInDir = command;
  330. }
  331. command = commandInDir.c_str();
  332. std::string commandToFile = command;
  333. commandToFile += " > ";
  334. std::string tempFile;
  335. tempFile += _tempnam(0, "cmake");
  336. commandToFile += tempFile;
  337. retVal = system(commandToFile.c_str());
  338. std::ifstream fin(tempFile.c_str());
  339. if(!fin)
  340. {
  341. if(verbose)
  342. {
  343. std::string errormsg = "RunCommand produced no output: command: \"";
  344. errormsg += command;
  345. errormsg += "\"";
  346. errormsg += "\nOutput file: ";
  347. errormsg += tempFile;
  348. cmSystemTools::Error(errormsg.c_str());
  349. }
  350. fin.close();
  351. cmSystemTools::RemoveFile(tempFile.c_str());
  352. return false;
  353. }
  354. bool multiLine = false;
  355. std::string line;
  356. while(cmSystemTools::GetLineFromStream(fin, line))
  357. {
  358. output += line;
  359. if(multiLine)
  360. {
  361. output += "\n";
  362. }
  363. multiLine = true;
  364. }
  365. fin.close();
  366. cmSystemTools::RemoveFile(tempFile.c_str());
  367. return true;
  368. }
  369. #else // We have popen
  370. bool RunCommandViaPopen(const char* command,
  371. const char* dir,
  372. std::string& output,
  373. int& retVal,
  374. bool verbose,
  375. int /*timeout*/)
  376. {
  377. // if only popen worked on windows.....
  378. std::string commandInDir;
  379. if(dir)
  380. {
  381. commandInDir = "cd \"";
  382. commandInDir += dir;
  383. commandInDir += "\" && ";
  384. commandInDir += command;
  385. }
  386. else
  387. {
  388. commandInDir = command;
  389. }
  390. commandInDir += " 2>&1";
  391. command = commandInDir.c_str();
  392. const int BUFFER_SIZE = 4096;
  393. char buffer[BUFFER_SIZE];
  394. if(verbose)
  395. {
  396. std::cout << "running " << command << std::endl;
  397. }
  398. fflush(stdout);
  399. fflush(stderr);
  400. FILE* cpipe = popen(command, "r");
  401. if(!cpipe)
  402. {
  403. return false;
  404. }
  405. fgets(buffer, BUFFER_SIZE, cpipe);
  406. while(!feof(cpipe))
  407. {
  408. if(verbose)
  409. {
  410. std::cout << buffer << std::flush;
  411. }
  412. output += buffer;
  413. fgets(buffer, BUFFER_SIZE, cpipe);
  414. }
  415. retVal = pclose(cpipe);
  416. if (WIFEXITED(retVal))
  417. {
  418. retVal = WEXITSTATUS(retVal);
  419. return true;
  420. }
  421. if (WIFSIGNALED(retVal))
  422. {
  423. retVal = WTERMSIG(retVal);
  424. cmOStringStream error;
  425. error << "\nProcess terminated due to ";
  426. switch (retVal)
  427. {
  428. #ifdef SIGKILL
  429. case SIGKILL:
  430. error << "SIGKILL";
  431. break;
  432. #endif
  433. #ifdef SIGFPE
  434. case SIGFPE:
  435. error << "SIGFPE";
  436. break;
  437. #endif
  438. #ifdef SIGBUS
  439. case SIGBUS:
  440. error << "SIGBUS";
  441. break;
  442. #endif
  443. #ifdef SIGSEGV
  444. case SIGSEGV:
  445. error << "SIGSEGV";
  446. break;
  447. #endif
  448. default:
  449. error << "signal " << retVal;
  450. break;
  451. }
  452. output += error.str();
  453. }
  454. return false;
  455. }
  456. #endif // endif WIN32 not CYGWIN
  457. // run a command unix uses popen (easy)
  458. // windows uses system and ShortPath
  459. bool cmSystemTools::RunCommand(const char* command,
  460. std::string& output,
  461. int &retVal,
  462. const char* dir,
  463. bool verbose,
  464. int timeout)
  465. {
  466. if(s_DisableRunCommandOutput)
  467. {
  468. verbose = false;
  469. }
  470. #if defined(WIN32) && !defined(__CYGWIN__)
  471. // if the command does not start with a quote, then
  472. // try to find the program, and if the program can not be
  473. // found use system to run the command as it must be a built in
  474. // shell command like echo or dir
  475. int count = 0;
  476. if(command[0] == '\"')
  477. {
  478. // count the number of quotes
  479. for(const char* s = command; *s != 0; ++s)
  480. {
  481. if(*s == '\"')
  482. {
  483. count++;
  484. if(count > 2)
  485. {
  486. break;
  487. }
  488. }
  489. }
  490. // if there are more than two double quotes use
  491. // GetShortPathName, the cmd.exe program in windows which
  492. // is used by system fails to execute if there are more than
  493. // one set of quotes in the arguments
  494. if(count > 2)
  495. {
  496. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  497. if(quoted.find(command))
  498. {
  499. std::string shortCmd;
  500. std::string cmd = quoted.match(1);
  501. std::string args = quoted.match(2);
  502. if(! cmSystemTools::FileExists(cmd.c_str()) )
  503. {
  504. shortCmd = cmd;
  505. }
  506. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  507. {
  508. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  509. return false;
  510. }
  511. shortCmd += " ";
  512. shortCmd += args;
  513. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  514. // output, retVal, verbose);
  515. //return WindowsRunCommand(shortCmd.c_str(), dir,
  516. //output, retVal, verbose);
  517. return RunCommandViaWin32(shortCmd.c_str(), dir,
  518. output, retVal, verbose, timeout);
  519. }
  520. else
  521. {
  522. cmSystemTools::Error("Could not parse command line with quotes ",
  523. command);
  524. }
  525. }
  526. }
  527. // if there is only one set of quotes or no quotes then just run the command
  528. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  529. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  530. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  531. #else
  532. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  533. #endif
  534. }
  535. bool cmSystemTools::DoesFileExistWithExtensions(
  536. const char* name,
  537. const std::vector<std::string>& headerExts)
  538. {
  539. std::string hname;
  540. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  541. ext != headerExts.end(); ++ext )
  542. {
  543. hname = name;
  544. hname += ".";
  545. hname += *ext;
  546. if(cmSystemTools::FileExists(hname.c_str()))
  547. {
  548. return true;
  549. }
  550. }
  551. return false;
  552. }
  553. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  554. {
  555. return Superclass::CopyFileAlways(source, destination);
  556. }
  557. void cmSystemTools::Glob(const char *directory, const char *regexp,
  558. std::vector<std::string>& files)
  559. {
  560. cmsys::Directory d;
  561. cmsys::RegularExpression reg(regexp);
  562. if (d.Load(directory))
  563. {
  564. size_t numf;
  565. unsigned int i;
  566. numf = d.GetNumberOfFiles();
  567. for (i = 0; i < numf; i++)
  568. {
  569. std::string fname = d.GetFile(i);
  570. if (reg.find(fname))
  571. {
  572. files.push_back(fname);
  573. }
  574. }
  575. }
  576. }
  577. void cmSystemTools::GlobDirs(const char *fullPath,
  578. std::vector<std::string>& files)
  579. {
  580. std::string path = fullPath;
  581. std::string::size_type pos = path.find("/*");
  582. if(pos == std::string::npos)
  583. {
  584. files.push_back(fullPath);
  585. return;
  586. }
  587. std::string startPath = path.substr(0, pos);
  588. std::string finishPath = path.substr(pos+2);
  589. cmsys::Directory d;
  590. if (d.Load(startPath.c_str()))
  591. {
  592. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  593. {
  594. if((std::string(d.GetFile(i)) != ".")
  595. && (std::string(d.GetFile(i)) != ".."))
  596. {
  597. std::string fname = startPath;
  598. fname +="/";
  599. fname += d.GetFile(i);
  600. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  601. {
  602. fname += finishPath;
  603. cmSystemTools::GlobDirs(fname.c_str(), files);
  604. }
  605. }
  606. }
  607. }
  608. }
  609. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  610. std::vector<std::string>& newargs)
  611. {
  612. std::vector<std::string>::const_iterator i;
  613. for(i = arguments.begin();i != arguments.end(); ++i)
  614. {
  615. cmSystemTools::ExpandListArgument(*i, newargs);
  616. }
  617. }
  618. void cmSystemTools::ExpandListArgument(const std::string& arg,
  619. std::vector<std::string>& newargs)
  620. {
  621. std::string newarg;
  622. // If argument is empty, it is an empty list.
  623. if(arg.length() == 0)
  624. {
  625. return;
  626. }
  627. // if there are no ; in the name then just copy the current string
  628. if(arg.find(';') == std::string::npos)
  629. {
  630. newargs.push_back(arg);
  631. }
  632. else
  633. {
  634. std::string::size_type start = 0;
  635. std::string::size_type endpos = 0;
  636. const std::string::size_type size = arg.size();
  637. // break up ; separated sections of the string into separate strings
  638. while(endpos != size)
  639. {
  640. endpos = arg.find(';', start);
  641. if(endpos == std::string::npos)
  642. {
  643. endpos = arg.size();
  644. }
  645. else
  646. {
  647. // skip right over escaped ; ( \; )
  648. while((endpos != std::string::npos)
  649. && (endpos > 0)
  650. && ((arg)[endpos-1] == '\\') )
  651. {
  652. endpos = arg.find(';', endpos+1);
  653. }
  654. if(endpos == std::string::npos)
  655. {
  656. endpos = arg.size();
  657. }
  658. }
  659. std::string::size_type len = endpos - start;
  660. if (len > 0)
  661. {
  662. // check for a closing ] after the start position
  663. if(arg.find('[', start) == std::string::npos)
  664. {
  665. // if there is no [ in the string then keep it
  666. newarg = arg.substr(start, len);
  667. }
  668. else
  669. {
  670. int opencount = 0;
  671. int closecount = 0;
  672. for(std::string::size_type j = start; j < endpos; ++j)
  673. {
  674. if(arg.at(j) == '[')
  675. {
  676. ++opencount;
  677. }
  678. else if (arg.at(j) == ']')
  679. {
  680. ++closecount;
  681. }
  682. }
  683. if(opencount != closecount)
  684. {
  685. // skip this one
  686. endpos = arg.find(';', endpos+1);
  687. if(endpos == std::string::npos)
  688. {
  689. endpos = arg.size();
  690. }
  691. len = endpos - start;
  692. }
  693. newarg = arg.substr(start, len);
  694. }
  695. std::string::size_type pos = newarg.find("\\;");
  696. if(pos != std::string::npos)
  697. {
  698. newarg.erase(pos, 1);
  699. }
  700. newargs.push_back(newarg);
  701. }
  702. start = endpos+1;
  703. }
  704. }
  705. }
  706. bool cmSystemTools::SimpleGlob(const std::string& glob,
  707. std::vector<std::string>& files,
  708. int type /* = 0 */)
  709. {
  710. files.clear();
  711. if ( glob[glob.size()-1] != '*' )
  712. {
  713. return false;
  714. }
  715. std::string path = cmSystemTools::GetFilenamePath(glob);
  716. std::string ppath = cmSystemTools::GetFilenameName(glob);
  717. ppath = ppath.substr(0, ppath.size()-1);
  718. if ( path.size() == 0 )
  719. {
  720. path = "/";
  721. }
  722. bool res = false;
  723. cmsys::Directory d;
  724. if (d.Load(path.c_str()))
  725. {
  726. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  727. {
  728. if((std::string(d.GetFile(i)) != ".")
  729. && (std::string(d.GetFile(i)) != ".."))
  730. {
  731. std::string fname = path;
  732. if ( path[path.size()-1] != '/' )
  733. {
  734. fname +="/";
  735. }
  736. fname += d.GetFile(i);
  737. std::string sfname = d.GetFile(i);
  738. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  739. {
  740. continue;
  741. }
  742. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  743. {
  744. continue;
  745. }
  746. if ( sfname.size() >= ppath.size() &&
  747. sfname.substr(0, ppath.size()) ==
  748. ppath )
  749. {
  750. files.push_back(fname);
  751. res = true;
  752. }
  753. }
  754. }
  755. }
  756. return res;
  757. }
  758. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  759. {
  760. if ( ! cext || *cext == 0 )
  761. {
  762. return cmSystemTools::NO_FILE_FORMAT;
  763. }
  764. //std::string ext = cmSystemTools::LowerCase(cext);
  765. std::string ext = cext;
  766. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  767. if (
  768. ext == "C" || ext == ".C" ||
  769. ext == "M" || ext == ".M" ||
  770. ext == "c++" || ext == ".c++" ||
  771. ext == "cc" || ext == ".cc" ||
  772. ext == "cpp" || ext == ".cpp" ||
  773. ext == "cxx" || ext == ".cxx" ||
  774. ext == "m" || ext == ".m" ||
  775. ext == "mm" || ext == ".mm"
  776. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  777. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  778. if (
  779. ext == "H" || ext == ".H" ||
  780. ext == "h" || ext == ".h" ||
  781. ext == "h++" || ext == ".h++" ||
  782. ext == "hm" || ext == ".hm" ||
  783. ext == "hpp" || ext == ".hpp" ||
  784. ext == "hxx" || ext == ".hxx" ||
  785. ext == "in" || ext == ".in" ||
  786. ext == "txx" || ext == ".txx"
  787. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  788. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  789. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  790. if ( ext == "lib" || ext == ".lib" ||
  791. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  792. if ( ext == "o" || ext == ".o" ||
  793. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  794. #ifdef __APPLE__
  795. if ( ext == "dylib" || ext == ".dylib" )
  796. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  797. if ( ext == "so" || ext == ".so" ||
  798. ext == "bundle" || ext == ".bundle" )
  799. { return cmSystemTools::MODULE_FILE_FORMAT; }
  800. #else // __APPLE__
  801. if ( ext == "so" || ext == ".so" ||
  802. ext == "sl" || ext == ".sl" ||
  803. ext == "dll" || ext == ".dll" )
  804. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  805. #endif // __APPLE__
  806. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  807. }
  808. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  809. {
  810. std::vector<std::string> temp;
  811. if(!Superclass::Split(s, temp))
  812. {
  813. return false;
  814. }
  815. for(std::vector<std::string>::const_iterator i = temp.begin();
  816. i != temp.end(); ++i)
  817. {
  818. l.push_back(*i);
  819. }
  820. return true;
  821. }