SystemTools.cxx 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  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. #include KWSYS_HEADER(ios/sstream)
  16. #ifdef _MSC_VER
  17. # pragma warning (disable: 4786)
  18. #endif
  19. #include <stdio.h>
  20. #include <sys/stat.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <time.h>
  24. // support for realpath call
  25. #ifndef _WIN32
  26. #include <limits.h>
  27. #include <stdlib.h>
  28. #include <sys/param.h>
  29. #include <sys/wait.h>
  30. #include <sys/ioctl.h>
  31. #include <unistd.h>
  32. #include <termios.h>
  33. #endif
  34. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__))
  35. #include <io.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <windows.h>
  40. #include <direct.h>
  41. #define _unlink unlink
  42. inline int Mkdir(const char* dir)
  43. {
  44. return _mkdir(dir);
  45. }
  46. inline int Rmdir(const char* dir)
  47. {
  48. return _rmdir(dir);
  49. }
  50. inline const char* Getcwd(char* buf, unsigned int len)
  51. {
  52. return _getcwd(buf, len);
  53. }
  54. inline int Chdir(const char* dir)
  55. {
  56. #if defined(__BORLANDC__)
  57. return chdir(dir);
  58. #else
  59. return _chdir(dir);
  60. #endif
  61. }
  62. inline void Realpath(const char *path, kwsys_stl::string & resolved_path)
  63. {
  64. char *ptemp;
  65. char fullpath[MAX_PATH];
  66. if( GetFullPathName(path, sizeof(fullpath), fullpath, &ptemp) )
  67. {
  68. resolved_path = fullpath;
  69. KWSYS_NAMESPACE::SystemTools::ConvertToUnixSlashes(resolved_path);
  70. }
  71. }
  72. #else
  73. #include <sys/types.h>
  74. #include <fcntl.h>
  75. #include <unistd.h>
  76. inline int Mkdir(const char* dir)
  77. {
  78. return mkdir(dir, 00777);
  79. }
  80. inline int Rmdir(const char* dir)
  81. {
  82. return rmdir(dir);
  83. }
  84. inline const char* Getcwd(char* buf, unsigned int len)
  85. {
  86. return getcwd(buf, len);
  87. }
  88. inline int Chdir(const char* dir)
  89. {
  90. return chdir(dir);
  91. }
  92. inline void Realpath(const char *path, kwsys_stl::string & resolved_path)
  93. {
  94. # ifdef MAXPATHLEN
  95. char resolved_name[MAXPATHLEN];
  96. # else
  97. # ifdef PATH_MAX
  98. char resolved_name[PATH_MAX];
  99. # else
  100. char resolved_name[5024];
  101. # endif //PATH_MAX
  102. # endif //MAXPATHLEN
  103. realpath(path, resolved_name);
  104. resolved_path = resolved_name;
  105. }
  106. #endif
  107. #if !defined(_WIN32) && defined(__COMO__)
  108. // Hack for como strict mode to avoid defining _SVID_SOURCE or _BSD_SOURCE.
  109. extern "C"
  110. {
  111. extern FILE *popen (__const char *__command, __const char *__modes) __THROW;
  112. extern int pclose (FILE *__stream) __THROW;
  113. extern char *realpath (__const char *__restrict __name,
  114. char *__restrict __resolved) __THROW;
  115. extern char *strdup (__const char *__s) __THROW;
  116. extern int putenv (char *__string) __THROW;
  117. }
  118. #endif
  119. /* Implement floattime() for various platforms */
  120. // Taken from Python 2.1.3
  121. #if defined( _WIN32 ) && !defined( __CYGWIN__ )
  122. # include <sys/timeb.h>
  123. # define HAVE_FTIME
  124. # if defined( __BORLANDC__)
  125. # define FTIME ftime
  126. # define TIMEB timeb
  127. # else // Visual studio?
  128. # define FTIME _ftime
  129. # define TIMEB _timeb
  130. # endif
  131. #elif defined( __CYGWIN__ ) || defined( __linux__ )
  132. # include <sys/time.h>
  133. # include <time.h>
  134. # define HAVE_GETTIMEOFDAY
  135. #endif
  136. namespace KWSYS_NAMESPACE
  137. {
  138. class SystemToolsTranslationMap :
  139. public kwsys_stl::map<kwsys_stl::string,kwsys_stl::string>
  140. {
  141. };
  142. double
  143. SystemTools::GetTime(void)
  144. {
  145. /* There are three ways to get the time:
  146. (1) gettimeofday() -- resolution in microseconds
  147. (2) ftime() -- resolution in milliseconds
  148. (3) time() -- resolution in seconds
  149. In all cases the return value is a float in seconds.
  150. Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  151. fail, so we fall back on ftime() or time().
  152. Note: clock resolution does not imply clock accuracy! */
  153. #ifdef HAVE_GETTIMEOFDAY
  154. {
  155. struct timeval t;
  156. #ifdef GETTIMEOFDAY_NO_TZ
  157. if (gettimeofday(&t) == 0)
  158. return (double)t.tv_sec + t.tv_usec*0.000001;
  159. #else /* !GETTIMEOFDAY_NO_TZ */
  160. if (gettimeofday(&t, (struct timezone *)NULL) == 0)
  161. return (double)t.tv_sec + t.tv_usec*0.000001;
  162. #endif /* !GETTIMEOFDAY_NO_TZ */
  163. }
  164. #endif /* !HAVE_GETTIMEOFDAY */
  165. {
  166. #if defined(HAVE_FTIME)
  167. struct TIMEB t;
  168. ::FTIME(&t);
  169. return (double)t.time + (double)t.millitm * (double)0.001;
  170. #else /* !HAVE_FTIME */
  171. time_t secs;
  172. time(&secs);
  173. return (double)secs;
  174. #endif /* !HAVE_FTIME */
  175. }
  176. }
  177. // adds the elements of the env variable path to the arg passed in
  178. void SystemTools::GetPath(kwsys_stl::vector<kwsys_stl::string>& path, const char* env)
  179. {
  180. #if defined(_WIN32) && !defined(__CYGWIN__)
  181. const char* pathSep = ";";
  182. #else
  183. const char* pathSep = ":";
  184. #endif
  185. if(!env)
  186. {
  187. env = "PATH";
  188. }
  189. const char* cpathEnv = SystemTools::GetEnv(env);
  190. if ( !cpathEnv )
  191. {
  192. return;
  193. }
  194. kwsys_stl::string pathEnv = cpathEnv;
  195. // A hack to make the below algorithm work.
  196. if(pathEnv[pathEnv.length()-1] != ':')
  197. {
  198. pathEnv += pathSep;
  199. }
  200. kwsys_stl::string::size_type start =0;
  201. bool done = false;
  202. while(!done)
  203. {
  204. kwsys_stl::string::size_type endpos = pathEnv.find(pathSep, start);
  205. if(endpos != kwsys_stl::string::npos)
  206. {
  207. path.push_back(pathEnv.substr(start, endpos-start));
  208. start = endpos+1;
  209. }
  210. else
  211. {
  212. done = true;
  213. }
  214. }
  215. for(kwsys_stl::vector<kwsys_stl::string>::iterator i = path.begin();
  216. i != path.end(); ++i)
  217. {
  218. SystemTools::ConvertToUnixSlashes(*i);
  219. }
  220. }
  221. const char* SystemTools::GetEnv(const char* key)
  222. {
  223. return getenv(key);
  224. }
  225. bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result)
  226. {
  227. const char* v = getenv(key);
  228. if(v)
  229. {
  230. result = v;
  231. return true;
  232. }
  233. else
  234. {
  235. return false;
  236. }
  237. }
  238. const char* SystemTools::GetExecutableExtension()
  239. {
  240. #if defined(_WIN32) || defined(__CYGWIN__)
  241. return ".exe";
  242. #else
  243. return "";
  244. #endif
  245. }
  246. bool SystemTools::MakeDirectory(const char* path)
  247. {
  248. if(SystemTools::FileExists(path))
  249. {
  250. return true;
  251. }
  252. kwsys_stl::string dir = path;
  253. if(dir.size() == 0)
  254. {
  255. return false;
  256. }
  257. SystemTools::ConvertToUnixSlashes(dir);
  258. kwsys_stl::string::size_type pos = dir.find(':');
  259. if(pos == kwsys_stl::string::npos)
  260. {
  261. pos = 0;
  262. }
  263. kwsys_stl::string topdir;
  264. while((pos = dir.find('/', pos)) != kwsys_stl::string::npos)
  265. {
  266. topdir = dir.substr(0, pos);
  267. Mkdir(topdir.c_str());
  268. pos++;
  269. }
  270. if(dir[dir.size()-1] == '/')
  271. {
  272. topdir = dir.substr(0, dir.size());
  273. }
  274. else
  275. {
  276. topdir = dir;
  277. }
  278. if(Mkdir(topdir.c_str()) != 0)
  279. {
  280. // There is a bug in the Borland Run time library which makes MKDIR
  281. // return EACCES when it should return EEXISTS
  282. // if it is some other error besides directory exists
  283. // then return false
  284. if( (errno != EEXIST)
  285. #ifdef __BORLANDC__
  286. && (errno != EACCES)
  287. #endif
  288. )
  289. {
  290. return false;
  291. }
  292. }
  293. return true;
  294. }
  295. // replace replace with with as many times as it shows up in source.
  296. // write the result into source.
  297. void SystemTools::ReplaceString(kwsys_stl::string& source,
  298. const char* replace,
  299. const char* with)
  300. {
  301. const char *src = source.c_str();
  302. char *searchPos = const_cast<char *>(strstr(src,replace));
  303. // get out quick if string is not found
  304. if (!searchPos)
  305. {
  306. return;
  307. }
  308. // perform replacements until done
  309. size_t replaceSize = strlen(replace);
  310. char *orig = strdup(src);
  311. char *currentPos = orig;
  312. searchPos = searchPos - src + orig;
  313. // initialize the result
  314. source.erase(source.begin(),source.end());
  315. do
  316. {
  317. *searchPos = '\0';
  318. source += currentPos;
  319. currentPos = searchPos + replaceSize;
  320. // replace
  321. source += with;
  322. searchPos = strstr(currentPos,replace);
  323. }
  324. while (searchPos);
  325. // copy any trailing text
  326. source += currentPos;
  327. free(orig);
  328. }
  329. // Read a registry value.
  330. // Example :
  331. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  332. // => will return the data of the "default" value of the key
  333. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  334. // => will return the data of the "Root" value of the key
  335. #if defined(_WIN32) && !defined(__CYGWIN__)
  336. bool SystemTools::ReadRegistryValue(const char *key, kwsys_stl::string &value)
  337. {
  338. kwsys_stl::string primary = key;
  339. kwsys_stl::string second;
  340. kwsys_stl::string valuename;
  341. size_t start = primary.find("\\");
  342. if (start == kwsys_stl::string::npos)
  343. {
  344. return false;
  345. }
  346. size_t valuenamepos = primary.find(";");
  347. if (valuenamepos != kwsys_stl::string::npos)
  348. {
  349. valuename = primary.substr(valuenamepos+1);
  350. }
  351. second = primary.substr(start+1, valuenamepos-start-1);
  352. primary = primary.substr(0, start);
  353. HKEY primaryKey = HKEY_CURRENT_USER;
  354. if (primary == "HKEY_CURRENT_USER")
  355. {
  356. primaryKey = HKEY_CURRENT_USER;
  357. }
  358. if (primary == "HKEY_CURRENT_CONFIG")
  359. {
  360. primaryKey = HKEY_CURRENT_CONFIG;
  361. }
  362. if (primary == "HKEY_CLASSES_ROOT")
  363. {
  364. primaryKey = HKEY_CLASSES_ROOT;
  365. }
  366. if (primary == "HKEY_LOCAL_MACHINE")
  367. {
  368. primaryKey = HKEY_LOCAL_MACHINE;
  369. }
  370. if (primary == "HKEY_USERS")
  371. {
  372. primaryKey = HKEY_USERS;
  373. }
  374. HKEY hKey;
  375. if(RegOpenKeyEx(primaryKey,
  376. second.c_str(),
  377. 0,
  378. KEY_READ,
  379. &hKey) != ERROR_SUCCESS)
  380. {
  381. return false;
  382. }
  383. else
  384. {
  385. DWORD dwType, dwSize;
  386. dwSize = 1023;
  387. char data[1024];
  388. if(RegQueryValueEx(hKey,
  389. (LPTSTR)valuename.c_str(),
  390. NULL,
  391. &dwType,
  392. (BYTE *)data,
  393. &dwSize) == ERROR_SUCCESS)
  394. {
  395. if (dwType == REG_SZ)
  396. {
  397. value = data;
  398. RegCloseKey(hKey);
  399. return true;
  400. }
  401. }
  402. }
  403. return false;
  404. }
  405. #else
  406. bool SystemTools::ReadRegistryValue(const char *, kwsys_stl::string &)
  407. {
  408. return false;
  409. }
  410. #endif
  411. // Write a registry value.
  412. // Example :
  413. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  414. // => will set the data of the "default" value of the key
  415. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  416. // => will set the data of the "Root" value of the key
  417. #if defined(_WIN32) && !defined(__CYGWIN__)
  418. bool SystemTools::WriteRegistryValue(const char *key, const char *value)
  419. {
  420. kwsys_stl::string primary = key;
  421. kwsys_stl::string second;
  422. kwsys_stl::string valuename;
  423. size_t start = primary.find("\\");
  424. if (start == kwsys_stl::string::npos)
  425. {
  426. return false;
  427. }
  428. size_t valuenamepos = primary.find(";");
  429. if (valuenamepos != kwsys_stl::string::npos)
  430. {
  431. valuename = primary.substr(valuenamepos+1);
  432. }
  433. second = primary.substr(start+1, valuenamepos-start-1);
  434. primary = primary.substr(0, start);
  435. HKEY primaryKey = HKEY_CURRENT_USER;
  436. if (primary == "HKEY_CURRENT_USER")
  437. {
  438. primaryKey = HKEY_CURRENT_USER;
  439. }
  440. if (primary == "HKEY_CURRENT_CONFIG")
  441. {
  442. primaryKey = HKEY_CURRENT_CONFIG;
  443. }
  444. if (primary == "HKEY_CLASSES_ROOT")
  445. {
  446. primaryKey = HKEY_CLASSES_ROOT;
  447. }
  448. if (primary == "HKEY_LOCAL_MACHINE")
  449. {
  450. primaryKey = HKEY_LOCAL_MACHINE;
  451. }
  452. if (primary == "HKEY_USERS")
  453. {
  454. primaryKey = HKEY_USERS;
  455. }
  456. HKEY hKey;
  457. DWORD dwDummy;
  458. if(RegCreateKeyEx(primaryKey,
  459. second.c_str(),
  460. 0,
  461. "",
  462. REG_OPTION_NON_VOLATILE,
  463. KEY_WRITE,
  464. NULL,
  465. &hKey,
  466. &dwDummy) != ERROR_SUCCESS)
  467. {
  468. return false;
  469. }
  470. if(RegSetValueEx(hKey,
  471. (LPTSTR)valuename.c_str(),
  472. 0,
  473. REG_SZ,
  474. (CONST BYTE *)value,
  475. (DWORD)(strlen(value) + 1)) == ERROR_SUCCESS)
  476. {
  477. return true;
  478. }
  479. return false;
  480. }
  481. #else
  482. bool SystemTools::WriteRegistryValue(const char *, const char *)
  483. {
  484. return false;
  485. }
  486. #endif
  487. // Delete a registry value.
  488. // Example :
  489. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  490. // => will delete the data of the "default" value of the key
  491. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  492. // => will delete the data of the "Root" value of the key
  493. #if defined(_WIN32) && !defined(__CYGWIN__)
  494. bool SystemTools::DeleteRegistryValue(const char *key)
  495. {
  496. kwsys_stl::string primary = key;
  497. kwsys_stl::string second;
  498. kwsys_stl::string valuename;
  499. size_t start = primary.find("\\");
  500. if (start == kwsys_stl::string::npos)
  501. {
  502. return false;
  503. }
  504. size_t valuenamepos = primary.find(";");
  505. if (valuenamepos != kwsys_stl::string::npos)
  506. {
  507. valuename = primary.substr(valuenamepos+1);
  508. }
  509. second = primary.substr(start+1, valuenamepos-start-1);
  510. primary = primary.substr(0, start);
  511. HKEY primaryKey = HKEY_CURRENT_USER;
  512. if (primary == "HKEY_CURRENT_USER")
  513. {
  514. primaryKey = HKEY_CURRENT_USER;
  515. }
  516. if (primary == "HKEY_CURRENT_CONFIG")
  517. {
  518. primaryKey = HKEY_CURRENT_CONFIG;
  519. }
  520. if (primary == "HKEY_CLASSES_ROOT")
  521. {
  522. primaryKey = HKEY_CLASSES_ROOT;
  523. }
  524. if (primary == "HKEY_LOCAL_MACHINE")
  525. {
  526. primaryKey = HKEY_LOCAL_MACHINE;
  527. }
  528. if (primary == "HKEY_USERS")
  529. {
  530. primaryKey = HKEY_USERS;
  531. }
  532. HKEY hKey;
  533. if(RegOpenKeyEx(primaryKey,
  534. second.c_str(),
  535. 0,
  536. KEY_WRITE,
  537. &hKey) != ERROR_SUCCESS)
  538. {
  539. return false;
  540. }
  541. else
  542. {
  543. if(RegDeleteValue(hKey,
  544. (LPTSTR)valuename.c_str()) == ERROR_SUCCESS)
  545. {
  546. RegCloseKey(hKey);
  547. return true;
  548. }
  549. }
  550. return false;
  551. }
  552. #else
  553. bool SystemTools::DeleteRegistryValue(const char *)
  554. {
  555. return false;
  556. }
  557. #endif
  558. bool SystemTools::SameFile(const char* file1, const char* file2)
  559. {
  560. #ifdef _WIN32
  561. HANDLE hFile1, hFile2;
  562. hFile1 = CreateFile( file1,
  563. GENERIC_READ,
  564. FILE_SHARE_READ ,
  565. NULL,
  566. OPEN_EXISTING,
  567. FILE_FLAG_BACKUP_SEMANTICS,
  568. NULL
  569. );
  570. hFile2 = CreateFile( file2,
  571. GENERIC_READ,
  572. FILE_SHARE_READ,
  573. NULL,
  574. OPEN_EXISTING,
  575. FILE_FLAG_BACKUP_SEMANTICS,
  576. NULL
  577. );
  578. if( hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE)
  579. {
  580. if(hFile1 != INVALID_HANDLE_VALUE)
  581. {
  582. CloseHandle(hFile1);
  583. }
  584. if(hFile2 != INVALID_HANDLE_VALUE)
  585. {
  586. CloseHandle(hFile2);
  587. }
  588. return false;
  589. }
  590. BY_HANDLE_FILE_INFORMATION fiBuf1;
  591. BY_HANDLE_FILE_INFORMATION fiBuf2;
  592. GetFileInformationByHandle( hFile1, &fiBuf1 );
  593. GetFileInformationByHandle( hFile2, &fiBuf2 );
  594. CloseHandle(hFile1);
  595. CloseHandle(hFile2);
  596. return (fiBuf1.dwVolumeSerialNumber == fiBuf2.dwVolumeSerialNumber &&
  597. fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh &&
  598. fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow);
  599. #else
  600. struct stat fileStat1, fileStat2;
  601. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  602. {
  603. // see if the files are the same file
  604. // check the device inode and size
  605. if(memcmp(&fileStat2.st_dev, &fileStat1.st_dev, sizeof(fileStat1.st_dev)) == 0 &&
  606. memcmp(&fileStat2.st_ino, &fileStat1.st_ino, sizeof(fileStat1.st_ino)) == 0 &&
  607. fileStat2.st_size == fileStat1.st_size
  608. )
  609. {
  610. return true;
  611. }
  612. }
  613. return false;
  614. #endif
  615. }
  616. // return true if the file exists
  617. bool SystemTools::FileExists(const char* filename)
  618. {
  619. #ifdef _MSC_VER
  620. # define access _access
  621. #endif
  622. #ifndef R_OK
  623. # define R_OK 04
  624. #endif
  625. if ( access(filename, R_OK) != 0 )
  626. {
  627. return false;
  628. }
  629. else
  630. {
  631. return true;
  632. }
  633. }
  634. // Return a capitalized string (i.e the first letter is uppercased, all other
  635. // are lowercased)
  636. kwsys_stl::string SystemTools::Capitalized(const kwsys_stl::string& s)
  637. {
  638. kwsys_stl::string n;
  639. if(s.size() == 0)
  640. {
  641. return n;
  642. }
  643. n.resize(s.size());
  644. n[0] = static_cast<kwsys_stl::string::value_type>(toupper(s[0]));
  645. for (size_t i = 1; i < s.size(); i++)
  646. {
  647. n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
  648. }
  649. return n;
  650. }
  651. // Return a lower case string
  652. kwsys_stl::string SystemTools::LowerCase(const kwsys_stl::string& s)
  653. {
  654. kwsys_stl::string n;
  655. n.resize(s.size());
  656. for (size_t i = 0; i < s.size(); i++)
  657. {
  658. n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
  659. }
  660. return n;
  661. }
  662. // Return a lower case string
  663. kwsys_stl::string SystemTools::UpperCase(const kwsys_stl::string& s)
  664. {
  665. kwsys_stl::string n;
  666. n.resize(s.size());
  667. for (size_t i = 0; i < s.size(); i++)
  668. {
  669. n[i] = static_cast<kwsys_stl::string::value_type>(toupper(s[i]));
  670. }
  671. return n;
  672. }
  673. // convert windows slashes to unix slashes
  674. void SystemTools::ConvertToUnixSlashes(kwsys_stl::string& path)
  675. {
  676. kwsys_stl::string::size_type pos = 0;
  677. while((pos = path.find('\\', pos)) != kwsys_stl::string::npos)
  678. {
  679. // make sure we don't convert an escaped space to a unix slash
  680. if(pos < path.size()-1)
  681. {
  682. if(path[pos+1] != ' ')
  683. {
  684. path[pos] = '/';
  685. }
  686. }
  687. pos++;
  688. }
  689. // Remove all // from the path just like most unix shells
  690. int start_find;
  691. #ifdef _WIN32
  692. // However, on windows if the first characters are both slashes,
  693. // then keep them that way, so that network paths can be handled.
  694. start_find = 1;
  695. #else
  696. start_find = 0;
  697. #endif
  698. while((pos = path.find("//", start_find)) != kwsys_stl::string::npos)
  699. {
  700. SystemTools::ReplaceString(path, "//", "/");
  701. }
  702. // remove any trailing slash
  703. if(path.size() > 1 && path[path.size()-1] == '/')
  704. {
  705. path = path.substr(0, path.size()-1);
  706. }
  707. // if there is a tilda ~ then replace it with HOME
  708. if(path.find("~") == 0)
  709. {
  710. if (getenv("HOME"))
  711. {
  712. path = kwsys_stl::string(getenv("HOME")) + path.substr(1);
  713. }
  714. }
  715. }
  716. // change // to /, and escape any spaces in the path
  717. kwsys_stl::string SystemTools::ConvertToUnixOutputPath(const char* path)
  718. {
  719. kwsys_stl::string ret = path;
  720. // remove // except at the beginning might be a cygwin drive
  721. kwsys_stl::string::size_type pos = 1;
  722. while((pos = ret.find("//", pos)) != kwsys_stl::string::npos)
  723. {
  724. ret.erase(pos, 1);
  725. }
  726. // now escape spaces if there is a space in the path
  727. if(ret.find(" ") != kwsys_stl::string::npos)
  728. {
  729. kwsys_stl::string result = "";
  730. char lastch = 1;
  731. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  732. {
  733. // if it is already escaped then don't try to escape it again
  734. if(*ch == ' ' && lastch != '\\')
  735. {
  736. result += '\\';
  737. }
  738. result += *ch;
  739. lastch = *ch;
  740. }
  741. ret = result;
  742. }
  743. return ret;
  744. }
  745. kwsys_stl::string SystemTools::ConvertToOutputPath(const char* path)
  746. {
  747. #if defined(_WIN32) && !defined(__CYGWIN__)
  748. return SystemTools::ConvertToWindowsOutputPath(path);
  749. #else
  750. return SystemTools::ConvertToUnixOutputPath(path);
  751. #endif
  752. }
  753. // remove double slashes not at the start
  754. kwsys_stl::string SystemTools::ConvertToWindowsOutputPath(const char* path)
  755. {
  756. kwsys_stl::string ret = path;
  757. kwsys_stl::string::size_type pos = 0;
  758. // first convert all of the slashes
  759. while((pos = ret.find('/', pos)) != kwsys_stl::string::npos)
  760. {
  761. ret[pos] = '\\';
  762. pos++;
  763. }
  764. // check for really small paths
  765. if(ret.size() < 2)
  766. {
  767. return ret;
  768. }
  769. // now clean up a bit and remove double slashes
  770. // Only if it is not the first position in the path which is a network
  771. // path on windows
  772. pos = 1; // start at position 1
  773. if(ret[0] == '\"')
  774. {
  775. pos = 2; // if the string is already quoted then start at 2
  776. if(ret.size() < 3)
  777. {
  778. return ret;
  779. }
  780. }
  781. while((pos = ret.find("\\\\", pos)) != kwsys_stl::string::npos)
  782. {
  783. ret.erase(pos, 1);
  784. }
  785. // now double quote the path if it has spaces in it
  786. // and is not already double quoted
  787. if(ret.find(" ") != kwsys_stl::string::npos
  788. && ret[0] != '\"')
  789. {
  790. kwsys_stl::string result;
  791. result = "\"" + ret + "\"";
  792. ret = result;
  793. }
  794. return ret;
  795. }
  796. bool SystemTools::CopyFileIfDifferent(const char* source,
  797. const char* destination)
  798. {
  799. if(SystemTools::FilesDiffer(source, destination))
  800. {
  801. return SystemTools::CopyFileAlways(source, destination);
  802. }
  803. return true;
  804. }
  805. bool SystemTools::FilesDiffer(const char* source,
  806. const char* destination)
  807. {
  808. struct stat statSource;
  809. if (stat(source, &statSource) != 0)
  810. {
  811. return true;
  812. }
  813. struct stat statDestination;
  814. if (stat(destination, &statDestination) != 0)
  815. {
  816. return true;
  817. }
  818. if(statSource.st_size != statDestination.st_size)
  819. {
  820. return true;
  821. }
  822. if(statSource.st_size == 0)
  823. {
  824. return false;
  825. }
  826. #if defined(_WIN32) || defined(__CYGWIN__)
  827. kwsys_ios::ifstream finSource(source, (kwsys_ios::ios::binary |
  828. kwsys_ios::ios::in));
  829. kwsys_ios::ifstream finDestination(destination, (kwsys_ios::ios::binary |
  830. kwsys_ios::ios::in));
  831. #else
  832. kwsys_ios::ifstream finSource(source);
  833. kwsys_ios::ifstream finDestination(destination);
  834. #endif
  835. if(!finSource || !finDestination)
  836. {
  837. return true;
  838. }
  839. char* source_buf = new char[statSource.st_size];
  840. char* dest_buf = new char[statSource.st_size];
  841. finSource.read(source_buf, statSource.st_size);
  842. finDestination.read(dest_buf, statSource.st_size);
  843. if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
  844. statSource.st_size != static_cast<long>(finDestination.gcount()))
  845. {
  846. // Failed to read files.
  847. delete [] source_buf;
  848. delete [] dest_buf;
  849. return true;
  850. }
  851. int ret = memcmp((const void*)source_buf,
  852. (const void*)dest_buf,
  853. statSource.st_size);
  854. delete [] dest_buf;
  855. delete [] source_buf;
  856. return ret != 0;
  857. }
  858. /**
  859. * Copy a file named by "source" to the file named by "destination".
  860. */
  861. bool SystemTools::CopyFileAlways(const char* source, const char* destination)
  862. {
  863. // If files are the same do not copy
  864. if ( SystemTools::SameFile(source, destination) )
  865. {
  866. return true;
  867. }
  868. mode_t perm = 0;
  869. bool perms = SystemTools::GetPermissions(source, perm);
  870. const int bufferSize = 4096;
  871. char buffer[bufferSize];
  872. // If destination is a directory, try to create a file with the same
  873. // name as the source in that directory.
  874. kwsys_stl::string new_destination;
  875. if(SystemTools::FileExists(destination) &&
  876. SystemTools::FileIsDirectory(destination))
  877. {
  878. new_destination = destination;
  879. SystemTools::ConvertToUnixSlashes(new_destination);
  880. new_destination += '/';
  881. kwsys_stl::string source_name = source;
  882. new_destination += SystemTools::GetFilenameName(source_name);
  883. destination = new_destination.c_str();
  884. }
  885. // Create destination directory
  886. kwsys_stl::string destination_dir = destination;
  887. destination_dir = SystemTools::GetFilenamePath(destination_dir);
  888. SystemTools::MakeDirectory(destination_dir.c_str());
  889. // Open files
  890. #if defined(_WIN32) || defined(__CYGWIN__)
  891. kwsys_ios::ifstream fin(source,
  892. kwsys_ios::ios::binary | kwsys_ios::ios::in);
  893. #else
  894. kwsys_ios::ifstream fin(source);
  895. #endif
  896. if(!fin)
  897. {
  898. return false;
  899. }
  900. // try and remove the destination file so that read only destination files
  901. // can be written to.
  902. // If the remove fails continue so that files in read only directories
  903. // that do not allow file removal can be modified.
  904. SystemTools::RemoveFile(destination);
  905. #if defined(_WIN32) || defined(__CYGWIN__)
  906. kwsys_ios::ofstream fout(destination,
  907. kwsys_ios::ios::binary | kwsys_ios::ios::out | kwsys_ios::ios::trunc);
  908. #else
  909. kwsys_ios::ofstream fout(destination,
  910. kwsys_ios::ios::out | kwsys_ios::ios::trunc);
  911. #endif
  912. if(!fout)
  913. {
  914. return false;
  915. }
  916. // This copy loop is very sensitive on certain platforms with
  917. // slightly broken stream libraries (like HPUX). Normally, it is
  918. // incorrect to not check the error condition on the fin.read()
  919. // before using the data, but the fin.gcount() will be zero if an
  920. // error occurred. Therefore, the loop should be safe everywhere.
  921. while(fin)
  922. {
  923. fin.read(buffer, bufferSize);
  924. if(fin.gcount())
  925. {
  926. fout.write(buffer, fin.gcount());
  927. }
  928. }
  929. // Make sure the operating system has finished writing the file
  930. // before closing it. This will ensure the file is finished before
  931. // the check below.
  932. fout.flush();
  933. fin.close();
  934. fout.close();
  935. // More checks.
  936. struct stat statSource, statDestination;
  937. statSource.st_size = 12345;
  938. statDestination.st_size = 12345;
  939. if(stat(source, &statSource) != 0)
  940. {
  941. return false;
  942. }
  943. else if(stat(destination, &statDestination) != 0)
  944. {
  945. return false;
  946. }
  947. else if(statSource.st_size != statDestination.st_size)
  948. {
  949. return false;
  950. }
  951. if ( perms )
  952. {
  953. if ( !SystemTools::SetPermissions(destination, perm) )
  954. {
  955. return false;
  956. }
  957. }
  958. return true;
  959. }
  960. // return size of file; also returns zero if no file exists
  961. unsigned long SystemTools::FileLength(const char* filename)
  962. {
  963. struct stat fs;
  964. if (stat(filename, &fs) != 0)
  965. {
  966. return 0;
  967. }
  968. else
  969. {
  970. return fs.st_size;
  971. }
  972. }
  973. int SystemTools::Strucmp(const char *s1, const char *s2)
  974. {
  975. // lifted from Graphvis http://www.graphviz.org
  976. while ((*s1 != '\0')
  977. && (tolower(*s1) == tolower(*s2)))
  978. {
  979. s1++;
  980. s2++;
  981. }
  982. return tolower(*s1) - tolower(*s2);
  983. }
  984. // return true if the file exists
  985. long int SystemTools::ModifiedTime(const char* filename)
  986. {
  987. struct stat fs;
  988. if (stat(filename, &fs) != 0)
  989. {
  990. return 0;
  991. }
  992. else
  993. {
  994. return (long int)fs.st_mtime;
  995. }
  996. }
  997. kwsys_stl::string SystemTools::GetLastSystemError()
  998. {
  999. int e = errno;
  1000. return strerror(e);
  1001. }
  1002. bool SystemTools::RemoveFile(const char* source)
  1003. {
  1004. return unlink(source) != 0 ? false : true;
  1005. }
  1006. bool SystemTools::RemoveADirectory(const char* source)
  1007. {
  1008. Directory dir;
  1009. dir.Load(source);
  1010. size_t fileNum;
  1011. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  1012. {
  1013. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  1014. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  1015. {
  1016. kwsys_stl::string fullPath = source;
  1017. fullPath += "/";
  1018. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  1019. if(SystemTools::FileIsDirectory(fullPath.c_str()))
  1020. {
  1021. if (!SystemTools::RemoveADirectory(fullPath.c_str()))
  1022. {
  1023. return false;
  1024. }
  1025. }
  1026. else
  1027. {
  1028. if(!SystemTools::RemoveFile(fullPath.c_str()))
  1029. {
  1030. return false;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. return (Rmdir(source) == 0);
  1036. }
  1037. /**
  1038. * Find the file the given name. Searches the given path and then
  1039. * the system search path. Returns the full path to the file if it is
  1040. * found. Otherwise, the empty string is returned.
  1041. */
  1042. kwsys_stl::string SystemTools
  1043. ::FindFile(const char* name,
  1044. const kwsys_stl::vector<kwsys_stl::string>& userPaths)
  1045. {
  1046. // Add the system search path to our path first
  1047. kwsys_stl::vector<kwsys_stl::string> path;
  1048. SystemTools::GetPath(path, "CMAKE_FILE_PATH");
  1049. SystemTools::GetPath(path);
  1050. // now add the additional paths
  1051. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1052. // now look for the file
  1053. kwsys_stl::string tryPath;
  1054. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1055. p != path.end(); ++p)
  1056. {
  1057. tryPath = *p;
  1058. tryPath += "/";
  1059. tryPath += name;
  1060. if(SystemTools::FileExists(tryPath.c_str()) &&
  1061. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1062. {
  1063. return SystemTools::CollapseFullPath(tryPath.c_str());
  1064. }
  1065. }
  1066. // Couldn't find the file.
  1067. return "";
  1068. }
  1069. /**
  1070. * Find the executable with the given name. Searches the given path and then
  1071. * the system search path. Returns the full path to the executable if it is
  1072. * found. Otherwise, the empty string is returned.
  1073. */
  1074. kwsys_stl::string SystemTools::FindProgram(
  1075. const char* name,
  1076. const kwsys_stl::vector<kwsys_stl::string>& userPaths,
  1077. bool no_system_path)
  1078. {
  1079. if(!name)
  1080. {
  1081. return "";
  1082. }
  1083. // See if the executable exists as written.
  1084. if(SystemTools::FileExists(name) &&
  1085. !SystemTools::FileIsDirectory(name))
  1086. {
  1087. return SystemTools::CollapseFullPath(name);
  1088. }
  1089. kwsys_stl::string tryPath = name;
  1090. tryPath += SystemTools::GetExecutableExtension();
  1091. if(SystemTools::FileExists(tryPath.c_str()) &&
  1092. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1093. {
  1094. return SystemTools::CollapseFullPath(tryPath.c_str());
  1095. }
  1096. kwsys_stl::vector<kwsys_stl::string> path;
  1097. SystemTools::GetPath(path, "CMAKE_PROGRAM_PATH");
  1098. // Add the system search path to our path.
  1099. if (!no_system_path)
  1100. {
  1101. SystemTools::GetPath(path);
  1102. }
  1103. // now add the additional paths
  1104. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1105. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1106. p != path.end(); ++p)
  1107. {
  1108. tryPath = *p;
  1109. tryPath += "/";
  1110. tryPath += name;
  1111. if(SystemTools::FileExists(tryPath.c_str()) &&
  1112. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1113. {
  1114. return SystemTools::CollapseFullPath(tryPath.c_str());
  1115. }
  1116. #ifdef _WIN32
  1117. tryPath += ".com";
  1118. if(SystemTools::FileExists(tryPath.c_str()) &&
  1119. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1120. {
  1121. return SystemTools::CollapseFullPath(tryPath.c_str());
  1122. }
  1123. tryPath = *p;
  1124. tryPath += "/";
  1125. tryPath += name;
  1126. #endif
  1127. tryPath += SystemTools::GetExecutableExtension();
  1128. if(SystemTools::FileExists(tryPath.c_str()) &&
  1129. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1130. {
  1131. return SystemTools::CollapseFullPath(tryPath.c_str());
  1132. }
  1133. }
  1134. // Couldn't find the program.
  1135. return "";
  1136. }
  1137. /**
  1138. * Find the library with the given name. Searches the given path and then
  1139. * the system search path. Returns the full path to the library if it is
  1140. * found. Otherwise, the empty string is returned.
  1141. */
  1142. kwsys_stl::string SystemTools
  1143. ::FindLibrary(const char* name,
  1144. const kwsys_stl::vector<kwsys_stl::string>& userPaths)
  1145. {
  1146. // See if the executable exists as written.
  1147. if(SystemTools::FileExists(name) &&
  1148. !SystemTools::FileIsDirectory(name))
  1149. {
  1150. return SystemTools::CollapseFullPath(name);
  1151. }
  1152. // Add the system search path to our path.
  1153. kwsys_stl::vector<kwsys_stl::string> path;
  1154. SystemTools::GetPath(path);
  1155. // now add the additional paths
  1156. path.insert(path.end(), userPaths.begin(), userPaths.end());
  1157. kwsys_stl::string tryPath;
  1158. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1159. p != path.end(); ++p)
  1160. {
  1161. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  1162. tryPath = *p;
  1163. tryPath += "/";
  1164. tryPath += name;
  1165. tryPath += ".lib";
  1166. if(SystemTools::FileExists(tryPath.c_str())
  1167. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1168. {
  1169. return SystemTools::CollapseFullPath(tryPath.c_str());
  1170. }
  1171. #else
  1172. tryPath = *p;
  1173. tryPath += "/lib";
  1174. tryPath += name;
  1175. tryPath += ".so";
  1176. if(SystemTools::FileExists(tryPath.c_str())
  1177. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1178. {
  1179. return SystemTools::CollapseFullPath(tryPath.c_str());
  1180. }
  1181. tryPath = *p;
  1182. tryPath += "/lib";
  1183. tryPath += name;
  1184. tryPath += ".a";
  1185. if(SystemTools::FileExists(tryPath.c_str())
  1186. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1187. {
  1188. return SystemTools::CollapseFullPath(tryPath.c_str());
  1189. }
  1190. tryPath = *p;
  1191. tryPath += "/lib";
  1192. tryPath += name;
  1193. tryPath += ".sl";
  1194. if(SystemTools::FileExists(tryPath.c_str())
  1195. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1196. {
  1197. return SystemTools::CollapseFullPath(tryPath.c_str());
  1198. }
  1199. tryPath = *p;
  1200. tryPath += "/lib";
  1201. tryPath += name;
  1202. tryPath += ".dylib";
  1203. if(SystemTools::FileExists(tryPath.c_str())
  1204. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1205. {
  1206. return SystemTools::CollapseFullPath(tryPath.c_str());
  1207. }
  1208. tryPath = *p;
  1209. tryPath += "/lib";
  1210. tryPath += name;
  1211. tryPath += ".dll";
  1212. if(SystemTools::FileExists(tryPath.c_str())
  1213. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1214. {
  1215. return SystemTools::CollapseFullPath(tryPath.c_str());
  1216. }
  1217. #endif
  1218. }
  1219. // Couldn't find the library.
  1220. return "";
  1221. }
  1222. bool SystemTools::FileIsDirectory(const char* name)
  1223. {
  1224. struct stat fs;
  1225. if(stat(name, &fs) == 0)
  1226. {
  1227. #if _WIN32
  1228. return ((fs.st_mode & _S_IFDIR) != 0);
  1229. #else
  1230. return S_ISDIR(fs.st_mode);
  1231. #endif
  1232. }
  1233. else
  1234. {
  1235. return false;
  1236. }
  1237. }
  1238. int SystemTools::ChangeDirectory(const char *dir)
  1239. {
  1240. return Chdir(dir);
  1241. }
  1242. kwsys_stl::string SystemTools::GetCurrentWorkingDirectory()
  1243. {
  1244. char buf[2048];
  1245. const char* cwd = Getcwd(buf, 2048);
  1246. kwsys_stl::string path;
  1247. if ( cwd )
  1248. {
  1249. path = cwd;
  1250. }
  1251. return SystemTools::CollapseFullPath(path.c_str());
  1252. }
  1253. kwsys_stl::string SystemTools::GetProgramPath(const char* in_name)
  1254. {
  1255. kwsys_stl::string dir, file;
  1256. SystemTools::SplitProgramPath(in_name, dir, file);
  1257. return dir;
  1258. }
  1259. bool SystemTools::SplitProgramPath(const char* in_name,
  1260. kwsys_stl::string& dir,
  1261. kwsys_stl::string& file,
  1262. bool)
  1263. {
  1264. dir = in_name;
  1265. file = "";
  1266. SystemTools::ConvertToUnixSlashes(dir);
  1267. if(!SystemTools::FileIsDirectory(dir.c_str()))
  1268. {
  1269. kwsys_stl::string::size_type slashPos = dir.rfind("/");
  1270. if(slashPos != kwsys_stl::string::npos)
  1271. {
  1272. file = dir.substr(slashPos+1);
  1273. dir = dir.substr(0, slashPos);
  1274. }
  1275. else
  1276. {
  1277. file = dir;
  1278. dir = "";
  1279. }
  1280. }
  1281. if(!(dir == "") && !SystemTools::FileIsDirectory(dir.c_str()))
  1282. {
  1283. kwsys_stl::string oldDir = in_name;
  1284. SystemTools::ConvertToUnixSlashes(oldDir);
  1285. dir = in_name;
  1286. return false;
  1287. }
  1288. return true;
  1289. }
  1290. bool SystemTools::FindProgramPath(const char* argv0,
  1291. kwsys_stl::string& pathOut,
  1292. kwsys_stl::string& errorMsg,
  1293. const char* exeName,
  1294. const char* buildDir,
  1295. const char* installPrefix )
  1296. {
  1297. kwsys_stl::vector<kwsys_stl::string> failures;
  1298. kwsys_stl::string self = argv0;
  1299. SystemTools::ConvertToUnixSlashes(self);
  1300. failures.push_back(argv0);
  1301. self = SystemTools::FindProgram(self.c_str());
  1302. if(!SystemTools::FileExists(self.c_str()))
  1303. {
  1304. if(buildDir)
  1305. {
  1306. kwsys_stl::string intdir = ".";
  1307. #ifdef CMAKE_INTDIR
  1308. intdir = CMAKE_INTDIR;
  1309. #endif
  1310. self = buildDir;
  1311. self += "/bin/";
  1312. self += intdir;
  1313. self += "/";
  1314. self += exeName;
  1315. self += SystemTools::GetExecutableExtension();
  1316. }
  1317. }
  1318. if(installPrefix)
  1319. {
  1320. if(!SystemTools::FileExists(self.c_str()))
  1321. {
  1322. failures.push_back(self);
  1323. self = installPrefix;
  1324. self += "/bin/";
  1325. self += exeName;
  1326. }
  1327. }
  1328. if(!SystemTools::FileExists(self.c_str()))
  1329. {
  1330. failures.push_back(self);
  1331. kwsys_ios::ostringstream msg;
  1332. msg << "Can not find the command line program " << exeName << "\n";
  1333. msg << " argv[0] = \"" << argv0 << "\"\n";
  1334. msg << " Attempted paths:\n";
  1335. kwsys_stl::vector<kwsys_stl::string>::iterator i;
  1336. for(i=failures.begin(); i != failures.end(); ++i)
  1337. {
  1338. msg << " \"" << i->c_str() << "\"\n";
  1339. }
  1340. errorMsg = msg.str();
  1341. return false;
  1342. }
  1343. pathOut = self;
  1344. return true;
  1345. }
  1346. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative)
  1347. {
  1348. return SystemTools::CollapseFullPath(in_relative, 0);
  1349. }
  1350. void SystemTools::AddTranslationPath(const char * a, const char * b)
  1351. {
  1352. kwsys_stl::string path_a = a;
  1353. kwsys_stl::string path_b = b;
  1354. SystemTools::ConvertToUnixSlashes(path_a);
  1355. SystemTools::ConvertToUnixSlashes(path_b);
  1356. // First check this is a directory path, since we don't want the table to
  1357. // grow too fat
  1358. if( SystemTools::FileIsDirectory( path_a.c_str() ) )
  1359. {
  1360. // Make sure the path is a full path and does not contain no '..'
  1361. if( SystemTools::FileIsFullPath(path_b.c_str()) && path_b.find("..")
  1362. == kwsys_stl::string::npos )
  1363. {
  1364. // Before inserting make sure path ends with '/'
  1365. path_a += '/'; path_b += '/';
  1366. if( !(path_a == path_b) )
  1367. {
  1368. SystemTools::TranslationMap->insert(
  1369. SystemToolsTranslationMap::value_type(path_a, path_b));
  1370. }
  1371. }
  1372. }
  1373. }
  1374. void SystemTools::AddKeepPath(const char* dir)
  1375. {
  1376. kwsys_stl::string cdir = SystemTools::CollapseFullPath(dir);
  1377. SystemTools::AddTranslationPath(cdir.c_str(), dir);
  1378. }
  1379. void SystemTools::CheckTranslationPath(kwsys_stl::string & path)
  1380. {
  1381. // In case a file was specified we still have to go through this:
  1382. // Now convert any path found in the table back to the one desired:
  1383. kwsys_stl::map<kwsys_stl::string,kwsys_stl::string>::const_iterator it;
  1384. for(it = SystemTools::TranslationMap->begin();
  1385. it != SystemTools::TranslationMap->end();
  1386. ++it )
  1387. {
  1388. // We need to check of the path is a substring of the other path
  1389. // But also check that the last character is a '/' otherwise we could
  1390. // have some weird case such as /tmp/VTK and /tmp/VTK-bin
  1391. if(path.size() > 1 && path[path.size()-1] != '/')
  1392. {
  1393. // Do not append '/' on a program name:
  1394. if( SystemTools::FileIsDirectory( path.c_str() ) )
  1395. {
  1396. path += "/";
  1397. }
  1398. }
  1399. if(path.find( it->first ) == 0)
  1400. {
  1401. path = path.replace( 0, it->first.size(), it->second);
  1402. }
  1403. }
  1404. }
  1405. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative,
  1406. const char* in_base)
  1407. {
  1408. static int initialized = 0;
  1409. kwsys_stl::string orig;
  1410. // Change to base of relative path.
  1411. if(in_base)
  1412. {
  1413. // Save original working directory.
  1414. orig = SystemTools::GetCurrentWorkingDirectory();
  1415. Chdir(in_base);
  1416. }
  1417. kwsys_stl::string dir, file;
  1418. SystemTools::SplitProgramPath(in_relative, dir, file, false);
  1419. if(dir.size() == 0 &&
  1420. in_relative && strlen(in_relative) > 0 &&
  1421. in_relative[0] == '/')
  1422. {
  1423. dir = "/";
  1424. }
  1425. // Resolve relative path.
  1426. kwsys_stl::string newDir;
  1427. if(!(dir == ""))
  1428. {
  1429. Realpath(dir.c_str(), newDir);
  1430. }
  1431. else
  1432. {
  1433. newDir = SystemTools::GetCurrentWorkingDirectory();
  1434. }
  1435. if(in_base)
  1436. {
  1437. // Restore original working directory.
  1438. Chdir(orig.c_str());
  1439. }
  1440. // Construct and return the full path.
  1441. kwsys_stl::string newPath = newDir;
  1442. if(!(file == ""))
  1443. {
  1444. if(!(newDir.size() == 1 && newDir[0] == '/'))
  1445. {
  1446. newPath += "/";
  1447. }
  1448. newPath += file;
  1449. }
  1450. // If the table has never been initialized, add default path:
  1451. if(!initialized)
  1452. {
  1453. initialized = 1;
  1454. // add some special translation paths for unix
  1455. // these are not added for windows because drive
  1456. // letters need to be maintained. Also, there
  1457. // are not sym-links and mount points on windows anyway.
  1458. #if !defined( _WIN32 )
  1459. //Also add some good default one:
  1460. // This one should always be there it fix a bug on sgi
  1461. SystemTools::AddTranslationPath("/tmp_mnt/", "/");
  1462. //This is a good default also:
  1463. SystemTools::AddKeepPath("/tmp/");
  1464. #endif
  1465. }
  1466. // Now we need to update the translation table with this potentially new path
  1467. SystemTools::AddTranslationPath(newPath.c_str(), in_relative);
  1468. SystemTools::CheckTranslationPath(newPath);
  1469. return newPath;
  1470. }
  1471. bool SystemTools::Split(const char* str, kwsys_stl::vector<kwsys_stl::string>& lines)
  1472. {
  1473. kwsys_stl::string data(str);
  1474. kwsys_stl::string::size_type lpos = 0;
  1475. while(lpos < data.length())
  1476. {
  1477. kwsys_stl::string::size_type rpos = data.find_first_of("\n", lpos);
  1478. if(rpos == kwsys_stl::string::npos)
  1479. {
  1480. // Line ends at end of string without a newline.
  1481. lines.push_back(data.substr(lpos));
  1482. return false;
  1483. }
  1484. if((rpos > lpos) && (data[rpos-1] == '\r'))
  1485. {
  1486. // Line ends in a "\r\n" pair, remove both characters.
  1487. lines.push_back(data.substr(lpos, (rpos-1)-lpos));
  1488. }
  1489. else
  1490. {
  1491. // Line ends in a "\n", remove the character.
  1492. lines.push_back(data.substr(lpos, rpos-lpos));
  1493. }
  1494. lpos = rpos+1;
  1495. }
  1496. return true;
  1497. }
  1498. /**
  1499. * Return path of a full filename (no trailing slashes).
  1500. * Warning: returned path is converted to Unix slashes format.
  1501. */
  1502. kwsys_stl::string SystemTools::GetFilenamePath(const kwsys_stl::string& filename)
  1503. {
  1504. kwsys_stl::string fn = filename;
  1505. SystemTools::ConvertToUnixSlashes(fn);
  1506. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1507. if(slash_pos != kwsys_stl::string::npos)
  1508. {
  1509. return fn.substr(0, slash_pos);
  1510. }
  1511. else
  1512. {
  1513. return "";
  1514. }
  1515. }
  1516. /**
  1517. * Return file name of a full filename (i.e. file name without path).
  1518. */
  1519. kwsys_stl::string SystemTools::GetFilenameName(const kwsys_stl::string& filename)
  1520. {
  1521. kwsys_stl::string fn = filename;
  1522. SystemTools::ConvertToUnixSlashes(fn);
  1523. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1524. if(slash_pos != kwsys_stl::string::npos)
  1525. {
  1526. return fn.substr(slash_pos + 1);
  1527. }
  1528. else
  1529. {
  1530. return filename;
  1531. }
  1532. }
  1533. /**
  1534. * Return file extension of a full filename (dot included).
  1535. * Warning: this is the longest extension (for example: .tar.gz)
  1536. */
  1537. kwsys_stl::string SystemTools::GetFilenameExtension(const kwsys_stl::string& filename)
  1538. {
  1539. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1540. kwsys_stl::string::size_type dot_pos = name.find(".");
  1541. if(dot_pos != kwsys_stl::string::npos)
  1542. {
  1543. return name.substr(dot_pos);
  1544. }
  1545. else
  1546. {
  1547. return "";
  1548. }
  1549. }
  1550. /**
  1551. * Return file extension of a full filename (dot included).
  1552. * Warning: this is the shortest extension (for example: .tar.gz)
  1553. */
  1554. kwsys_stl::string SystemTools::GetFilenameLastExtension(const kwsys_stl::string& filename)
  1555. {
  1556. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1557. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1558. if(dot_pos != kwsys_stl::string::npos)
  1559. {
  1560. return name.substr(dot_pos);
  1561. }
  1562. else
  1563. {
  1564. return "";
  1565. }
  1566. }
  1567. /**
  1568. * Return file name without extension of a full filename (i.e. without path).
  1569. * Warning: it considers the longest extension (for example: .tar.gz)
  1570. */
  1571. kwsys_stl::string SystemTools::GetFilenameWithoutExtension(const kwsys_stl::string& filename)
  1572. {
  1573. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1574. kwsys_stl::string::size_type dot_pos = name.find(".");
  1575. if(dot_pos != kwsys_stl::string::npos)
  1576. {
  1577. return name.substr(0, dot_pos);
  1578. }
  1579. else
  1580. {
  1581. return name;
  1582. }
  1583. }
  1584. /**
  1585. * Return file name without extension of a full filename (i.e. without path).
  1586. * Warning: it considers the last extension (for example: removes .gz
  1587. * from .tar.gz)
  1588. */
  1589. kwsys_stl::string
  1590. SystemTools::GetFilenameWithoutLastExtension(const kwsys_stl::string& filename)
  1591. {
  1592. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1593. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1594. if(dot_pos != kwsys_stl::string::npos)
  1595. {
  1596. return name.substr(0, dot_pos);
  1597. }
  1598. else
  1599. {
  1600. return name;
  1601. }
  1602. }
  1603. bool SystemTools::FileIsFullPath(const char* in_name)
  1604. {
  1605. kwsys_stl::string name = in_name;
  1606. #if defined(_WIN32) || defined(__CYGWIN__)
  1607. // On Windows, the name must be at least two characters long.
  1608. if(name.length() < 2)
  1609. {
  1610. return false;
  1611. }
  1612. if(name[1] == ':')
  1613. {
  1614. return true;
  1615. }
  1616. if(name[0] == '\\')
  1617. {
  1618. return true;
  1619. }
  1620. #else
  1621. // On UNIX, the name must be at least one character long.
  1622. if(name.length() < 1)
  1623. {
  1624. return false;
  1625. }
  1626. #endif
  1627. // On UNIX, the name must begin in a '/'.
  1628. // On Windows, if the name begins in a '/', then it is a full
  1629. // network path.
  1630. if(name[0] == '/')
  1631. {
  1632. return true;
  1633. }
  1634. return false;
  1635. }
  1636. bool SystemTools::GetShortPath(const char* path, kwsys_stl::string& shortPath)
  1637. {
  1638. #if defined(WIN32) && !defined(__CYGWIN__)
  1639. const int size = int(strlen(path)) +1; // size of return
  1640. char *buffer = new char[size]; // create a buffer
  1641. char *tempPath = new char[size]; // create a buffer
  1642. int ret;
  1643. // if the path passed in has quotes around it, first remove the quotes
  1644. if (path[0] == '"' && path[strlen(path)-1] == '"')
  1645. {
  1646. strcpy(tempPath,path+1);
  1647. tempPath[strlen(tempPath)-1] = '\0';
  1648. }
  1649. else
  1650. {
  1651. strcpy(tempPath,path);
  1652. }
  1653. buffer[0] = 0;
  1654. ret = GetShortPathName(tempPath, buffer, size);
  1655. if(buffer[0] == 0 || ret > size)
  1656. {
  1657. delete [] buffer;
  1658. delete [] tempPath;
  1659. return false;
  1660. }
  1661. else
  1662. {
  1663. shortPath = buffer;
  1664. delete [] buffer;
  1665. delete [] tempPath;
  1666. return true;
  1667. }
  1668. #else
  1669. shortPath = path;
  1670. return true;
  1671. #endif
  1672. }
  1673. void SystemTools::SplitProgramFromArgs(const char* path,
  1674. kwsys_stl::string& program, kwsys_stl::string& args)
  1675. {
  1676. // see if this is a full path to a program
  1677. // if so then set program to path and args to nothing
  1678. if(SystemTools::FileExists(path))
  1679. {
  1680. program = path;
  1681. args = "";
  1682. return;
  1683. }
  1684. // Try to find the program in the path, note the program
  1685. // may have spaces in its name so we have to look for it
  1686. kwsys_stl::vector<kwsys_stl::string> e;
  1687. kwsys_stl::string findProg = SystemTools::FindProgram(path, e);
  1688. if(findProg.size())
  1689. {
  1690. program = findProg;
  1691. args = "";
  1692. return;
  1693. }
  1694. // Now try and peel off space separated chunks from the end of the string
  1695. // so the largest path possible is found allowing for spaces in the path
  1696. kwsys_stl::string dir = path;
  1697. kwsys_stl::string::size_type spacePos = dir.rfind(' ');
  1698. while(spacePos != kwsys_stl::string::npos)
  1699. {
  1700. kwsys_stl::string tryProg = dir.substr(0, spacePos);
  1701. // See if the file exists
  1702. if(SystemTools::FileExists(tryProg.c_str()))
  1703. {
  1704. program = tryProg;
  1705. // remove trailing spaces from program
  1706. kwsys_stl::string::size_type pos = program.size()-1;
  1707. while(program[pos] == ' ')
  1708. {
  1709. program.erase(pos);
  1710. pos--;
  1711. }
  1712. args = dir.substr(spacePos, dir.size()-spacePos);
  1713. return;
  1714. }
  1715. // Now try and find the the program in the path
  1716. findProg = SystemTools::FindProgram(tryProg.c_str(), e);
  1717. if(findProg.size())
  1718. {
  1719. program = findProg;
  1720. // remove trailing spaces from program
  1721. kwsys_stl::string::size_type pos = program.size()-1;
  1722. while(program[pos] == ' ')
  1723. {
  1724. program.erase(pos);
  1725. pos--;
  1726. }
  1727. args = dir.substr(spacePos, dir.size()-spacePos);
  1728. return;
  1729. }
  1730. // move past the space for the next search
  1731. spacePos--;
  1732. spacePos = dir.rfind(' ', spacePos);
  1733. }
  1734. program = "";
  1735. args = "";
  1736. }
  1737. kwsys_stl::string SystemTools::GetCurrentDateTime(const char* format)
  1738. {
  1739. char buf[1024];
  1740. time_t t;
  1741. time(&t);
  1742. strftime(buf, sizeof(buf), format, localtime(&t));
  1743. return buf;
  1744. }
  1745. kwsys_stl::string SystemTools::MakeCindentifier(const char* s)
  1746. {
  1747. kwsys_stl::string str(s);
  1748. if (str.find_first_of("0123456789") == 0)
  1749. {
  1750. str = "_" + str;
  1751. }
  1752. kwsys_stl::string permited_chars("_"
  1753. "abcdefghijklmnopqrstuvwxyz"
  1754. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1755. "0123456789");
  1756. kwsys_stl::string::size_type pos = 0;
  1757. while ((pos = str.find_first_not_of(permited_chars, pos)) != kwsys_stl::string::npos)
  1758. {
  1759. str[pos] = '_';
  1760. }
  1761. return str;
  1762. }
  1763. // Due to a buggy stream library on the HP and another on Mac OSX, we
  1764. // need this very carefully written version of getline. Returns true
  1765. // if any data were read before the end-of-file was reached.
  1766. bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, kwsys_stl::string& line,
  1767. bool *has_newline /* = 0 */)
  1768. {
  1769. const int bufferSize = 1024;
  1770. char buffer[bufferSize];
  1771. line = "";
  1772. bool haveData = false;
  1773. if ( has_newline )
  1774. {
  1775. *has_newline = false;
  1776. }
  1777. // If no characters are read from the stream, the end of file has
  1778. // been reached.
  1779. while((is.getline(buffer, bufferSize), is.gcount() > 0))
  1780. {
  1781. haveData = true;
  1782. line.append(buffer);
  1783. // If newline character was read, the gcount includes the
  1784. // character, but the buffer does not. The end of line has been
  1785. // reached.
  1786. if(strlen(buffer) < static_cast<size_t>(is.gcount()))
  1787. {
  1788. if ( has_newline )
  1789. {
  1790. *has_newline = true;
  1791. }
  1792. break;
  1793. }
  1794. // The fail bit may be set. Clear it.
  1795. is.clear(is.rdstate() & ~kwsys_ios::ios::failbit);
  1796. }
  1797. return haveData;
  1798. }
  1799. int SystemTools::GetTerminalWidth()
  1800. {
  1801. int width = -1;
  1802. #ifndef _WIN32
  1803. struct winsize ws;
  1804. char *columns; /* Unix98 environment variable */
  1805. if(ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col>0 && ws.ws_row>0)
  1806. {
  1807. width = ws.ws_col;
  1808. }
  1809. if(!isatty(STDOUT_FILENO))
  1810. {
  1811. width = -1;
  1812. }
  1813. columns = getenv("COLUMNS");
  1814. if(columns && *columns)
  1815. {
  1816. long t;
  1817. char *endptr;
  1818. t = strtol(columns, &endptr, 0);
  1819. if(endptr && !*endptr && (t>0) && (t<1000))
  1820. {
  1821. width = (int)t;
  1822. }
  1823. }
  1824. if ( width < 9 )
  1825. {
  1826. width = -1;
  1827. }
  1828. #endif
  1829. return width;
  1830. }
  1831. bool SystemTools::GetPermissions(const char* file, mode_t& mode)
  1832. {
  1833. if ( !file )
  1834. {
  1835. return false;
  1836. }
  1837. struct stat st;
  1838. if ( stat(file, &st) < 0 )
  1839. {
  1840. return false;
  1841. }
  1842. mode = st.st_mode;
  1843. return true;
  1844. }
  1845. bool SystemTools::SetPermissions(const char* file, mode_t mode)
  1846. {
  1847. if ( !file )
  1848. {
  1849. return false;
  1850. }
  1851. if ( !SystemTools::FileExists(file) )
  1852. {
  1853. return false;
  1854. }
  1855. if ( chmod(file, mode) < 0 )
  1856. {
  1857. return false;
  1858. }
  1859. return true;
  1860. }
  1861. kwsys_stl::string SystemTools::GetParentDirectory(const char* fileOrDir)
  1862. {
  1863. if ( !fileOrDir || !*fileOrDir )
  1864. {
  1865. return "";
  1866. }
  1867. kwsys_stl::string res = fileOrDir;
  1868. SystemTools::ConvertToUnixSlashes(res);
  1869. kwsys_stl::string::size_type cc = res.size()-1;
  1870. if ( res[cc] == '/' )
  1871. {
  1872. cc --;
  1873. }
  1874. for ( ; cc > 0; cc -- )
  1875. {
  1876. if ( res[cc] == '/' )
  1877. {
  1878. break;
  1879. }
  1880. }
  1881. return res.substr(0, cc);
  1882. }
  1883. bool SystemTools::IsSubDirectory(const char* cSubdir, const char* cDir)
  1884. {
  1885. kwsys_stl::string subdir = cSubdir;
  1886. kwsys_stl::string dir = cDir;
  1887. SystemTools::ConvertToUnixSlashes(dir);
  1888. kwsys_stl::string path = subdir;
  1889. do
  1890. {
  1891. path = SystemTools::GetParentDirectory(path.c_str());
  1892. if ( dir == path )
  1893. {
  1894. return true;
  1895. }
  1896. }
  1897. while ( path.size() > dir.size() );
  1898. return false;
  1899. }
  1900. kwsys_stl::string SystemTools::FileExistsInParentDirectories(const char* fname,
  1901. const char* directory, const char* toplevel)
  1902. {
  1903. kwsys_stl::string file = fname;
  1904. SystemTools::ConvertToUnixSlashes(file);
  1905. kwsys_stl::string dir = directory;
  1906. SystemTools::ConvertToUnixSlashes(dir);
  1907. while ( !dir.empty() )
  1908. {
  1909. kwsys_stl::string path = dir + "/" + file;
  1910. if ( SystemTools::FileExists(path.c_str()) )
  1911. {
  1912. return path;
  1913. }
  1914. if ( dir.size() < strlen(toplevel) )
  1915. {
  1916. break;
  1917. }
  1918. dir = SystemTools::GetParentDirectory(dir.c_str());
  1919. }
  1920. return "";
  1921. }
  1922. // These must NOT be initialized. Default initialization to zero is
  1923. // necessary.
  1924. unsigned int SystemToolsManagerCount;
  1925. SystemToolsTranslationMap *SystemTools::TranslationMap;
  1926. // SystemToolsManager manages the SystemTools singleton.
  1927. // SystemToolsManager should be included in any translation unit
  1928. // that will use SystemTools or that implements the singleton
  1929. // pattern. It makes sure that the SystemTools singleton is created
  1930. // before and destroyed after all other singletons in CMake.
  1931. SystemToolsManager::SystemToolsManager()
  1932. {
  1933. if(++SystemToolsManagerCount == 1)
  1934. {
  1935. SystemTools::ClassInitialize();
  1936. }
  1937. }
  1938. SystemToolsManager::~SystemToolsManager()
  1939. {
  1940. if(--SystemToolsManagerCount == 0)
  1941. {
  1942. SystemTools::ClassFinalize();
  1943. }
  1944. }
  1945. void SystemTools::ClassInitialize()
  1946. {
  1947. SystemTools::TranslationMap = new SystemToolsTranslationMap;
  1948. }
  1949. void SystemTools::ClassFinalize()
  1950. {
  1951. delete SystemTools::TranslationMap;
  1952. }
  1953. } // namespace KWSYS_NAMESPACE
  1954. #if defined(_MSC_VER) && defined(_DEBUG)
  1955. # include <crtdbg.h>
  1956. # include <stdio.h>
  1957. # include <stdlib.h>
  1958. namespace KWSYS_NAMESPACE
  1959. {
  1960. static int SystemToolsDebugReport(int, char* message, int*)
  1961. {
  1962. fprintf(stderr, message);
  1963. exit(1);
  1964. }
  1965. void SystemTools::EnableMSVCDebugHook()
  1966. {
  1967. if(getenv("DART_TEST_FROM_DART"))
  1968. {
  1969. _CrtSetReportHook(SystemToolsDebugReport);
  1970. }
  1971. }
  1972. } // namespace KWSYS_NAMESPACE
  1973. #else
  1974. namespace KWSYS_NAMESPACE
  1975. {
  1976. void SystemTools::EnableMSVCDebugHook() {}
  1977. } // namespace KWSYS_NAMESPACE
  1978. #endif