SystemTools.cxx 43 KB

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