cmSystemTools.cxx 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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. while(fin)
  961. {
  962. fin.getline(buffer, BUFFER_SIZE);
  963. output += buffer;
  964. }
  965. fin.close();
  966. cmSystemTools::RemoveFile(tempFile.c_str());
  967. return true;
  968. }
  969. #endif // endif WIN32 not CYGWIN
  970. // run a command unix uses popen (easy)
  971. // windows uses system and ShortPath
  972. bool cmSystemTools::RunCommand(const char* command,
  973. std::string& output,
  974. int &retVal,
  975. const char* dir,
  976. bool verbose)
  977. {
  978. if(s_DisableRunCommandOutput)
  979. {
  980. verbose = false;
  981. }
  982. #if defined(WIN32) && !defined(__CYGWIN__)
  983. // if the command does not start with a quote, then
  984. // try to find the program, and if the program can not be
  985. // found use system to run the command as it must be a built in
  986. // shell command like echo or dir
  987. int count = 0;
  988. if(command[0] == '\"')
  989. {
  990. // count the number of quotes
  991. for(const char* s = command; *s != 0; ++s)
  992. {
  993. if(*s == '\"')
  994. {
  995. count++;
  996. if(count > 2)
  997. {
  998. break;
  999. }
  1000. }
  1001. }
  1002. // if there are more than two double quotes use
  1003. // GetShortPathName, the cmd.exe program in windows which
  1004. // is used by system fails to execute if there are more than
  1005. // one set of quotes in the arguments
  1006. if(count > 2)
  1007. {
  1008. cmRegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  1009. if(quoted.find(command))
  1010. {
  1011. std::string shortCmd;
  1012. std::string cmd = quoted.match(1);
  1013. std::string args = quoted.match(2);
  1014. char *buffer = new char[cmd.size()+1];
  1015. if(GetShortPathName(cmd.c_str(), buffer,
  1016. int(cmd.size())) != 0)
  1017. {
  1018. shortCmd = buffer;
  1019. shortCmd += " ";
  1020. shortCmd += args;
  1021. }
  1022. else
  1023. {
  1024. cmSystemTools::Error("Could not get GetShortPathName for ",
  1025. cmd.c_str());
  1026. return false;
  1027. }
  1028. return RunCommandViaSystem(shortCmd.c_str(), dir,
  1029. output, retVal, verbose);
  1030. }
  1031. else
  1032. {
  1033. cmSystemTools::Error("Could not parse command line with quotes ",
  1034. command);
  1035. }
  1036. }
  1037. }
  1038. // if there is only one set of quotes or no quotes then just run the command
  1039. return RunCommandViaSystem(command, dir, output, retVal, verbose);
  1040. #else
  1041. // if only popen worked on windows.....
  1042. std::string commandInDir;
  1043. if(dir)
  1044. {
  1045. commandInDir = "cd ";
  1046. commandInDir += dir;
  1047. commandInDir += " && ";
  1048. commandInDir += command;
  1049. }
  1050. else
  1051. {
  1052. commandInDir = command;
  1053. }
  1054. command = commandInDir.c_str();
  1055. const int BUFFER_SIZE = 4096;
  1056. char buffer[BUFFER_SIZE];
  1057. if(verbose)
  1058. {
  1059. std::cout << "running " << command << std::endl;
  1060. }
  1061. fflush(stdout);
  1062. fflush(stderr);
  1063. FILE* cpipe = popen(command, "r");
  1064. if(!cpipe)
  1065. {
  1066. return false;
  1067. }
  1068. fgets(buffer, BUFFER_SIZE, cpipe);
  1069. while(!feof(cpipe))
  1070. {
  1071. if(verbose)
  1072. {
  1073. std::cout << buffer << std::flush;
  1074. }
  1075. output += buffer;
  1076. fgets(buffer, BUFFER_SIZE, cpipe);
  1077. }
  1078. retVal = pclose(cpipe);
  1079. retVal = WEXITSTATUS(retVal);
  1080. return true;
  1081. #endif
  1082. }
  1083. /**
  1084. * Find the file the given name. Searches the given path and then
  1085. * the system search path. Returns the full path to the file if it is
  1086. * found. Otherwise, the empty string is returned.
  1087. */
  1088. std::string cmSystemTools::FindFile(const char* name,
  1089. const std::vector<std::string>& userPaths)
  1090. {
  1091. // Add the system search path to our path.
  1092. std::vector<std::string> path = userPaths;
  1093. cmSystemTools::GetPath(path);
  1094. std::string tryPath;
  1095. for(std::vector<std::string>::const_iterator p = path.begin();
  1096. p != path.end(); ++p)
  1097. {
  1098. tryPath = *p;
  1099. tryPath += "/";
  1100. tryPath += name;
  1101. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1102. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1103. {
  1104. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1105. }
  1106. }
  1107. // Couldn't find the file.
  1108. return "";
  1109. }
  1110. /**
  1111. * Find the executable with the given name. Searches the given path and then
  1112. * the system search path. Returns the full path to the executable if it is
  1113. * found. Otherwise, the empty string is returned.
  1114. */
  1115. std::string cmSystemTools::FindProgram(const char* name,
  1116. const std::vector<std::string>& userPaths)
  1117. {
  1118. // See if the executable exists as written.
  1119. if(cmSystemTools::FileExists(name) &&
  1120. !cmSystemTools::FileIsDirectory(name))
  1121. {
  1122. return cmSystemTools::CollapseFullPath(name);
  1123. }
  1124. std::string tryPath = name;
  1125. tryPath += cmSystemTools::GetExecutableExtension();
  1126. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1127. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1128. {
  1129. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1130. }
  1131. // Add the system search path to our path.
  1132. std::vector<std::string> path = userPaths;
  1133. cmSystemTools::GetPath(path);
  1134. for(std::vector<std::string>::const_iterator p = path.begin();
  1135. p != path.end(); ++p)
  1136. {
  1137. tryPath = *p;
  1138. tryPath += "/";
  1139. tryPath += name;
  1140. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1141. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1142. {
  1143. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1144. }
  1145. #ifdef _WIN32
  1146. tryPath += ".com";
  1147. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1148. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1149. {
  1150. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1151. }
  1152. tryPath = *p;
  1153. tryPath += "/";
  1154. tryPath += name;
  1155. #endif
  1156. tryPath += cmSystemTools::GetExecutableExtension();
  1157. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1158. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1159. {
  1160. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1161. }
  1162. }
  1163. // Couldn't find the program.
  1164. return "";
  1165. }
  1166. /**
  1167. * Find the library with the given name. Searches the given path and then
  1168. * the system search path. Returns the full path to the library if it is
  1169. * found. Otherwise, the empty string is returned.
  1170. */
  1171. std::string cmSystemTools::FindLibrary(const char* name,
  1172. const std::vector<std::string>& userPaths)
  1173. {
  1174. // See if the executable exists as written.
  1175. if(cmSystemTools::FileExists(name))
  1176. {
  1177. return cmSystemTools::CollapseFullPath(name);
  1178. }
  1179. // Add the system search path to our path.
  1180. std::vector<std::string> path = userPaths;
  1181. cmSystemTools::GetPath(path);
  1182. std::string tryPath;
  1183. for(std::vector<std::string>::const_iterator p = path.begin();
  1184. p != path.end(); ++p)
  1185. {
  1186. #if defined(_WIN32) && !defined(__CYGWIN__)
  1187. tryPath = *p;
  1188. tryPath += "/";
  1189. tryPath += name;
  1190. tryPath += ".lib";
  1191. if(cmSystemTools::FileExists(tryPath.c_str()))
  1192. {
  1193. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1194. }
  1195. #else
  1196. tryPath = *p;
  1197. tryPath += "/lib";
  1198. tryPath += name;
  1199. tryPath += ".so";
  1200. if(cmSystemTools::FileExists(tryPath.c_str()))
  1201. {
  1202. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1203. }
  1204. tryPath = *p;
  1205. tryPath += "/lib";
  1206. tryPath += name;
  1207. tryPath += ".a";
  1208. if(cmSystemTools::FileExists(tryPath.c_str()))
  1209. {
  1210. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1211. }
  1212. tryPath = *p;
  1213. tryPath += "/lib";
  1214. tryPath += name;
  1215. tryPath += ".sl";
  1216. if(cmSystemTools::FileExists(tryPath.c_str()))
  1217. {
  1218. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1219. }
  1220. #endif
  1221. }
  1222. // Couldn't find the library.
  1223. return "";
  1224. }
  1225. bool cmSystemTools::FileIsDirectory(const char* name)
  1226. {
  1227. struct stat fs;
  1228. if(stat(name, &fs) == 0)
  1229. {
  1230. #if _WIN32
  1231. return ((fs.st_mode & _S_IFDIR) != 0);
  1232. #else
  1233. return S_ISDIR(fs.st_mode);
  1234. #endif
  1235. }
  1236. else
  1237. {
  1238. return false;
  1239. }
  1240. }
  1241. int cmSystemTools::ChangeDirectory(const char *dir)
  1242. {
  1243. return Chdir(dir);
  1244. }
  1245. std::string cmSystemTools::GetCurrentWorkingDirectory()
  1246. {
  1247. char buf[2048];
  1248. std::string path = Getcwd(buf, 2048);
  1249. return path;
  1250. }
  1251. /**
  1252. * Given the path to a program executable, get the directory part of the path with the
  1253. * file stripped off. If there is no directory part, the empty string is returned.
  1254. */
  1255. std::string cmSystemTools::GetProgramPath(const char* in_name)
  1256. {
  1257. std::string dir, file;
  1258. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1259. return dir;
  1260. }
  1261. /**
  1262. * Given the path to a program executable, get the directory part of the path
  1263. * with the file stripped off. If there is no directory part, the empty
  1264. * string is returned.
  1265. */
  1266. void cmSystemTools::SplitProgramPath(const char* in_name,
  1267. std::string& dir,
  1268. std::string& file)
  1269. {
  1270. dir = in_name;
  1271. file = "";
  1272. cmSystemTools::ConvertToUnixSlashes(dir);
  1273. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  1274. {
  1275. std::string::size_type slashPos = dir.rfind("/");
  1276. if(slashPos != std::string::npos)
  1277. {
  1278. file = dir.substr(slashPos+1);
  1279. dir = dir.substr(0, slashPos);
  1280. }
  1281. else
  1282. {
  1283. file = dir;
  1284. dir = "";
  1285. }
  1286. }
  1287. if((dir != "") && !cmSystemTools::FileIsDirectory(dir.c_str()))
  1288. {
  1289. std::string oldDir = in_name;
  1290. cmSystemTools::ConvertToUnixSlashes(oldDir);
  1291. cmSystemTools::Error("Error splitting file name off end of path:\n",
  1292. oldDir.c_str(), "\nDirectory not found: ",
  1293. dir.c_str());
  1294. dir = in_name;
  1295. return;
  1296. }
  1297. }
  1298. /**
  1299. * Given a path to a file or directory, convert it to a full path.
  1300. * This collapses away relative paths. The full path is returned.
  1301. */
  1302. std::string cmSystemTools::CollapseFullPath(const char* in_name)
  1303. {
  1304. std::string dir, file;
  1305. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1306. #ifdef _WIN32
  1307. // Ultra-hack warning:
  1308. // This changes to the target directory, saves the working directory,
  1309. // and then changes back to the original working directory.
  1310. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1311. if(dir != "") { Chdir(dir.c_str()); }
  1312. std::string newDir = cmSystemTools::GetCurrentWorkingDirectory();
  1313. Chdir(cwd.c_str());
  1314. cmSystemTools::ConvertToUnixSlashes(newDir);
  1315. std::string newPath = newDir+"/"+file;
  1316. return newPath;
  1317. #else
  1318. # ifdef MAXPATHLEN
  1319. char resolved_name[MAXPATHLEN];
  1320. # else
  1321. # ifdef PATH_MAX
  1322. char resolved_name[PATH_MAX];
  1323. # else
  1324. char resolved_name[5024];
  1325. # endif
  1326. # endif
  1327. if(dir != "")
  1328. {
  1329. realpath(dir.c_str(), resolved_name);
  1330. dir = resolved_name;
  1331. }
  1332. else
  1333. {
  1334. dir = cmSystemTools::GetCurrentWorkingDirectory();
  1335. }
  1336. if(file == "")
  1337. {
  1338. return dir;
  1339. }
  1340. return dir + "/" + file;
  1341. #endif
  1342. }
  1343. /**
  1344. * Return path of a full filename (no trailing slashes).
  1345. * Warning: returned path is converted to Unix slashes format.
  1346. */
  1347. std::string cmSystemTools::GetFilenamePath(const std::string& filename)
  1348. {
  1349. std::string fn = filename;
  1350. cmSystemTools::ConvertToUnixSlashes(fn);
  1351. std::string::size_type slash_pos = fn.rfind("/");
  1352. if(slash_pos != std::string::npos)
  1353. {
  1354. return fn.substr(0, slash_pos);
  1355. }
  1356. else
  1357. {
  1358. return "";
  1359. }
  1360. }
  1361. /**
  1362. * Return file name of a full filename (i.e. file name without path).
  1363. */
  1364. std::string cmSystemTools::GetFilenameName(const std::string& filename)
  1365. {
  1366. std::string fn = filename;
  1367. cmSystemTools::ConvertToUnixSlashes(fn);
  1368. std::string::size_type slash_pos = fn.rfind("/");
  1369. if(slash_pos != std::string::npos)
  1370. {
  1371. return fn.substr(slash_pos + 1);
  1372. }
  1373. else
  1374. {
  1375. return filename;
  1376. }
  1377. }
  1378. /**
  1379. * Return file extension of a full filename (dot included).
  1380. * Warning: this is the longest extension (for example: .tar.gz)
  1381. */
  1382. std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
  1383. {
  1384. std::string name = cmSystemTools::GetFilenameName(filename);
  1385. std::string::size_type dot_pos = name.find(".");
  1386. if(dot_pos != std::string::npos)
  1387. {
  1388. return name.substr(dot_pos);
  1389. }
  1390. else
  1391. {
  1392. return "";
  1393. }
  1394. }
  1395. /**
  1396. * Return file extension of a full filename (dot included).
  1397. */
  1398. std::string cmSystemTools::GetFilenameShortestExtension(const std::string& filename)
  1399. {
  1400. std::string name = cmSystemTools::GetFilenameName(filename);
  1401. std::string::size_type dot_pos = name.rfind(".");
  1402. if(dot_pos != std::string::npos)
  1403. {
  1404. return name.substr(dot_pos);
  1405. }
  1406. else
  1407. {
  1408. return "";
  1409. }
  1410. }
  1411. /**
  1412. * Return file name without extension of a full filename (i.e. without path).
  1413. * Warning: it considers the longest extension (for example: .tar.gz)
  1414. */
  1415. std::string cmSystemTools::GetFilenameNameWithoutExtension(const std::string& filename)
  1416. {
  1417. std::string name = cmSystemTools::GetFilenameName(filename);
  1418. std::string::size_type dot_pos = name.find(".");
  1419. if(dot_pos != std::string::npos)
  1420. {
  1421. return name.substr(0, dot_pos);
  1422. }
  1423. else
  1424. {
  1425. return name;
  1426. }
  1427. }
  1428. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1429. std::vector<std::string>& files)
  1430. {
  1431. cmDirectory d;
  1432. cmRegularExpression reg(regexp);
  1433. if (d.Load(directory))
  1434. {
  1435. size_t numf;
  1436. unsigned int i;
  1437. numf = d.GetNumberOfFiles();
  1438. for (i = 0; i < numf; i++)
  1439. {
  1440. std::string fname = d.GetFile(i);
  1441. if (reg.find(fname))
  1442. {
  1443. files.push_back(fname);
  1444. }
  1445. }
  1446. }
  1447. }
  1448. void cmSystemTools::GlobDirs(const char *fullPath,
  1449. std::vector<std::string>& files)
  1450. {
  1451. std::string path = fullPath;
  1452. std::string::size_type pos = path.find("/*");
  1453. if(pos == std::string::npos)
  1454. {
  1455. files.push_back(fullPath);
  1456. return;
  1457. }
  1458. std::string startPath = path.substr(0, pos);
  1459. std::string finishPath = path.substr(pos+2);
  1460. cmDirectory d;
  1461. if (d.Load(startPath.c_str()))
  1462. {
  1463. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1464. {
  1465. if((std::string(d.GetFile(i)) != ".")
  1466. && (std::string(d.GetFile(i)) != ".."))
  1467. {
  1468. std::string fname = startPath;
  1469. fname +="/";
  1470. fname += d.GetFile(i);
  1471. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1472. {
  1473. fname += finishPath;
  1474. cmSystemTools::GlobDirs(fname.c_str(), files);
  1475. }
  1476. }
  1477. }
  1478. }
  1479. }
  1480. void cmSystemTools::ExpandListArguments(std::vector<std::string> const& arguments,
  1481. std::vector<std::string>& newargs,
  1482. bool ignore_empty)
  1483. {
  1484. std::vector<std::string>::const_iterator i;
  1485. for(i = arguments.begin();i != arguments.end(); ++i)
  1486. {
  1487. if(i->find(';') != std::string::npos)
  1488. {
  1489. std::string::size_type start = 0;
  1490. std::string::size_type endpos = 0;
  1491. while(endpos != std::string::npos)
  1492. {
  1493. endpos = i->find(';', start);
  1494. std::string::size_type len;
  1495. if(endpos != std::string::npos)
  1496. {
  1497. len = endpos - start;
  1498. }
  1499. else
  1500. {
  1501. len = i->size()-start;
  1502. }
  1503. if (ignore_empty == false || len > 0)
  1504. {
  1505. newargs.push_back(i->substr(start, len));
  1506. }
  1507. start = endpos+1;
  1508. }
  1509. }
  1510. else
  1511. {
  1512. newargs.push_back(*i);
  1513. }
  1514. }
  1515. }