SystemTools.cxx 40 KB

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