SystemTools.cxx 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(SystemTools.hxx)
  12. #include KWSYS_HEADER(Directory.hxx)
  13. #include KWSYS_HEADER(ios/iostream)
  14. #include KWSYS_HEADER(ios/fstream)
  15. #ifdef _MSC_VER
  16. # pragma warning (disable: 4786)
  17. #endif
  18. #include <stdio.h>
  19. #include <sys/stat.h>
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <time.h>
  23. // support for realpath call
  24. #ifndef _WIN32
  25. #include <limits.h>
  26. #include <stdlib.h>
  27. #include <sys/param.h>
  28. #include <sys/wait.h>
  29. #endif
  30. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__))
  31. #include <io.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <windows.h>
  36. #include <direct.h>
  37. #define _unlink unlink
  38. inline int Mkdir(const char* dir)
  39. {
  40. return _mkdir(dir);
  41. }
  42. inline int Rmdir(const char* dir)
  43. {
  44. return _rmdir(dir);
  45. }
  46. inline const char* Getcwd(char* buf, unsigned int len)
  47. {
  48. return _getcwd(buf, len);
  49. }
  50. inline int Chdir(const char* dir)
  51. {
  52. #if defined(__BORLANDC__)
  53. return chdir(dir);
  54. #else
  55. return _chdir(dir);
  56. #endif
  57. }
  58. #else
  59. #include <sys/types.h>
  60. #include <fcntl.h>
  61. #include <unistd.h>
  62. inline int Mkdir(const char* dir)
  63. {
  64. return mkdir(dir, 00777);
  65. }
  66. inline int Rmdir(const char* dir)
  67. {
  68. return rmdir(dir);
  69. }
  70. inline const char* Getcwd(char* buf, unsigned int len)
  71. {
  72. return getcwd(buf, len);
  73. }
  74. inline int Chdir(const char* dir)
  75. {
  76. return chdir(dir);
  77. }
  78. #endif
  79. #if !defined(_WIN32) && defined(__COMO__)
  80. // Hack for como strict mode to avoid defining _SVID_SOURCE or _BSD_SOURCE.
  81. extern "C"
  82. {
  83. extern FILE *popen (__const char *__command, __const char *__modes) __THROW;
  84. extern int pclose (FILE *__stream) __THROW;
  85. extern char *realpath (__const char *__restrict __name,
  86. char *__restrict __resolved) __THROW;
  87. extern char *strdup (__const char *__s) __THROW;
  88. extern int putenv (char *__string) __THROW;
  89. }
  90. #endif
  91. /* Implement floattime() for various platforms */
  92. // Taken from Python 2.1.3
  93. #if defined( _WIN32 ) && !defined( __CYGWIN__ )
  94. # include <sys/timeb.h>
  95. # define HAVE_FTIME
  96. # if defined( __BORLANDC__)
  97. # define FTIME ftime
  98. # define TIMEB timeb
  99. # else // Visual studio?
  100. # define FTIME _ftime
  101. # define TIMEB _timeb
  102. # endif
  103. #elif defined( __CYGWIN__ ) || defined( __linux__ )
  104. # include <sys/time.h>
  105. # include <time.h>
  106. # define HAVE_GETTIMEOFDAY
  107. #endif
  108. namespace KWSYS_NAMESPACE
  109. {
  110. double
  111. SystemTools::GetTime(void)
  112. {
  113. /* There are three ways to get the time:
  114. (1) gettimeofday() -- resolution in microseconds
  115. (2) ftime() -- resolution in milliseconds
  116. (3) time() -- resolution in seconds
  117. In all cases the return value is a float in seconds.
  118. Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  119. fail, so we fall back on ftime() or time().
  120. Note: clock resolution does not imply clock accuracy! */
  121. #ifdef HAVE_GETTIMEOFDAY
  122. {
  123. struct timeval t;
  124. #ifdef GETTIMEOFDAY_NO_TZ
  125. if (gettimeofday(&t) == 0)
  126. return (double)t.tv_sec + t.tv_usec*0.000001;
  127. #else /* !GETTIMEOFDAY_NO_TZ */
  128. if (gettimeofday(&t, (struct timezone *)NULL) == 0)
  129. return (double)t.tv_sec + t.tv_usec*0.000001;
  130. #endif /* !GETTIMEOFDAY_NO_TZ */
  131. }
  132. #endif /* !HAVE_GETTIMEOFDAY */
  133. {
  134. #if defined(HAVE_FTIME)
  135. struct TIMEB t;
  136. ::FTIME(&t);
  137. return (double)t.time + (double)t.millitm * (double)0.001;
  138. #else /* !HAVE_FTIME */
  139. time_t secs;
  140. time(&secs);
  141. return (double)secs;
  142. #endif /* !HAVE_FTIME */
  143. }
  144. }
  145. // adds the elements of the env variable path to the arg passed in
  146. void SystemTools::GetPath(kwsys_stl::vector<kwsys_stl::string>& path)
  147. {
  148. #if defined(_WIN32) && !defined(__CYGWIN__)
  149. const char* pathSep = ";";
  150. #else
  151. const char* pathSep = ":";
  152. #endif
  153. kwsys_stl::string pathEnv = getenv("PATH");
  154. // A hack to make the below algorithm work.
  155. if(pathEnv[pathEnv.length()-1] != ':')
  156. {
  157. pathEnv += pathSep;
  158. }
  159. kwsys_stl::string::size_type start =0;
  160. bool done = false;
  161. while(!done)
  162. {
  163. kwsys_stl::string::size_type endpos = pathEnv.find(pathSep, start);
  164. if(endpos != kwsys_stl::string::npos)
  165. {
  166. path.push_back(pathEnv.substr(start, endpos-start));
  167. start = endpos+1;
  168. }
  169. else
  170. {
  171. done = true;
  172. }
  173. }
  174. for(kwsys_stl::vector<kwsys_stl::string>::iterator i = path.begin();
  175. i != path.end(); ++i)
  176. {
  177. SystemTools::ConvertToUnixSlashes(*i);
  178. }
  179. }
  180. const char* SystemTools::GetEnv(const char* key)
  181. {
  182. return getenv(key);
  183. }
  184. bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result)
  185. {
  186. const char* v = getenv(key);
  187. if(v)
  188. {
  189. result = v;
  190. return true;
  191. }
  192. else
  193. {
  194. return false;
  195. }
  196. }
  197. const char* SystemTools::GetExecutableExtension()
  198. {
  199. #if defined(_WIN32) || defined(__CYGWIN__)
  200. return ".exe";
  201. #else
  202. return "";
  203. #endif
  204. }
  205. bool SystemTools::MakeDirectory(const char* path)
  206. {
  207. if(SystemTools::FileExists(path))
  208. {
  209. return true;
  210. }
  211. kwsys_stl::string dir = path;
  212. if(dir.size() == 0)
  213. {
  214. return false;
  215. }
  216. SystemTools::ConvertToUnixSlashes(dir);
  217. kwsys_stl::string::size_type pos = dir.find(':');
  218. if(pos == kwsys_stl::string::npos)
  219. {
  220. pos = 0;
  221. }
  222. kwsys_stl::string topdir;
  223. while((pos = dir.find('/', pos)) != kwsys_stl::string::npos)
  224. {
  225. topdir = dir.substr(0, pos);
  226. Mkdir(topdir.c_str());
  227. pos++;
  228. }
  229. if(dir[dir.size()-1] == '/')
  230. {
  231. topdir = dir.substr(0, dir.size());
  232. }
  233. else
  234. {
  235. topdir = dir;
  236. }
  237. if(Mkdir(topdir.c_str()) != 0)
  238. {
  239. // There is a bug in the Borland Run time library which makes MKDIR
  240. // return EACCES when it should return EEXISTS
  241. // if it is some other error besides directory exists
  242. // then return false
  243. if( (errno != EEXIST)
  244. #ifdef __BORLANDC__
  245. && (errno != EACCES)
  246. #endif
  247. )
  248. {
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. // replace replace with with as many times as it shows up in source.
  255. // write the result into source.
  256. void SystemTools::ReplaceString(kwsys_stl::string& source,
  257. const char* replace,
  258. const char* with)
  259. {
  260. const char *src = source.c_str();
  261. char *searchPos = const_cast<char *>(strstr(src,replace));
  262. // get out quick if string is not found
  263. if (!searchPos)
  264. {
  265. return;
  266. }
  267. // perform replacements until done
  268. size_t replaceSize = strlen(replace);
  269. char *orig = strdup(src);
  270. char *currentPos = orig;
  271. searchPos = searchPos - src + orig;
  272. // initialize the result
  273. source.erase(source.begin(),source.end());
  274. do
  275. {
  276. *searchPos = '\0';
  277. source += currentPos;
  278. currentPos = searchPos + replaceSize;
  279. // replace
  280. source += with;
  281. searchPos = strstr(currentPos,replace);
  282. }
  283. while (searchPos);
  284. // copy any trailing text
  285. source += currentPos;
  286. free(orig);
  287. }
  288. // Read a registry value.
  289. // Example :
  290. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  291. // => will return the data of the "default" value of the key
  292. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  293. // => will return the data of the "Root" value of the key
  294. #if defined(_WIN32) && !defined(__CYGWIN__)
  295. bool SystemTools::ReadRegistryValue(const char *key, kwsys_stl::string &value)
  296. {
  297. kwsys_stl::string primary = key;
  298. kwsys_stl::string second;
  299. kwsys_stl::string valuename;
  300. size_t start = primary.find("\\");
  301. if (start == kwsys_stl::string::npos)
  302. {
  303. return false;
  304. }
  305. size_t valuenamepos = primary.find(";");
  306. if (valuenamepos != kwsys_stl::string::npos)
  307. {
  308. valuename = primary.substr(valuenamepos+1);
  309. }
  310. second = primary.substr(start+1, valuenamepos-start-1);
  311. primary = primary.substr(0, start);
  312. HKEY primaryKey;
  313. if (primary == "HKEY_CURRENT_USER")
  314. {
  315. primaryKey = HKEY_CURRENT_USER;
  316. }
  317. if (primary == "HKEY_CURRENT_CONFIG")
  318. {
  319. primaryKey = HKEY_CURRENT_CONFIG;
  320. }
  321. if (primary == "HKEY_CLASSES_ROOT")
  322. {
  323. primaryKey = HKEY_CLASSES_ROOT;
  324. }
  325. if (primary == "HKEY_LOCAL_MACHINE")
  326. {
  327. primaryKey = HKEY_LOCAL_MACHINE;
  328. }
  329. if (primary == "HKEY_USERS")
  330. {
  331. primaryKey = HKEY_USERS;
  332. }
  333. HKEY hKey;
  334. if(RegOpenKeyEx(primaryKey,
  335. second.c_str(),
  336. 0,
  337. KEY_READ,
  338. &hKey) != ERROR_SUCCESS)
  339. {
  340. return false;
  341. }
  342. else
  343. {
  344. DWORD dwType, dwSize;
  345. dwSize = 1023;
  346. char data[1024];
  347. if(RegQueryValueEx(hKey,
  348. (LPTSTR)valuename.c_str(),
  349. NULL,
  350. &dwType,
  351. (BYTE *)data,
  352. &dwSize) == ERROR_SUCCESS)
  353. {
  354. if (dwType == REG_SZ)
  355. {
  356. value = data;
  357. return true;
  358. }
  359. }
  360. }
  361. return false;
  362. }
  363. #else
  364. bool SystemTools::ReadRegistryValue(const char *, kwsys_stl::string &)
  365. {
  366. return false;
  367. }
  368. #endif
  369. // Write a registry value.
  370. // Example :
  371. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  372. // => will set the data of the "default" value of the key
  373. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  374. // => will set the data of the "Root" value of the key
  375. #if defined(_WIN32) && !defined(__CYGWIN__)
  376. bool SystemTools::WriteRegistryValue(const char *key, const char *value)
  377. {
  378. kwsys_stl::string primary = key;
  379. kwsys_stl::string second;
  380. kwsys_stl::string valuename;
  381. size_t start = primary.find("\\");
  382. if (start == kwsys_stl::string::npos)
  383. {
  384. return false;
  385. }
  386. size_t valuenamepos = primary.find(";");
  387. if (valuenamepos != kwsys_stl::string::npos)
  388. {
  389. valuename = primary.substr(valuenamepos+1);
  390. }
  391. second = primary.substr(start+1, valuenamepos-start-1);
  392. primary = primary.substr(0, start);
  393. HKEY primaryKey;
  394. if (primary == "HKEY_CURRENT_USER")
  395. {
  396. primaryKey = HKEY_CURRENT_USER;
  397. }
  398. if (primary == "HKEY_CURRENT_CONFIG")
  399. {
  400. primaryKey = HKEY_CURRENT_CONFIG;
  401. }
  402. if (primary == "HKEY_CLASSES_ROOT")
  403. {
  404. primaryKey = HKEY_CLASSES_ROOT;
  405. }
  406. if (primary == "HKEY_LOCAL_MACHINE")
  407. {
  408. primaryKey = HKEY_LOCAL_MACHINE;
  409. }
  410. if (primary == "HKEY_USERS")
  411. {
  412. primaryKey = HKEY_USERS;
  413. }
  414. HKEY hKey;
  415. DWORD dwDummy;
  416. if(RegCreateKeyEx(primaryKey,
  417. second.c_str(),
  418. 0,
  419. "",
  420. REG_OPTION_NON_VOLATILE,
  421. KEY_WRITE,
  422. NULL,
  423. &hKey,
  424. &dwDummy) != ERROR_SUCCESS)
  425. {
  426. return false;
  427. }
  428. if(RegSetValueEx(hKey,
  429. (LPTSTR)valuename.c_str(),
  430. 0,
  431. REG_SZ,
  432. (CONST BYTE *)value,
  433. (DWORD)(strlen(value) + 1)) == ERROR_SUCCESS)
  434. {
  435. return true;
  436. }
  437. return false;
  438. }
  439. #else
  440. bool SystemTools::WriteRegistryValue(const char *, const char *)
  441. {
  442. return false;
  443. }
  444. #endif
  445. // Delete a registry value.
  446. // Example :
  447. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  448. // => will delete the data of the "default" value of the key
  449. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  450. // => will delete the data of the "Root" value of the key
  451. #if defined(_WIN32) && !defined(__CYGWIN__)
  452. bool SystemTools::DeleteRegistryValue(const char *key)
  453. {
  454. kwsys_stl::string primary = key;
  455. kwsys_stl::string second;
  456. kwsys_stl::string valuename;
  457. size_t start = primary.find("\\");
  458. if (start == kwsys_stl::string::npos)
  459. {
  460. return false;
  461. }
  462. size_t valuenamepos = primary.find(";");
  463. if (valuenamepos != kwsys_stl::string::npos)
  464. {
  465. valuename = primary.substr(valuenamepos+1);
  466. }
  467. second = primary.substr(start+1, valuenamepos-start-1);
  468. primary = primary.substr(0, start);
  469. HKEY primaryKey;
  470. if (primary == "HKEY_CURRENT_USER")
  471. {
  472. primaryKey = HKEY_CURRENT_USER;
  473. }
  474. if (primary == "HKEY_CURRENT_CONFIG")
  475. {
  476. primaryKey = HKEY_CURRENT_CONFIG;
  477. }
  478. if (primary == "HKEY_CLASSES_ROOT")
  479. {
  480. primaryKey = HKEY_CLASSES_ROOT;
  481. }
  482. if (primary == "HKEY_LOCAL_MACHINE")
  483. {
  484. primaryKey = HKEY_LOCAL_MACHINE;
  485. }
  486. if (primary == "HKEY_USERS")
  487. {
  488. primaryKey = HKEY_USERS;
  489. }
  490. HKEY hKey;
  491. if(RegOpenKeyEx(primaryKey,
  492. second.c_str(),
  493. 0,
  494. KEY_WRITE,
  495. &hKey) != ERROR_SUCCESS)
  496. {
  497. return false;
  498. }
  499. else
  500. {
  501. if(RegDeleteValue(hKey,
  502. (LPTSTR)valuename.c_str()) == ERROR_SUCCESS)
  503. {
  504. return true;
  505. }
  506. }
  507. return false;
  508. }
  509. #else
  510. bool SystemTools::DeleteRegistryValue(const char *)
  511. {
  512. return false;
  513. }
  514. #endif
  515. bool SystemTools::SameFile(const char* file1, const char* file2)
  516. {
  517. #ifdef _WIN32
  518. HANDLE hFile1, hFile2;
  519. hFile1 = CreateFile( file1,
  520. GENERIC_READ,
  521. FILE_SHARE_READ ,
  522. NULL,
  523. OPEN_EXISTING,
  524. FILE_FLAG_BACKUP_SEMANTICS,
  525. NULL
  526. );
  527. hFile2 = CreateFile( file2,
  528. GENERIC_READ,
  529. FILE_SHARE_READ,
  530. NULL,
  531. OPEN_EXISTING,
  532. FILE_FLAG_BACKUP_SEMANTICS,
  533. NULL
  534. );
  535. if( hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE)
  536. {
  537. if(hFile1 != INVALID_HANDLE_VALUE)
  538. {
  539. CloseHandle(hFile1);
  540. }
  541. if(hFile2 != INVALID_HANDLE_VALUE)
  542. {
  543. CloseHandle(hFile2);
  544. }
  545. return false;
  546. }
  547. BY_HANDLE_FILE_INFORMATION fiBuf1;
  548. BY_HANDLE_FILE_INFORMATION fiBuf2;
  549. GetFileInformationByHandle( hFile1, &fiBuf1 );
  550. GetFileInformationByHandle( hFile2, &fiBuf2 );
  551. CloseHandle(hFile1);
  552. CloseHandle(hFile2);
  553. return (fiBuf1.dwVolumeSerialNumber == fiBuf2.dwVolumeSerialNumber &&
  554. fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh &&
  555. fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow);
  556. #else
  557. struct stat fileStat1, fileStat2;
  558. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  559. {
  560. // see if the files are the same file
  561. // check the device inode and size
  562. if(memcmp(&fileStat2.st_dev, &fileStat1.st_dev, sizeof(fileStat1.st_dev)) == 0 &&
  563. memcmp(&fileStat2.st_ino, &fileStat1.st_ino, sizeof(fileStat1.st_ino)) == 0 &&
  564. fileStat2.st_size == fileStat1.st_size
  565. )
  566. {
  567. return true;
  568. }
  569. }
  570. return false;
  571. #endif
  572. }
  573. // return true if the file exists
  574. bool SystemTools::FileExists(const char* filename)
  575. {
  576. #ifdef _MSC_VER
  577. # define access _access
  578. #endif
  579. #ifndef F_OK
  580. #define F_OK 0
  581. #endif
  582. if ( access(filename, F_OK) != 0 )
  583. {
  584. return false;
  585. }
  586. else
  587. {
  588. return true;
  589. }
  590. }
  591. // Return a capitalized string (i.e the first letter is uppercased, all other
  592. // are lowercased)
  593. kwsys_stl::string SystemTools::Capitalized(const kwsys_stl::string& s)
  594. {
  595. kwsys_stl::string n;
  596. n.resize(s.size());
  597. n[0] = toupper(s[0]);
  598. for (size_t i = 1; i < s.size(); i++)
  599. {
  600. n[i] = tolower(s[i]);
  601. }
  602. return n;
  603. }
  604. // Return a lower case string
  605. kwsys_stl::string SystemTools::LowerCase(const kwsys_stl::string& s)
  606. {
  607. kwsys_stl::string n;
  608. n.resize(s.size());
  609. for (size_t i = 0; i < s.size(); i++)
  610. {
  611. n[i] = tolower(s[i]);
  612. }
  613. return n;
  614. }
  615. // Return a lower case string
  616. kwsys_stl::string SystemTools::UpperCase(const kwsys_stl::string& s)
  617. {
  618. kwsys_stl::string n;
  619. n.resize(s.size());
  620. for (size_t i = 0; i < s.size(); i++)
  621. {
  622. n[i] = toupper(s[i]);
  623. }
  624. return n;
  625. }
  626. // convert windows slashes to unix slashes
  627. void SystemTools::ConvertToUnixSlashes(kwsys_stl::string& path)
  628. {
  629. kwsys_stl::string::size_type pos = 0;
  630. while((pos = path.find('\\', pos)) != kwsys_stl::string::npos)
  631. {
  632. path[pos] = '/';
  633. pos++;
  634. }
  635. // Remove all // from the path just like most unix shells
  636. int start_find = 0;
  637. #ifdef _WIN32
  638. // However, on windows if the first characters are both slashes,
  639. // then keep them that way, so that network paths can be handled.
  640. start_find = 1;
  641. #endif
  642. while((pos = path.find("//", start_find)) != kwsys_stl::string::npos)
  643. {
  644. SystemTools::ReplaceString(path, "//", "/");
  645. }
  646. // remove any trailing slash
  647. if(path.size() && path[path.size()-1] == '/')
  648. {
  649. path = path.substr(0, path.size()-1);
  650. }
  651. // if there is a tilda ~ then replace it with HOME
  652. if(path.find("~") == 0)
  653. {
  654. if (getenv("HOME"))
  655. {
  656. path = kwsys_stl::string(getenv("HOME")) + path.substr(1);
  657. }
  658. }
  659. // if there is a /tmp_mnt in a path get rid of it!
  660. if(path.find("/tmp_mnt") == 0)
  661. {
  662. path = path.substr(8);
  663. }
  664. }
  665. // change // to /, and escape any spaces in the path
  666. kwsys_stl::string SystemTools::ConvertToUnixOutputPath(const char* path)
  667. {
  668. kwsys_stl::string ret = path;
  669. // remove // except at the beginning might be a cygwin drive
  670. kwsys_stl::string::size_type pos = 1;
  671. while((pos = ret.find("//", pos)) != kwsys_stl::string::npos)
  672. {
  673. ret.erase(pos, 1);
  674. }
  675. // now escape spaces if there is a space in the path
  676. if(ret.find(" ") != kwsys_stl::string::npos)
  677. {
  678. kwsys_stl::string result = "";
  679. char lastch = 1;
  680. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  681. {
  682. // if it is already escaped then don't try to escape it again
  683. if(*ch == ' ' && lastch != '\\')
  684. {
  685. result += '\\';
  686. }
  687. result += *ch;
  688. lastch = *ch;
  689. }
  690. ret = result;
  691. }
  692. return ret;
  693. }
  694. kwsys_stl::string SystemTools::ConvertToOutputPath(const char* path)
  695. {
  696. #if defined(_WIN32) && !defined(__CYGWIN__)
  697. return SystemTools::ConvertToWindowsOutputPath(path);
  698. #else
  699. return SystemTools::ConvertToUnixOutputPath(path);
  700. #endif
  701. }
  702. // remove double slashes not at the start
  703. kwsys_stl::string SystemTools::ConvertToWindowsOutputPath(const char* path)
  704. {
  705. kwsys_stl::string ret = path;
  706. kwsys_stl::string::size_type pos = 0;
  707. // first convert all of the slashes
  708. while((pos = ret.find('/', pos)) != kwsys_stl::string::npos)
  709. {
  710. ret[pos] = '\\';
  711. pos++;
  712. }
  713. // check for really small paths
  714. if(ret.size() < 2)
  715. {
  716. return ret;
  717. }
  718. // now clean up a bit and remove double slashes
  719. // Only if it is not the first position in the path which is a network
  720. // path on windows
  721. pos = 1; // start at position 1
  722. if(ret[0] == '\"')
  723. {
  724. pos = 2; // if the string is already quoted then start at 2
  725. if(ret.size() < 3)
  726. {
  727. return ret;
  728. }
  729. }
  730. while((pos = ret.find("\\\\", pos)) != kwsys_stl::string::npos)
  731. {
  732. ret.erase(pos, 1);
  733. }
  734. // now double quote the path if it has spaces in it
  735. // and is not already double quoted
  736. if(ret.find(" ") != kwsys_stl::string::npos
  737. && ret[0] != '\"')
  738. {
  739. kwsys_stl::string result;
  740. result = "\"" + ret + "\"";
  741. ret = result;
  742. }
  743. return ret;
  744. }
  745. bool SystemTools::CopyFileIfDifferent(const char* source,
  746. const char* destination)
  747. {
  748. if(SystemTools::FilesDiffer(source, destination))
  749. {
  750. return SystemTools::CopyFileAlways(source, destination);
  751. }
  752. return true;
  753. }
  754. bool SystemTools::FilesDiffer(const char* source,
  755. const char* destination)
  756. {
  757. struct stat statSource;
  758. if (stat(source, &statSource) != 0)
  759. {
  760. return true;
  761. }
  762. struct stat statDestination;
  763. if (stat(destination, &statDestination) != 0)
  764. {
  765. return true;
  766. }
  767. if(statSource.st_size != statDestination.st_size)
  768. {
  769. return true;
  770. }
  771. if(statSource.st_size == 0)
  772. {
  773. return false;
  774. }
  775. #if defined(_WIN32) || defined(__CYGWIN__)
  776. kwsys_ios::ifstream finSource(source, (kwsys_ios::ios::binary |
  777. kwsys_ios::ios::in));
  778. kwsys_ios::ifstream finDestination(destination, (kwsys_ios::ios::binary |
  779. kwsys_ios::ios::in));
  780. #else
  781. kwsys_ios::ifstream finSource(source);
  782. kwsys_ios::ifstream finDestination(destination);
  783. #endif
  784. if(!finSource || !finDestination)
  785. {
  786. return true;
  787. }
  788. char* source_buf = new char[statSource.st_size];
  789. char* dest_buf = new char[statSource.st_size];
  790. finSource.read(source_buf, statSource.st_size);
  791. finDestination.read(dest_buf, statSource.st_size);
  792. if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
  793. statSource.st_size != static_cast<long>(finDestination.gcount()))
  794. {
  795. // Failed to read files.
  796. delete [] source_buf;
  797. delete [] dest_buf;
  798. return true;
  799. }
  800. int ret = memcmp((const void*)source_buf,
  801. (const void*)dest_buf,
  802. statSource.st_size);
  803. delete [] dest_buf;
  804. delete [] source_buf;
  805. return ret != 0;
  806. }
  807. /**
  808. * Copy a file named by "source" to the file named by "destination".
  809. */
  810. bool SystemTools::CopyFileAlways(const char* source, const char* destination)
  811. {
  812. const int bufferSize = 4096;
  813. char buffer[bufferSize];
  814. // If destination is a directory, try to create a file with the same
  815. // name as the source in that directory.
  816. kwsys_stl::string new_destination;
  817. if(SystemTools::FileExists(destination) &&
  818. SystemTools::FileIsDirectory(destination))
  819. {
  820. new_destination = destination;
  821. SystemTools::ConvertToUnixSlashes(new_destination);
  822. new_destination += '/';
  823. kwsys_stl::string source_name = source;
  824. new_destination += SystemTools::GetFilenameName(source_name);
  825. destination = new_destination.c_str();
  826. }
  827. // Create destination directory
  828. kwsys_stl::string destination_dir = destination;
  829. destination_dir = SystemTools::GetFilenamePath(destination_dir);
  830. SystemTools::MakeDirectory(destination_dir.c_str());
  831. // Open files
  832. #if defined(_WIN32) || defined(__CYGWIN__)
  833. kwsys_ios::ifstream fin(source,
  834. kwsys_ios::ios::binary | kwsys_ios::ios::in);
  835. #else
  836. kwsys_ios::ifstream fin(source);
  837. #endif
  838. if(!fin)
  839. {
  840. return false;
  841. }
  842. #if defined(_WIN32) || defined(__CYGWIN__)
  843. kwsys_ios::ofstream fout(destination,
  844. kwsys_ios::ios::binary | kwsys_ios::ios::out | kwsys_ios::ios::trunc);
  845. #else
  846. kwsys_ios::ofstream fout(destination,
  847. kwsys_ios::ios::out | kwsys_ios::ios::trunc);
  848. #endif
  849. if(!fout)
  850. {
  851. return false;
  852. }
  853. // This copy loop is very sensitive on certain platforms with
  854. // slightly broken stream libraries (like HPUX). Normally, it is
  855. // incorrect to not check the error condition on the fin.read()
  856. // before using the data, but the fin.gcount() will be zero if an
  857. // error occurred. Therefore, the loop should be safe everywhere.
  858. while(fin)
  859. {
  860. fin.read(buffer, bufferSize);
  861. if(fin.gcount())
  862. {
  863. fout.write(buffer, fin.gcount());
  864. }
  865. }
  866. // Make sure the operating system has finished writing the file
  867. // before closing it. This will ensure the file is finished before
  868. // the check below.
  869. fout.flush();
  870. fin.close();
  871. fout.close();
  872. // More checks.
  873. struct stat statSource, statDestination;
  874. statSource.st_size = 12345;
  875. statDestination.st_size = 12345;
  876. if(stat(source, &statSource) != 0)
  877. {
  878. return false;
  879. }
  880. else if(stat(destination, &statDestination) != 0)
  881. {
  882. return false;
  883. }
  884. else if(statSource.st_size != statDestination.st_size)
  885. {
  886. return false;
  887. }
  888. return true;
  889. }
  890. // return size of file; also returns zero if no file exists
  891. unsigned long SystemTools::FileLength(const char* filename)
  892. {
  893. struct stat fs;
  894. if (stat(filename, &fs) != 0)
  895. {
  896. return 0;
  897. }
  898. else
  899. {
  900. return fs.st_size;
  901. }
  902. }
  903. int SystemTools::Strucmp(const char *s1, const char *s2)
  904. {
  905. // lifted from Graphvis http://www.graphviz.org
  906. while ((*s1 != '\0')
  907. && (tolower(*s1) == tolower(*s2)))
  908. {
  909. s1++;
  910. s2++;
  911. }
  912. return tolower(*s1) - tolower(*s2);
  913. }
  914. // return true if the file exists
  915. long int SystemTools::ModifiedTime(const char* filename)
  916. {
  917. struct stat fs;
  918. if (stat(filename, &fs) != 0)
  919. {
  920. return 0;
  921. }
  922. else
  923. {
  924. return (long int)fs.st_mtime;
  925. }
  926. }
  927. kwsys_stl::string SystemTools::GetLastSystemError()
  928. {
  929. int e = errno;
  930. return strerror(e);
  931. }
  932. bool SystemTools::RemoveFile(const char* source)
  933. {
  934. return unlink(source) != 0 ? false : true;
  935. }
  936. bool SystemTools::RemoveADirectory(const char* source)
  937. {
  938. Directory dir;
  939. dir.Load(source);
  940. size_t fileNum;
  941. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  942. {
  943. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  944. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  945. {
  946. kwsys_stl::string fullPath = source;
  947. fullPath += "/";
  948. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  949. if(SystemTools::FileIsDirectory(fullPath.c_str()))
  950. {
  951. if (!SystemTools::RemoveADirectory(fullPath.c_str()))
  952. {
  953. return false;
  954. }
  955. }
  956. else
  957. {
  958. if(!SystemTools::RemoveFile(fullPath.c_str()))
  959. {
  960. return false;
  961. }
  962. }
  963. }
  964. }
  965. return (Rmdir(source) == 0);
  966. }
  967. /**
  968. * Find the file the given name. Searches the given path and then
  969. * the system search path. Returns the full path to the file if it is
  970. * found. Otherwise, the empty string is returned.
  971. */
  972. kwsys_stl::string SystemTools::FindFile(const char* name,
  973. const kwsys_stl::vector<kwsys_stl::string>& userPaths)
  974. {
  975. // Add the system search path to our path first
  976. kwsys_stl::vector<kwsys_stl::string> path;
  977. SystemTools::GetPath(path);
  978. // now add the additional paths
  979. path.insert(path.end(), userPaths.begin(), userPaths.end());
  980. // now look for the file
  981. kwsys_stl::string tryPath;
  982. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  983. p != path.end(); ++p)
  984. {
  985. tryPath = *p;
  986. tryPath += "/";
  987. tryPath += name;
  988. if(SystemTools::FileExists(tryPath.c_str()) &&
  989. !SystemTools::FileIsDirectory(tryPath.c_str()))
  990. {
  991. return SystemTools::CollapseFullPath(tryPath.c_str());
  992. }
  993. }
  994. // Couldn't find the file.
  995. return "";
  996. }
  997. /**
  998. * Find the executable with the given name. Searches the given path and then
  999. * the system search path. Returns the full path to the executable if it is
  1000. * found. Otherwise, the empty string is returned.
  1001. */
  1002. kwsys_stl::string SystemTools::FindProgram(
  1003. const char* name,
  1004. const kwsys_stl::vector<kwsys_stl::string>& userPaths,
  1005. bool no_system_path)
  1006. {
  1007. if(!name)
  1008. {
  1009. return "";
  1010. }
  1011. // See if the executable exists as written.
  1012. if(SystemTools::FileExists(name) &&
  1013. !SystemTools::FileIsDirectory(name))
  1014. {
  1015. return SystemTools::CollapseFullPath(name);
  1016. }
  1017. kwsys_stl::string tryPath = name;
  1018. tryPath += SystemTools::GetExecutableExtension();
  1019. if(SystemTools::FileExists(tryPath.c_str()) &&
  1020. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1021. {
  1022. return SystemTools::CollapseFullPath(tryPath.c_str());
  1023. }
  1024. // Add the system search path to our path.
  1025. kwsys_stl::vector<kwsys_stl::string> path;
  1026. if (!no_system_path)
  1027. {
  1028. SystemTools::GetPath(path);
  1029. }
  1030. // now add the additional paths
  1031. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1032. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1033. p != path.end(); ++p)
  1034. {
  1035. tryPath = *p;
  1036. tryPath += "/";
  1037. tryPath += name;
  1038. if(SystemTools::FileExists(tryPath.c_str()) &&
  1039. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1040. {
  1041. return SystemTools::CollapseFullPath(tryPath.c_str());
  1042. }
  1043. #ifdef _WIN32
  1044. tryPath += ".com";
  1045. if(SystemTools::FileExists(tryPath.c_str()) &&
  1046. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1047. {
  1048. return SystemTools::CollapseFullPath(tryPath.c_str());
  1049. }
  1050. tryPath = *p;
  1051. tryPath += "/";
  1052. tryPath += name;
  1053. #endif
  1054. tryPath += SystemTools::GetExecutableExtension();
  1055. if(SystemTools::FileExists(tryPath.c_str()) &&
  1056. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1057. {
  1058. return SystemTools::CollapseFullPath(tryPath.c_str());
  1059. }
  1060. }
  1061. // Couldn't find the program.
  1062. return "";
  1063. }
  1064. /**
  1065. * Find the library with the given name. Searches the given path and then
  1066. * the system search path. Returns the full path to the library if it is
  1067. * found. Otherwise, the empty string is returned.
  1068. */
  1069. kwsys_stl::string SystemTools::FindLibrary(const char* name,
  1070. const kwsys_stl::vector<kwsys_stl::string>& userPaths)
  1071. {
  1072. // See if the executable exists as written.
  1073. if(SystemTools::FileExists(name) &&
  1074. !SystemTools::FileIsDirectory(name))
  1075. {
  1076. return SystemTools::CollapseFullPath(name);
  1077. }
  1078. // Add the system search path to our path.
  1079. kwsys_stl::vector<kwsys_stl::string> path;
  1080. SystemTools::GetPath(path);
  1081. // now add the additional paths
  1082. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1083. kwsys_stl::string tryPath;
  1084. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1085. p != path.end(); ++p)
  1086. {
  1087. #if defined(_WIN32) && !defined(__CYGWIN__)
  1088. tryPath = *p;
  1089. tryPath += "/";
  1090. tryPath += name;
  1091. tryPath += ".lib";
  1092. if(SystemTools::FileExists(tryPath.c_str()))
  1093. {
  1094. return SystemTools::CollapseFullPath(tryPath.c_str());
  1095. }
  1096. #else
  1097. tryPath = *p;
  1098. tryPath += "/lib";
  1099. tryPath += name;
  1100. tryPath += ".so";
  1101. if(SystemTools::FileExists(tryPath.c_str()))
  1102. {
  1103. return SystemTools::CollapseFullPath(tryPath.c_str());
  1104. }
  1105. tryPath = *p;
  1106. tryPath += "/lib";
  1107. tryPath += name;
  1108. tryPath += ".a";
  1109. if(SystemTools::FileExists(tryPath.c_str()))
  1110. {
  1111. return SystemTools::CollapseFullPath(tryPath.c_str());
  1112. }
  1113. tryPath = *p;
  1114. tryPath += "/lib";
  1115. tryPath += name;
  1116. tryPath += ".sl";
  1117. if(SystemTools::FileExists(tryPath.c_str()))
  1118. {
  1119. return SystemTools::CollapseFullPath(tryPath.c_str());
  1120. }
  1121. tryPath = *p;
  1122. tryPath += "/lib";
  1123. tryPath += name;
  1124. tryPath += ".dylib";
  1125. if(SystemTools::FileExists(tryPath.c_str()))
  1126. {
  1127. return SystemTools::CollapseFullPath(tryPath.c_str());
  1128. }
  1129. tryPath = *p;
  1130. tryPath += "/lib";
  1131. tryPath += name;
  1132. tryPath += ".dll";
  1133. if(SystemTools::FileExists(tryPath.c_str()))
  1134. {
  1135. return SystemTools::CollapseFullPath(tryPath.c_str());
  1136. }
  1137. #endif
  1138. }
  1139. // Couldn't find the library.
  1140. return "";
  1141. }
  1142. bool SystemTools::FileIsDirectory(const char* name)
  1143. {
  1144. struct stat fs;
  1145. if(stat(name, &fs) == 0)
  1146. {
  1147. #if _WIN32
  1148. return ((fs.st_mode & _S_IFDIR) != 0);
  1149. #else
  1150. return S_ISDIR(fs.st_mode);
  1151. #endif
  1152. }
  1153. else
  1154. {
  1155. return false;
  1156. }
  1157. }
  1158. int SystemTools::ChangeDirectory(const char *dir)
  1159. {
  1160. return Chdir(dir);
  1161. }
  1162. kwsys_stl::string SystemTools::GetCurrentWorkingDirectory()
  1163. {
  1164. char buf[2048];
  1165. kwsys_stl::string path = Getcwd(buf, 2048);
  1166. return path;
  1167. }
  1168. kwsys_stl::string SystemTools::GetProgramPath(const char* in_name)
  1169. {
  1170. kwsys_stl::string dir, file;
  1171. SystemTools::SplitProgramPath(in_name, dir, file);
  1172. return dir;
  1173. }
  1174. bool SystemTools::SplitProgramPath(const char* in_name,
  1175. kwsys_stl::string& dir,
  1176. kwsys_stl::string& file,
  1177. bool)
  1178. {
  1179. dir = in_name;
  1180. file = "";
  1181. SystemTools::ConvertToUnixSlashes(dir);
  1182. if(!SystemTools::FileIsDirectory(dir.c_str()))
  1183. {
  1184. kwsys_stl::string::size_type slashPos = dir.rfind("/");
  1185. if(slashPos != kwsys_stl::string::npos)
  1186. {
  1187. file = dir.substr(slashPos+1);
  1188. dir = dir.substr(0, slashPos);
  1189. }
  1190. else
  1191. {
  1192. file = dir;
  1193. dir = "";
  1194. }
  1195. }
  1196. if(!(dir == "") && !SystemTools::FileIsDirectory(dir.c_str()))
  1197. {
  1198. kwsys_stl::string oldDir = in_name;
  1199. SystemTools::ConvertToUnixSlashes(oldDir);
  1200. dir = in_name;
  1201. return false;
  1202. }
  1203. return true;
  1204. }
  1205. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative)
  1206. {
  1207. return SystemTools::CollapseFullPath(in_relative, 0);
  1208. }
  1209. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative,
  1210. const char* in_base)
  1211. {
  1212. // Save original working directory.
  1213. kwsys_stl::string orig = SystemTools::GetCurrentWorkingDirectory();
  1214. // Change to base of relative path.
  1215. if(in_base)
  1216. {
  1217. Chdir(in_base);
  1218. }
  1219. kwsys_stl::string dir, file;
  1220. SystemTools::SplitProgramPath(in_relative, dir, file, false);
  1221. #ifdef _WIN32
  1222. // Follow relative path.
  1223. if(!(dir == ""))
  1224. {
  1225. Chdir(dir.c_str());
  1226. }
  1227. // Get the resulting directory.
  1228. kwsys_stl::string newDir = SystemTools::GetCurrentWorkingDirectory();
  1229. // Add the file back on to the directory.
  1230. SystemTools::ConvertToUnixSlashes(newDir);
  1231. #else
  1232. # ifdef MAXPATHLEN
  1233. char resolved_name[MAXPATHLEN];
  1234. # else
  1235. # ifdef PATH_MAX
  1236. char resolved_name[PATH_MAX];
  1237. # else
  1238. char resolved_name[5024];
  1239. # endif
  1240. # endif
  1241. // Resolve relative path.
  1242. kwsys_stl::string newDir;
  1243. if(!(dir == ""))
  1244. {
  1245. realpath(dir.c_str(), resolved_name);
  1246. newDir = resolved_name;
  1247. }
  1248. else
  1249. {
  1250. newDir = SystemTools::GetCurrentWorkingDirectory();
  1251. }
  1252. // if there is a /tmp_mnt in a path get rid of it!
  1253. if(newDir.find("/tmp_mnt") == 0)
  1254. {
  1255. newDir = newDir.substr(8);
  1256. }
  1257. #endif
  1258. // Restore original working directory.
  1259. Chdir(orig.c_str());
  1260. // Construct and return the full path.
  1261. kwsys_stl::string newPath = newDir;
  1262. if(!(file == ""))
  1263. {
  1264. newPath += "/";
  1265. newPath += file;
  1266. }
  1267. return newPath;
  1268. }
  1269. bool SystemTools::Split(const char* str, kwsys_stl::vector<kwsys_stl::string>& lines)
  1270. {
  1271. kwsys_stl::string data(str);
  1272. kwsys_stl::string::size_type lpos = 0;
  1273. while(lpos < data.length())
  1274. {
  1275. kwsys_stl::string::size_type rpos = data.find_first_of("\n", lpos);
  1276. if(rpos == kwsys_stl::string::npos)
  1277. {
  1278. // Line ends at end of string without a newline.
  1279. lines.push_back(data.substr(lpos));
  1280. return false;
  1281. }
  1282. if((rpos > lpos) && (data[rpos-1] == '\r'))
  1283. {
  1284. // Line ends in a "\r\n" pair, remove both characters.
  1285. lines.push_back(data.substr(lpos, (rpos-1)-lpos));
  1286. }
  1287. else
  1288. {
  1289. // Line ends in a "\n", remove the character.
  1290. lines.push_back(data.substr(lpos, rpos-lpos));
  1291. }
  1292. lpos = rpos+1;
  1293. }
  1294. return true;
  1295. }
  1296. /**
  1297. * Return path of a full filename (no trailing slashes).
  1298. * Warning: returned path is converted to Unix slashes format.
  1299. */
  1300. kwsys_stl::string SystemTools::GetFilenamePath(const kwsys_stl::string& filename)
  1301. {
  1302. kwsys_stl::string fn = filename;
  1303. SystemTools::ConvertToUnixSlashes(fn);
  1304. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1305. if(slash_pos != kwsys_stl::string::npos)
  1306. {
  1307. return fn.substr(0, slash_pos);
  1308. }
  1309. else
  1310. {
  1311. return "";
  1312. }
  1313. }
  1314. /**
  1315. * Return file name of a full filename (i.e. file name without path).
  1316. */
  1317. kwsys_stl::string SystemTools::GetFilenameName(const kwsys_stl::string& filename)
  1318. {
  1319. kwsys_stl::string fn = filename;
  1320. SystemTools::ConvertToUnixSlashes(fn);
  1321. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1322. if(slash_pos != kwsys_stl::string::npos)
  1323. {
  1324. return fn.substr(slash_pos + 1);
  1325. }
  1326. else
  1327. {
  1328. return filename;
  1329. }
  1330. }
  1331. /**
  1332. * Return file extension of a full filename (dot included).
  1333. * Warning: this is the longest extension (for example: .tar.gz)
  1334. */
  1335. kwsys_stl::string SystemTools::GetFilenameExtension(const kwsys_stl::string& filename)
  1336. {
  1337. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1338. kwsys_stl::string::size_type dot_pos = name.find(".");
  1339. if(dot_pos != kwsys_stl::string::npos)
  1340. {
  1341. return name.substr(dot_pos);
  1342. }
  1343. else
  1344. {
  1345. return "";
  1346. }
  1347. }
  1348. /**
  1349. * Return file extension of a full filename (dot included).
  1350. * Warning: this is the shortest extension (for example: .tar.gz)
  1351. */
  1352. kwsys_stl::string SystemTools::GetFilenameLastExtension(const kwsys_stl::string& filename)
  1353. {
  1354. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1355. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1356. if(dot_pos != kwsys_stl::string::npos)
  1357. {
  1358. return name.substr(dot_pos);
  1359. }
  1360. else
  1361. {
  1362. return "";
  1363. }
  1364. }
  1365. /**
  1366. * Return file name without extension of a full filename (i.e. without path).
  1367. * Warning: it considers the longest extension (for example: .tar.gz)
  1368. */
  1369. kwsys_stl::string SystemTools::GetFilenameWithoutExtension(const kwsys_stl::string& filename)
  1370. {
  1371. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1372. kwsys_stl::string::size_type dot_pos = name.find(".");
  1373. if(dot_pos != kwsys_stl::string::npos)
  1374. {
  1375. return name.substr(0, dot_pos);
  1376. }
  1377. else
  1378. {
  1379. return name;
  1380. }
  1381. }
  1382. /**
  1383. * Return file name without extension of a full filename (i.e. without path).
  1384. * Warning: it considers the last extension (for example: removes .gz
  1385. * from .tar.gz)
  1386. */
  1387. kwsys_stl::string
  1388. SystemTools::GetFilenameWithoutLastExtension(const kwsys_stl::string& filename)
  1389. {
  1390. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1391. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1392. if(dot_pos != kwsys_stl::string::npos)
  1393. {
  1394. return name.substr(0, dot_pos);
  1395. }
  1396. else
  1397. {
  1398. return name;
  1399. }
  1400. }
  1401. bool SystemTools::FileIsFullPath(const char* in_name)
  1402. {
  1403. kwsys_stl::string name = in_name;
  1404. #if defined(_WIN32)
  1405. // On Windows, the name must be at least two characters long.
  1406. if(name.length() < 2)
  1407. {
  1408. return false;
  1409. }
  1410. if(name[1] == ':')
  1411. {
  1412. return true;
  1413. }
  1414. if(name[0] == '\\')
  1415. {
  1416. return true;
  1417. }
  1418. #else
  1419. // On UNIX, the name must be at least one character long.
  1420. if(name.length() < 1)
  1421. {
  1422. return false;
  1423. }
  1424. #endif
  1425. // On UNIX, the name must begin in a '/'.
  1426. // On Windows, if the name begins in a '/', then it is a full
  1427. // network path.
  1428. if(name[0] == '/')
  1429. {
  1430. return true;
  1431. }
  1432. return false;
  1433. }
  1434. bool SystemTools::GetShortPath(const char* path, kwsys_stl::string& shortPath)
  1435. {
  1436. #if defined(WIN32) && !defined(__CYGWIN__)
  1437. const int size = int(strlen(path)) +1; // size of return
  1438. char *buffer = new char[size]; // create a buffer
  1439. char *tempPath = new char[size]; // create a buffer
  1440. int ret;
  1441. // if the path passed in has quotes around it, first remove the quotes
  1442. if (path[0] == '"' && path[strlen(path)-1] == '"')
  1443. {
  1444. strcpy(tempPath,path+1);
  1445. tempPath[strlen(tempPath)-1] = '\0';
  1446. }
  1447. else
  1448. {
  1449. strcpy(tempPath,path);
  1450. }
  1451. buffer[0] = 0;
  1452. ret = GetShortPathName(tempPath, buffer, size);
  1453. if(buffer[0] == 0 || ret > size)
  1454. {
  1455. delete [] buffer;
  1456. delete [] tempPath;
  1457. return false;
  1458. }
  1459. else
  1460. {
  1461. shortPath = buffer;
  1462. delete [] buffer;
  1463. delete [] tempPath;
  1464. return true;
  1465. }
  1466. #else
  1467. shortPath = path;
  1468. return true;
  1469. #endif
  1470. }
  1471. void SystemTools::SplitProgramFromArgs(const char* path,
  1472. kwsys_stl::string& program, kwsys_stl::string& args)
  1473. {
  1474. if(SystemTools::FileExists(path))
  1475. {
  1476. program = path;
  1477. args = "";
  1478. return;
  1479. }
  1480. kwsys_stl::vector<kwsys_stl::string> e;
  1481. kwsys_stl::string findProg = SystemTools::FindProgram(path, e);
  1482. if(findProg.size())
  1483. {
  1484. program = findProg;
  1485. args = "";
  1486. return;
  1487. }
  1488. kwsys_stl::string dir = path;
  1489. kwsys_stl::string::size_type spacePos = dir.rfind(' ');
  1490. if(spacePos == kwsys_stl::string::npos)
  1491. {
  1492. program = "";
  1493. args = "";
  1494. return;
  1495. }
  1496. while(spacePos != kwsys_stl::string::npos)
  1497. {
  1498. kwsys_stl::string tryProg = dir.substr(0, spacePos);
  1499. if(SystemTools::FileExists(tryProg.c_str()))
  1500. {
  1501. program = tryProg;
  1502. args = dir.substr(spacePos, dir.size()-spacePos);
  1503. return;
  1504. }
  1505. findProg = SystemTools::FindProgram(tryProg.c_str(), e);
  1506. if(findProg.size())
  1507. {
  1508. program = findProg;
  1509. args = dir.substr(spacePos, dir.size()-spacePos);
  1510. return;
  1511. }
  1512. spacePos = dir.rfind(' ', spacePos--);
  1513. }
  1514. program = "";
  1515. args = "";
  1516. }
  1517. kwsys_stl::string SystemTools::GetCurrentDateTime(const char* format)
  1518. {
  1519. char buf[1024];
  1520. time_t t;
  1521. time(&t);
  1522. strftime(buf, sizeof(buf), format, localtime(&t));
  1523. return buf;
  1524. }
  1525. kwsys_stl::string SystemTools::MakeCindentifier(const char* s)
  1526. {
  1527. kwsys_stl::string str(s);
  1528. if (str.find_first_of("0123456789") == 0)
  1529. {
  1530. str = "_" + str;
  1531. }
  1532. kwsys_stl::string permited_chars("_"
  1533. "abcdefghijklmnopqrstuvwxyz"
  1534. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1535. "0123456789");
  1536. kwsys_stl::string::size_type pos = 0;
  1537. while ((pos = str.find_first_not_of(permited_chars, pos)) != kwsys_stl::string::npos)
  1538. {
  1539. str[pos] = '_';
  1540. }
  1541. return str;
  1542. }
  1543. // Due to a buggy stream library on the HP and another on Mac OSX, we
  1544. // need this very carefully written version of getline. Returns true
  1545. // if any data were read before the end-of-file was reached.
  1546. bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, kwsys_stl::string& line,
  1547. bool *has_newline /* = 0 */)
  1548. {
  1549. const int bufferSize = 1024;
  1550. char buffer[bufferSize];
  1551. line = "";
  1552. bool haveData = false;
  1553. if ( has_newline )
  1554. {
  1555. *has_newline = false;
  1556. }
  1557. // If no characters are read from the stream, the end of file has
  1558. // been reached.
  1559. while((is.getline(buffer, bufferSize), is.gcount() > 0))
  1560. {
  1561. haveData = true;
  1562. line.append(buffer);
  1563. // If newline character was read, the gcount includes the
  1564. // character, but the buffer does not. The end of line has been
  1565. // reached.
  1566. if(strlen(buffer) < static_cast<size_t>(is.gcount()))
  1567. {
  1568. if ( has_newline )
  1569. {
  1570. *has_newline = true;
  1571. }
  1572. break;
  1573. }
  1574. // The fail bit may be set. Clear it.
  1575. is.clear(is.rdstate() & ~kwsys_ios::ios::failbit);
  1576. }
  1577. return haveData;
  1578. }
  1579. } // namespace KWSYS_NAMESPACE
  1580. #if defined(_MSC_VER) && defined(_DEBUG)
  1581. # include <crtdbg.h>
  1582. # include <stdio.h>
  1583. # include <stdlib.h>
  1584. namespace KWSYS_NAMESPACE
  1585. {
  1586. static int SystemToolsDebugReport(int, char* message, int*)
  1587. {
  1588. fprintf(stderr, message);
  1589. exit(1);
  1590. }
  1591. void SystemTools::EnableMSVCDebugHook()
  1592. {
  1593. if(getenv("DART_TEST_FROM_DART"))
  1594. {
  1595. _CrtSetReportHook(SystemToolsDebugReport);
  1596. }
  1597. }
  1598. } // namespace KWSYS_NAMESPACE
  1599. #else
  1600. namespace KWSYS_NAMESPACE
  1601. {
  1602. void SystemTools::EnableMSVCDebugHook() {}
  1603. } // namespace KWSYS_NAMESPACE
  1604. #endif