cmSystemTools.cxx 23 KB

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