SystemTools.cxx 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  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. //Also add some good default one:
  1455. // This one should always be there it fix a bug on sgi
  1456. SystemTools::AddTranslationPath("/tmp_mnt/", "/");
  1457. //This is a good default also:
  1458. SystemTools::AddKeepPath("/tmp/");
  1459. }
  1460. // Now we need to update the translation table with this potentially new path
  1461. SystemTools::AddTranslationPath(newPath.c_str(), in_relative);
  1462. SystemTools::CheckTranslationPath(newPath);
  1463. return newPath;
  1464. }
  1465. bool SystemTools::Split(const char* str, kwsys_stl::vector<kwsys_stl::string>& lines)
  1466. {
  1467. kwsys_stl::string data(str);
  1468. kwsys_stl::string::size_type lpos = 0;
  1469. while(lpos < data.length())
  1470. {
  1471. kwsys_stl::string::size_type rpos = data.find_first_of("\n", lpos);
  1472. if(rpos == kwsys_stl::string::npos)
  1473. {
  1474. // Line ends at end of string without a newline.
  1475. lines.push_back(data.substr(lpos));
  1476. return false;
  1477. }
  1478. if((rpos > lpos) && (data[rpos-1] == '\r'))
  1479. {
  1480. // Line ends in a "\r\n" pair, remove both characters.
  1481. lines.push_back(data.substr(lpos, (rpos-1)-lpos));
  1482. }
  1483. else
  1484. {
  1485. // Line ends in a "\n", remove the character.
  1486. lines.push_back(data.substr(lpos, rpos-lpos));
  1487. }
  1488. lpos = rpos+1;
  1489. }
  1490. return true;
  1491. }
  1492. /**
  1493. * Return path of a full filename (no trailing slashes).
  1494. * Warning: returned path is converted to Unix slashes format.
  1495. */
  1496. kwsys_stl::string SystemTools::GetFilenamePath(const kwsys_stl::string& filename)
  1497. {
  1498. kwsys_stl::string fn = filename;
  1499. SystemTools::ConvertToUnixSlashes(fn);
  1500. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1501. if(slash_pos != kwsys_stl::string::npos)
  1502. {
  1503. return fn.substr(0, slash_pos);
  1504. }
  1505. else
  1506. {
  1507. return "";
  1508. }
  1509. }
  1510. /**
  1511. * Return file name of a full filename (i.e. file name without path).
  1512. */
  1513. kwsys_stl::string SystemTools::GetFilenameName(const kwsys_stl::string& filename)
  1514. {
  1515. kwsys_stl::string fn = filename;
  1516. SystemTools::ConvertToUnixSlashes(fn);
  1517. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  1518. if(slash_pos != kwsys_stl::string::npos)
  1519. {
  1520. return fn.substr(slash_pos + 1);
  1521. }
  1522. else
  1523. {
  1524. return filename;
  1525. }
  1526. }
  1527. /**
  1528. * Return file extension of a full filename (dot included).
  1529. * Warning: this is the longest extension (for example: .tar.gz)
  1530. */
  1531. kwsys_stl::string SystemTools::GetFilenameExtension(const kwsys_stl::string& filename)
  1532. {
  1533. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1534. kwsys_stl::string::size_type dot_pos = name.find(".");
  1535. if(dot_pos != kwsys_stl::string::npos)
  1536. {
  1537. return name.substr(dot_pos);
  1538. }
  1539. else
  1540. {
  1541. return "";
  1542. }
  1543. }
  1544. /**
  1545. * Return file extension of a full filename (dot included).
  1546. * Warning: this is the shortest extension (for example: .tar.gz)
  1547. */
  1548. kwsys_stl::string SystemTools::GetFilenameLastExtension(const kwsys_stl::string& filename)
  1549. {
  1550. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1551. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1552. if(dot_pos != kwsys_stl::string::npos)
  1553. {
  1554. return name.substr(dot_pos);
  1555. }
  1556. else
  1557. {
  1558. return "";
  1559. }
  1560. }
  1561. /**
  1562. * Return file name without extension of a full filename (i.e. without path).
  1563. * Warning: it considers the longest extension (for example: .tar.gz)
  1564. */
  1565. kwsys_stl::string SystemTools::GetFilenameWithoutExtension(const kwsys_stl::string& filename)
  1566. {
  1567. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1568. kwsys_stl::string::size_type dot_pos = name.find(".");
  1569. if(dot_pos != kwsys_stl::string::npos)
  1570. {
  1571. return name.substr(0, dot_pos);
  1572. }
  1573. else
  1574. {
  1575. return name;
  1576. }
  1577. }
  1578. /**
  1579. * Return file name without extension of a full filename (i.e. without path).
  1580. * Warning: it considers the last extension (for example: removes .gz
  1581. * from .tar.gz)
  1582. */
  1583. kwsys_stl::string
  1584. SystemTools::GetFilenameWithoutLastExtension(const kwsys_stl::string& filename)
  1585. {
  1586. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  1587. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1588. if(dot_pos != kwsys_stl::string::npos)
  1589. {
  1590. return name.substr(0, dot_pos);
  1591. }
  1592. else
  1593. {
  1594. return name;
  1595. }
  1596. }
  1597. bool SystemTools::FileIsFullPath(const char* in_name)
  1598. {
  1599. kwsys_stl::string name = in_name;
  1600. #if defined(_WIN32) || defined(__CYGWIN__)
  1601. // On Windows, the name must be at least two characters long.
  1602. if(name.length() < 2)
  1603. {
  1604. return false;
  1605. }
  1606. if(name[1] == ':')
  1607. {
  1608. return true;
  1609. }
  1610. if(name[0] == '\\')
  1611. {
  1612. return true;
  1613. }
  1614. #else
  1615. // On UNIX, the name must be at least one character long.
  1616. if(name.length() < 1)
  1617. {
  1618. return false;
  1619. }
  1620. #endif
  1621. // On UNIX, the name must begin in a '/'.
  1622. // On Windows, if the name begins in a '/', then it is a full
  1623. // network path.
  1624. if(name[0] == '/')
  1625. {
  1626. return true;
  1627. }
  1628. return false;
  1629. }
  1630. bool SystemTools::GetShortPath(const char* path, kwsys_stl::string& shortPath)
  1631. {
  1632. #if defined(WIN32) && !defined(__CYGWIN__)
  1633. const int size = int(strlen(path)) +1; // size of return
  1634. char *buffer = new char[size]; // create a buffer
  1635. char *tempPath = new char[size]; // create a buffer
  1636. int ret;
  1637. // if the path passed in has quotes around it, first remove the quotes
  1638. if (path[0] == '"' && path[strlen(path)-1] == '"')
  1639. {
  1640. strcpy(tempPath,path+1);
  1641. tempPath[strlen(tempPath)-1] = '\0';
  1642. }
  1643. else
  1644. {
  1645. strcpy(tempPath,path);
  1646. }
  1647. buffer[0] = 0;
  1648. ret = GetShortPathName(tempPath, buffer, size);
  1649. if(buffer[0] == 0 || ret > size)
  1650. {
  1651. delete [] buffer;
  1652. delete [] tempPath;
  1653. return false;
  1654. }
  1655. else
  1656. {
  1657. shortPath = buffer;
  1658. delete [] buffer;
  1659. delete [] tempPath;
  1660. return true;
  1661. }
  1662. #else
  1663. shortPath = path;
  1664. return true;
  1665. #endif
  1666. }
  1667. void SystemTools::SplitProgramFromArgs(const char* path,
  1668. kwsys_stl::string& program, kwsys_stl::string& args)
  1669. {
  1670. // see if this is a full path to a program
  1671. // if so then set program to path and args to nothing
  1672. if(SystemTools::FileExists(path))
  1673. {
  1674. program = path;
  1675. args = "";
  1676. return;
  1677. }
  1678. // Try to find the program in the path, note the program
  1679. // may have spaces in its name so we have to look for it
  1680. kwsys_stl::vector<kwsys_stl::string> e;
  1681. kwsys_stl::string findProg = SystemTools::FindProgram(path, e);
  1682. if(findProg.size())
  1683. {
  1684. program = findProg;
  1685. args = "";
  1686. return;
  1687. }
  1688. // Now try and peel off space separated chunks from the end of the string
  1689. // so the largest path possible is found allowing for spaces in the path
  1690. kwsys_stl::string dir = path;
  1691. kwsys_stl::string::size_type spacePos = dir.rfind(' ');
  1692. while(spacePos != kwsys_stl::string::npos)
  1693. {
  1694. kwsys_stl::string tryProg = dir.substr(0, spacePos);
  1695. // See if the file exists
  1696. if(SystemTools::FileExists(tryProg.c_str()))
  1697. {
  1698. program = tryProg;
  1699. // remove trailing spaces from program
  1700. kwsys_stl::string::size_type pos = program.size()-1;
  1701. while(program[pos] == ' ')
  1702. {
  1703. program.erase(pos);
  1704. pos--;
  1705. }
  1706. args = dir.substr(spacePos, dir.size()-spacePos);
  1707. return;
  1708. }
  1709. // Now try and find the the program in the path
  1710. findProg = SystemTools::FindProgram(tryProg.c_str(), e);
  1711. if(findProg.size())
  1712. {
  1713. program = findProg;
  1714. // remove trailing spaces from program
  1715. kwsys_stl::string::size_type pos = program.size()-1;
  1716. while(program[pos] == ' ')
  1717. {
  1718. program.erase(pos);
  1719. pos--;
  1720. }
  1721. args = dir.substr(spacePos, dir.size()-spacePos);
  1722. return;
  1723. }
  1724. // move past the space for the next search
  1725. spacePos--;
  1726. spacePos = dir.rfind(' ', spacePos);
  1727. }
  1728. program = "";
  1729. args = "";
  1730. }
  1731. kwsys_stl::string SystemTools::GetCurrentDateTime(const char* format)
  1732. {
  1733. char buf[1024];
  1734. time_t t;
  1735. time(&t);
  1736. strftime(buf, sizeof(buf), format, localtime(&t));
  1737. return buf;
  1738. }
  1739. kwsys_stl::string SystemTools::MakeCindentifier(const char* s)
  1740. {
  1741. kwsys_stl::string str(s);
  1742. if (str.find_first_of("0123456789") == 0)
  1743. {
  1744. str = "_" + str;
  1745. }
  1746. kwsys_stl::string permited_chars("_"
  1747. "abcdefghijklmnopqrstuvwxyz"
  1748. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1749. "0123456789");
  1750. kwsys_stl::string::size_type pos = 0;
  1751. while ((pos = str.find_first_not_of(permited_chars, pos)) != kwsys_stl::string::npos)
  1752. {
  1753. str[pos] = '_';
  1754. }
  1755. return str;
  1756. }
  1757. // Due to a buggy stream library on the HP and another on Mac OSX, we
  1758. // need this very carefully written version of getline. Returns true
  1759. // if any data were read before the end-of-file was reached.
  1760. bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, kwsys_stl::string& line,
  1761. bool *has_newline /* = 0 */)
  1762. {
  1763. const int bufferSize = 1024;
  1764. char buffer[bufferSize];
  1765. line = "";
  1766. bool haveData = false;
  1767. if ( has_newline )
  1768. {
  1769. *has_newline = false;
  1770. }
  1771. // If no characters are read from the stream, the end of file has
  1772. // been reached.
  1773. while((is.getline(buffer, bufferSize), is.gcount() > 0))
  1774. {
  1775. haveData = true;
  1776. line.append(buffer);
  1777. // If newline character was read, the gcount includes the
  1778. // character, but the buffer does not. The end of line has been
  1779. // reached.
  1780. if(strlen(buffer) < static_cast<size_t>(is.gcount()))
  1781. {
  1782. if ( has_newline )
  1783. {
  1784. *has_newline = true;
  1785. }
  1786. break;
  1787. }
  1788. // The fail bit may be set. Clear it.
  1789. is.clear(is.rdstate() & ~kwsys_ios::ios::failbit);
  1790. }
  1791. return haveData;
  1792. }
  1793. int SystemTools::GetTerminalWidth()
  1794. {
  1795. int width = -1;
  1796. #ifndef _WIN32
  1797. struct winsize ws;
  1798. char *columns; /* Unix98 environment variable */
  1799. if(ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col>0 && ws.ws_row>0)
  1800. {
  1801. width = ws.ws_col;
  1802. }
  1803. if(!isatty(STDOUT_FILENO))
  1804. {
  1805. width = -1;
  1806. }
  1807. columns = getenv("COLUMNS");
  1808. if(columns && *columns)
  1809. {
  1810. long t;
  1811. char *endptr;
  1812. t = strtol(columns, &endptr, 0);
  1813. if(endptr && !*endptr && (t>0) && (t<1000))
  1814. {
  1815. width = (int)t;
  1816. }
  1817. }
  1818. if ( width < 9 )
  1819. {
  1820. width = -1;
  1821. }
  1822. #endif
  1823. return width;
  1824. }
  1825. bool SystemTools::GetPermissions(const char* file, mode_t& mode)
  1826. {
  1827. if ( !file )
  1828. {
  1829. return false;
  1830. }
  1831. struct stat st;
  1832. if ( stat(file, &st) < 0 )
  1833. {
  1834. return false;
  1835. }
  1836. mode = st.st_mode;
  1837. return true;
  1838. }
  1839. bool SystemTools::SetPermissions(const char* file, mode_t mode)
  1840. {
  1841. if ( !file )
  1842. {
  1843. return false;
  1844. }
  1845. if ( !SystemTools::FileExists(file) )
  1846. {
  1847. return false;
  1848. }
  1849. if ( chmod(file, mode) < 0 )
  1850. {
  1851. return false;
  1852. }
  1853. return true;
  1854. }
  1855. kwsys_stl::string SystemTools::GetParentDirectory(const char* fileOrDir)
  1856. {
  1857. if ( !fileOrDir || !*fileOrDir )
  1858. {
  1859. return "";
  1860. }
  1861. kwsys_stl::string res = fileOrDir;
  1862. SystemTools::ConvertToUnixSlashes(res);
  1863. kwsys_stl::string::size_type cc = res.size()-1;
  1864. if ( res[cc] == '/' )
  1865. {
  1866. cc --;
  1867. }
  1868. for ( ; cc > 0; cc -- )
  1869. {
  1870. if ( res[cc] == '/' )
  1871. {
  1872. break;
  1873. }
  1874. }
  1875. return res.substr(0, cc);
  1876. }
  1877. bool SystemTools::IsSubDirectory(const char* cSubdir, const char* cDir)
  1878. {
  1879. kwsys_stl::string subdir = cSubdir;
  1880. kwsys_stl::string dir = cDir;
  1881. SystemTools::ConvertToUnixSlashes(dir);
  1882. kwsys_stl::string path = subdir;
  1883. do
  1884. {
  1885. path = SystemTools::GetParentDirectory(path.c_str());
  1886. if ( dir == path )
  1887. {
  1888. return true;
  1889. }
  1890. }
  1891. while ( path.size() > dir.size() );
  1892. return false;
  1893. }
  1894. kwsys_stl::string SystemTools::FileExistsInParentDirectories(const char* fname,
  1895. const char* directory, const char* toplevel)
  1896. {
  1897. kwsys_stl::string file = fname;
  1898. SystemTools::ConvertToUnixSlashes(file);
  1899. kwsys_stl::string dir = directory;
  1900. SystemTools::ConvertToUnixSlashes(dir);
  1901. while ( !dir.empty() )
  1902. {
  1903. kwsys_stl::string path = dir + "/" + file;
  1904. if ( SystemTools::FileExists(path.c_str()) )
  1905. {
  1906. return path;
  1907. }
  1908. if ( dir.size() < strlen(toplevel) )
  1909. {
  1910. break;
  1911. }
  1912. dir = SystemTools::GetParentDirectory(dir.c_str());
  1913. }
  1914. return "";
  1915. }
  1916. // These must NOT be initialized. Default initialization to zero is
  1917. // necessary.
  1918. unsigned int SystemToolsManagerCount;
  1919. SystemToolsTranslationMap *SystemTools::TranslationMap;
  1920. // SystemToolsManager manages the SystemTools singleton.
  1921. // SystemToolsManager should be included in any translation unit
  1922. // that will use SystemTools or that implements the singleton
  1923. // pattern. It makes sure that the SystemTools singleton is created
  1924. // before and destroyed after all other singletons in CMake.
  1925. SystemToolsManager::SystemToolsManager()
  1926. {
  1927. if(++SystemToolsManagerCount == 1)
  1928. {
  1929. SystemTools::ClassInitialize();
  1930. }
  1931. }
  1932. SystemToolsManager::~SystemToolsManager()
  1933. {
  1934. if(--SystemToolsManagerCount == 0)
  1935. {
  1936. SystemTools::ClassFinalize();
  1937. }
  1938. }
  1939. void SystemTools::ClassInitialize()
  1940. {
  1941. SystemTools::TranslationMap = new SystemToolsTranslationMap;
  1942. }
  1943. void SystemTools::ClassFinalize()
  1944. {
  1945. delete SystemTools::TranslationMap;
  1946. }
  1947. } // namespace KWSYS_NAMESPACE
  1948. #if defined(_MSC_VER) && defined(_DEBUG)
  1949. # include <crtdbg.h>
  1950. # include <stdio.h>
  1951. # include <stdlib.h>
  1952. namespace KWSYS_NAMESPACE
  1953. {
  1954. static int SystemToolsDebugReport(int, char* message, int*)
  1955. {
  1956. fprintf(stderr, message);
  1957. exit(1);
  1958. }
  1959. void SystemTools::EnableMSVCDebugHook()
  1960. {
  1961. if(getenv("DART_TEST_FROM_DART"))
  1962. {
  1963. _CrtSetReportHook(SystemToolsDebugReport);
  1964. }
  1965. }
  1966. } // namespace KWSYS_NAMESPACE
  1967. #else
  1968. namespace KWSYS_NAMESPACE
  1969. {
  1970. void SystemTools::EnableMSVCDebugHook() {}
  1971. } // namespace KWSYS_NAMESPACE
  1972. #endif