cmSystemTools.cxx 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "errno.h"
  15. #include "stdio.h"
  16. #include <sys/stat.h>
  17. #include "cmRegularExpression.h"
  18. #include <ctype.h>
  19. #include "cmDirectory.h"
  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(_MSC_VER) || defined(__BORLANDC__)
  28. #include <string.h>
  29. #include <windows.h>
  30. #include <direct.h>
  31. #define _unlink unlink
  32. inline int Mkdir(const char* dir)
  33. {
  34. return _mkdir(dir);
  35. }
  36. inline const char* Getcwd(char* buf, unsigned int len)
  37. {
  38. return _getcwd(buf, len);
  39. }
  40. inline int Chdir(const char* dir)
  41. {
  42. #if defined(__BORLANDC__)
  43. return chdir(dir);
  44. #else
  45. return _chdir(dir);
  46. #endif
  47. }
  48. #else
  49. #include <sys/types.h>
  50. #include <fcntl.h>
  51. #include <unistd.h>
  52. inline int Mkdir(const char* dir)
  53. {
  54. return mkdir(dir, 00777);
  55. }
  56. inline const char* Getcwd(char* buf, unsigned int len)
  57. {
  58. return getcwd(buf, len);
  59. }
  60. inline int Chdir(const char* dir)
  61. {
  62. return chdir(dir);
  63. }
  64. #endif
  65. bool cmSystemTools::s_DisableRunCommandOutput = false;
  66. bool cmSystemTools::s_ErrorOccured = false;
  67. bool cmSystemTools::s_DisableMessages = false;
  68. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&);
  69. // adds the elements of the env variable path to the arg passed in
  70. void cmSystemTools::GetPath(std::vector<std::string>& path)
  71. {
  72. #if defined(_WIN32) && !defined(__CYGWIN__)
  73. const char* pathSep = ";";
  74. #else
  75. const char* pathSep = ":";
  76. #endif
  77. std::string pathEnv = getenv("PATH");
  78. // A hack to make the below algorithm work.
  79. if(pathEnv[pathEnv.length()-1] != ':')
  80. {
  81. pathEnv += pathSep;
  82. }
  83. std::string::size_type start =0;
  84. bool done = false;
  85. while(!done)
  86. {
  87. std::string::size_type endpos = pathEnv.find(pathSep, start);
  88. if(endpos != std::string::npos)
  89. {
  90. path.push_back(pathEnv.substr(start, endpos-start));
  91. start = endpos+1;
  92. }
  93. else
  94. {
  95. done = true;
  96. }
  97. }
  98. for(std::vector<std::string>::iterator i = path.begin();
  99. i != path.end(); ++i)
  100. {
  101. cmSystemTools::ConvertToUnixSlashes(*i);
  102. }
  103. }
  104. const char* cmSystemTools::GetExecutableExtension()
  105. {
  106. #if defined(_WIN32) || defined(__CYGWIN__)
  107. return ".exe";
  108. #else
  109. return "";
  110. #endif
  111. }
  112. bool cmSystemTools::MakeDirectory(const char* path)
  113. {
  114. if(cmSystemTools::FileExists(path))
  115. {
  116. return true;
  117. }
  118. std::string dir = path;
  119. if(dir.size() == 0)
  120. {
  121. return false;
  122. }
  123. cmSystemTools::ConvertToUnixSlashes(dir);
  124. std::string::size_type pos = dir.find(':');
  125. if(pos == std::string::npos)
  126. {
  127. pos = 0;
  128. }
  129. std::string topdir;
  130. while((pos = dir.find('/', pos)) != std::string::npos)
  131. {
  132. topdir = dir.substr(0, pos);
  133. Mkdir(topdir.c_str());
  134. pos++;
  135. }
  136. if(dir[dir.size()-1] == '/')
  137. {
  138. topdir = dir.substr(0, dir.size());
  139. }
  140. else
  141. {
  142. topdir = dir;
  143. }
  144. if(Mkdir(topdir.c_str()) != 0)
  145. {
  146. // There is a bug in the Borland Run time library which makes MKDIR
  147. // return EACCES when it should return EEXISTS
  148. // if it is some other error besides directory exists
  149. // then return false
  150. if( (errno != EEXIST)
  151. #ifdef __BORLANDC__
  152. && (errno != EACCES)
  153. #endif
  154. )
  155. {
  156. cmSystemTools::Error("Faild to create directory:", path);
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. // replace replace with with as many times as it shows up in source.
  163. // write the result into source.
  164. void cmSystemTools::ReplaceString(std::string& source,
  165. const char* replace,
  166. const char* with)
  167. {
  168. std::string::size_type lengthReplace = strlen(replace);
  169. std::string rest;
  170. std::string::size_type start = source.find(replace);
  171. while(start != std::string::npos)
  172. {
  173. rest = source.substr(start+lengthReplace);
  174. source = source.substr(0, start);
  175. source += with;
  176. source += rest;
  177. start = source.find(replace, start + lengthReplace );
  178. }
  179. }
  180. #if defined(_WIN32) && !defined(__CYGWIN__)
  181. // Get the data of key value.
  182. // Example :
  183. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  184. // => will return the data of the "default" value of the key
  185. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  186. // => will return the data of the "Root" value of the key
  187. bool ReadAValue(std::string &res, const char *key)
  188. {
  189. // find the primary key
  190. std::string primary = key;
  191. std::string second;
  192. std::string valuename;
  193. size_t start = primary.find("\\");
  194. if (start == std::string::npos)
  195. {
  196. return false;
  197. }
  198. size_t valuenamepos = primary.find(";");
  199. if (valuenamepos != std::string::npos)
  200. {
  201. valuename = primary.substr(valuenamepos+1);
  202. }
  203. second = primary.substr(start+1, valuenamepos-start-1);
  204. primary = primary.substr(0, start);
  205. HKEY primaryKey;
  206. if (primary == "HKEY_CURRENT_USER")
  207. {
  208. primaryKey = HKEY_CURRENT_USER;
  209. }
  210. if (primary == "HKEY_CURRENT_CONFIG")
  211. {
  212. primaryKey = HKEY_CURRENT_CONFIG;
  213. }
  214. if (primary == "HKEY_CLASSES_ROOT")
  215. {
  216. primaryKey = HKEY_CLASSES_ROOT;
  217. }
  218. if (primary == "HKEY_LOCAL_MACHINE")
  219. {
  220. primaryKey = HKEY_LOCAL_MACHINE;
  221. }
  222. if (primary == "HKEY_USERS")
  223. {
  224. primaryKey = HKEY_USERS;
  225. }
  226. HKEY hKey;
  227. if(RegOpenKeyEx(primaryKey, second.c_str(),
  228. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  229. {
  230. return false;
  231. }
  232. else
  233. {
  234. DWORD dwType, dwSize;
  235. dwSize = 1023;
  236. char data[1024];
  237. if(RegQueryValueEx(hKey, (LPTSTR)valuename.c_str(), NULL, &dwType,
  238. (BYTE *)data, &dwSize) == ERROR_SUCCESS)
  239. {
  240. if (dwType == REG_SZ)
  241. {
  242. res = data;
  243. return true;
  244. }
  245. }
  246. }
  247. return false;
  248. }
  249. #endif
  250. // replace replace with with as many times as it shows up in source.
  251. // write the result into source.
  252. void cmSystemTools::ExpandRegistryValues(std::string& source)
  253. {
  254. #if defined(_WIN32) && !defined(__CYGWIN__)
  255. // Regular expression to match anything inside [...] that begins in HKEY.
  256. // Note that there is a special rule for regular expressions to match a
  257. // close square-bracket inside a list delimited by square brackets.
  258. // The "[^]]" part of this expression will match any character except
  259. // a close square-bracket. The ']' character must be the first in the
  260. // list of characters inside the [^...] block of the expression.
  261. cmRegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  262. // check for black line or comment
  263. while (regEntry.find(source))
  264. {
  265. // the arguments are the second match
  266. std::string key = regEntry.match(1);
  267. std::string val;
  268. if (ReadAValue(val,key.c_str()))
  269. {
  270. std::string reg = "[";
  271. reg += key + "]";
  272. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  273. }
  274. else
  275. {
  276. std::string reg = "[";
  277. reg += key + "]";
  278. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  279. }
  280. }
  281. #endif
  282. }
  283. std::string cmSystemTools::EscapeQuotes(const char* str)
  284. {
  285. std::string result = "";
  286. for(const char* ch = str; *ch != '\0'; ++ch)
  287. {
  288. if(*ch == '"')
  289. {
  290. result += '\\';
  291. }
  292. result += *ch;
  293. }
  294. return result;
  295. }
  296. bool cmSystemTools::SameFile(const char* file1, const char* file2)
  297. {
  298. struct stat fileStat1, fileStat2;
  299. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  300. {
  301. // see if the files are the same file
  302. // check the device inode and size
  303. if(fileStat2.st_dev == fileStat1.st_dev &&
  304. fileStat2.st_ino == fileStat1.st_ino &&
  305. fileStat2.st_size == fileStat1.st_size
  306. )
  307. {
  308. return true;
  309. }
  310. }
  311. return false;
  312. }
  313. // return true if the file exists
  314. bool cmSystemTools::FileExists(const char* filename)
  315. {
  316. struct stat fs;
  317. if (stat(filename, &fs) != 0)
  318. {
  319. return false;
  320. }
  321. else
  322. {
  323. return true;
  324. }
  325. }
  326. // Return a capitalized string (i.e the first letter is uppercased, all other
  327. // are lowercased)
  328. std::string cmSystemTools::Capitalized(const std::string& s)
  329. {
  330. std::string n;
  331. n.resize(s.size());
  332. n[0] = toupper(s[0]);
  333. for (size_t i = 1; i < s.size(); i++)
  334. {
  335. n[i] = tolower(s[i]);
  336. }
  337. return n;
  338. }
  339. // Return a lower case string
  340. std::string cmSystemTools::LowerCase(const std::string& s)
  341. {
  342. std::string n;
  343. n.resize(s.size());
  344. for (size_t i = 0; i < s.size(); i++)
  345. {
  346. n[i] = tolower(s[i]);
  347. }
  348. return n;
  349. }
  350. // Return a lower case string
  351. std::string cmSystemTools::UpperCase(const std::string& s)
  352. {
  353. std::string n;
  354. n.resize(s.size());
  355. for (size_t i = 0; i < s.size(); i++)
  356. {
  357. n[i] = toupper(s[i]);
  358. }
  359. return n;
  360. }
  361. // convert windows slashes to unix slashes
  362. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  363. {
  364. std::string::size_type pos = 0;
  365. while((pos = path.find('\\', pos)) != std::string::npos)
  366. {
  367. path[pos] = '/';
  368. pos++;
  369. }
  370. // remove any trailing slash
  371. if(path.size() && path[path.size()-1] == '/')
  372. {
  373. path = path.substr(0, path.size()-1);
  374. }
  375. // if there is a tilda ~ then replace it with HOME
  376. if(path.find("~") == 0)
  377. {
  378. if (getenv("HOME"))
  379. {
  380. path = std::string(getenv("HOME")) + path.substr(1);
  381. }
  382. }
  383. // if there is a /tmp_mnt in a path get rid of it!
  384. // stupid sgi's
  385. if(path.find("/tmp_mnt") == 0)
  386. {
  387. path = path.substr(8);
  388. }
  389. }
  390. // change // to /, and escape any spaces in the path
  391. std::string cmSystemTools::ConvertToUnixOutputPath(const char* path)
  392. {
  393. std::string ret = path;
  394. // remove // except at the beginning might be a cygwin drive
  395. std::string::size_type pos = 1;
  396. while((pos = ret.find("//", pos)) != std::string::npos)
  397. {
  398. ret.erase(pos, 1);
  399. }
  400. // now escape spaces if there is a space in the path
  401. if(ret.find(" ") != std::string::npos)
  402. {
  403. std::string result = "";
  404. char lastch = 1;
  405. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  406. {
  407. // if it is already escaped then don't try to escape it again
  408. if(*ch == ' ' && lastch != '\\')
  409. {
  410. result += '\\';
  411. }
  412. result += *ch;
  413. lastch = *ch;
  414. }
  415. ret = result;
  416. }
  417. return ret;
  418. }
  419. std::string cmSystemTools::EscapeSpaces(const char* str)
  420. {
  421. #if defined(_WIN32) && !defined(__CYGWIN__)
  422. std::string result;
  423. // if there are spaces
  424. std::string temp = str;
  425. if (temp.find(" ") != std::string::npos &&
  426. temp.find("\"")==std::string::npos)
  427. {
  428. result = "\"";
  429. result += str;
  430. result += "\"";
  431. return result;
  432. }
  433. return str;
  434. #else
  435. std::string result = "";
  436. for(const char* ch = str; *ch != '\0'; ++ch)
  437. {
  438. if(*ch == ' ')
  439. {
  440. result += '\\';
  441. }
  442. result += *ch;
  443. }
  444. return result;
  445. #endif
  446. }
  447. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  448. {
  449. #if defined(_WIN32) && !defined(__CYGWIN__)
  450. return cmSystemTools::ConvertToWindowsOutputPath(path);
  451. #else
  452. return cmSystemTools::ConvertToUnixOutputPath(path);
  453. #endif
  454. }
  455. // remove double slashes not at the start
  456. std::string cmSystemTools::ConvertToWindowsOutputPath(const char* path)
  457. {
  458. std::string ret = path;
  459. std::string::size_type pos = 0;
  460. // first convert all of the slashes
  461. while((pos = ret.find('/', pos)) != std::string::npos)
  462. {
  463. ret[pos] = '\\';
  464. pos++;
  465. }
  466. // check for really small paths
  467. if(ret.size() < 2)
  468. {
  469. return ret;
  470. }
  471. // now clean up a bit and remove double slashes
  472. // Only if it is not the first position in the path which is a network
  473. // path on windows
  474. pos = 1; // start at position 1
  475. while((pos = ret.find("\\\\", pos)) != std::string::npos)
  476. {
  477. ret.erase(pos, 1);
  478. }
  479. // now double quote the path if it has spaces in it
  480. // and is not already double quoted
  481. if(ret.find(" ") != std::string::npos
  482. && ret[0] != '\"')
  483. {
  484. std::string result;
  485. result = "\"" + ret + "\"";
  486. ret = result;
  487. }
  488. return ret;
  489. }
  490. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  491. std::string& name,
  492. std::vector<std::string>& arguments,
  493. const char* filename,
  494. bool& parseError)
  495. {
  496. parseError = false;
  497. name = "";
  498. arguments = std::vector<std::string>();
  499. const int BUFFER_SIZE = 4096;
  500. char inbuffer[BUFFER_SIZE];
  501. if(!fin)
  502. {
  503. return false;
  504. }
  505. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  506. {
  507. cmRegularExpression blankLine("^[ \t\r]*$");
  508. cmRegularExpression comment("^[ \t]*#.*$");
  509. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t\r]*$");
  510. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  511. cmRegularExpression lastLine("^(.*)\\)[ \t\r]*$");
  512. // check for black line or comment
  513. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  514. {
  515. return false;
  516. }
  517. // look for a oneline fun(arg arg2)
  518. else if(oneLiner.find(inbuffer))
  519. {
  520. // the arguments are the second match
  521. std::string args = oneLiner.match(2);
  522. name = oneLiner.match(1);
  523. // break up the arguments
  524. cmSystemTools::GetArguments(args, arguments);
  525. return true;
  526. }
  527. // look for a start of a multiline with no trailing ")" fun(arg arg2
  528. else if(multiLine.find(inbuffer))
  529. {
  530. name = multiLine.match(1);
  531. std::string args = multiLine.match(2);
  532. cmSystemTools::GetArguments(args, arguments);
  533. // Read lines until the closing paren is hit
  534. bool done = false;
  535. while(!done)
  536. {
  537. // read lines until the end paren is found
  538. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  539. {
  540. // Check for comment lines and ignore them.
  541. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  542. { continue; }
  543. // Is this the last line?
  544. if(lastLine.find(inbuffer))
  545. {
  546. done = true;
  547. std::string args = lastLine.match(1);
  548. cmSystemTools::GetArguments(args, arguments);
  549. }
  550. else
  551. {
  552. std::string line = inbuffer;
  553. cmSystemTools::GetArguments(line, arguments);
  554. }
  555. }
  556. else
  557. {
  558. parseError = true;
  559. cmSystemTools::Error("Parse error in read function missing end )\nIn File: ",
  560. filename, "\nCurrent line:", inbuffer);
  561. return false;
  562. }
  563. }
  564. return true;
  565. }
  566. else
  567. {
  568. parseError = true;
  569. cmSystemTools::Error("Parse error in read function\nIn file:",
  570. filename, "\nCurrent line:", inbuffer);
  571. return false;
  572. }
  573. }
  574. return false;
  575. }
  576. void cmSystemTools::GetArguments(std::string& line,
  577. std::vector<std::string>& arguments)
  578. {
  579. // Match a normal argument (not quoted, no spaces).
  580. cmRegularExpression normalArgument("[ \t]*(([^ \t\r\\]|[\\].)+)[ \t\r]*");
  581. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  582. cmRegularExpression quotedArgument("[ \t]*(\"([^\"\\]|[\\].)*\")[ \t\r]*");
  583. bool done = false;
  584. while(!done)
  585. {
  586. std::string arg;
  587. std::string::size_type endpos;
  588. bool foundQuoted = quotedArgument.find(line.c_str());
  589. bool foundNormal = normalArgument.find(line.c_str());
  590. if(foundQuoted && foundNormal)
  591. {
  592. // Both matches were found. Take the earlier one.
  593. // Favor double-quoted version if there is a tie.
  594. if(normalArgument.start(1) < quotedArgument.start(1))
  595. {
  596. arg = normalArgument.match(1);
  597. endpos = normalArgument.end(1);
  598. }
  599. else
  600. {
  601. arg = quotedArgument.match(1);
  602. endpos = quotedArgument.end(1);
  603. // Strip off the double quotes on the ends.
  604. arg = arg.substr(1, arg.length()-2);
  605. }
  606. }
  607. else if (foundQuoted)
  608. {
  609. arg = quotedArgument.match(1);
  610. endpos = quotedArgument.end(1);
  611. // Strip off the double quotes on the ends.
  612. arg = arg.substr(1, arg.length()-2);
  613. }
  614. else if(foundNormal)
  615. {
  616. arg = normalArgument.match(1);
  617. endpos = normalArgument.end(1);
  618. }
  619. else
  620. {
  621. done = true;
  622. }
  623. if(!done)
  624. {
  625. arguments.push_back(cmSystemTools::RemoveEscapes(arg.c_str()));
  626. line = line.substr(endpos, line.length() - endpos);
  627. }
  628. }
  629. }
  630. std::string cmSystemTools::RemoveEscapes(const char* s)
  631. {
  632. std::string result = "";
  633. for(const char* ch = s; *ch; ++ch)
  634. {
  635. if(*ch == '\\')
  636. {
  637. ++ch;
  638. switch (*ch)
  639. {
  640. case '\\': result.insert(result.end(), '\\'); break;
  641. case '"': result.insert(result.end(), '"'); break;
  642. case ' ': result.insert(result.end(), ' '); break;
  643. case 't': result.insert(result.end(), '\t'); break;
  644. case 'n': result.insert(result.end(), '\n'); break;
  645. case 'r': result.insert(result.end(), '\r'); break;
  646. case '0': result.insert(result.end(), '\0'); break;
  647. case '\0':
  648. {
  649. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  650. return result;
  651. }
  652. default:
  653. {
  654. std::string chStr(1, *ch);
  655. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  656. "\nin argument ", s);
  657. }
  658. }
  659. }
  660. else
  661. {
  662. result.insert(result.end(), *ch);
  663. }
  664. }
  665. return result;
  666. }
  667. void cmSystemTools::Error(const char* m1, const char* m2,
  668. const char* m3, const char* m4)
  669. {
  670. std::string message = "CMake Error: ";
  671. if(m1)
  672. {
  673. message += m1;
  674. }
  675. if(m2)
  676. {
  677. message += m2;
  678. }
  679. if(m3)
  680. {
  681. message += m3;
  682. }
  683. if(m4)
  684. {
  685. message += m4;
  686. }
  687. cmSystemTools::s_ErrorOccured = true;
  688. cmSystemTools::Message(message.c_str(),"Error");
  689. }
  690. void cmSystemTools::SetErrorCallback(ErrorCallback f)
  691. {
  692. s_ErrorCallback = f;
  693. }
  694. void cmSystemTools::Message(const char* m1, const char *title)
  695. {
  696. if(s_DisableMessages)
  697. {
  698. return;
  699. }
  700. if(s_ErrorCallback)
  701. {
  702. (*s_ErrorCallback)(m1, title, s_DisableMessages);
  703. return;
  704. }
  705. else
  706. {
  707. std::cerr << m1 << std::endl;
  708. }
  709. }
  710. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  711. const char* destination)
  712. {
  713. if(cmSystemTools::FilesDiffer(source, destination))
  714. {
  715. cmSystemTools::cmCopyFile(source, destination);
  716. return true;
  717. }
  718. return false;
  719. }
  720. bool cmSystemTools::FilesDiffer(const char* source,
  721. const char* destination)
  722. {
  723. struct stat statSource;
  724. if (stat(source, &statSource) != 0)
  725. {
  726. return true;
  727. }
  728. struct stat statDestination;
  729. if (stat(destination, &statDestination) != 0)
  730. {
  731. return true;
  732. }
  733. if(statSource.st_size != statDestination.st_size)
  734. {
  735. return true;
  736. }
  737. std::ifstream finSource(source);
  738. std::ifstream finDestination(destination);
  739. if(!finSource || !finDestination)
  740. {
  741. return true;
  742. }
  743. const int buffer_length = 4096;
  744. char bufferSource[buffer_length];
  745. char bufferDest[buffer_length];
  746. while(finSource && finDestination)
  747. {
  748. if(finSource.getline(bufferSource, buffer_length, '\n')
  749. || finSource.gcount())
  750. {
  751. if(finDestination.getline(bufferDest, buffer_length, '\n')
  752. || finDestination.gcount())
  753. {
  754. // both if statements passed
  755. if(finSource.eof())
  756. {
  757. if(!finDestination.eof())
  758. {
  759. return true;
  760. }
  761. if(finSource.gcount() != finDestination.gcount())
  762. {
  763. return true;
  764. }
  765. if(strncmp(bufferSource, bufferDest, finSource.gcount()) != 0)
  766. {
  767. return true;
  768. }
  769. }
  770. else if(finSource.fail())
  771. {
  772. if(!finDestination.fail())
  773. {
  774. return true;
  775. }
  776. if(strcmp(bufferSource, bufferDest) != 0)
  777. {
  778. return true;
  779. }
  780. finSource.clear(finSource.rdstate() & ~std::ios::failbit);
  781. finDestination.clear(finDestination.rdstate() & ~std::ios::failbit);
  782. }
  783. else
  784. {
  785. if(strcmp(bufferSource, bufferDest) != 0)
  786. {
  787. return true;
  788. }
  789. }
  790. }
  791. else
  792. {
  793. return true;
  794. }
  795. }
  796. }
  797. return false;
  798. }
  799. /**
  800. * Copy a file named by "source" to the file named by "destination".
  801. */
  802. void cmSystemTools::cmCopyFile(const char* source,
  803. const char* destination)
  804. {
  805. const int bufferSize = 4096;
  806. char buffer[bufferSize];
  807. std::ifstream fin(source,
  808. #ifdef _WIN32
  809. std::ios::binary |
  810. #endif
  811. std::ios::in);
  812. if(!fin)
  813. {
  814. cmSystemTools::Error("CopyFile failed to open input file \"",
  815. source, "\"");
  816. return;
  817. }
  818. // If destination is a directory, try to create a file with the same
  819. // name as the source in that directory.
  820. const char* dest = destination;
  821. std::string new_destination;
  822. if(cmSystemTools::FileExists(destination) &&
  823. cmSystemTools::FileIsDirectory(destination))
  824. {
  825. new_destination = destination;
  826. cmSystemTools::ConvertToUnixSlashes(new_destination);
  827. new_destination += '/';
  828. std::string source_name = source;
  829. new_destination += cmSystemTools::GetFilenameName(source_name);
  830. dest = new_destination.c_str();
  831. }
  832. // Create destination directory
  833. std::string destination_dir = dest;
  834. destination_dir = cmSystemTools::GetFilenamePath(destination_dir);
  835. cmSystemTools::MakeDirectory(destination_dir.c_str());
  836. std::ofstream fout(dest,
  837. #ifdef _WIN32
  838. std::ios::binary |
  839. #endif
  840. std::ios::out | std::ios::trunc);
  841. if(!fout)
  842. {
  843. cmSystemTools::Error("CopyFile failed to open output file \"",
  844. dest, "\"");
  845. return;
  846. }
  847. // This copy loop is very sensitive on certain platforms with
  848. // slightly broken stream libraries (like HPUX). Normally, it is
  849. // incorrect to not check the error condition on the fin.read()
  850. // before using the data, but the fin.gcount() will be zero if an
  851. // error occurred. Therefore, the loop should be safe everywhere.
  852. while(fin)
  853. {
  854. fin.read(buffer, bufferSize);
  855. if(fin.gcount())
  856. {
  857. fout.write(buffer, fin.gcount());
  858. }
  859. }
  860. }
  861. // return true if the file exists
  862. long int cmSystemTools::ModifiedTime(const char* filename)
  863. {
  864. struct stat fs;
  865. if (stat(filename, &fs) != 0)
  866. {
  867. return 0;
  868. }
  869. else
  870. {
  871. return (long int)fs.st_mtime;
  872. }
  873. }
  874. bool cmSystemTools::RemoveFile(const char* source)
  875. {
  876. return unlink(source) != 0 ? false : true;
  877. }
  878. bool cmSystemTools::IsOn(const char* val)
  879. {
  880. if (!val)
  881. {
  882. return false;
  883. }
  884. std::basic_string<char> v = val;
  885. for(std::basic_string<char>::iterator c = v.begin();
  886. c != v.end(); c++)
  887. {
  888. *c = toupper(*c);
  889. }
  890. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  891. }
  892. bool cmSystemTools::IsOff(const char* val)
  893. {
  894. if (!val || strlen(val) == 0)
  895. {
  896. return true;
  897. }
  898. std::basic_string<char> v = val;
  899. for(std::basic_string<char>::iterator c = v.begin();
  900. c != v.end(); c++)
  901. {
  902. *c = toupper(*c);
  903. }
  904. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  905. v == "N" || v == "NOTFOUND" || v == "IGNORE");
  906. }
  907. bool cmSystemTools::RunCommand(const char* command,
  908. std::string& output,
  909. const char* dir,
  910. bool verbose)
  911. {
  912. int foo;
  913. return cmSystemTools::RunCommand(command, output, foo, dir, verbose);
  914. }
  915. #if defined(WIN32) && !defined(__CYGWIN__)
  916. // use this for shell commands like echo and dir
  917. bool RunCommandViaSystem(const char* command,
  918. const char* dir,
  919. std::string& output,
  920. int& retVal,
  921. bool verbose)
  922. {
  923. const int BUFFER_SIZE = 4096;
  924. char buffer[BUFFER_SIZE];
  925. std::string commandInDir;
  926. if(dir)
  927. {
  928. commandInDir = "cd ";
  929. commandInDir += dir;
  930. commandInDir += " && ";
  931. commandInDir += command;
  932. }
  933. else
  934. {
  935. commandInDir = command;
  936. }
  937. command = commandInDir.c_str();
  938. std::string commandToFile = command;
  939. commandToFile += " > ";
  940. std::string tempFile;
  941. tempFile += _tempnam(0, "cmake");
  942. commandToFile += tempFile;
  943. retVal = system(commandToFile.c_str());
  944. std::ifstream fin(tempFile.c_str());
  945. if(!fin)
  946. {
  947. if(verbose)
  948. {
  949. std::string errormsg = "RunCommand produced no output: command: \"";
  950. errormsg += command;
  951. errormsg += "\"";
  952. errormsg += "\nOutput file: ";
  953. errormsg += tempFile;
  954. cmSystemTools::Error(errormsg.c_str());
  955. }
  956. fin.close();
  957. cmSystemTools::RemoveFile(tempFile.c_str());
  958. return false;
  959. }
  960. bool multiLine = false;
  961. while(fin)
  962. {
  963. fin.getline(buffer, BUFFER_SIZE);
  964. output += buffer;
  965. if(multiLine)
  966. {
  967. output += "\n";
  968. }
  969. multiLine = true;
  970. }
  971. fin.close();
  972. cmSystemTools::RemoveFile(tempFile.c_str());
  973. return true;
  974. }
  975. #endif // endif WIN32 not CYGWIN
  976. // run a command unix uses popen (easy)
  977. // windows uses system and ShortPath
  978. bool cmSystemTools::RunCommand(const char* command,
  979. std::string& output,
  980. int &retVal,
  981. const char* dir,
  982. bool verbose)
  983. {
  984. if(s_DisableRunCommandOutput)
  985. {
  986. verbose = false;
  987. }
  988. #if defined(WIN32) && !defined(__CYGWIN__)
  989. // if the command does not start with a quote, then
  990. // try to find the program, and if the program can not be
  991. // found use system to run the command as it must be a built in
  992. // shell command like echo or dir
  993. int count = 0;
  994. if(command[0] == '\"')
  995. {
  996. // count the number of quotes
  997. for(const char* s = command; *s != 0; ++s)
  998. {
  999. if(*s == '\"')
  1000. {
  1001. count++;
  1002. if(count > 2)
  1003. {
  1004. break;
  1005. }
  1006. }
  1007. }
  1008. // if there are more than two double quotes use
  1009. // GetShortPathName, the cmd.exe program in windows which
  1010. // is used by system fails to execute if there are more than
  1011. // one set of quotes in the arguments
  1012. if(count > 2)
  1013. {
  1014. cmRegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  1015. if(quoted.find(command))
  1016. {
  1017. std::string shortCmd;
  1018. std::string cmd = quoted.match(1);
  1019. std::string args = quoted.match(2);
  1020. char *buffer = new char[cmd.size()+1];
  1021. if(GetShortPathName(cmd.c_str(), buffer,
  1022. int(cmd.size())) != 0)
  1023. {
  1024. shortCmd = buffer;
  1025. shortCmd += " ";
  1026. shortCmd += args;
  1027. }
  1028. else
  1029. {
  1030. cmSystemTools::Error("Could not get GetShortPathName for ",
  1031. cmd.c_str());
  1032. return false;
  1033. }
  1034. return RunCommandViaSystem(shortCmd.c_str(), dir,
  1035. output, retVal, verbose);
  1036. }
  1037. else
  1038. {
  1039. cmSystemTools::Error("Could not parse command line with quotes ",
  1040. command);
  1041. }
  1042. }
  1043. }
  1044. // if there is only one set of quotes or no quotes then just run the command
  1045. return RunCommandViaSystem(command, dir, output, retVal, verbose);
  1046. #else
  1047. // if only popen worked on windows.....
  1048. std::string commandInDir;
  1049. if(dir)
  1050. {
  1051. commandInDir = "cd ";
  1052. commandInDir += dir;
  1053. commandInDir += " && ";
  1054. commandInDir += command;
  1055. }
  1056. else
  1057. {
  1058. commandInDir = command;
  1059. }
  1060. command = commandInDir.c_str();
  1061. const int BUFFER_SIZE = 4096;
  1062. char buffer[BUFFER_SIZE];
  1063. if(verbose)
  1064. {
  1065. std::cout << "running " << command << std::endl;
  1066. }
  1067. fflush(stdout);
  1068. fflush(stderr);
  1069. FILE* cpipe = popen(command, "r");
  1070. if(!cpipe)
  1071. {
  1072. return false;
  1073. }
  1074. fgets(buffer, BUFFER_SIZE, cpipe);
  1075. while(!feof(cpipe))
  1076. {
  1077. if(verbose)
  1078. {
  1079. std::cout << buffer << std::flush;
  1080. }
  1081. output += buffer;
  1082. fgets(buffer, BUFFER_SIZE, cpipe);
  1083. }
  1084. retVal = pclose(cpipe);
  1085. retVal = WEXITSTATUS(retVal);
  1086. return true;
  1087. #endif
  1088. }
  1089. /**
  1090. * Find the file the given name. Searches the given path and then
  1091. * the system search path. Returns the full path to the file if it is
  1092. * found. Otherwise, the empty string is returned.
  1093. */
  1094. std::string cmSystemTools::FindFile(const char* name,
  1095. const std::vector<std::string>& userPaths)
  1096. {
  1097. // Add the system search path to our path.
  1098. std::vector<std::string> path = userPaths;
  1099. cmSystemTools::GetPath(path);
  1100. std::string tryPath;
  1101. for(std::vector<std::string>::const_iterator p = path.begin();
  1102. p != path.end(); ++p)
  1103. {
  1104. tryPath = *p;
  1105. tryPath += "/";
  1106. tryPath += name;
  1107. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1108. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1109. {
  1110. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1111. }
  1112. }
  1113. // Couldn't find the file.
  1114. return "";
  1115. }
  1116. /**
  1117. * Find the executable with the given name. Searches the given path and then
  1118. * the system search path. Returns the full path to the executable if it is
  1119. * found. Otherwise, the empty string is returned.
  1120. */
  1121. std::string cmSystemTools::FindProgram(const char* name,
  1122. const std::vector<std::string>& userPaths)
  1123. {
  1124. // See if the executable exists as written.
  1125. if(cmSystemTools::FileExists(name) &&
  1126. !cmSystemTools::FileIsDirectory(name))
  1127. {
  1128. return cmSystemTools::CollapseFullPath(name);
  1129. }
  1130. std::string tryPath = name;
  1131. tryPath += cmSystemTools::GetExecutableExtension();
  1132. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1133. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1134. {
  1135. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1136. }
  1137. // Add the system search path to our path.
  1138. std::vector<std::string> path = userPaths;
  1139. cmSystemTools::GetPath(path);
  1140. for(std::vector<std::string>::const_iterator p = path.begin();
  1141. p != path.end(); ++p)
  1142. {
  1143. tryPath = *p;
  1144. tryPath += "/";
  1145. tryPath += name;
  1146. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1147. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1148. {
  1149. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1150. }
  1151. #ifdef _WIN32
  1152. tryPath += ".com";
  1153. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1154. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1155. {
  1156. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1157. }
  1158. tryPath = *p;
  1159. tryPath += "/";
  1160. tryPath += name;
  1161. #endif
  1162. tryPath += cmSystemTools::GetExecutableExtension();
  1163. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1164. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1165. {
  1166. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1167. }
  1168. }
  1169. // Couldn't find the program.
  1170. return "";
  1171. }
  1172. /**
  1173. * Find the library with the given name. Searches the given path and then
  1174. * the system search path. Returns the full path to the library if it is
  1175. * found. Otherwise, the empty string is returned.
  1176. */
  1177. std::string cmSystemTools::FindLibrary(const char* name,
  1178. const std::vector<std::string>& userPaths)
  1179. {
  1180. // See if the executable exists as written.
  1181. if(cmSystemTools::FileExists(name))
  1182. {
  1183. return cmSystemTools::CollapseFullPath(name);
  1184. }
  1185. // Add the system search path to our path.
  1186. std::vector<std::string> path = userPaths;
  1187. cmSystemTools::GetPath(path);
  1188. std::string tryPath;
  1189. for(std::vector<std::string>::const_iterator p = path.begin();
  1190. p != path.end(); ++p)
  1191. {
  1192. #if defined(_WIN32) && !defined(__CYGWIN__)
  1193. tryPath = *p;
  1194. tryPath += "/";
  1195. tryPath += name;
  1196. tryPath += ".lib";
  1197. if(cmSystemTools::FileExists(tryPath.c_str()))
  1198. {
  1199. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1200. }
  1201. #else
  1202. tryPath = *p;
  1203. tryPath += "/lib";
  1204. tryPath += name;
  1205. tryPath += ".so";
  1206. if(cmSystemTools::FileExists(tryPath.c_str()))
  1207. {
  1208. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1209. }
  1210. tryPath = *p;
  1211. tryPath += "/lib";
  1212. tryPath += name;
  1213. tryPath += ".a";
  1214. if(cmSystemTools::FileExists(tryPath.c_str()))
  1215. {
  1216. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1217. }
  1218. tryPath = *p;
  1219. tryPath += "/lib";
  1220. tryPath += name;
  1221. tryPath += ".sl";
  1222. if(cmSystemTools::FileExists(tryPath.c_str()))
  1223. {
  1224. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1225. }
  1226. #endif
  1227. }
  1228. // Couldn't find the library.
  1229. return "";
  1230. }
  1231. bool cmSystemTools::FileIsDirectory(const char* name)
  1232. {
  1233. struct stat fs;
  1234. if(stat(name, &fs) == 0)
  1235. {
  1236. #if _WIN32
  1237. return ((fs.st_mode & _S_IFDIR) != 0);
  1238. #else
  1239. return S_ISDIR(fs.st_mode);
  1240. #endif
  1241. }
  1242. else
  1243. {
  1244. return false;
  1245. }
  1246. }
  1247. int cmSystemTools::ChangeDirectory(const char *dir)
  1248. {
  1249. return Chdir(dir);
  1250. }
  1251. std::string cmSystemTools::GetCurrentWorkingDirectory()
  1252. {
  1253. char buf[2048];
  1254. std::string path = Getcwd(buf, 2048);
  1255. return path;
  1256. }
  1257. /**
  1258. * Given the path to a program executable, get the directory part of the path with the
  1259. * file stripped off. If there is no directory part, the empty string is returned.
  1260. */
  1261. std::string cmSystemTools::GetProgramPath(const char* in_name)
  1262. {
  1263. std::string dir, file;
  1264. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1265. return dir;
  1266. }
  1267. /**
  1268. * Given the path to a program executable, get the directory part of the path
  1269. * with the file stripped off. If there is no directory part, the empty
  1270. * string is returned.
  1271. */
  1272. void cmSystemTools::SplitProgramPath(const char* in_name,
  1273. std::string& dir,
  1274. std::string& file)
  1275. {
  1276. dir = in_name;
  1277. file = "";
  1278. cmSystemTools::ConvertToUnixSlashes(dir);
  1279. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  1280. {
  1281. std::string::size_type slashPos = dir.rfind("/");
  1282. if(slashPos != std::string::npos)
  1283. {
  1284. file = dir.substr(slashPos+1);
  1285. dir = dir.substr(0, slashPos);
  1286. }
  1287. else
  1288. {
  1289. file = dir;
  1290. dir = "";
  1291. }
  1292. }
  1293. if((dir != "") && !cmSystemTools::FileIsDirectory(dir.c_str()))
  1294. {
  1295. std::string oldDir = in_name;
  1296. cmSystemTools::ConvertToUnixSlashes(oldDir);
  1297. cmSystemTools::Error("Error splitting file name off end of path:\n",
  1298. oldDir.c_str(), "\nDirectory not found: ",
  1299. dir.c_str());
  1300. dir = in_name;
  1301. return;
  1302. }
  1303. }
  1304. /**
  1305. * Given a path to a file or directory, convert it to a full path.
  1306. * This collapses away relative paths. The full path is returned.
  1307. */
  1308. std::string cmSystemTools::CollapseFullPath(const char* in_name)
  1309. {
  1310. std::string dir, file;
  1311. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1312. #ifdef _WIN32
  1313. // Ultra-hack warning:
  1314. // This changes to the target directory, saves the working directory,
  1315. // and then changes back to the original working directory.
  1316. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1317. if(dir != "") { Chdir(dir.c_str()); }
  1318. std::string newDir = cmSystemTools::GetCurrentWorkingDirectory();
  1319. Chdir(cwd.c_str());
  1320. cmSystemTools::ConvertToUnixSlashes(newDir);
  1321. std::string newPath = newDir+"/"+file;
  1322. return newPath;
  1323. #else
  1324. # ifdef MAXPATHLEN
  1325. char resolved_name[MAXPATHLEN];
  1326. # else
  1327. # ifdef PATH_MAX
  1328. char resolved_name[PATH_MAX];
  1329. # else
  1330. char resolved_name[5024];
  1331. # endif
  1332. # endif
  1333. if(dir != "")
  1334. {
  1335. realpath(dir.c_str(), resolved_name);
  1336. dir = resolved_name;
  1337. }
  1338. else
  1339. {
  1340. dir = cmSystemTools::GetCurrentWorkingDirectory();
  1341. }
  1342. if(file == "")
  1343. {
  1344. return dir;
  1345. }
  1346. return dir + "/" + file;
  1347. #endif
  1348. }
  1349. /**
  1350. * Return path of a full filename (no trailing slashes).
  1351. * Warning: returned path is converted to Unix slashes format.
  1352. */
  1353. std::string cmSystemTools::GetFilenamePath(const std::string& filename)
  1354. {
  1355. std::string fn = filename;
  1356. cmSystemTools::ConvertToUnixSlashes(fn);
  1357. std::string::size_type slash_pos = fn.rfind("/");
  1358. if(slash_pos != std::string::npos)
  1359. {
  1360. return fn.substr(0, slash_pos);
  1361. }
  1362. else
  1363. {
  1364. return "";
  1365. }
  1366. }
  1367. /**
  1368. * Return file name of a full filename (i.e. file name without path).
  1369. */
  1370. std::string cmSystemTools::GetFilenameName(const std::string& filename)
  1371. {
  1372. std::string fn = filename;
  1373. cmSystemTools::ConvertToUnixSlashes(fn);
  1374. std::string::size_type slash_pos = fn.rfind("/");
  1375. if(slash_pos != std::string::npos)
  1376. {
  1377. return fn.substr(slash_pos + 1);
  1378. }
  1379. else
  1380. {
  1381. return filename;
  1382. }
  1383. }
  1384. /**
  1385. * Return file extension of a full filename (dot included).
  1386. * Warning: this is the longest extension (for example: .tar.gz)
  1387. */
  1388. std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
  1389. {
  1390. std::string name = cmSystemTools::GetFilenameName(filename);
  1391. std::string::size_type dot_pos = name.find(".");
  1392. if(dot_pos != std::string::npos)
  1393. {
  1394. return name.substr(dot_pos);
  1395. }
  1396. else
  1397. {
  1398. return "";
  1399. }
  1400. }
  1401. /**
  1402. * Return file extension of a full filename (dot included).
  1403. */
  1404. std::string cmSystemTools::GetFilenameShortestExtension(const std::string& filename)
  1405. {
  1406. std::string name = cmSystemTools::GetFilenameName(filename);
  1407. std::string::size_type dot_pos = name.rfind(".");
  1408. if(dot_pos != std::string::npos)
  1409. {
  1410. return name.substr(dot_pos);
  1411. }
  1412. else
  1413. {
  1414. return "";
  1415. }
  1416. }
  1417. /**
  1418. * Return file name without extension of a full filename (i.e. without path).
  1419. * Warning: it considers the longest extension (for example: .tar.gz)
  1420. */
  1421. std::string cmSystemTools::GetFilenameNameWithoutExtension(const std::string& filename)
  1422. {
  1423. std::string name = cmSystemTools::GetFilenameName(filename);
  1424. std::string::size_type dot_pos = name.find(".");
  1425. if(dot_pos != std::string::npos)
  1426. {
  1427. return name.substr(0, dot_pos);
  1428. }
  1429. else
  1430. {
  1431. return name;
  1432. }
  1433. }
  1434. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1435. std::vector<std::string>& files)
  1436. {
  1437. cmDirectory d;
  1438. cmRegularExpression reg(regexp);
  1439. if (d.Load(directory))
  1440. {
  1441. size_t numf;
  1442. unsigned int i;
  1443. numf = d.GetNumberOfFiles();
  1444. for (i = 0; i < numf; i++)
  1445. {
  1446. std::string fname = d.GetFile(i);
  1447. if (reg.find(fname))
  1448. {
  1449. files.push_back(fname);
  1450. }
  1451. }
  1452. }
  1453. }
  1454. void cmSystemTools::GlobDirs(const char *fullPath,
  1455. std::vector<std::string>& files)
  1456. {
  1457. std::string path = fullPath;
  1458. std::string::size_type pos = path.find("/*");
  1459. if(pos == std::string::npos)
  1460. {
  1461. files.push_back(fullPath);
  1462. return;
  1463. }
  1464. std::string startPath = path.substr(0, pos);
  1465. std::string finishPath = path.substr(pos+2);
  1466. cmDirectory d;
  1467. if (d.Load(startPath.c_str()))
  1468. {
  1469. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1470. {
  1471. if((std::string(d.GetFile(i)) != ".")
  1472. && (std::string(d.GetFile(i)) != ".."))
  1473. {
  1474. std::string fname = startPath;
  1475. fname +="/";
  1476. fname += d.GetFile(i);
  1477. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1478. {
  1479. fname += finishPath;
  1480. cmSystemTools::GlobDirs(fname.c_str(), files);
  1481. }
  1482. }
  1483. }
  1484. }
  1485. }
  1486. void cmSystemTools::ExpandListArguments(std::vector<std::string> const& arguments,
  1487. std::vector<std::string>& newargs)
  1488. {
  1489. std::vector<std::string>::const_iterator i;
  1490. for(i = arguments.begin();i != arguments.end(); ++i)
  1491. {
  1492. if(i->find(';') != std::string::npos)
  1493. {
  1494. std::string::size_type start = 0;
  1495. std::string::size_type endpos = 0;
  1496. while(endpos != std::string::npos)
  1497. {
  1498. endpos = i->find(';', start);
  1499. std::string::size_type len;
  1500. if(endpos != std::string::npos)
  1501. {
  1502. len = endpos - start;
  1503. }
  1504. else
  1505. {
  1506. len = i->size()-start;
  1507. }
  1508. if (len > 0)
  1509. {
  1510. newargs.push_back(i->substr(start, len));
  1511. }
  1512. start = endpos+1;
  1513. }
  1514. }
  1515. else
  1516. {
  1517. newargs.push_back(*i);
  1518. }
  1519. }
  1520. }