SystemTools.cxx 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143
  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. // Work-around CMake dependency scanning limitation. This must
  17. // duplicate the above list of headers.
  18. #if 0
  19. # include "SystemTools.hxx.in"
  20. # include "Directory.hxx.in"
  21. # include "kwsys_ios_iostream.h.in"
  22. # include "kwsys_ios_fstream.h.in"
  23. # include "kwsys_ios_sstream.h.in"
  24. #endif
  25. #ifdef _MSC_VER
  26. # pragma warning (disable: 4786)
  27. #endif
  28. #if defined(__sgi) && !defined(__GNUC__)
  29. # pragma set woff 1375 /* base class destructor not virtual */
  30. #endif
  31. #include <ctype.h>
  32. #include <errno.h>
  33. #ifdef __QNX__
  34. # include <malloc.h> /* for malloc/free on QNX */
  35. #endif
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/stat.h>
  40. #include <time.h>
  41. // support for realpath call
  42. #ifndef _WIN32
  43. #include <limits.h>
  44. #include <sys/param.h>
  45. #include <sys/wait.h>
  46. #include <sys/ioctl.h>
  47. #include <unistd.h>
  48. #include <termios.h>
  49. #include <signal.h> /* sigprocmask */
  50. #endif
  51. // Windows API. Some parts used even on cygwin.
  52. #if defined(_WIN32)
  53. # include <windows.h>
  54. #endif
  55. // This is a hack to prevent warnings about these functions being
  56. // declared but not referenced.
  57. #if defined(__sgi) && !defined(__GNUC__)
  58. # include <sys/termios.h>
  59. namespace KWSYS_NAMESPACE
  60. {
  61. class SystemToolsHack
  62. {
  63. public:
  64. enum
  65. {
  66. Ref1 = sizeof(cfgetospeed(0)),
  67. Ref2 = sizeof(cfgetispeed(0)),
  68. Ref3 = sizeof(tcgetattr(0, 0)),
  69. Ref4 = sizeof(tcsetattr(0, 0, 0)),
  70. Ref5 = sizeof(cfsetospeed(0,0)),
  71. Ref6 = sizeof(cfsetispeed(0,0))
  72. };
  73. };
  74. }
  75. #endif
  76. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__WATCOMC__) ||defined(__BORLANDC__) || defined(__MINGW32__))
  77. #include <io.h>
  78. #include <direct.h>
  79. #define _unlink unlink
  80. #endif
  81. /* The maximum length of a file name. */
  82. #if defined(PATH_MAX)
  83. # define KWSYS_SYSTEMTOOLS_MAXPATH PATH_MAX
  84. #elif defined(MAXPATHLEN)
  85. # define KWSYS_SYSTEMTOOLS_MAXPATH MAXPATHLEN
  86. #else
  87. # define KWSYS_SYSTEMTOOLS_MAXPATH 16384
  88. #endif
  89. #if defined(__WATCOMC__)
  90. #include <direct.h>
  91. #define _mkdir mkdir
  92. #define _rmdir rmdir
  93. #define _getcwd getcwd
  94. #define _chdir chdir
  95. #endif
  96. #if defined(__BEOS__) && !defined(__ZETA__)
  97. #include <be/kernel/OS.h>
  98. #include <be/storage/Path.h>
  99. // BeOS 5 doesn't have usleep(), but it has snooze(), which is identical.
  100. static inline void usleep(unsigned int msec)
  101. {
  102. ::snooze(msec);
  103. }
  104. // BeOS 5 also doesn't have realpath(), but its C++ API offers something close.
  105. static inline char *realpath(const char *path, char *resolved_path)
  106. {
  107. const size_t maxlen = KWSYS_SYSTEMTOOLS_MAXPATH;
  108. snprintf(resolved_path, maxlen, "%s", path);
  109. BPath normalized(resolved_path, NULL, true);
  110. const char *resolved = normalized.Path();
  111. if (resolved != NULL) // NULL == No such file.
  112. {
  113. if (snprintf(resolved_path, maxlen, "%s", resolved) < maxlen)
  114. {
  115. return resolved_path;
  116. }
  117. }
  118. return NULL; // something went wrong.
  119. }
  120. #endif
  121. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__MINGW32__))
  122. inline int Mkdir(const char* dir)
  123. {
  124. return _mkdir(dir);
  125. }
  126. inline int Rmdir(const char* dir)
  127. {
  128. return _rmdir(dir);
  129. }
  130. inline const char* Getcwd(char* buf, unsigned int len)
  131. {
  132. const char* ret = _getcwd(buf, len);
  133. if(!ret)
  134. {
  135. fprintf(stderr, "No current working directory.\n");
  136. abort();
  137. }
  138. return ret;
  139. }
  140. inline int Chdir(const char* dir)
  141. {
  142. #if defined(__BORLANDC__)
  143. return chdir(dir);
  144. #else
  145. return _chdir(dir);
  146. #endif
  147. }
  148. inline void Realpath(const char *path, kwsys_stl::string & resolved_path)
  149. {
  150. char *ptemp;
  151. char fullpath[MAX_PATH];
  152. if( GetFullPathName(path, sizeof(fullpath), fullpath, &ptemp) )
  153. {
  154. resolved_path = fullpath;
  155. KWSYS_NAMESPACE::SystemTools::ConvertToUnixSlashes(resolved_path);
  156. }
  157. }
  158. #else
  159. #include <sys/types.h>
  160. #include <fcntl.h>
  161. #include <unistd.h>
  162. inline int Mkdir(const char* dir)
  163. {
  164. return mkdir(dir, 00777);
  165. }
  166. inline int Rmdir(const char* dir)
  167. {
  168. return rmdir(dir);
  169. }
  170. inline const char* Getcwd(char* buf, unsigned int len)
  171. {
  172. const char* ret = getcwd(buf, len);
  173. if(!ret)
  174. {
  175. fprintf(stderr, "No current working directory\n");
  176. abort();
  177. }
  178. return ret;
  179. }
  180. inline int Chdir(const char* dir)
  181. {
  182. return chdir(dir);
  183. }
  184. inline void Realpath(const char *path, kwsys_stl::string & resolved_path)
  185. {
  186. char resolved_name[KWSYS_SYSTEMTOOLS_MAXPATH];
  187. realpath(path, resolved_name);
  188. resolved_path = resolved_name;
  189. }
  190. #endif
  191. #if !defined(_WIN32) && defined(__COMO__)
  192. // Hack for como strict mode to avoid defining _SVID_SOURCE or _BSD_SOURCE.
  193. extern "C"
  194. {
  195. extern FILE *popen (__const char *__command, __const char *__modes) __THROW;
  196. extern int pclose (FILE *__stream) __THROW;
  197. extern char *realpath (__const char *__restrict __name,
  198. char *__restrict __resolved) __THROW;
  199. extern char *strdup (__const char *__s) __THROW;
  200. extern int putenv (char *__string) __THROW;
  201. }
  202. #endif
  203. /* Implement floattime() for various platforms */
  204. // Taken from Python 2.1.3
  205. #if defined( _WIN32 ) && !defined( __CYGWIN__ )
  206. # include <sys/timeb.h>
  207. # define HAVE_FTIME
  208. # if defined( __BORLANDC__)
  209. # define FTIME ftime
  210. # define TIMEB timeb
  211. # else // Visual studio?
  212. # define FTIME _ftime
  213. # define TIMEB _timeb
  214. # endif
  215. #elif defined( __CYGWIN__ ) || defined( __linux__ )
  216. # include <sys/time.h>
  217. # include <time.h>
  218. # define HAVE_GETTIMEOFDAY
  219. #endif
  220. namespace KWSYS_NAMESPACE
  221. {
  222. class SystemToolsTranslationMap :
  223. public kwsys_stl::map<kwsys_stl::string,kwsys_stl::string>
  224. {
  225. };
  226. double
  227. SystemTools::GetTime(void)
  228. {
  229. /* There are three ways to get the time:
  230. (1) gettimeofday() -- resolution in microseconds
  231. (2) ftime() -- resolution in milliseconds
  232. (3) time() -- resolution in seconds
  233. In all cases the return value is a float in seconds.
  234. Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  235. fail, so we fall back on ftime() or time().
  236. Note: clock resolution does not imply clock accuracy! */
  237. #ifdef HAVE_GETTIMEOFDAY
  238. {
  239. struct timeval t;
  240. #ifdef GETTIMEOFDAY_NO_TZ
  241. if (gettimeofday(&t) == 0)
  242. return static_cast<double>(t.tv_sec) + t.tv_usec*0.000001;
  243. #else /* !GETTIMEOFDAY_NO_TZ */
  244. if (gettimeofday(&t, static_cast<struct timezone *>(NULL)) == 0)
  245. return static_cast<double>(t.tv_sec) + t.tv_usec*0.000001;
  246. #endif /* !GETTIMEOFDAY_NO_TZ */
  247. }
  248. #endif /* !HAVE_GETTIMEOFDAY */
  249. {
  250. #if defined(HAVE_FTIME)
  251. struct TIMEB t;
  252. ::FTIME(&t);
  253. return static_cast<double>(t.time) +
  254. static_cast<double>(t.millitm) * static_cast<double>(0.001);
  255. #else /* !HAVE_FTIME */
  256. time_t secs;
  257. time(&secs);
  258. return static_cast<double>(secs);
  259. #endif /* !HAVE_FTIME */
  260. }
  261. }
  262. // adds the elements of the env variable path to the arg passed in
  263. void SystemTools::GetPath(kwsys_stl::vector<kwsys_stl::string>& path, const char* env)
  264. {
  265. #if defined(_WIN32) && !defined(__CYGWIN__)
  266. const char* pathSep = ";";
  267. #else
  268. const char* pathSep = ":";
  269. #endif
  270. if(!env)
  271. {
  272. env = "PATH";
  273. }
  274. const char* cpathEnv = SystemTools::GetEnv(env);
  275. if ( !cpathEnv )
  276. {
  277. return;
  278. }
  279. kwsys_stl::string pathEnv = cpathEnv;
  280. // A hack to make the below algorithm work.
  281. if(pathEnv[pathEnv.length()-1] != ':')
  282. {
  283. pathEnv += pathSep;
  284. }
  285. kwsys_stl::string::size_type start =0;
  286. bool done = false;
  287. while(!done)
  288. {
  289. kwsys_stl::string::size_type endpos = pathEnv.find(pathSep, start);
  290. if(endpos != kwsys_stl::string::npos)
  291. {
  292. kwsys_stl::string convertedPath;
  293. Realpath(pathEnv.substr(start, endpos-start).c_str(), convertedPath);
  294. path.push_back(convertedPath);
  295. start = endpos+1;
  296. }
  297. else
  298. {
  299. done = true;
  300. }
  301. }
  302. for(kwsys_stl::vector<kwsys_stl::string>::iterator i = path.begin();
  303. i != path.end(); ++i)
  304. {
  305. SystemTools::ConvertToUnixSlashes(*i);
  306. }
  307. }
  308. const char* SystemTools::GetEnv(const char* key)
  309. {
  310. return getenv(key);
  311. }
  312. bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result)
  313. {
  314. const char* v = getenv(key);
  315. if(v)
  316. {
  317. result = v;
  318. return true;
  319. }
  320. else
  321. {
  322. return false;
  323. }
  324. }
  325. const char* SystemTools::GetExecutableExtension()
  326. {
  327. #if defined(_WIN32) || defined(__CYGWIN__)
  328. return ".exe";
  329. #else
  330. return "";
  331. #endif
  332. }
  333. bool SystemTools::MakeDirectory(const char* path)
  334. {
  335. if(!path)
  336. {
  337. return false;
  338. }
  339. if(SystemTools::FileExists(path))
  340. {
  341. return true;
  342. }
  343. kwsys_stl::string dir = path;
  344. if(dir.size() == 0)
  345. {
  346. return false;
  347. }
  348. SystemTools::ConvertToUnixSlashes(dir);
  349. kwsys_stl::string::size_type pos = dir.find(':');
  350. if(pos == kwsys_stl::string::npos)
  351. {
  352. pos = 0;
  353. }
  354. kwsys_stl::string topdir;
  355. while((pos = dir.find('/', pos)) != kwsys_stl::string::npos)
  356. {
  357. topdir = dir.substr(0, pos);
  358. Mkdir(topdir.c_str());
  359. pos++;
  360. }
  361. if(dir[dir.size()-1] == '/')
  362. {
  363. topdir = dir.substr(0, dir.size());
  364. }
  365. else
  366. {
  367. topdir = dir;
  368. }
  369. if(Mkdir(topdir.c_str()) != 0)
  370. {
  371. // There is a bug in the Borland Run time library which makes MKDIR
  372. // return EACCES when it should return EEXISTS
  373. // if it is some other error besides directory exists
  374. // then return false
  375. if( (errno != EEXIST)
  376. #ifdef __BORLANDC__
  377. && (errno != EACCES)
  378. #endif
  379. )
  380. {
  381. return false;
  382. }
  383. }
  384. return true;
  385. }
  386. // replace replace with with as many times as it shows up in source.
  387. // write the result into source.
  388. void SystemTools::ReplaceString(kwsys_stl::string& source,
  389. const char* replace,
  390. const char* with)
  391. {
  392. const char *src = source.c_str();
  393. char *searchPos = const_cast<char *>(strstr(src,replace));
  394. // get out quick if string is not found
  395. if (!searchPos)
  396. {
  397. return;
  398. }
  399. // perform replacements until done
  400. size_t replaceSize = strlen(replace);
  401. char *orig = strdup(src);
  402. char *currentPos = orig;
  403. searchPos = searchPos - src + orig;
  404. // initialize the result
  405. source.erase(source.begin(),source.end());
  406. do
  407. {
  408. *searchPos = '\0';
  409. source += currentPos;
  410. currentPos = searchPos + replaceSize;
  411. // replace
  412. source += with;
  413. searchPos = strstr(currentPos,replace);
  414. }
  415. while (searchPos);
  416. // copy any trailing text
  417. source += currentPos;
  418. free(orig);
  419. }
  420. // Read a registry value.
  421. // Example :
  422. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  423. // => will return the data of the "default" value of the key
  424. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  425. // => will return the data of the "Root" value of the key
  426. #if defined(_WIN32) && !defined(__CYGWIN__)
  427. bool SystemTools::ReadRegistryValue(const char *key, kwsys_stl::string &value)
  428. {
  429. kwsys_stl::string primary = key;
  430. kwsys_stl::string second;
  431. kwsys_stl::string valuename;
  432. size_t start = primary.find("\\");
  433. if (start == kwsys_stl::string::npos)
  434. {
  435. return false;
  436. }
  437. size_t valuenamepos = primary.find(";");
  438. if (valuenamepos != kwsys_stl::string::npos)
  439. {
  440. valuename = primary.substr(valuenamepos+1);
  441. }
  442. second = primary.substr(start+1, valuenamepos-start-1);
  443. primary = primary.substr(0, start);
  444. HKEY primaryKey = HKEY_CURRENT_USER;
  445. if (primary == "HKEY_CURRENT_USER")
  446. {
  447. primaryKey = HKEY_CURRENT_USER;
  448. }
  449. if (primary == "HKEY_CURRENT_CONFIG")
  450. {
  451. primaryKey = HKEY_CURRENT_CONFIG;
  452. }
  453. if (primary == "HKEY_CLASSES_ROOT")
  454. {
  455. primaryKey = HKEY_CLASSES_ROOT;
  456. }
  457. if (primary == "HKEY_LOCAL_MACHINE")
  458. {
  459. primaryKey = HKEY_LOCAL_MACHINE;
  460. }
  461. if (primary == "HKEY_USERS")
  462. {
  463. primaryKey = HKEY_USERS;
  464. }
  465. HKEY hKey;
  466. if(RegOpenKeyEx(primaryKey,
  467. second.c_str(),
  468. 0,
  469. KEY_READ,
  470. &hKey) != ERROR_SUCCESS)
  471. {
  472. return false;
  473. }
  474. else
  475. {
  476. DWORD dwType, dwSize;
  477. dwSize = 1023;
  478. char data[1024];
  479. if(RegQueryValueEx(hKey,
  480. (LPTSTR)valuename.c_str(),
  481. NULL,
  482. &dwType,
  483. (BYTE *)data,
  484. &dwSize) == ERROR_SUCCESS)
  485. {
  486. if (dwType == REG_SZ)
  487. {
  488. value = data;
  489. RegCloseKey(hKey);
  490. return true;
  491. }
  492. }
  493. }
  494. return false;
  495. }
  496. #else
  497. bool SystemTools::ReadRegistryValue(const char *, kwsys_stl::string &)
  498. {
  499. return false;
  500. }
  501. #endif
  502. // Write a registry value.
  503. // Example :
  504. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  505. // => will set the data of the "default" value of the key
  506. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  507. // => will set the data of the "Root" value of the key
  508. #if defined(_WIN32) && !defined(__CYGWIN__)
  509. bool SystemTools::WriteRegistryValue(const char *key, const char *value)
  510. {
  511. kwsys_stl::string primary = key;
  512. kwsys_stl::string second;
  513. kwsys_stl::string valuename;
  514. size_t start = primary.find("\\");
  515. if (start == kwsys_stl::string::npos)
  516. {
  517. return false;
  518. }
  519. size_t valuenamepos = primary.find(";");
  520. if (valuenamepos != kwsys_stl::string::npos)
  521. {
  522. valuename = primary.substr(valuenamepos+1);
  523. }
  524. second = primary.substr(start+1, valuenamepos-start-1);
  525. primary = primary.substr(0, start);
  526. HKEY primaryKey = HKEY_CURRENT_USER;
  527. if (primary == "HKEY_CURRENT_USER")
  528. {
  529. primaryKey = HKEY_CURRENT_USER;
  530. }
  531. if (primary == "HKEY_CURRENT_CONFIG")
  532. {
  533. primaryKey = HKEY_CURRENT_CONFIG;
  534. }
  535. if (primary == "HKEY_CLASSES_ROOT")
  536. {
  537. primaryKey = HKEY_CLASSES_ROOT;
  538. }
  539. if (primary == "HKEY_LOCAL_MACHINE")
  540. {
  541. primaryKey = HKEY_LOCAL_MACHINE;
  542. }
  543. if (primary == "HKEY_USERS")
  544. {
  545. primaryKey = HKEY_USERS;
  546. }
  547. HKEY hKey;
  548. DWORD dwDummy;
  549. if(RegCreateKeyEx(primaryKey,
  550. second.c_str(),
  551. 0,
  552. "",
  553. REG_OPTION_NON_VOLATILE,
  554. KEY_WRITE,
  555. NULL,
  556. &hKey,
  557. &dwDummy) != ERROR_SUCCESS)
  558. {
  559. return false;
  560. }
  561. if(RegSetValueEx(hKey,
  562. (LPTSTR)valuename.c_str(),
  563. 0,
  564. REG_SZ,
  565. (CONST BYTE *)value,
  566. (DWORD)(strlen(value) + 1)) == ERROR_SUCCESS)
  567. {
  568. return true;
  569. }
  570. return false;
  571. }
  572. #else
  573. bool SystemTools::WriteRegistryValue(const char *, const char *)
  574. {
  575. return false;
  576. }
  577. #endif
  578. // Delete a registry value.
  579. // Example :
  580. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  581. // => will delete the data of the "default" value of the key
  582. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  583. // => will delete the data of the "Root" value of the key
  584. #if defined(_WIN32) && !defined(__CYGWIN__)
  585. bool SystemTools::DeleteRegistryValue(const char *key)
  586. {
  587. kwsys_stl::string primary = key;
  588. kwsys_stl::string second;
  589. kwsys_stl::string valuename;
  590. size_t start = primary.find("\\");
  591. if (start == kwsys_stl::string::npos)
  592. {
  593. return false;
  594. }
  595. size_t valuenamepos = primary.find(";");
  596. if (valuenamepos != kwsys_stl::string::npos)
  597. {
  598. valuename = primary.substr(valuenamepos+1);
  599. }
  600. second = primary.substr(start+1, valuenamepos-start-1);
  601. primary = primary.substr(0, start);
  602. HKEY primaryKey = HKEY_CURRENT_USER;
  603. if (primary == "HKEY_CURRENT_USER")
  604. {
  605. primaryKey = HKEY_CURRENT_USER;
  606. }
  607. if (primary == "HKEY_CURRENT_CONFIG")
  608. {
  609. primaryKey = HKEY_CURRENT_CONFIG;
  610. }
  611. if (primary == "HKEY_CLASSES_ROOT")
  612. {
  613. primaryKey = HKEY_CLASSES_ROOT;
  614. }
  615. if (primary == "HKEY_LOCAL_MACHINE")
  616. {
  617. primaryKey = HKEY_LOCAL_MACHINE;
  618. }
  619. if (primary == "HKEY_USERS")
  620. {
  621. primaryKey = HKEY_USERS;
  622. }
  623. HKEY hKey;
  624. if(RegOpenKeyEx(primaryKey,
  625. second.c_str(),
  626. 0,
  627. KEY_WRITE,
  628. &hKey) != ERROR_SUCCESS)
  629. {
  630. return false;
  631. }
  632. else
  633. {
  634. if(RegDeleteValue(hKey,
  635. (LPTSTR)valuename.c_str()) == ERROR_SUCCESS)
  636. {
  637. RegCloseKey(hKey);
  638. return true;
  639. }
  640. }
  641. return false;
  642. }
  643. #else
  644. bool SystemTools::DeleteRegistryValue(const char *)
  645. {
  646. return false;
  647. }
  648. #endif
  649. bool SystemTools::SameFile(const char* file1, const char* file2)
  650. {
  651. #ifdef _WIN32
  652. HANDLE hFile1, hFile2;
  653. hFile1 = CreateFile( file1,
  654. GENERIC_READ,
  655. FILE_SHARE_READ ,
  656. NULL,
  657. OPEN_EXISTING,
  658. FILE_FLAG_BACKUP_SEMANTICS,
  659. NULL
  660. );
  661. hFile2 = CreateFile( file2,
  662. GENERIC_READ,
  663. FILE_SHARE_READ,
  664. NULL,
  665. OPEN_EXISTING,
  666. FILE_FLAG_BACKUP_SEMANTICS,
  667. NULL
  668. );
  669. if( hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE)
  670. {
  671. if(hFile1 != INVALID_HANDLE_VALUE)
  672. {
  673. CloseHandle(hFile1);
  674. }
  675. if(hFile2 != INVALID_HANDLE_VALUE)
  676. {
  677. CloseHandle(hFile2);
  678. }
  679. return false;
  680. }
  681. BY_HANDLE_FILE_INFORMATION fiBuf1;
  682. BY_HANDLE_FILE_INFORMATION fiBuf2;
  683. GetFileInformationByHandle( hFile1, &fiBuf1 );
  684. GetFileInformationByHandle( hFile2, &fiBuf2 );
  685. CloseHandle(hFile1);
  686. CloseHandle(hFile2);
  687. return (fiBuf1.dwVolumeSerialNumber == fiBuf2.dwVolumeSerialNumber &&
  688. fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh &&
  689. fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow);
  690. #else
  691. struct stat fileStat1, fileStat2;
  692. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  693. {
  694. // see if the files are the same file
  695. // check the device inode and size
  696. if(memcmp(&fileStat2.st_dev, &fileStat1.st_dev, sizeof(fileStat1.st_dev)) == 0 &&
  697. memcmp(&fileStat2.st_ino, &fileStat1.st_ino, sizeof(fileStat1.st_ino)) == 0 &&
  698. fileStat2.st_size == fileStat1.st_size
  699. )
  700. {
  701. return true;
  702. }
  703. }
  704. return false;
  705. #endif
  706. }
  707. // return true if the file exists
  708. bool SystemTools::FileExists(const char* filename)
  709. {
  710. #ifdef _MSC_VER
  711. # define access _access
  712. #endif
  713. #ifndef R_OK
  714. # define R_OK 04
  715. #endif
  716. if ( access(filename, R_OK) != 0 )
  717. {
  718. return false;
  719. }
  720. else
  721. {
  722. return true;
  723. }
  724. }
  725. bool SystemTools::FileTimeCompare(const char* f1, const char* f2,
  726. int* result)
  727. {
  728. // Default to same time.
  729. *result = 0;
  730. #if !defined(_WIN32) || defined(__CYGWIN__)
  731. // POSIX version. Use stat function to get file modification time.
  732. struct stat s1;
  733. if(stat(f1, &s1) != 0)
  734. {
  735. return false;
  736. }
  737. struct stat s2;
  738. if(stat(f2, &s2) != 0)
  739. {
  740. return false;
  741. }
  742. # if KWSYS_STAT_HAS_ST_MTIM
  743. // Compare using nanosecond resolution.
  744. if(s1.st_mtim.tv_sec < s2.st_mtim.tv_sec)
  745. {
  746. *result = -1;
  747. }
  748. else if(s1.st_mtim.tv_sec > s2.st_mtim.tv_sec)
  749. {
  750. *result = 1;
  751. }
  752. else if(s1.st_mtim.tv_nsec < s2.st_mtim.tv_nsec)
  753. {
  754. *result = -1;
  755. }
  756. else if(s1.st_mtim.tv_nsec > s2.st_mtim.tv_nsec)
  757. {
  758. *result = 1;
  759. }
  760. # else
  761. // Compare using 1 second resolution.
  762. if(s1.st_mtime < s2.st_mtime)
  763. {
  764. *result = -1;
  765. }
  766. else if(s1.st_mtime > s2.st_mtime)
  767. {
  768. *result = 1;
  769. }
  770. # endif
  771. #else
  772. // Windows version. Get the modification time from extended file attributes.
  773. WIN32_FILE_ATTRIBUTE_DATA f1d;
  774. WIN32_FILE_ATTRIBUTE_DATA f2d;
  775. if(!GetFileAttributesEx(f1, GetFileExInfoStandard, &f1d))
  776. {
  777. return false;
  778. }
  779. if(!GetFileAttributesEx(f2, GetFileExInfoStandard, &f2d))
  780. {
  781. return false;
  782. }
  783. // Compare the file times using resolution provided by system call.
  784. *result = (int)CompareFileTime(&f1d.ftLastWriteTime, &f2d.ftLastWriteTime);
  785. #endif
  786. return true;
  787. }
  788. // Return a capitalized string (i.e the first letter is uppercased, all other
  789. // are lowercased)
  790. kwsys_stl::string SystemTools::Capitalized(const kwsys_stl::string& s)
  791. {
  792. kwsys_stl::string n;
  793. if(s.size() == 0)
  794. {
  795. return n;
  796. }
  797. n.resize(s.size());
  798. n[0] = static_cast<kwsys_stl::string::value_type>(toupper(s[0]));
  799. for (size_t i = 1; i < s.size(); i++)
  800. {
  801. n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
  802. }
  803. return n;
  804. }
  805. // Return capitalized words
  806. kwsys_stl::string SystemTools::CapitalizedWords(const kwsys_stl::string& s)
  807. {
  808. kwsys_stl::string n(s);
  809. for (size_t i = 0; i < s.size(); i++)
  810. {
  811. #if defined(_MSC_VER) && defined (_MT) && defined (_DEBUG)
  812. // MS has an assert that will fail if s[i] < 0; setting
  813. // LC_CTYPE using setlocale() does *not* help. Painful.
  814. if ((int)s[i] >= 0 && isalpha(s[i]) &&
  815. (i == 0 || ((int)s[i - 1] >= 0 && isspace(s[i - 1]))))
  816. #else
  817. if (isalpha(s[i]) && (i == 0 || isspace(s[i - 1])))
  818. #endif
  819. {
  820. n[i] = static_cast<kwsys_stl::string::value_type>(toupper(s[i]));
  821. }
  822. }
  823. return n;
  824. }
  825. // Return uncapitalized words
  826. kwsys_stl::string SystemTools::UnCapitalizedWords(const kwsys_stl::string& s)
  827. {
  828. kwsys_stl::string n(s);
  829. for (size_t i = 0; i < s.size(); i++)
  830. {
  831. #if defined(_MSC_VER) && defined (_MT) && defined (_DEBUG)
  832. // MS has an assert that will fail if s[i] < 0; setting
  833. // LC_CTYPE using setlocale() does *not* help. Painful.
  834. if ((int)s[i] >= 0 && isalpha(s[i]) &&
  835. (i == 0 || ((int)s[i - 1] >= 0 && isspace(s[i - 1]))))
  836. #else
  837. if (isalpha(s[i]) && (i == 0 || isspace(s[i - 1])))
  838. #endif
  839. {
  840. n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
  841. }
  842. }
  843. return n;
  844. }
  845. // only works for words with at least two letters
  846. kwsys_stl::string SystemTools::AddSpaceBetweenCapitalizedWords(
  847. const kwsys_stl::string& s)
  848. {
  849. kwsys_stl::string n;
  850. if (s.size())
  851. {
  852. n.reserve(s.size());
  853. n += s[0];
  854. for (size_t i = 1; i < s.size(); i++)
  855. {
  856. if (isupper(s[i]) && !isspace(s[i - 1]) && !isupper(s[i - 1]))
  857. {
  858. n += ' ';
  859. }
  860. n += s[i];
  861. }
  862. }
  863. return n;
  864. }
  865. char* SystemTools::AppendStrings(const char* str1, const char* str2)
  866. {
  867. if (!str1)
  868. {
  869. return SystemTools::DuplicateString(str2);
  870. }
  871. if (!str2)
  872. {
  873. return SystemTools::DuplicateString(str1);
  874. }
  875. size_t len1 = strlen(str1);
  876. char *newstr = new char[len1 + strlen(str2) + 1];
  877. if (!newstr)
  878. {
  879. return 0;
  880. }
  881. strcpy(newstr, str1);
  882. strcat(newstr + len1, str2);
  883. return newstr;
  884. }
  885. char* SystemTools::AppendStrings(
  886. const char* str1, const char* str2, const char* str3)
  887. {
  888. if (!str1)
  889. {
  890. return SystemTools::AppendStrings(str2, str3);
  891. }
  892. if (!str2)
  893. {
  894. return SystemTools::AppendStrings(str1, str3);
  895. }
  896. if (!str3)
  897. {
  898. return SystemTools::AppendStrings(str1, str2);
  899. }
  900. size_t len1 = strlen(str1), len2 = strlen(str2);
  901. char *newstr = new char[len1 + len2 + strlen(str3) + 1];
  902. if (!newstr)
  903. {
  904. return 0;
  905. }
  906. strcpy(newstr, str1);
  907. strcat(newstr + len1, str2);
  908. strcat(newstr + len1 + len2, str3);
  909. return newstr;
  910. }
  911. // Return a lower case string
  912. kwsys_stl::string SystemTools::LowerCase(const kwsys_stl::string& s)
  913. {
  914. kwsys_stl::string n;
  915. n.resize(s.size());
  916. for (size_t i = 0; i < s.size(); i++)
  917. {
  918. n[i] = static_cast<kwsys_stl::string::value_type>(tolower(s[i]));
  919. }
  920. return n;
  921. }
  922. // Return a lower case string
  923. kwsys_stl::string SystemTools::UpperCase(const kwsys_stl::string& s)
  924. {
  925. kwsys_stl::string n;
  926. n.resize(s.size());
  927. for (size_t i = 0; i < s.size(); i++)
  928. {
  929. n[i] = static_cast<kwsys_stl::string::value_type>(toupper(s[i]));
  930. }
  931. return n;
  932. }
  933. // Count char in string
  934. size_t SystemTools::CountChar(const char* str, char c)
  935. {
  936. size_t count = 0;
  937. if (str)
  938. {
  939. while (*str)
  940. {
  941. if (*str == c)
  942. {
  943. ++count;
  944. }
  945. ++str;
  946. }
  947. }
  948. return count;
  949. }
  950. // Remove chars in string
  951. char* SystemTools::RemoveChars(const char* str, const char *toremove)
  952. {
  953. if (!str)
  954. {
  955. return NULL;
  956. }
  957. char *clean_str = new char [strlen(str) + 1];
  958. char *ptr = clean_str;
  959. while (*str)
  960. {
  961. const char *str2 = toremove;
  962. while (*str2 && *str != *str2)
  963. {
  964. ++str2;
  965. }
  966. if (!*str2)
  967. {
  968. *ptr++ = *str;
  969. }
  970. ++str;
  971. }
  972. *ptr = '\0';
  973. return clean_str;
  974. }
  975. // Remove chars in string
  976. char* SystemTools::RemoveCharsButUpperHex(const char* str)
  977. {
  978. if (!str)
  979. {
  980. return 0;
  981. }
  982. char *clean_str = new char [strlen(str) + 1];
  983. char *ptr = clean_str;
  984. while (*str)
  985. {
  986. if ((*str >= '0' && *str <= '9') || (*str >= 'A' && *str <= 'F'))
  987. {
  988. *ptr++ = *str;
  989. }
  990. ++str;
  991. }
  992. *ptr = '\0';
  993. return clean_str;
  994. }
  995. // Replace chars in string
  996. char* SystemTools::ReplaceChars(char* str, const char *toreplace, char replacement)
  997. {
  998. if (str)
  999. {
  1000. char *ptr = str;
  1001. while (*ptr)
  1002. {
  1003. const char *ptr2 = toreplace;
  1004. while (*ptr2)
  1005. {
  1006. if (*ptr == *ptr2)
  1007. {
  1008. *ptr = replacement;
  1009. }
  1010. ++ptr2;
  1011. }
  1012. ++ptr;
  1013. }
  1014. }
  1015. return str;
  1016. }
  1017. // Returns if string starts with another string
  1018. bool SystemTools::StringStartsWith(const char* str1, const char* str2)
  1019. {
  1020. if (!str1 || !str2)
  1021. {
  1022. return false;
  1023. }
  1024. size_t len1 = strlen(str1), len2 = strlen(str2);
  1025. return len1 >= len2 && !strncmp(str1, str2, len2) ? true : false;
  1026. }
  1027. // Returns if string ends with another string
  1028. bool SystemTools::StringEndsWith(const char* str1, const char* str2)
  1029. {
  1030. if (!str1 || !str2)
  1031. {
  1032. return false;
  1033. }
  1034. size_t len1 = strlen(str1), len2 = strlen(str2);
  1035. return len1 >= len2 && !strncmp(str1 + (len1 - len2), str2, len2) ? true : false;
  1036. }
  1037. // Returns a pointer to the last occurence of str2 in str1
  1038. const char* SystemTools::FindLastString(const char* str1, const char* str2)
  1039. {
  1040. if (!str1 || !str2)
  1041. {
  1042. return NULL;
  1043. }
  1044. size_t len1 = strlen(str1), len2 = strlen(str2);
  1045. if (len1 >= len2)
  1046. {
  1047. const char *ptr = str1 + len1 - len2;
  1048. do
  1049. {
  1050. if (!strncmp(ptr, str2, len2))
  1051. {
  1052. return ptr;
  1053. }
  1054. } while (ptr-- != str1);
  1055. }
  1056. return NULL;
  1057. }
  1058. // Duplicate string
  1059. char* SystemTools::DuplicateString(const char* str)
  1060. {
  1061. if (str)
  1062. {
  1063. char *newstr = new char [strlen(str) + 1];
  1064. return strcpy(newstr, str);
  1065. }
  1066. return NULL;
  1067. }
  1068. // Return a cropped string
  1069. kwsys_stl::string SystemTools::CropString(const kwsys_stl::string& s,
  1070. size_t max_len)
  1071. {
  1072. if (!s.size() || max_len == 0 || max_len >= s.size())
  1073. {
  1074. return s;
  1075. }
  1076. kwsys_stl::string n;
  1077. n.reserve(max_len);
  1078. size_t middle = max_len / 2;
  1079. n += s.substr(0, middle);
  1080. n += s.substr(s.size() - (max_len - middle), kwsys_stl::string::npos);
  1081. if (max_len > 2)
  1082. {
  1083. n[middle] = '.';
  1084. if (max_len > 3)
  1085. {
  1086. n[middle - 1] = '.';
  1087. if (max_len > 4)
  1088. {
  1089. n[middle + 1] = '.';
  1090. }
  1091. }
  1092. }
  1093. return n;
  1094. }
  1095. //----------------------------------------------------------------------------
  1096. kwsys_stl::vector<kwsys::String> SystemTools::SplitString(const char* p, char sep, bool isPath)
  1097. {
  1098. kwsys_stl::string path = p;
  1099. kwsys_stl::vector<kwsys::String> paths;
  1100. if(isPath && path[0] == '/')
  1101. {
  1102. path.erase(path.begin());
  1103. paths.push_back("/");
  1104. }
  1105. kwsys_stl::string::size_type pos1 = 0;
  1106. kwsys_stl::string::size_type pos2 = path.find(sep, pos1+1);
  1107. while(pos2 != kwsys_stl::string::npos)
  1108. {
  1109. paths.push_back(path.substr(pos1, pos2-pos1));
  1110. pos1 = pos2+1;
  1111. pos2 = path.find(sep, pos1+1);
  1112. }
  1113. paths.push_back(path.substr(pos1, pos2-pos1));
  1114. return paths;
  1115. }
  1116. //----------------------------------------------------------------------------
  1117. int SystemTools::EstimateFormatLength(const char *format, va_list ap)
  1118. {
  1119. if (!format)
  1120. {
  1121. return 0;
  1122. }
  1123. // Quick-hack attempt at estimating the length of the string.
  1124. // Should never under-estimate.
  1125. // Start with the length of the format string itself.
  1126. size_t length = strlen(format);
  1127. // Increase the length for every argument in the format.
  1128. const char* cur = format;
  1129. while(*cur)
  1130. {
  1131. if(*cur++ == '%')
  1132. {
  1133. // Skip "%%" since it doesn't correspond to a va_arg.
  1134. if(*cur != '%')
  1135. {
  1136. while(!int(isalpha(*cur)))
  1137. {
  1138. ++cur;
  1139. }
  1140. switch (*cur)
  1141. {
  1142. case 's':
  1143. {
  1144. // Check the length of the string.
  1145. char* s = va_arg(ap, char*);
  1146. if(s)
  1147. {
  1148. length += strlen(s);
  1149. }
  1150. } break;
  1151. case 'e':
  1152. case 'f':
  1153. case 'g':
  1154. {
  1155. // Assume the argument contributes no more than 64 characters.
  1156. length += 64;
  1157. // Eat the argument.
  1158. static_cast<void>(va_arg(ap, double));
  1159. } break;
  1160. default:
  1161. {
  1162. // Assume the argument contributes no more than 64 characters.
  1163. length += 64;
  1164. // Eat the argument.
  1165. static_cast<void>(va_arg(ap, int));
  1166. } break;
  1167. }
  1168. }
  1169. // Move past the characters just tested.
  1170. ++cur;
  1171. }
  1172. }
  1173. return static_cast<int>(length);
  1174. }
  1175. kwsys_stl::string SystemTools::EscapeChars(
  1176. const char *str,
  1177. const char *chars_to_escape,
  1178. char escape_char)
  1179. {
  1180. kwsys_stl::string n;
  1181. if (str)
  1182. {
  1183. if (!chars_to_escape | !*chars_to_escape)
  1184. {
  1185. n.append(str);
  1186. }
  1187. else
  1188. {
  1189. n.reserve(strlen(str));
  1190. while (*str)
  1191. {
  1192. const char *ptr = chars_to_escape;
  1193. while (*ptr)
  1194. {
  1195. if (*str == *ptr)
  1196. {
  1197. n += escape_char;
  1198. break;
  1199. }
  1200. ++ptr;
  1201. }
  1202. n += *str;
  1203. ++str;
  1204. }
  1205. }
  1206. }
  1207. return n;
  1208. }
  1209. // convert windows slashes to unix slashes
  1210. void SystemTools::ConvertToUnixSlashes(kwsys_stl::string& path)
  1211. {
  1212. const char* pathCString = path.c_str();
  1213. bool hasDoubleSlash = false;
  1214. const char* pos0 = pathCString;
  1215. const char* pos1 = pathCString+1;
  1216. for (kwsys_stl::string::size_type pos = 0; *pos0; ++ pos )
  1217. {
  1218. // make sure we don't convert an escaped space to a unix slash
  1219. if ( *pos0 == '\\' && *pos1 != ' ' )
  1220. {
  1221. path[pos] = '/';
  1222. }
  1223. // Also, reuse the loop to check for slash followed by another slash
  1224. if ( !hasDoubleSlash && *pos1 &&
  1225. *pos1 == '/' && *(pos1+1) == '/' )
  1226. {
  1227. #ifdef _WIN32
  1228. // However, on windows if the first characters are both slashes,
  1229. // then keep them that way, so that network paths can be handled.
  1230. if ( pos > 0)
  1231. {
  1232. hasDoubleSlash = true;
  1233. }
  1234. #else
  1235. hasDoubleSlash = true;
  1236. #endif
  1237. }
  1238. pos0 ++;
  1239. pos1 ++;
  1240. }
  1241. if ( hasDoubleSlash )
  1242. {
  1243. SystemTools::ReplaceString(path, "//", "/");
  1244. }
  1245. // remove any trailing slash
  1246. if(!path.empty())
  1247. {
  1248. // if there is a tilda ~ then replace it with HOME
  1249. pathCString = path.c_str();
  1250. if(*pathCString == '~')
  1251. {
  1252. const char* homeEnv = SystemTools::GetEnv("HOME");
  1253. if (homeEnv)
  1254. {
  1255. path.replace(0,1,homeEnv);
  1256. }
  1257. }
  1258. // remove trailing slash if the path is more than
  1259. // a single /
  1260. pathCString = path.c_str();
  1261. if(path.size() > 1 && *(pathCString+(path.size()-1)) == '/')
  1262. {
  1263. // if it is c:/ then do not remove the trailing slash
  1264. if(!((path.size() == 3 && pathCString[1] == ':')))
  1265. {
  1266. path = path.substr(0, path.size()-1);
  1267. }
  1268. }
  1269. }
  1270. }
  1271. // change // to /, and escape any spaces in the path
  1272. kwsys_stl::string SystemTools::ConvertToUnixOutputPath(const char* path)
  1273. {
  1274. kwsys_stl::string ret = path;
  1275. // remove // except at the beginning might be a cygwin drive
  1276. kwsys_stl::string::size_type pos=0;
  1277. while((pos = ret.find("//", pos)) != kwsys_stl::string::npos)
  1278. {
  1279. ret.erase(pos, 1);
  1280. }
  1281. // escape spaces and () in the path
  1282. if(ret.find_first_of(" ()") != kwsys_stl::string::npos)
  1283. {
  1284. kwsys_stl::string result = "";
  1285. char lastch = 1;
  1286. bool inDollarVariable = false;
  1287. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  1288. {
  1289. // if it is already escaped then don't try to escape it again
  1290. if((*ch == ' ' || *ch == '(' || *ch == ')') && lastch != '\\')
  1291. {
  1292. if(*ch == '(' && lastch == '$')
  1293. {
  1294. inDollarVariable = true;
  1295. }
  1296. // if we are in a $(..... and we get a ) then do not escape
  1297. // the ) and but set inDollarVariable to false
  1298. else if(*ch == ')' && inDollarVariable)
  1299. {
  1300. inDollarVariable = false;
  1301. }
  1302. else
  1303. {
  1304. result += '\\';
  1305. }
  1306. }
  1307. result += *ch;
  1308. lastch = *ch;
  1309. }
  1310. ret = result;
  1311. }
  1312. return ret;
  1313. }
  1314. kwsys_stl::string SystemTools::ConvertToOutputPath(const char* path)
  1315. {
  1316. #if defined(_WIN32) && !defined(__CYGWIN__)
  1317. return SystemTools::ConvertToWindowsOutputPath(path);
  1318. #else
  1319. return SystemTools::ConvertToUnixOutputPath(path);
  1320. #endif
  1321. }
  1322. // remove double slashes not at the start
  1323. kwsys_stl::string SystemTools::ConvertToWindowsOutputPath(const char* path)
  1324. {
  1325. kwsys_stl::string ret;
  1326. // make it big enough for all of path and double quotes
  1327. ret.reserve(strlen(path)+3);
  1328. // put path into the string
  1329. ret.insert(0,path);
  1330. kwsys_stl::string::size_type pos = 0;
  1331. // first convert all of the slashes
  1332. while((pos = ret.find('/', pos)) != kwsys_stl::string::npos)
  1333. {
  1334. ret[pos] = '\\';
  1335. pos++;
  1336. }
  1337. // check for really small paths
  1338. if(ret.size() < 2)
  1339. {
  1340. return ret;
  1341. }
  1342. // now clean up a bit and remove double slashes
  1343. // Only if it is not the first position in the path which is a network
  1344. // path on windows
  1345. pos = 1; // start at position 1
  1346. if(ret[0] == '\"')
  1347. {
  1348. pos = 2; // if the string is already quoted then start at 2
  1349. if(ret.size() < 3)
  1350. {
  1351. return ret;
  1352. }
  1353. }
  1354. while((pos = ret.find("\\\\", pos)) != kwsys_stl::string::npos)
  1355. {
  1356. ret.erase(pos, 1);
  1357. }
  1358. // now double quote the path if it has spaces in it
  1359. // and is not already double quoted
  1360. if(ret.find(' ') != kwsys_stl::string::npos
  1361. && ret[0] != '\"')
  1362. {
  1363. ret.insert(static_cast<kwsys_stl::string::size_type>(0),
  1364. static_cast<kwsys_stl::string::size_type>(1), '\"');
  1365. ret.append(1, '\"');
  1366. }
  1367. return ret;
  1368. }
  1369. bool SystemTools::CopyFileIfDifferent(const char* source,
  1370. const char* destination)
  1371. {
  1372. if(SystemTools::FilesDiffer(source, destination))
  1373. {
  1374. return SystemTools::CopyFileAlways(source, destination);
  1375. }
  1376. return true;
  1377. }
  1378. #define KWSYS_ST_BUFFER 4096
  1379. bool SystemTools::FilesDiffer(const char* source,
  1380. const char* destination)
  1381. {
  1382. struct stat statSource;
  1383. if (stat(source, &statSource) != 0)
  1384. {
  1385. return true;
  1386. }
  1387. struct stat statDestination;
  1388. if (stat(destination, &statDestination) != 0)
  1389. {
  1390. return true;
  1391. }
  1392. if(statSource.st_size != statDestination.st_size)
  1393. {
  1394. return true;
  1395. }
  1396. if(statSource.st_size == 0)
  1397. {
  1398. return false;
  1399. }
  1400. #if defined(_WIN32) || defined(__CYGWIN__)
  1401. kwsys_ios::ifstream finSource(source, (kwsys_ios::ios::binary |
  1402. kwsys_ios::ios::in));
  1403. kwsys_ios::ifstream finDestination(destination, (kwsys_ios::ios::binary |
  1404. kwsys_ios::ios::in));
  1405. #else
  1406. kwsys_ios::ifstream finSource(source);
  1407. kwsys_ios::ifstream finDestination(destination);
  1408. #endif
  1409. if(!finSource || !finDestination)
  1410. {
  1411. return true;
  1412. }
  1413. // Compare the files a block at a time.
  1414. char source_buf[KWSYS_ST_BUFFER];
  1415. char dest_buf[KWSYS_ST_BUFFER];
  1416. long nleft = statSource.st_size;
  1417. while(nleft > 0)
  1418. {
  1419. // Read a block from each file.
  1420. long nnext = (nleft > KWSYS_ST_BUFFER)? KWSYS_ST_BUFFER : nleft;
  1421. finSource.read(source_buf, nnext);
  1422. finDestination.read(dest_buf, nnext);
  1423. // If either failed to read assume they are different.
  1424. if(static_cast<long>(finSource.gcount()) != nnext ||
  1425. static_cast<long>(finDestination.gcount()) != nnext)
  1426. {
  1427. return true;
  1428. }
  1429. // If this block differs the file differs.
  1430. if(memcmp(static_cast<const void*>(source_buf),
  1431. static_cast<const void*>(dest_buf), nnext) != 0)
  1432. {
  1433. return true;
  1434. }
  1435. // Update the byte count remaining.
  1436. nleft -= nnext;
  1437. }
  1438. // No differences found.
  1439. return false;
  1440. }
  1441. /**
  1442. * Copy a file named by "source" to the file named by "destination".
  1443. */
  1444. bool SystemTools::CopyFileAlways(const char* source, const char* destination)
  1445. {
  1446. // If files are the same do not copy
  1447. if ( SystemTools::SameFile(source, destination) )
  1448. {
  1449. return true;
  1450. }
  1451. mode_t perm = 0;
  1452. bool perms = SystemTools::GetPermissions(source, perm);
  1453. const int bufferSize = 4096;
  1454. char buffer[bufferSize];
  1455. // If destination is a directory, try to create a file with the same
  1456. // name as the source in that directory.
  1457. kwsys_stl::string new_destination;
  1458. if(SystemTools::FileExists(destination) &&
  1459. SystemTools::FileIsDirectory(destination))
  1460. {
  1461. new_destination = destination;
  1462. SystemTools::ConvertToUnixSlashes(new_destination);
  1463. new_destination += '/';
  1464. kwsys_stl::string source_name = source;
  1465. new_destination += SystemTools::GetFilenameName(source_name);
  1466. destination = new_destination.c_str();
  1467. }
  1468. // Create destination directory
  1469. kwsys_stl::string destination_dir = destination;
  1470. destination_dir = SystemTools::GetFilenamePath(destination_dir);
  1471. SystemTools::MakeDirectory(destination_dir.c_str());
  1472. // Open files
  1473. #if defined(_WIN32) || defined(__CYGWIN__)
  1474. kwsys_ios::ifstream fin(source,
  1475. kwsys_ios::ios::binary | kwsys_ios::ios::in);
  1476. #else
  1477. kwsys_ios::ifstream fin(source);
  1478. #endif
  1479. if(!fin)
  1480. {
  1481. return false;
  1482. }
  1483. // try and remove the destination file so that read only destination files
  1484. // can be written to.
  1485. // If the remove fails continue so that files in read only directories
  1486. // that do not allow file removal can be modified.
  1487. SystemTools::RemoveFile(destination);
  1488. #if defined(_WIN32) || defined(__CYGWIN__)
  1489. kwsys_ios::ofstream fout(destination,
  1490. kwsys_ios::ios::binary | kwsys_ios::ios::out | kwsys_ios::ios::trunc);
  1491. #else
  1492. kwsys_ios::ofstream fout(destination,
  1493. kwsys_ios::ios::out | kwsys_ios::ios::trunc);
  1494. #endif
  1495. if(!fout)
  1496. {
  1497. return false;
  1498. }
  1499. // This copy loop is very sensitive on certain platforms with
  1500. // slightly broken stream libraries (like HPUX). Normally, it is
  1501. // incorrect to not check the error condition on the fin.read()
  1502. // before using the data, but the fin.gcount() will be zero if an
  1503. // error occurred. Therefore, the loop should be safe everywhere.
  1504. while(fin)
  1505. {
  1506. fin.read(buffer, bufferSize);
  1507. if(fin.gcount())
  1508. {
  1509. fout.write(buffer, fin.gcount());
  1510. }
  1511. }
  1512. // Make sure the operating system has finished writing the file
  1513. // before closing it. This will ensure the file is finished before
  1514. // the check below.
  1515. fout.flush();
  1516. fin.close();
  1517. fout.close();
  1518. // More checks.
  1519. struct stat statSource, statDestination;
  1520. statSource.st_size = 12345;
  1521. statDestination.st_size = 12345;
  1522. if(stat(source, &statSource) != 0)
  1523. {
  1524. return false;
  1525. }
  1526. else if(stat(destination, &statDestination) != 0)
  1527. {
  1528. return false;
  1529. }
  1530. else if(statSource.st_size != statDestination.st_size)
  1531. {
  1532. return false;
  1533. }
  1534. if ( perms )
  1535. {
  1536. if ( !SystemTools::SetPermissions(destination, perm) )
  1537. {
  1538. return false;
  1539. }
  1540. }
  1541. return true;
  1542. }
  1543. //----------------------------------------------------------------------------
  1544. bool SystemTools::CopyAFile(const char* source, const char* destination,
  1545. bool always)
  1546. {
  1547. if(always)
  1548. {
  1549. return SystemTools::CopyFileAlways(source, destination);
  1550. }
  1551. else
  1552. {
  1553. return SystemTools::CopyFileIfDifferent(source, destination);
  1554. }
  1555. }
  1556. /**
  1557. * Copy a directory content from "source" directory to the directory named by
  1558. * "destination".
  1559. */
  1560. bool SystemTools::CopyADirectory(const char* source, const char* destination,
  1561. bool always)
  1562. {
  1563. Directory dir;
  1564. dir.Load(source);
  1565. size_t fileNum;
  1566. if ( !SystemTools::MakeDirectory(destination) )
  1567. {
  1568. return false;
  1569. }
  1570. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  1571. {
  1572. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  1573. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  1574. {
  1575. kwsys_stl::string fullPath = source;
  1576. fullPath += "/";
  1577. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  1578. if(SystemTools::FileIsDirectory(fullPath.c_str()))
  1579. {
  1580. kwsys_stl::string fullDestPath = destination;
  1581. fullDestPath += "/";
  1582. fullDestPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  1583. if (!SystemTools::CopyADirectory(fullPath.c_str(),
  1584. fullDestPath.c_str(),
  1585. always))
  1586. {
  1587. return false;
  1588. }
  1589. }
  1590. else
  1591. {
  1592. if(!SystemTools::CopyAFile(fullPath.c_str(), destination, always))
  1593. {
  1594. return false;
  1595. }
  1596. }
  1597. }
  1598. }
  1599. return true;
  1600. }
  1601. // return size of file; also returns zero if no file exists
  1602. unsigned long SystemTools::FileLength(const char* filename)
  1603. {
  1604. struct stat fs;
  1605. if (stat(filename, &fs) != 0)
  1606. {
  1607. return 0;
  1608. }
  1609. else
  1610. {
  1611. return static_cast<unsigned long>(fs.st_size);
  1612. }
  1613. }
  1614. int SystemTools::Strucmp(const char *s1, const char *s2)
  1615. {
  1616. // lifted from Graphvis http://www.graphviz.org
  1617. while ((*s1 != '\0')
  1618. && (tolower(*s1) == tolower(*s2)))
  1619. {
  1620. s1++;
  1621. s2++;
  1622. }
  1623. return tolower(*s1) - tolower(*s2);
  1624. }
  1625. // return file's modified time
  1626. long int SystemTools::ModifiedTime(const char* filename)
  1627. {
  1628. struct stat fs;
  1629. if (stat(filename, &fs) != 0)
  1630. {
  1631. return 0;
  1632. }
  1633. else
  1634. {
  1635. return static_cast<long int>(fs.st_mtime);
  1636. }
  1637. }
  1638. // return file's creation time
  1639. long int SystemTools::CreationTime(const char* filename)
  1640. {
  1641. struct stat fs;
  1642. if (stat(filename, &fs) != 0)
  1643. {
  1644. return 0;
  1645. }
  1646. else
  1647. {
  1648. return fs.st_ctime >= 0 ? static_cast<long int>(fs.st_ctime) : 0;
  1649. }
  1650. }
  1651. bool SystemTools::ConvertDateMacroString(const char *str, time_t *tmt)
  1652. {
  1653. if (!str || !tmt || strlen(str) > 11)
  1654. {
  1655. return false;
  1656. }
  1657. struct tm tmt2;
  1658. // __DATE__
  1659. // The compilation date of the current source file. The date is a string
  1660. // literal of the form Mmm dd yyyy. The month name Mmm is the same as for
  1661. // dates generated by the library function asctime declared in TIME.H.
  1662. // index: 012345678901
  1663. // format: Mmm dd yyyy
  1664. // example: Dec 19 2003
  1665. static char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  1666. char buffer[12];
  1667. strcpy(buffer, str);
  1668. buffer[3] = 0;
  1669. char *ptr = strstr(month_names, buffer);
  1670. if (!ptr)
  1671. {
  1672. return false;
  1673. }
  1674. int month = (ptr - month_names) / 3;
  1675. int day = atoi(buffer + 4);
  1676. int year = atoi(buffer + 7);
  1677. tmt2.tm_isdst = -1;
  1678. tmt2.tm_hour = 0;
  1679. tmt2.tm_min = 0;
  1680. tmt2.tm_sec = 0;
  1681. tmt2.tm_wday = 0;
  1682. tmt2.tm_yday = 0;
  1683. tmt2.tm_mday = day;
  1684. tmt2.tm_mon = month;
  1685. tmt2.tm_year = year - 1900;
  1686. *tmt = mktime(&tmt2);
  1687. return true;
  1688. }
  1689. bool SystemTools::ConvertTimeStampMacroString(const char *str, time_t *tmt)
  1690. {
  1691. if (!str || !tmt || strlen(str) > 26)
  1692. {
  1693. return false;
  1694. }
  1695. struct tm tmt2;
  1696. // __TIMESTAMP__
  1697. // The date and time of the last modification of the current source file,
  1698. // expressed as a string literal in the form Ddd Mmm Date hh:mm:ss yyyy,
  1699. /// where Ddd is the abbreviated day of the week and Date is an integer
  1700. // from 1 to 31.
  1701. // index: 0123456789
  1702. // 0123456789
  1703. // 0123456789
  1704. // format: Ddd Mmm Date hh:mm:ss yyyy
  1705. // example: Fri Dec 19 14:34:58 2003
  1706. static char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  1707. char buffer[27];
  1708. strcpy(buffer, str);
  1709. buffer[7] = 0;
  1710. char *ptr = strstr(month_names, buffer + 4);
  1711. if (!ptr)
  1712. {
  1713. return false;
  1714. }
  1715. int month = (ptr - month_names) / 3;
  1716. int day = atoi(buffer + 8);
  1717. int hour = atoi(buffer + 11);
  1718. int min = atoi(buffer + 14);
  1719. int sec = atoi(buffer + 17);
  1720. int year = atoi(buffer + 20);
  1721. tmt2.tm_isdst = -1;
  1722. tmt2.tm_hour = hour;
  1723. tmt2.tm_min = min;
  1724. tmt2.tm_sec = sec;
  1725. tmt2.tm_wday = 0;
  1726. tmt2.tm_yday = 0;
  1727. tmt2.tm_mday = day;
  1728. tmt2.tm_mon = month;
  1729. tmt2.tm_year = year - 1900;
  1730. *tmt = mktime(&tmt2);
  1731. return true;
  1732. }
  1733. kwsys_stl::string SystemTools::GetLastSystemError()
  1734. {
  1735. int e = errno;
  1736. return strerror(e);
  1737. }
  1738. bool SystemTools::RemoveFile(const char* source)
  1739. {
  1740. #ifdef _WIN32
  1741. mode_t mode;
  1742. if ( !SystemTools::GetPermissions(source, mode) )
  1743. {
  1744. return false;
  1745. }
  1746. /* Win32 unlink is stupid --- it fails if the file is read-only */
  1747. SystemTools::SetPermissions(source, S_IWRITE);
  1748. #endif
  1749. bool res = unlink(source) != 0 ? false : true;
  1750. #ifdef _WIN32
  1751. if ( !res )
  1752. {
  1753. SystemTools::SetPermissions(source, mode);
  1754. }
  1755. #endif
  1756. return res;
  1757. }
  1758. bool SystemTools::RemoveADirectory(const char* source)
  1759. {
  1760. Directory dir;
  1761. dir.Load(source);
  1762. size_t fileNum;
  1763. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  1764. {
  1765. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  1766. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  1767. {
  1768. kwsys_stl::string fullPath = source;
  1769. fullPath += "/";
  1770. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  1771. if(SystemTools::FileIsDirectory(fullPath.c_str()) &&
  1772. !SystemTools::FileIsSymlink(fullPath.c_str()))
  1773. {
  1774. if (!SystemTools::RemoveADirectory(fullPath.c_str()))
  1775. {
  1776. return false;
  1777. }
  1778. }
  1779. else
  1780. {
  1781. if(!SystemTools::RemoveFile(fullPath.c_str()))
  1782. {
  1783. return false;
  1784. }
  1785. }
  1786. }
  1787. }
  1788. return (Rmdir(source) == 0);
  1789. }
  1790. /**
  1791. */
  1792. size_t SystemTools::GetMaximumFilePathLength()
  1793. {
  1794. return KWSYS_SYSTEMTOOLS_MAXPATH;
  1795. }
  1796. /**
  1797. * Find the file the given name. Searches the given path and then
  1798. * the system search path. Returns the full path to the file if it is
  1799. * found. Otherwise, the empty string is returned.
  1800. */
  1801. kwsys_stl::string SystemTools
  1802. ::FindName(const char* name,
  1803. const kwsys_stl::vector<kwsys_stl::string>& userPaths,
  1804. bool no_system_path)
  1805. {
  1806. // Add the system search path to our path first
  1807. kwsys_stl::vector<kwsys_stl::string> path;
  1808. if (!no_system_path)
  1809. {
  1810. SystemTools::GetPath(path, "CMAKE_FILE_PATH");
  1811. SystemTools::GetPath(path);
  1812. }
  1813. // now add the additional paths
  1814. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = userPaths.begin();
  1815. i != userPaths.end(); ++i)
  1816. {
  1817. path.push_back(*i);
  1818. }
  1819. // now look for the file
  1820. kwsys_stl::string tryPath;
  1821. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  1822. p != path.end(); ++p)
  1823. {
  1824. tryPath = *p;
  1825. tryPath += "/";
  1826. tryPath += name;
  1827. if(SystemTools::FileExists(tryPath.c_str()))
  1828. {
  1829. return tryPath;
  1830. }
  1831. }
  1832. // Couldn't find the file.
  1833. return "";
  1834. }
  1835. /**
  1836. * Find the file the given name. Searches the given path and then
  1837. * the system search path. Returns the full path to the file if it is
  1838. * found. Otherwise, the empty string is returned.
  1839. */
  1840. kwsys_stl::string SystemTools
  1841. ::FindFile(const char* name,
  1842. const kwsys_stl::vector<kwsys_stl::string>& userPaths,
  1843. bool no_system_path)
  1844. {
  1845. kwsys_stl::string tryPath = SystemTools::FindName(name, userPaths, no_system_path);
  1846. if(tryPath != "" && !SystemTools::FileIsDirectory(tryPath.c_str()))
  1847. {
  1848. return SystemTools::CollapseFullPath(tryPath.c_str());
  1849. }
  1850. // Couldn't find the file.
  1851. return "";
  1852. }
  1853. /**
  1854. * Find the directory the given name. Searches the given path and then
  1855. * the system search path. Returns the full path to the directory if it is
  1856. * found. Otherwise, the empty string is returned.
  1857. */
  1858. kwsys_stl::string SystemTools
  1859. ::FindDirectory(const char* name,
  1860. const kwsys_stl::vector<kwsys_stl::string>& userPaths,
  1861. bool no_system_path)
  1862. {
  1863. kwsys_stl::string tryPath = SystemTools::FindName(name, userPaths, no_system_path);
  1864. if(tryPath != "" && SystemTools::FileIsDirectory(tryPath.c_str()))
  1865. {
  1866. return SystemTools::CollapseFullPath(tryPath.c_str());
  1867. }
  1868. // Couldn't find the file.
  1869. return "";
  1870. }
  1871. /**
  1872. * Find the executable with the given name. Searches the given path and then
  1873. * the system search path. Returns the full path to the executable if it is
  1874. * found. Otherwise, the empty string is returned.
  1875. */
  1876. kwsys_stl::string SystemTools::FindProgram(
  1877. const char* nameIn,
  1878. const kwsys_stl::vector<kwsys_stl::string>& userPaths,
  1879. bool no_system_path)
  1880. {
  1881. if(!nameIn || !*nameIn)
  1882. {
  1883. return "";
  1884. }
  1885. kwsys_stl::string name = nameIn;
  1886. kwsys_stl::vector<kwsys_stl::string> extensions;
  1887. #if defined (_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
  1888. bool hasExtension = false;
  1889. // check to see if the name already has a .xxx at
  1890. // the end of it
  1891. if(name.size() > 3 && name[name.size()-4] == '.')
  1892. {
  1893. hasExtension = true;
  1894. }
  1895. // on windows try .com then .exe
  1896. if(!hasExtension)
  1897. {
  1898. extensions.push_back(".com");
  1899. extensions.push_back(".exe");
  1900. }
  1901. #endif
  1902. kwsys_stl::string tryPath;
  1903. // first try with extensions if the os supports them
  1904. if(extensions.size())
  1905. {
  1906. for(kwsys_stl::vector<kwsys_stl::string>::iterator i =
  1907. extensions.begin(); i != extensions.end(); ++i)
  1908. {
  1909. tryPath = name;
  1910. tryPath += *i;
  1911. if(SystemTools::FileExists(tryPath.c_str()) &&
  1912. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1913. {
  1914. return SystemTools::CollapseFullPath(tryPath.c_str());
  1915. }
  1916. }
  1917. }
  1918. // now try just the name
  1919. tryPath = name;
  1920. if(SystemTools::FileExists(tryPath.c_str()) &&
  1921. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1922. {
  1923. return SystemTools::CollapseFullPath(tryPath.c_str());
  1924. }
  1925. // now construct the path
  1926. kwsys_stl::vector<kwsys_stl::string> path;
  1927. // Add the system search path to our path.
  1928. if (!no_system_path)
  1929. {
  1930. SystemTools::GetPath(path);
  1931. }
  1932. // now add the additional paths
  1933. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i =
  1934. userPaths.begin(); i != userPaths.end(); ++i)
  1935. {
  1936. path.push_back(*i);
  1937. }
  1938. // Try each path
  1939. for(kwsys_stl::vector<kwsys_stl::string>::iterator p = path.begin();
  1940. p != path.end(); ++p)
  1941. {
  1942. #ifdef _WIN32
  1943. // Remove double quotes from the path on windows
  1944. SystemTools::ReplaceString(*p, "\"", "");
  1945. #endif
  1946. // first try with extensions
  1947. if(extensions.size())
  1948. {
  1949. for(kwsys_stl::vector<kwsys_stl::string>::iterator ext
  1950. = extensions.begin(); ext != extensions.end(); ++ext)
  1951. {
  1952. tryPath = *p;
  1953. tryPath += "/";
  1954. tryPath += name;
  1955. tryPath += *ext;
  1956. if(SystemTools::FileExists(tryPath.c_str()) &&
  1957. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1958. {
  1959. return SystemTools::CollapseFullPath(tryPath.c_str());
  1960. }
  1961. }
  1962. }
  1963. // now try it without them
  1964. tryPath = *p;
  1965. tryPath += "/";
  1966. tryPath += name;
  1967. if(SystemTools::FileExists(tryPath.c_str()) &&
  1968. !SystemTools::FileIsDirectory(tryPath.c_str()))
  1969. {
  1970. return SystemTools::CollapseFullPath(tryPath.c_str());
  1971. }
  1972. }
  1973. // Couldn't find the program.
  1974. return "";
  1975. }
  1976. kwsys_stl::string SystemTools::FindProgram(
  1977. const kwsys_stl::vector<kwsys_stl::string>& names,
  1978. const kwsys_stl::vector<kwsys_stl::string>& path,
  1979. bool noSystemPath)
  1980. {
  1981. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator it = names.begin();
  1982. it != names.end() ; ++it)
  1983. {
  1984. // Try to find the program.
  1985. kwsys_stl::string result = SystemTools::FindProgram(it->c_str(),
  1986. path,
  1987. noSystemPath);
  1988. if ( !result.empty() )
  1989. {
  1990. return result;
  1991. }
  1992. }
  1993. return "";
  1994. }
  1995. /**
  1996. * Find the library with the given name. Searches the given path and then
  1997. * the system search path. Returns the full path to the library if it is
  1998. * found. Otherwise, the empty string is returned.
  1999. */
  2000. kwsys_stl::string SystemTools
  2001. ::FindLibrary(const char* name,
  2002. const kwsys_stl::vector<kwsys_stl::string>& userPaths)
  2003. {
  2004. // See if the executable exists as written.
  2005. if(SystemTools::FileExists(name) &&
  2006. !SystemTools::FileIsDirectory(name))
  2007. {
  2008. return SystemTools::CollapseFullPath(name);
  2009. }
  2010. // Add the system search path to our path.
  2011. kwsys_stl::vector<kwsys_stl::string> path;
  2012. SystemTools::GetPath(path);
  2013. // now add the additional paths
  2014. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = userPaths.begin();
  2015. i != userPaths.end(); ++i)
  2016. {
  2017. path.push_back(*i);
  2018. }
  2019. kwsys_stl::string tryPath;
  2020. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator p = path.begin();
  2021. p != path.end(); ++p)
  2022. {
  2023. #if defined(__APPLE__)
  2024. tryPath = *p;
  2025. tryPath += "/";
  2026. tryPath += name;
  2027. tryPath += ".framework";
  2028. if(SystemTools::FileExists(tryPath.c_str())
  2029. && SystemTools::FileIsDirectory(tryPath.c_str()))
  2030. {
  2031. return SystemTools::CollapseFullPath(tryPath.c_str());
  2032. }
  2033. #endif
  2034. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  2035. tryPath = *p;
  2036. tryPath += "/";
  2037. tryPath += name;
  2038. tryPath += ".lib";
  2039. if(SystemTools::FileExists(tryPath.c_str())
  2040. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  2041. {
  2042. return SystemTools::CollapseFullPath(tryPath.c_str());
  2043. }
  2044. #else
  2045. tryPath = *p;
  2046. tryPath += "/lib";
  2047. tryPath += name;
  2048. tryPath += ".so";
  2049. if(SystemTools::FileExists(tryPath.c_str())
  2050. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  2051. {
  2052. return SystemTools::CollapseFullPath(tryPath.c_str());
  2053. }
  2054. tryPath = *p;
  2055. tryPath += "/lib";
  2056. tryPath += name;
  2057. tryPath += ".a";
  2058. if(SystemTools::FileExists(tryPath.c_str())
  2059. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  2060. {
  2061. return SystemTools::CollapseFullPath(tryPath.c_str());
  2062. }
  2063. tryPath = *p;
  2064. tryPath += "/lib";
  2065. tryPath += name;
  2066. tryPath += ".sl";
  2067. if(SystemTools::FileExists(tryPath.c_str())
  2068. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  2069. {
  2070. return SystemTools::CollapseFullPath(tryPath.c_str());
  2071. }
  2072. tryPath = *p;
  2073. tryPath += "/lib";
  2074. tryPath += name;
  2075. tryPath += ".dylib";
  2076. if(SystemTools::FileExists(tryPath.c_str())
  2077. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  2078. {
  2079. return SystemTools::CollapseFullPath(tryPath.c_str());
  2080. }
  2081. tryPath = *p;
  2082. tryPath += "/lib";
  2083. tryPath += name;
  2084. tryPath += ".dll";
  2085. if(SystemTools::FileExists(tryPath.c_str())
  2086. && !SystemTools::FileIsDirectory(tryPath.c_str()))
  2087. {
  2088. return SystemTools::CollapseFullPath(tryPath.c_str());
  2089. }
  2090. #endif
  2091. }
  2092. // Couldn't find the library.
  2093. return "";
  2094. }
  2095. bool SystemTools::FileIsDirectory(const char* name)
  2096. {
  2097. // Remove any trailing slash from the name.
  2098. char buffer[KWSYS_SYSTEMTOOLS_MAXPATH];
  2099. int last = static_cast<int>(strlen(name))-1;
  2100. if(last >= 0 && (name[last] == '/' || name[last] == '\\'))
  2101. {
  2102. memcpy(buffer, name, last);
  2103. buffer[last] = 0;
  2104. name = buffer;
  2105. }
  2106. // Now check the file node type.
  2107. struct stat fs;
  2108. if(stat(name, &fs) == 0)
  2109. {
  2110. #if defined( _WIN32 )
  2111. return ((fs.st_mode & _S_IFDIR) != 0);
  2112. #else
  2113. return S_ISDIR(fs.st_mode);
  2114. #endif
  2115. }
  2116. else
  2117. {
  2118. return false;
  2119. }
  2120. }
  2121. bool SystemTools::FileIsSymlink(const char* name)
  2122. {
  2123. #if defined( _WIN32 )
  2124. (void)name;
  2125. return false;
  2126. #else
  2127. struct stat fs;
  2128. if(lstat(name, &fs) == 0)
  2129. {
  2130. return S_ISLNK(fs.st_mode);
  2131. }
  2132. else
  2133. {
  2134. return false;
  2135. }
  2136. #endif
  2137. }
  2138. int SystemTools::ChangeDirectory(const char *dir)
  2139. {
  2140. return Chdir(dir);
  2141. }
  2142. kwsys_stl::string SystemTools::GetCurrentWorkingDirectory(bool collapse)
  2143. {
  2144. char buf[2048];
  2145. const char* cwd = Getcwd(buf, 2048);
  2146. kwsys_stl::string path;
  2147. if ( cwd )
  2148. {
  2149. path = cwd;
  2150. }
  2151. if(collapse)
  2152. {
  2153. return SystemTools::CollapseFullPath(path.c_str());
  2154. }
  2155. return path;
  2156. }
  2157. kwsys_stl::string SystemTools::GetProgramPath(const char* in_name)
  2158. {
  2159. kwsys_stl::string dir, file;
  2160. SystemTools::SplitProgramPath(in_name, dir, file);
  2161. return dir;
  2162. }
  2163. bool SystemTools::SplitProgramPath(const char* in_name,
  2164. kwsys_stl::string& dir,
  2165. kwsys_stl::string& file,
  2166. bool)
  2167. {
  2168. dir = in_name;
  2169. file = "";
  2170. SystemTools::ConvertToUnixSlashes(dir);
  2171. if(!SystemTools::FileIsDirectory(dir.c_str()))
  2172. {
  2173. kwsys_stl::string::size_type slashPos = dir.rfind("/");
  2174. if(slashPos != kwsys_stl::string::npos)
  2175. {
  2176. file = dir.substr(slashPos+1);
  2177. dir = dir.substr(0, slashPos);
  2178. }
  2179. else
  2180. {
  2181. file = dir;
  2182. dir = "";
  2183. }
  2184. }
  2185. if(!(dir == "") && !SystemTools::FileIsDirectory(dir.c_str()))
  2186. {
  2187. kwsys_stl::string oldDir = in_name;
  2188. SystemTools::ConvertToUnixSlashes(oldDir);
  2189. dir = in_name;
  2190. return false;
  2191. }
  2192. return true;
  2193. }
  2194. bool SystemTools::FindProgramPath(const char* argv0,
  2195. kwsys_stl::string& pathOut,
  2196. kwsys_stl::string& errorMsg,
  2197. const char* exeName,
  2198. const char* buildDir,
  2199. const char* installPrefix )
  2200. {
  2201. kwsys_stl::vector<kwsys_stl::string> failures;
  2202. kwsys_stl::string self = argv0 ? argv0 : "";
  2203. failures.push_back(self);
  2204. SystemTools::ConvertToUnixSlashes(self);
  2205. self = SystemTools::FindProgram(self.c_str());
  2206. if(!SystemTools::FileExists(self.c_str()))
  2207. {
  2208. if(buildDir)
  2209. {
  2210. kwsys_stl::string intdir = ".";
  2211. #ifdef CMAKE_INTDIR
  2212. intdir = CMAKE_INTDIR;
  2213. #endif
  2214. self = buildDir;
  2215. self += "/bin/";
  2216. self += intdir;
  2217. self += "/";
  2218. self += exeName;
  2219. self += SystemTools::GetExecutableExtension();
  2220. }
  2221. }
  2222. if(installPrefix)
  2223. {
  2224. if(!SystemTools::FileExists(self.c_str()))
  2225. {
  2226. failures.push_back(self);
  2227. self = installPrefix;
  2228. self += "/bin/";
  2229. self += exeName;
  2230. }
  2231. }
  2232. if(!SystemTools::FileExists(self.c_str()))
  2233. {
  2234. failures.push_back(self);
  2235. kwsys_ios::ostringstream msg;
  2236. msg << "Can not find the command line program ";
  2237. if (exeName)
  2238. {
  2239. msg << exeName;
  2240. }
  2241. msg << "\n";
  2242. if (argv0)
  2243. {
  2244. msg << " argv[0] = \"" << argv0 << "\"\n";
  2245. }
  2246. msg << " Attempted paths:\n";
  2247. kwsys_stl::vector<kwsys_stl::string>::iterator i;
  2248. for(i=failures.begin(); i != failures.end(); ++i)
  2249. {
  2250. msg << " \"" << i->c_str() << "\"\n";
  2251. }
  2252. errorMsg = msg.str();
  2253. return false;
  2254. }
  2255. pathOut = self;
  2256. return true;
  2257. }
  2258. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative)
  2259. {
  2260. return SystemTools::CollapseFullPath(in_relative, 0);
  2261. }
  2262. void SystemTools::AddTranslationPath(const char * a, const char * b)
  2263. {
  2264. kwsys_stl::string path_a = a;
  2265. kwsys_stl::string path_b = b;
  2266. SystemTools::ConvertToUnixSlashes(path_a);
  2267. SystemTools::ConvertToUnixSlashes(path_b);
  2268. // First check this is a directory path, since we don't want the table to
  2269. // grow too fat
  2270. if( SystemTools::FileIsDirectory( path_a.c_str() ) )
  2271. {
  2272. // Make sure the path is a full path and does not contain no '..'
  2273. if( SystemTools::FileIsFullPath(path_b.c_str()) && path_b.find("..")
  2274. == kwsys_stl::string::npos )
  2275. {
  2276. // Before inserting make sure path ends with '/'
  2277. if(path_a.size() && path_a[path_a.size() -1] != '/')
  2278. {
  2279. path_a += '/';
  2280. }
  2281. if(path_b.size() && path_b[path_b.size() -1] != '/')
  2282. {
  2283. path_b += '/';
  2284. }
  2285. if( !(path_a == path_b) )
  2286. {
  2287. SystemTools::TranslationMap->insert(
  2288. SystemToolsTranslationMap::value_type(path_a, path_b));
  2289. }
  2290. }
  2291. }
  2292. }
  2293. void SystemTools::AddKeepPath(const char* dir)
  2294. {
  2295. kwsys_stl::string cdir;
  2296. Realpath(SystemTools::CollapseFullPath(dir).c_str(), cdir);
  2297. SystemTools::AddTranslationPath(cdir.c_str(), dir);
  2298. }
  2299. void SystemTools::CheckTranslationPath(kwsys_stl::string & path)
  2300. {
  2301. // Do not translate paths that are too short to have meaningful
  2302. // translations.
  2303. if(path.size() < 2)
  2304. {
  2305. return;
  2306. }
  2307. // Always add a trailing slash before translation. It does not
  2308. // matter if this adds an extra slash, but we do not want to
  2309. // translate part of a directory (like the foo part of foo-dir).
  2310. path += "/";
  2311. // In case a file was specified we still have to go through this:
  2312. // Now convert any path found in the table back to the one desired:
  2313. kwsys_stl::map<kwsys_stl::string,kwsys_stl::string>::const_iterator it;
  2314. for(it = SystemTools::TranslationMap->begin();
  2315. it != SystemTools::TranslationMap->end();
  2316. ++it )
  2317. {
  2318. // We need to check of the path is a substring of the other path
  2319. if(path.find( it->first ) == 0)
  2320. {
  2321. path = path.replace( 0, it->first.size(), it->second);
  2322. }
  2323. }
  2324. // Remove the trailing slash we added before.
  2325. path.erase(path.end()-1, path.end());
  2326. }
  2327. void
  2328. SystemToolsAppendComponents(
  2329. kwsys_stl::vector<kwsys_stl::string>& out_components,
  2330. kwsys_stl::vector<kwsys_stl::string>::const_iterator first,
  2331. kwsys_stl::vector<kwsys_stl::string>::const_iterator last)
  2332. {
  2333. for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = first;
  2334. i != last; ++i)
  2335. {
  2336. if(*i == "..")
  2337. {
  2338. if(out_components.begin() != out_components.end())
  2339. {
  2340. out_components.erase(out_components.end()-1, out_components.end());
  2341. }
  2342. }
  2343. else if(!(*i == ".") && !(*i == ""))
  2344. {
  2345. out_components.push_back(*i);
  2346. }
  2347. }
  2348. }
  2349. kwsys_stl::string SystemTools::CollapseFullPath(const char* in_path,
  2350. const char* in_base)
  2351. {
  2352. // Collect the output path components.
  2353. kwsys_stl::vector<kwsys_stl::string> out_components;
  2354. // Split the input path components.
  2355. kwsys_stl::vector<kwsys_stl::string> path_components;
  2356. SystemTools::SplitPath(in_path, path_components);
  2357. // If the input path is relative, start with a base path.
  2358. if(path_components[0].length() == 0)
  2359. {
  2360. kwsys_stl::vector<kwsys_stl::string> base_components;
  2361. if(in_base)
  2362. {
  2363. // Use the given base path.
  2364. SystemTools::SplitPath(in_base, base_components);
  2365. }
  2366. else
  2367. {
  2368. // Use the current working directory as a base path.
  2369. char buf[2048];
  2370. if(const char* cwd = Getcwd(buf, 2048))
  2371. {
  2372. SystemTools::SplitPath(cwd, base_components);
  2373. }
  2374. else
  2375. {
  2376. // ??
  2377. }
  2378. }
  2379. // Append base path components to the output path.
  2380. out_components.push_back(base_components[0]);
  2381. SystemToolsAppendComponents(out_components,
  2382. base_components.begin()+1,
  2383. base_components.end());
  2384. }
  2385. // Append input path components to the output path.
  2386. SystemToolsAppendComponents(out_components,
  2387. path_components.begin(),
  2388. path_components.end());
  2389. // Transform the path back to a string.
  2390. kwsys_stl::string newPath = SystemTools::JoinPath(out_components);
  2391. // Update the translation table with this potentially new path.
  2392. SystemTools::AddTranslationPath(newPath.c_str(), in_path);
  2393. SystemTools::CheckTranslationPath(newPath);
  2394. #ifdef _WIN32
  2395. newPath = SystemTools::GetActualCaseForPath(newPath.c_str());
  2396. SystemTools::ConvertToUnixSlashes(newPath);
  2397. #endif
  2398. // Return the reconstructed path.
  2399. return newPath;
  2400. }
  2401. // compute the relative path from here to there
  2402. kwsys_stl::string SystemTools::RelativePath(const char* local, const char* remote)
  2403. {
  2404. if(!SystemTools::FileIsFullPath(local))
  2405. {
  2406. return "";
  2407. }
  2408. if(!SystemTools::FileIsFullPath(remote))
  2409. {
  2410. return "";
  2411. }
  2412. // split up both paths into arrays of strings using / as a separator
  2413. kwsys_stl::vector<kwsys::String> localSplit = SystemTools::SplitString(local, '/', true);
  2414. kwsys_stl::vector<kwsys::String> remoteSplit = SystemTools::SplitString(remote, '/', true);
  2415. kwsys_stl::vector<kwsys::String> commonPath; // store shared parts of path in this array
  2416. kwsys_stl::vector<kwsys::String> finalPath; // store the final relative path here
  2417. // count up how many matching directory names there are from the start
  2418. unsigned int sameCount = 0;
  2419. while(
  2420. ((sameCount <= (localSplit.size()-1)) && (sameCount <= (remoteSplit.size()-1)))
  2421. &&
  2422. // for windows and apple do a case insensitive string compare
  2423. #if defined(_WIN32) || defined(__APPLE__)
  2424. SystemTools::Strucmp(localSplit[sameCount].c_str(),
  2425. remoteSplit[sameCount].c_str()) == 0
  2426. #else
  2427. localSplit[sameCount] == remoteSplit[sameCount]
  2428. #endif
  2429. )
  2430. {
  2431. // put the common parts of the path into the commonPath array
  2432. commonPath.push_back(localSplit[sameCount]);
  2433. // erase the common parts of the path from the original path arrays
  2434. localSplit[sameCount] = "";
  2435. remoteSplit[sameCount] = "";
  2436. sameCount++;
  2437. }
  2438. // If there is nothing in common but the root directory, then just
  2439. // return the full path.
  2440. if(sameCount <= 1)
  2441. {
  2442. return remote;
  2443. }
  2444. // for each entry that is not common in the local path
  2445. // add a ../ to the finalpath array, this gets us out of the local
  2446. // path into the remote dir
  2447. for(unsigned int i = 0; i < localSplit.size(); ++i)
  2448. {
  2449. if(localSplit[i].size())
  2450. {
  2451. finalPath.push_back("../");
  2452. }
  2453. }
  2454. // for each entry that is not common in the remote path add it
  2455. // to the final path.
  2456. for(kwsys_stl::vector<String>::iterator vit = remoteSplit.begin();
  2457. vit != remoteSplit.end(); ++vit)
  2458. {
  2459. if(vit->size())
  2460. {
  2461. finalPath.push_back(*vit);
  2462. }
  2463. }
  2464. kwsys_stl::string relativePath; // result string
  2465. // now turn the array of directories into a unix path by puttint /
  2466. // between each entry that does not already have one
  2467. for(kwsys_stl::vector<String>::iterator vit1 = finalPath.begin();
  2468. vit1 != finalPath.end(); ++vit1)
  2469. {
  2470. if(relativePath.size() && relativePath[relativePath.size()-1] != '/')
  2471. {
  2472. relativePath += "/";
  2473. }
  2474. relativePath += *vit1;
  2475. }
  2476. return relativePath;
  2477. }
  2478. // OK, some fun stuff to get the actual case of a given path.
  2479. // Basically, you just need to call ShortPath, then GetLongPathName,
  2480. // However, GetLongPathName is not implemented on windows NT and 95,
  2481. // so we have to simulate it on those versions
  2482. #ifdef _WIN32
  2483. int OldWindowsGetLongPath(kwsys_stl::string const& shortPath,
  2484. kwsys_stl::string& longPath )
  2485. {
  2486. kwsys_stl::string::size_type iFound = shortPath.rfind('/');
  2487. if (iFound > 1 && iFound != shortPath.npos)
  2488. {
  2489. // recurse to peel off components
  2490. //
  2491. if (OldWindowsGetLongPath(shortPath.substr(0, iFound), longPath) > 0)
  2492. {
  2493. longPath += '/';
  2494. if (shortPath[1] != '/')
  2495. {
  2496. WIN32_FIND_DATA findData;
  2497. // append the long component name to the path
  2498. //
  2499. if (INVALID_HANDLE_VALUE != ::FindFirstFile
  2500. (shortPath.c_str(), &findData))
  2501. {
  2502. longPath += findData.cFileName;
  2503. }
  2504. else
  2505. {
  2506. // if FindFirstFile fails, return the error code
  2507. //
  2508. longPath = "";
  2509. return 0;
  2510. }
  2511. }
  2512. }
  2513. }
  2514. else
  2515. {
  2516. longPath = shortPath;
  2517. }
  2518. return (int)longPath.size();
  2519. }
  2520. int PortableGetLongPathName(const char* pathIn,
  2521. kwsys_stl::string & longPath)
  2522. {
  2523. HMODULE lh = LoadLibrary("Kernel32.dll");
  2524. if(lh)
  2525. {
  2526. FARPROC proc = GetProcAddress(lh, "GetLongPathNameA");
  2527. if(proc)
  2528. {
  2529. typedef DWORD (WINAPI * GetLongFunctionPtr) (LPCSTR,LPSTR,DWORD);
  2530. GetLongFunctionPtr func = (GetLongFunctionPtr)proc;
  2531. char buffer[MAX_PATH+1];
  2532. int len = (*func)(pathIn, buffer, MAX_PATH+1);
  2533. if(len == 0 || len > MAX_PATH+1)
  2534. {
  2535. FreeLibrary(lh);
  2536. return 0;
  2537. }
  2538. longPath = buffer;
  2539. FreeLibrary(lh);
  2540. return len;
  2541. }
  2542. FreeLibrary(lh);
  2543. }
  2544. return OldWindowsGetLongPath(pathIn, longPath);
  2545. }
  2546. #endif
  2547. //----------------------------------------------------------------------------
  2548. kwsys_stl::string SystemTools::GetActualCaseForPath(const char* p)
  2549. {
  2550. #ifndef _WIN32
  2551. return p;
  2552. #else
  2553. kwsys_stl::string shortPath;
  2554. if(!SystemTools::GetShortPath(p, shortPath))
  2555. {
  2556. return p;
  2557. }
  2558. kwsys_stl::string longPath;
  2559. int len = PortableGetLongPathName(shortPath.c_str(), longPath);
  2560. if(len == 0 || len > MAX_PATH+1)
  2561. {
  2562. return p;
  2563. }
  2564. return longPath;
  2565. #endif
  2566. }
  2567. //----------------------------------------------------------------------------
  2568. void SystemTools::SplitPath(const char* p,
  2569. kwsys_stl::vector<kwsys_stl::string>& components)
  2570. {
  2571. components.clear();
  2572. // Identify the root component.
  2573. const char* c = p;
  2574. if((c[0] == '/' && c[1] == '/') || (c[0] == '\\' && c[1] == '\\'))
  2575. {
  2576. // Network path.
  2577. components.push_back("//");
  2578. c += 2;
  2579. }
  2580. else if(c[0] == '/')
  2581. {
  2582. // Unix path.
  2583. components.push_back("/");
  2584. c += 1;
  2585. }
  2586. else if(c[0] && c[1] == ':' && (c[2] == '/' || c[2] == '\\'))
  2587. {
  2588. // Windows path.
  2589. kwsys_stl::string root = "_:/";
  2590. root[0] = c[0];
  2591. components.push_back(root);
  2592. c += 3;
  2593. }
  2594. else if(c[0] && c[1] == ':')
  2595. {
  2596. // Path relative to a windows drive working directory.
  2597. kwsys_stl::string root = "_:";
  2598. root[0] = c[0];
  2599. components.push_back(root);
  2600. c += 2;
  2601. }
  2602. else
  2603. {
  2604. // Relative path.
  2605. components.push_back("");
  2606. }
  2607. // Parse the remaining components.
  2608. const char* first = c;
  2609. const char* last = first;
  2610. for(;*last; ++last)
  2611. {
  2612. if(*last == '/' || *last == '\\')
  2613. {
  2614. // End of a component. Save it.
  2615. components.push_back(kwsys_stl::string(first, last-first));
  2616. first = last+1;
  2617. }
  2618. }
  2619. // Save the last component unless there were no components.
  2620. if(last != c)
  2621. {
  2622. components.push_back(kwsys_stl::string(first, last-first));
  2623. }
  2624. }
  2625. //----------------------------------------------------------------------------
  2626. kwsys_stl::string
  2627. SystemTools::JoinPath(const kwsys_stl::vector<kwsys_stl::string>& components)
  2628. {
  2629. return SystemTools::JoinPath(components.begin(), components.end());
  2630. }
  2631. //----------------------------------------------------------------------------
  2632. kwsys_stl::string
  2633. SystemTools
  2634. ::JoinPath(kwsys_stl::vector<kwsys_stl::string>::const_iterator first,
  2635. kwsys_stl::vector<kwsys_stl::string>::const_iterator last)
  2636. {
  2637. // Construct result in a single string.
  2638. kwsys_stl::string result;
  2639. // The first two components do not add a slash.
  2640. if(first != last)
  2641. {
  2642. result += *first++;
  2643. }
  2644. if(first != last)
  2645. {
  2646. result += *first++;
  2647. }
  2648. // All remaining components are always separated with a slash.
  2649. while(first != last)
  2650. {
  2651. result += "/";
  2652. result += *first++;
  2653. }
  2654. // Return the concatenated result.
  2655. return result;
  2656. }
  2657. //----------------------------------------------------------------------------
  2658. bool SystemTools::ComparePath(const char* c1, const char* c2)
  2659. {
  2660. #if defined(_WIN32) || defined(__APPLE__)
  2661. # ifdef _MSC_VER
  2662. return _stricmp(c1, c2) == 0;
  2663. # elif defined(__APPLE__) || defined(__GNUC__)
  2664. return strcasecmp(c1, c2) == 0;
  2665. #else
  2666. return SystemTools::Strucmp(c1, c2) == 0;
  2667. # endif
  2668. #else
  2669. return strcmp(c1, c2) == 0;
  2670. #endif
  2671. }
  2672. //----------------------------------------------------------------------------
  2673. bool SystemTools::Split(const char* str, kwsys_stl::vector<kwsys_stl::string>& lines, char separator)
  2674. {
  2675. kwsys_stl::string data(str);
  2676. kwsys_stl::string::size_type lpos = 0;
  2677. while(lpos < data.length())
  2678. {
  2679. kwsys_stl::string::size_type rpos = data.find_first_of(separator, lpos);
  2680. if(rpos == kwsys_stl::string::npos)
  2681. {
  2682. // Line ends at end of string without a newline.
  2683. lines.push_back(data.substr(lpos));
  2684. return false;
  2685. }
  2686. else
  2687. {
  2688. // Line ends in a "\n", remove the character.
  2689. lines.push_back(data.substr(lpos, rpos-lpos));
  2690. }
  2691. lpos = rpos+1;
  2692. }
  2693. return true;
  2694. }
  2695. //----------------------------------------------------------------------------
  2696. bool SystemTools::Split(const char* str, kwsys_stl::vector<kwsys_stl::string>& lines)
  2697. {
  2698. kwsys_stl::string data(str);
  2699. kwsys_stl::string::size_type lpos = 0;
  2700. while(lpos < data.length())
  2701. {
  2702. kwsys_stl::string::size_type rpos = data.find_first_of("\n", lpos);
  2703. if(rpos == kwsys_stl::string::npos)
  2704. {
  2705. // Line ends at end of string without a newline.
  2706. lines.push_back(data.substr(lpos));
  2707. return false;
  2708. }
  2709. if((rpos > lpos) && (data[rpos-1] == '\r'))
  2710. {
  2711. // Line ends in a "\r\n" pair, remove both characters.
  2712. lines.push_back(data.substr(lpos, (rpos-1)-lpos));
  2713. }
  2714. else
  2715. {
  2716. // Line ends in a "\n", remove the character.
  2717. lines.push_back(data.substr(lpos, rpos-lpos));
  2718. }
  2719. lpos = rpos+1;
  2720. }
  2721. return true;
  2722. }
  2723. /**
  2724. * Return path of a full filename (no trailing slashes).
  2725. * Warning: returned path is converted to Unix slashes format.
  2726. */
  2727. kwsys_stl::string SystemTools::GetFilenamePath(const kwsys_stl::string& filename)
  2728. {
  2729. kwsys_stl::string fn = filename;
  2730. SystemTools::ConvertToUnixSlashes(fn);
  2731. kwsys_stl::string::size_type slash_pos = fn.rfind("/");
  2732. if(slash_pos != kwsys_stl::string::npos)
  2733. {
  2734. kwsys_stl::string ret = fn.substr(0, slash_pos);
  2735. if(ret.size() == 2 && ret[1] == ':')
  2736. {
  2737. return ret + '/';
  2738. }
  2739. if(ret.size() == 0)
  2740. {
  2741. return "/";
  2742. }
  2743. return ret;
  2744. }
  2745. else
  2746. {
  2747. return "";
  2748. }
  2749. }
  2750. /**
  2751. * Return file name of a full filename (i.e. file name without path).
  2752. */
  2753. kwsys_stl::string SystemTools::GetFilenameName(const kwsys_stl::string& filename)
  2754. {
  2755. #if defined(_WIN32)
  2756. kwsys_stl::string::size_type slash_pos = filename.find_last_of("/\\");
  2757. #else
  2758. kwsys_stl::string::size_type slash_pos = filename.find_last_of("/");
  2759. #endif
  2760. if(slash_pos != kwsys_stl::string::npos)
  2761. {
  2762. return filename.substr(slash_pos + 1);
  2763. }
  2764. else
  2765. {
  2766. return filename;
  2767. }
  2768. }
  2769. /**
  2770. * Return file extension of a full filename (dot included).
  2771. * Warning: this is the longest extension (for example: .tar.gz)
  2772. */
  2773. kwsys_stl::string SystemTools::GetFilenameExtension(const kwsys_stl::string& filename)
  2774. {
  2775. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  2776. kwsys_stl::string::size_type dot_pos = name.find(".");
  2777. if(dot_pos != kwsys_stl::string::npos)
  2778. {
  2779. return name.substr(dot_pos);
  2780. }
  2781. else
  2782. {
  2783. return "";
  2784. }
  2785. }
  2786. /**
  2787. * Return file extension of a full filename (dot included).
  2788. * Warning: this is the shortest extension (for example: .tar.gz)
  2789. */
  2790. kwsys_stl::string SystemTools::GetFilenameLastExtension(const kwsys_stl::string& filename)
  2791. {
  2792. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  2793. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  2794. if(dot_pos != kwsys_stl::string::npos)
  2795. {
  2796. return name.substr(dot_pos);
  2797. }
  2798. else
  2799. {
  2800. return "";
  2801. }
  2802. }
  2803. /**
  2804. * Return file name without extension of a full filename (i.e. without path).
  2805. * Warning: it considers the longest extension (for example: .tar.gz)
  2806. */
  2807. kwsys_stl::string SystemTools::GetFilenameWithoutExtension(const kwsys_stl::string& filename)
  2808. {
  2809. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  2810. kwsys_stl::string::size_type dot_pos = name.find(".");
  2811. if(dot_pos != kwsys_stl::string::npos)
  2812. {
  2813. return name.substr(0, dot_pos);
  2814. }
  2815. else
  2816. {
  2817. return name;
  2818. }
  2819. }
  2820. /**
  2821. * Return file name without extension of a full filename (i.e. without path).
  2822. * Warning: it considers the last extension (for example: removes .gz
  2823. * from .tar.gz)
  2824. */
  2825. kwsys_stl::string
  2826. SystemTools::GetFilenameWithoutLastExtension(const kwsys_stl::string& filename)
  2827. {
  2828. kwsys_stl::string name = SystemTools::GetFilenameName(filename);
  2829. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  2830. if(dot_pos != kwsys_stl::string::npos)
  2831. {
  2832. return name.substr(0, dot_pos);
  2833. }
  2834. else
  2835. {
  2836. return name;
  2837. }
  2838. }
  2839. bool SystemTools::FileHasSignature(const char *filename,
  2840. const char *signature,
  2841. long offset)
  2842. {
  2843. if (!filename || !signature)
  2844. {
  2845. return false;
  2846. }
  2847. FILE *fp;
  2848. fp = fopen(filename, "rb");
  2849. if (!fp)
  2850. {
  2851. return false;
  2852. }
  2853. fseek(fp, offset, SEEK_SET);
  2854. bool res = false;
  2855. size_t signature_len = strlen(signature);
  2856. char *buffer = new char [signature_len];
  2857. if (fread(buffer, 1, signature_len, fp) == signature_len)
  2858. {
  2859. res = (!strncmp(buffer, signature, signature_len) ? true : false);
  2860. }
  2861. delete [] buffer;
  2862. fclose(fp);
  2863. return res;
  2864. }
  2865. SystemTools::FileTypeEnum
  2866. SystemTools::DetectFileType(const char *filename,
  2867. unsigned long length,
  2868. double percent_bin)
  2869. {
  2870. if (!filename || percent_bin < 0)
  2871. {
  2872. return SystemTools::FileTypeUnknown;
  2873. }
  2874. FILE *fp;
  2875. fp = fopen(filename, "rb");
  2876. if (!fp)
  2877. {
  2878. return SystemTools::FileTypeUnknown;
  2879. }
  2880. // Allocate buffer and read bytes
  2881. unsigned char *buffer = new unsigned char [length];
  2882. size_t read_length = fread(buffer, 1, length, fp);
  2883. fclose(fp);
  2884. if (read_length == 0)
  2885. {
  2886. return SystemTools::FileTypeUnknown;
  2887. }
  2888. // Loop over contents and count
  2889. size_t text_count = 0;
  2890. const unsigned char *ptr = buffer;
  2891. const unsigned char *buffer_end = buffer + read_length;
  2892. while (ptr != buffer_end)
  2893. {
  2894. if ((*ptr >= 0x20 && *ptr <= 0x7F) ||
  2895. *ptr == '\n' ||
  2896. *ptr == '\r' ||
  2897. *ptr == '\t')
  2898. {
  2899. text_count++;
  2900. }
  2901. ptr++;
  2902. }
  2903. delete [] buffer;
  2904. double current_percent_bin =
  2905. (static_cast<double>(read_length - text_count) /
  2906. static_cast<double>(read_length));
  2907. if (current_percent_bin >= percent_bin)
  2908. {
  2909. return SystemTools::FileTypeBinary;
  2910. }
  2911. return SystemTools::FileTypeText;
  2912. }
  2913. bool SystemTools::LocateFileInDir(const char *filename,
  2914. const char *dir,
  2915. kwsys_stl::string& filename_found,
  2916. int try_filename_dirs)
  2917. {
  2918. if (!filename || !dir)
  2919. {
  2920. return false;
  2921. }
  2922. // Get the basename of 'filename'
  2923. kwsys_stl::string filename_base = SystemTools::GetFilenameName(filename);
  2924. // Check if 'dir' is really a directory
  2925. // If win32 and matches something like C:, accept it as a dir
  2926. kwsys_stl::string real_dir;
  2927. if (!SystemTools::FileIsDirectory(dir))
  2928. {
  2929. #if defined( _WIN32 )
  2930. size_t dir_len = strlen(dir);
  2931. if (dir_len < 2 || dir[dir_len - 1] != ':')
  2932. {
  2933. #endif
  2934. real_dir = SystemTools::GetFilenamePath(dir);
  2935. dir = real_dir.c_str();
  2936. #if defined( _WIN32 )
  2937. }
  2938. #endif
  2939. }
  2940. // Try to find the file in 'dir'
  2941. bool res = false;
  2942. if (filename_base.size() && dir)
  2943. {
  2944. size_t dir_len = strlen(dir);
  2945. int need_slash =
  2946. (dir_len && dir[dir_len - 1] != '/' && dir[dir_len - 1] != '\\');
  2947. kwsys_stl::string temp = dir;
  2948. if (need_slash)
  2949. {
  2950. temp += "/";
  2951. }
  2952. temp += filename_base;
  2953. if (SystemTools::FileExists(filename_found.c_str()))
  2954. {
  2955. res = true;
  2956. filename_found = temp;
  2957. }
  2958. // If not found, we can try harder by appending part of the file to
  2959. // to the directory to look inside.
  2960. // Example: if we were looking for /foo/bar/yo.txt in /d1/d2, then
  2961. // try to find yo.txt in /d1/d2/bar, then /d1/d2/foo/bar, etc.
  2962. else if (try_filename_dirs)
  2963. {
  2964. kwsys_stl::string filename_dir(filename);
  2965. kwsys_stl::string filename_dir_base;
  2966. kwsys_stl::string filename_dir_bases;
  2967. do
  2968. {
  2969. filename_dir = SystemTools::GetFilenamePath(filename_dir);
  2970. filename_dir_base = SystemTools::GetFilenameName(filename_dir);
  2971. #if defined( _WIN32 )
  2972. if (!filename_dir_base.size() ||
  2973. filename_dir_base[filename_dir_base.size() - 1] == ':')
  2974. #else
  2975. if (!filename_dir_base.size())
  2976. #endif
  2977. {
  2978. break;
  2979. }
  2980. filename_dir_bases = filename_dir_base + "/" + filename_dir_bases;
  2981. temp = dir;
  2982. if (need_slash)
  2983. {
  2984. temp += "/";
  2985. }
  2986. temp += filename_dir_bases;
  2987. res = SystemTools::LocateFileInDir(
  2988. filename_base.c_str(), temp.c_str(), filename_found, 0);
  2989. } while (!res && filename_dir_base.size());
  2990. }
  2991. }
  2992. return res;
  2993. }
  2994. bool SystemTools::FileIsFullPath(const char* in_name)
  2995. {
  2996. kwsys_stl::string name = in_name;
  2997. #if defined(_WIN32) || defined(__CYGWIN__)
  2998. // On Windows, the name must be at least two characters long.
  2999. if(name.length() < 2)
  3000. {
  3001. return false;
  3002. }
  3003. if(name[1] == ':')
  3004. {
  3005. return true;
  3006. }
  3007. if(name[0] == '\\')
  3008. {
  3009. return true;
  3010. }
  3011. #else
  3012. // On UNIX, the name must be at least one character long.
  3013. if(name.length() < 1)
  3014. {
  3015. return false;
  3016. }
  3017. #endif
  3018. // On UNIX, the name must begin in a '/'.
  3019. // On Windows, if the name begins in a '/', then it is a full
  3020. // network path.
  3021. if(name[0] == '/')
  3022. {
  3023. return true;
  3024. }
  3025. return false;
  3026. }
  3027. bool SystemTools::GetShortPath(const char* path, kwsys_stl::string& shortPath)
  3028. {
  3029. #if defined(WIN32) && !defined(__CYGWIN__)
  3030. const int size = int(strlen(path)) +1; // size of return
  3031. char *buffer = new char[size]; // create a buffer
  3032. char *tempPath = new char[size]; // create a buffer
  3033. int ret;
  3034. // if the path passed in has quotes around it, first remove the quotes
  3035. if (path[0] == '"' && path[strlen(path)-1] == '"')
  3036. {
  3037. strcpy(tempPath,path+1);
  3038. tempPath[strlen(tempPath)-1] = '\0';
  3039. }
  3040. else
  3041. {
  3042. strcpy(tempPath,path);
  3043. }
  3044. buffer[0] = 0;
  3045. ret = GetShortPathName(tempPath, buffer, size);
  3046. if(buffer[0] == 0 || ret > size)
  3047. {
  3048. delete [] buffer;
  3049. delete [] tempPath;
  3050. return false;
  3051. }
  3052. else
  3053. {
  3054. shortPath = buffer;
  3055. delete [] buffer;
  3056. delete [] tempPath;
  3057. return true;
  3058. }
  3059. #else
  3060. shortPath = path;
  3061. return true;
  3062. #endif
  3063. }
  3064. void SystemTools::SplitProgramFromArgs(const char* path,
  3065. kwsys_stl::string& program, kwsys_stl::string& args)
  3066. {
  3067. // see if this is a full path to a program
  3068. // if so then set program to path and args to nothing
  3069. if(SystemTools::FileExists(path))
  3070. {
  3071. program = path;
  3072. args = "";
  3073. return;
  3074. }
  3075. // Try to find the program in the path, note the program
  3076. // may have spaces in its name so we have to look for it
  3077. kwsys_stl::vector<kwsys_stl::string> e;
  3078. kwsys_stl::string findProg = SystemTools::FindProgram(path, e);
  3079. if(findProg.size())
  3080. {
  3081. program = findProg;
  3082. args = "";
  3083. return;
  3084. }
  3085. // Now try and peel off space separated chunks from the end of the string
  3086. // so the largest path possible is found allowing for spaces in the path
  3087. kwsys_stl::string dir = path;
  3088. kwsys_stl::string::size_type spacePos = dir.rfind(' ');
  3089. while(spacePos != kwsys_stl::string::npos)
  3090. {
  3091. kwsys_stl::string tryProg = dir.substr(0, spacePos);
  3092. // See if the file exists
  3093. if(SystemTools::FileExists(tryProg.c_str()))
  3094. {
  3095. program = tryProg;
  3096. // remove trailing spaces from program
  3097. kwsys_stl::string::size_type pos = program.size()-1;
  3098. while(program[pos] == ' ')
  3099. {
  3100. program.erase(pos);
  3101. pos--;
  3102. }
  3103. args = dir.substr(spacePos, dir.size()-spacePos);
  3104. return;
  3105. }
  3106. // Now try and find the the program in the path
  3107. findProg = SystemTools::FindProgram(tryProg.c_str(), e);
  3108. if(findProg.size())
  3109. {
  3110. program = findProg;
  3111. // remove trailing spaces from program
  3112. kwsys_stl::string::size_type pos = program.size()-1;
  3113. while(program[pos] == ' ')
  3114. {
  3115. program.erase(pos);
  3116. pos--;
  3117. }
  3118. args = dir.substr(spacePos, dir.size()-spacePos);
  3119. return;
  3120. }
  3121. // move past the space for the next search
  3122. spacePos--;
  3123. spacePos = dir.rfind(' ', spacePos);
  3124. }
  3125. program = "";
  3126. args = "";
  3127. }
  3128. kwsys_stl::string SystemTools::GetCurrentDateTime(const char* format)
  3129. {
  3130. char buf[1024];
  3131. time_t t;
  3132. time(&t);
  3133. strftime(buf, sizeof(buf), format, localtime(&t));
  3134. return kwsys_stl::string(buf);
  3135. }
  3136. kwsys_stl::string SystemTools::MakeCindentifier(const char* s)
  3137. {
  3138. kwsys_stl::string str(s);
  3139. if (str.find_first_of("0123456789") == 0)
  3140. {
  3141. str = "_" + str;
  3142. }
  3143. kwsys_stl::string permited_chars("_"
  3144. "abcdefghijklmnopqrstuvwxyz"
  3145. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  3146. "0123456789");
  3147. kwsys_stl::string::size_type pos = 0;
  3148. while ((pos = str.find_first_not_of(permited_chars, pos)) != kwsys_stl::string::npos)
  3149. {
  3150. str[pos] = '_';
  3151. }
  3152. return str;
  3153. }
  3154. // Due to a buggy stream library on the HP and another on Mac OSX, we
  3155. // need this very carefully written version of getline. Returns true
  3156. // if any data were read before the end-of-file was reached.
  3157. bool SystemTools::GetLineFromStream(kwsys_ios::istream& is,
  3158. kwsys_stl::string& line,
  3159. bool* has_newline /* = 0 */)
  3160. {
  3161. const int bufferSize = 1024;
  3162. char buffer[bufferSize];
  3163. bool haveData = false;
  3164. bool haveNewline = false;
  3165. // Start with an empty line.
  3166. line = "";
  3167. // If no characters are read from the stream, the end of file has
  3168. // been reached. Clear the fail bit just before reading.
  3169. while(!haveNewline &&
  3170. (is.clear(is.rdstate() & ~kwsys_ios::ios::failbit),
  3171. is.getline(buffer, bufferSize), is.gcount() > 0))
  3172. {
  3173. // We have read at least one byte.
  3174. haveData = true;
  3175. // If newline character was read the gcount includes the character
  3176. // but the buffer does not: the end of line has been reached.
  3177. size_t length = strlen(buffer);
  3178. if(length < static_cast<size_t>(is.gcount()))
  3179. {
  3180. haveNewline = true;
  3181. }
  3182. // Avoid storing a carriage return character.
  3183. if(length > 0 && buffer[length-1] == '\r')
  3184. {
  3185. buffer[length-1] = 0;
  3186. }
  3187. // Append the data read to the line.
  3188. line.append(buffer);
  3189. }
  3190. // Return the results.
  3191. if(has_newline)
  3192. {
  3193. *has_newline = haveNewline;
  3194. }
  3195. return haveData;
  3196. }
  3197. int SystemTools::GetTerminalWidth()
  3198. {
  3199. int width = -1;
  3200. #ifndef _WIN32
  3201. struct winsize ws;
  3202. char *columns; /* Unix98 environment variable */
  3203. if(ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col>0 && ws.ws_row>0)
  3204. {
  3205. width = ws.ws_col;
  3206. }
  3207. if(!isatty(STDOUT_FILENO))
  3208. {
  3209. width = -1;
  3210. }
  3211. columns = getenv("COLUMNS");
  3212. if(columns && *columns)
  3213. {
  3214. long t;
  3215. char *endptr;
  3216. t = strtol(columns, &endptr, 0);
  3217. if(endptr && !*endptr && (t>0) && (t<1000))
  3218. {
  3219. width = static_cast<int>(t);
  3220. }
  3221. }
  3222. if ( width < 9 )
  3223. {
  3224. width = -1;
  3225. }
  3226. #endif
  3227. return width;
  3228. }
  3229. bool SystemTools::GetPermissions(const char* file, mode_t& mode)
  3230. {
  3231. if ( !file )
  3232. {
  3233. return false;
  3234. }
  3235. struct stat st;
  3236. if ( stat(file, &st) < 0 )
  3237. {
  3238. return false;
  3239. }
  3240. mode = st.st_mode;
  3241. return true;
  3242. }
  3243. bool SystemTools::SetPermissions(const char* file, mode_t mode)
  3244. {
  3245. if ( !file )
  3246. {
  3247. return false;
  3248. }
  3249. if ( !SystemTools::FileExists(file) )
  3250. {
  3251. return false;
  3252. }
  3253. if ( chmod(file, mode) < 0 )
  3254. {
  3255. return false;
  3256. }
  3257. return true;
  3258. }
  3259. kwsys_stl::string SystemTools::GetParentDirectory(const char* fileOrDir)
  3260. {
  3261. if ( !fileOrDir || !*fileOrDir )
  3262. {
  3263. return "";
  3264. }
  3265. kwsys_stl::string res = fileOrDir;
  3266. SystemTools::ConvertToUnixSlashes(res);
  3267. kwsys_stl::string::size_type cc = res.size()-1;
  3268. if ( res[cc] == '/' )
  3269. {
  3270. cc --;
  3271. }
  3272. for ( ; cc > 0; cc -- )
  3273. {
  3274. if ( res[cc] == '/' )
  3275. {
  3276. break;
  3277. }
  3278. }
  3279. return res.substr(0, cc);
  3280. }
  3281. bool SystemTools::IsSubDirectory(const char* cSubdir, const char* cDir)
  3282. {
  3283. kwsys_stl::string subdir = cSubdir;
  3284. kwsys_stl::string dir = cDir;
  3285. SystemTools::ConvertToUnixSlashes(dir);
  3286. kwsys_stl::string path = subdir;
  3287. do
  3288. {
  3289. path = SystemTools::GetParentDirectory(path.c_str());
  3290. if(SystemTools::ComparePath(dir.c_str(), path.c_str()))
  3291. {
  3292. return true;
  3293. }
  3294. }
  3295. while ( path.size() > dir.size() );
  3296. return false;
  3297. }
  3298. kwsys_stl::string SystemTools::FileExistsInParentDirectories(const char* fname,
  3299. const char* directory, const char* toplevel)
  3300. {
  3301. kwsys_stl::string file = fname;
  3302. SystemTools::ConvertToUnixSlashes(file);
  3303. kwsys_stl::string dir = directory;
  3304. SystemTools::ConvertToUnixSlashes(dir);
  3305. while ( !dir.empty() )
  3306. {
  3307. kwsys_stl::string path = dir + "/" + file;
  3308. if ( SystemTools::FileExists(path.c_str()) )
  3309. {
  3310. return path;
  3311. }
  3312. if ( dir.size() < strlen(toplevel) )
  3313. {
  3314. break;
  3315. }
  3316. dir = SystemTools::GetParentDirectory(dir.c_str());
  3317. }
  3318. return "";
  3319. }
  3320. void SystemTools::Delay(unsigned int msec)
  3321. {
  3322. #ifdef _WIN32
  3323. Sleep(msec);
  3324. #else
  3325. // The sleep function gives 1 second resolution and the usleep
  3326. // function gives 1e-6 second resolution but on some platforms has a
  3327. // maximum sleep time of 1 second. This could be re-implemented to
  3328. // use select with masked signals or pselect to mask signals
  3329. // atomically. If select is given empty sets and zero as the max
  3330. // file descriptor but a non-zero timeout it can be used to block
  3331. // for a precise amount of time.
  3332. if(msec >= 1000)
  3333. {
  3334. sleep(msec / 1000);
  3335. usleep((msec % 1000) * 1000);
  3336. }
  3337. else
  3338. {
  3339. usleep(msec * 1000);
  3340. }
  3341. #endif
  3342. }
  3343. void SystemTools::ConvertWindowsCommandLineToUnixArguments(
  3344. const char *cmd_line, int *argc, char ***argv)
  3345. {
  3346. if (!cmd_line || !argc || !argv)
  3347. {
  3348. return;
  3349. }
  3350. // A space delimites an argument except when it is inside a quote
  3351. (*argc) = 1;
  3352. size_t cmd_line_len = strlen(cmd_line);
  3353. size_t i;
  3354. for (i = 0; i < cmd_line_len; i++)
  3355. {
  3356. while (isspace(cmd_line[i]) && i < cmd_line_len)
  3357. {
  3358. i++;
  3359. }
  3360. if (i < cmd_line_len)
  3361. {
  3362. if (cmd_line[i] == '\"')
  3363. {
  3364. i++;
  3365. while (cmd_line[i] != '\"' && i < cmd_line_len)
  3366. {
  3367. i++;
  3368. }
  3369. (*argc)++;
  3370. }
  3371. else
  3372. {
  3373. while (!isspace(cmd_line[i]) && i < cmd_line_len)
  3374. {
  3375. i++;
  3376. }
  3377. (*argc)++;
  3378. }
  3379. }
  3380. }
  3381. (*argv) = new char* [(*argc) + 1];
  3382. (*argv)[(*argc)] = NULL;
  3383. // Set the first arg to be the exec name
  3384. (*argv)[0] = new char [1024];
  3385. #ifdef _WIN32
  3386. ::GetModuleFileName(0, (*argv)[0], 1024);
  3387. #else
  3388. (*argv)[0][0] = '\0';
  3389. #endif
  3390. // Allocate the others
  3391. int j;
  3392. for (j = 1; j < (*argc); j++)
  3393. {
  3394. (*argv)[j] = new char [cmd_line_len + 10];
  3395. }
  3396. // Grab the args
  3397. size_t pos;
  3398. int argc_idx = 1;
  3399. for (i = 0; i < cmd_line_len; i++)
  3400. {
  3401. while (isspace(cmd_line[i]) && i < cmd_line_len)
  3402. {
  3403. i++;
  3404. }
  3405. if (i < cmd_line_len)
  3406. {
  3407. if (cmd_line[i] == '\"')
  3408. {
  3409. i++;
  3410. pos = i;
  3411. while (cmd_line[i] != '\"' && i < cmd_line_len)
  3412. {
  3413. i++;
  3414. }
  3415. memcpy((*argv)[argc_idx], &cmd_line[pos], i - pos);
  3416. (*argv)[argc_idx][i - pos] = '\0';
  3417. argc_idx++;
  3418. }
  3419. else
  3420. {
  3421. pos = i;
  3422. while (!isspace(cmd_line[i]) && i < cmd_line_len)
  3423. {
  3424. i++;
  3425. }
  3426. memcpy((*argv)[argc_idx], &cmd_line[pos], i - pos);
  3427. (*argv)[argc_idx][i - pos] = '\0';
  3428. argc_idx++;
  3429. }
  3430. }
  3431. }
  3432. }
  3433. kwsys_stl::string SystemTools::GetOperatingSystemNameAndVersion()
  3434. {
  3435. kwsys_stl::string res;
  3436. #ifdef _WIN32
  3437. char buffer[256];
  3438. OSVERSIONINFOEX osvi;
  3439. BOOL bOsVersionInfoEx;
  3440. // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
  3441. // If that fails, try using the OSVERSIONINFO structure.
  3442. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  3443. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  3444. bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi);
  3445. if (!bOsVersionInfoEx)
  3446. {
  3447. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  3448. if (!GetVersionEx((OSVERSIONINFO *)&osvi))
  3449. {
  3450. return 0;
  3451. }
  3452. }
  3453. switch (osvi.dwPlatformId)
  3454. {
  3455. // Test for the Windows NT product family.
  3456. case VER_PLATFORM_WIN32_NT:
  3457. // Test for the specific product family.
  3458. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  3459. {
  3460. res += "Microsoft Windows Server 2003 family";
  3461. }
  3462. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  3463. {
  3464. res += "Microsoft Windows XP";
  3465. }
  3466. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  3467. {
  3468. res += "Microsoft Windows 2000";
  3469. }
  3470. if (osvi.dwMajorVersion <= 4)
  3471. {
  3472. res += "Microsoft Windows NT";
  3473. }
  3474. // Test for specific product on Windows NT 4.0 SP6 and later.
  3475. if (bOsVersionInfoEx)
  3476. {
  3477. // Test for the workstation type.
  3478. #if (_MSC_VER >= 1300)
  3479. if (osvi.wProductType == VER_NT_WORKSTATION)
  3480. {
  3481. if (osvi.dwMajorVersion == 4)
  3482. {
  3483. res += " Workstation 4.0";
  3484. }
  3485. else if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
  3486. {
  3487. res += " Home Edition";
  3488. }
  3489. else
  3490. {
  3491. res += " Professional";
  3492. }
  3493. }
  3494. // Test for the server type.
  3495. else if (osvi.wProductType == VER_NT_SERVER)
  3496. {
  3497. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  3498. {
  3499. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  3500. {
  3501. res += " Datacenter Edition";
  3502. }
  3503. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  3504. {
  3505. res += " Enterprise Edition";
  3506. }
  3507. else if (osvi.wSuiteMask == VER_SUITE_BLADE)
  3508. {
  3509. res += " Web Edition";
  3510. }
  3511. else
  3512. {
  3513. res += " Standard Edition";
  3514. }
  3515. }
  3516. else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  3517. {
  3518. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  3519. {
  3520. res += " Datacenter Server";
  3521. }
  3522. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  3523. {
  3524. res += " Advanced Server";
  3525. }
  3526. else
  3527. {
  3528. res += " Server";
  3529. }
  3530. }
  3531. else // Windows NT 4.0
  3532. {
  3533. if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  3534. {
  3535. res += " Server 4.0, Enterprise Edition";
  3536. }
  3537. else
  3538. {
  3539. res += " Server 4.0";
  3540. }
  3541. }
  3542. }
  3543. #endif // Visual Studio 7 and up
  3544. }
  3545. // Test for specific product on Windows NT 4.0 SP5 and earlier
  3546. else
  3547. {
  3548. HKEY hKey;
  3549. #define BUFSIZE 80
  3550. char szProductType[BUFSIZE];
  3551. DWORD dwBufLen=BUFSIZE;
  3552. LONG lRet;
  3553. lRet = RegOpenKeyEx(
  3554. HKEY_LOCAL_MACHINE,
  3555. "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
  3556. 0, KEY_QUERY_VALUE, &hKey);
  3557. if (lRet != ERROR_SUCCESS)
  3558. {
  3559. return 0;
  3560. }
  3561. lRet = RegQueryValueEx(hKey, "ProductType", NULL, NULL,
  3562. (LPBYTE) szProductType, &dwBufLen);
  3563. if ((lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE))
  3564. {
  3565. return 0;
  3566. }
  3567. RegCloseKey(hKey);
  3568. if (lstrcmpi("WINNT", szProductType) == 0)
  3569. {
  3570. res += " Workstation";
  3571. }
  3572. if (lstrcmpi("LANMANNT", szProductType) == 0)
  3573. {
  3574. res += " Server";
  3575. }
  3576. if (lstrcmpi("SERVERNT", szProductType) == 0)
  3577. {
  3578. res += " Advanced Server";
  3579. }
  3580. res += " ";
  3581. sprintf(buffer, "%d", osvi.dwMajorVersion);
  3582. res += buffer;
  3583. res += ".";
  3584. sprintf(buffer, "%d", osvi.dwMinorVersion);
  3585. res += buffer;
  3586. }
  3587. // Display service pack (if any) and build number.
  3588. if (osvi.dwMajorVersion == 4 &&
  3589. lstrcmpi(osvi.szCSDVersion, "Service Pack 6") == 0)
  3590. {
  3591. HKEY hKey;
  3592. LONG lRet;
  3593. // Test for SP6 versus SP6a.
  3594. lRet = RegOpenKeyEx(
  3595. HKEY_LOCAL_MACHINE,
  3596. "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009",
  3597. 0, KEY_QUERY_VALUE, &hKey);
  3598. if (lRet == ERROR_SUCCESS)
  3599. {
  3600. res += " Service Pack 6a (Build ";
  3601. sprintf(buffer, "%d", osvi.dwBuildNumber & 0xFFFF);
  3602. res += buffer;
  3603. res += ")";
  3604. }
  3605. else // Windows NT 4.0 prior to SP6a
  3606. {
  3607. res += " ";
  3608. res += osvi.szCSDVersion;
  3609. res += " (Build ";
  3610. sprintf(buffer, "%d", osvi.dwBuildNumber & 0xFFFF);
  3611. res += buffer;
  3612. res += ")";
  3613. }
  3614. RegCloseKey(hKey);
  3615. }
  3616. else // Windows NT 3.51 and earlier or Windows 2000 and later
  3617. {
  3618. res += " ";
  3619. res += osvi.szCSDVersion;
  3620. res += " (Build ";
  3621. sprintf(buffer, "%d", osvi.dwBuildNumber & 0xFFFF);
  3622. res += buffer;
  3623. res += ")";
  3624. }
  3625. break;
  3626. // Test for the Windows 95 product family.
  3627. case VER_PLATFORM_WIN32_WINDOWS:
  3628. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
  3629. {
  3630. res += "Microsoft Windows 95";
  3631. if (osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B')
  3632. {
  3633. res += " OSR2";
  3634. }
  3635. }
  3636. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
  3637. {
  3638. res += "Microsoft Windows 98";
  3639. if (osvi.szCSDVersion[1] == 'A')
  3640. {
  3641. res += " SE";
  3642. }
  3643. }
  3644. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
  3645. {
  3646. res += "Microsoft Windows Millennium Edition";
  3647. }
  3648. break;
  3649. case VER_PLATFORM_WIN32s:
  3650. res += "Microsoft Win32s";
  3651. break;
  3652. }
  3653. #endif
  3654. return res;
  3655. }
  3656. // These must NOT be initialized. Default initialization to zero is
  3657. // necessary.
  3658. unsigned int SystemToolsManagerCount;
  3659. SystemToolsTranslationMap *SystemTools::TranslationMap;
  3660. // SystemToolsManager manages the SystemTools singleton.
  3661. // SystemToolsManager should be included in any translation unit
  3662. // that will use SystemTools or that implements the singleton
  3663. // pattern. It makes sure that the SystemTools singleton is created
  3664. // before and destroyed after all other singletons in CMake.
  3665. SystemToolsManager::SystemToolsManager()
  3666. {
  3667. if(++SystemToolsManagerCount == 1)
  3668. {
  3669. SystemTools::ClassInitialize();
  3670. }
  3671. }
  3672. SystemToolsManager::~SystemToolsManager()
  3673. {
  3674. if(--SystemToolsManagerCount == 0)
  3675. {
  3676. SystemTools::ClassFinalize();
  3677. }
  3678. }
  3679. void SystemTools::ClassInitialize()
  3680. {
  3681. // Allocate the translation map first.
  3682. SystemTools::TranslationMap = new SystemToolsTranslationMap;
  3683. // Add some special translation paths for unix. These are not added
  3684. // for windows because drive letters need to be maintained. Also,
  3685. // there are not sym-links and mount points on windows anyway.
  3686. #if !defined(_WIN32) || defined(__CYGWIN__)
  3687. // Work-around an SGI problem by always adding this mapping:
  3688. SystemTools::AddTranslationPath("/tmp_mnt/", "/");
  3689. // The tmp path is frequently a logical path so always keep it:
  3690. SystemTools::AddKeepPath("/tmp/");
  3691. // If the current working directory is a logical path then keep the
  3692. // logical name.
  3693. if(const char* pwd = getenv("PWD"))
  3694. {
  3695. char buf[2048];
  3696. if(const char* cwd = Getcwd(buf, 2048))
  3697. {
  3698. // The current working directory may be a logical path. Find
  3699. // the shortest logical path that still produces the correct
  3700. // physical path.
  3701. kwsys_stl::string cwd_changed;
  3702. kwsys_stl::string pwd_changed;
  3703. // Test progressively shorter logical-to-physical mappings.
  3704. kwsys_stl::string pwd_str = pwd;
  3705. kwsys_stl::string cwd_str = cwd;
  3706. kwsys_stl::string pwd_path;
  3707. Realpath(pwd, pwd_path);
  3708. while(cwd_str == pwd_path && cwd_str != pwd_str)
  3709. {
  3710. // The current pair of paths is a working logical mapping.
  3711. cwd_changed = cwd_str;
  3712. pwd_changed = pwd_str;
  3713. // Strip off one directory level and see if the logical
  3714. // mapping still works.
  3715. pwd_str = SystemTools::GetFilenamePath(pwd_str.c_str());
  3716. cwd_str = SystemTools::GetFilenamePath(cwd_str.c_str());
  3717. Realpath(pwd_str.c_str(), pwd_path);
  3718. }
  3719. // Add the translation to keep the logical path name.
  3720. if(!cwd_changed.empty() && !pwd_changed.empty())
  3721. {
  3722. SystemTools::AddTranslationPath(cwd_changed.c_str(),
  3723. pwd_changed.c_str());
  3724. }
  3725. }
  3726. }
  3727. #endif
  3728. }
  3729. void SystemTools::ClassFinalize()
  3730. {
  3731. delete SystemTools::TranslationMap;
  3732. }
  3733. } // namespace KWSYS_NAMESPACE
  3734. #if defined(_MSC_VER) && defined(_DEBUG)
  3735. # include <crtdbg.h>
  3736. # include <stdio.h>
  3737. # include <stdlib.h>
  3738. namespace KWSYS_NAMESPACE
  3739. {
  3740. static int SystemToolsDebugReport(int, char* message, int*)
  3741. {
  3742. fprintf(stderr, "%s", message);
  3743. fflush(stderr);
  3744. return 1; // no further reporting required
  3745. }
  3746. void SystemTools::EnableMSVCDebugHook()
  3747. {
  3748. if (getenv("DART_TEST_FROM_DART"))
  3749. {
  3750. _CrtSetReportHook(SystemToolsDebugReport);
  3751. }
  3752. }
  3753. } // namespace KWSYS_NAMESPACE
  3754. #else
  3755. namespace KWSYS_NAMESPACE
  3756. {
  3757. void SystemTools::EnableMSVCDebugHook() {}
  3758. } // namespace KWSYS_NAMESPACE
  3759. #endif