SystemTools.hxx.in 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_SystemTools_hxx
  11. #define @KWSYS_NAMESPACE@_SystemTools_hxx
  12. #include <@KWSYS_NAMESPACE@/ios/iosfwd>
  13. #include <@KWSYS_NAMESPACE@/stl/string>
  14. #include <@KWSYS_NAMESPACE@/stl/vector>
  15. #include <@KWSYS_NAMESPACE@/stl/map>
  16. #include <@KWSYS_NAMESPACE@/Configure.h>
  17. #include <@KWSYS_NAMESPACE@/String.hxx>
  18. #include <sys/types.h>
  19. // Required for va_list
  20. #include <stdarg.h>
  21. #if @KWSYS_NAMESPACE@_STL_HAVE_STD && !defined(va_list)
  22. // Some compilers move va_list into the std namespace and there is no way to
  23. // tell that this has been done. Playing with things being included before or
  24. // after stdarg.h does not solve things because we do not have control over
  25. // what the user does. This hack solves this problem by moving va_list to our
  26. // own namespace that is local for kwsys.
  27. namespace std {} // Required for platforms that do not have std namespace
  28. namespace @KWSYS_NAMESPACE@_VA_LIST
  29. {
  30. using namespace std;
  31. typedef va_list hack_va_list;
  32. }
  33. namespace @KWSYS_NAMESPACE@
  34. {
  35. typedef @KWSYS_NAMESPACE@_VA_LIST::hack_va_list va_list;
  36. }
  37. #endif // va_list
  38. #if defined( _MSC_VER )
  39. typedef unsigned short mode_t;
  40. #endif
  41. /* Define these macros temporarily to keep the code readable. */
  42. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  43. # define kwsys_stl @KWSYS_NAMESPACE@_stl
  44. # define kwsys_ios @KWSYS_NAMESPACE@_ios
  45. #endif
  46. namespace @KWSYS_NAMESPACE@
  47. {
  48. class SystemToolsTranslationMap;
  49. /** \class SystemToolsManager
  50. * \brief Use to make sure SystemTools is initialized before it is used
  51. * and is the last static object destroyed
  52. */
  53. class @KWSYS_NAMESPACE@_EXPORT SystemToolsManager
  54. {
  55. public:
  56. SystemToolsManager();
  57. ~SystemToolsManager();
  58. };
  59. // This instance will show up in any translation unit that uses
  60. // SystemTools. It will make sure SystemTools is initialized
  61. // before it is used and is the last static object destroyed.
  62. static SystemToolsManager SystemToolsManagerInstance;
  63. /** \class SystemTools
  64. * \brief A collection of useful platform-independent system functions.
  65. */
  66. class @KWSYS_NAMESPACE@_EXPORT SystemTools
  67. {
  68. public:
  69. /** -----------------------------------------------------------------
  70. * String Manipulation Routines
  71. * -----------------------------------------------------------------
  72. */
  73. /**
  74. * Replace symbols in str that are not valid in C identifiers as
  75. * defined by the 1999 standard, ie. anything except [A-Za-z0-9_].
  76. * They are replaced with `_' and if the first character is a digit
  77. * then an underscore is prepended. Note that this can produce
  78. * identifiers that the standard reserves (_[A-Z].* and __.*).
  79. */
  80. static kwsys_stl::string MakeCidentifier(const char* s);
  81. static kwsys_stl::string MakeCindentifier(const char* s)
  82. {
  83. return MakeCidentifier(s);
  84. }
  85. /**
  86. * Replace replace all occurences of the string in the source string.
  87. */
  88. static void ReplaceString(kwsys_stl::string& source,
  89. const char* replace,
  90. const char* with);
  91. /**
  92. * Return a capitalized string (i.e the first letter is uppercased,
  93. * all other are lowercased).
  94. */
  95. static kwsys_stl::string Capitalized(const kwsys_stl::string&);
  96. /**
  97. * Return a 'capitalized words' string (i.e the first letter of each word
  98. * is uppercased all other are left untouched though).
  99. */
  100. static kwsys_stl::string CapitalizedWords(const kwsys_stl::string&);
  101. /**
  102. * Return a 'uncapitalized words' string (i.e the first letter of each word
  103. * is lowercased all other are left untouched though).
  104. */
  105. static kwsys_stl::string UnCapitalizedWords(const kwsys_stl::string&);
  106. /**
  107. * Return a lower case string
  108. */
  109. static kwsys_stl::string LowerCase(const kwsys_stl::string&);
  110. /**
  111. * Return a lower case string
  112. */
  113. static kwsys_stl::string UpperCase(const kwsys_stl::string&);
  114. /**
  115. * Count char in string
  116. */
  117. static size_t CountChar(const char* str, char c);
  118. /**
  119. * Remove some characters from a string.
  120. * Return a pointer to the new resulting string (allocated with 'new')
  121. */
  122. static char* RemoveChars(const char* str, const char *toremove);
  123. /**
  124. * Remove remove all but 0->9, A->F characters from a string.
  125. * Return a pointer to the new resulting string (allocated with 'new')
  126. */
  127. static char* RemoveCharsButUpperHex(const char* str);
  128. /**
  129. * Replace some characters by another character in a string (in-place)
  130. * Return a pointer to string
  131. */
  132. static char* ReplaceChars(char* str, const char *toreplace,char replacement);
  133. /**
  134. * Returns true if str1 starts (respectively ends) with str2
  135. */
  136. static bool StringStartsWith(const char* str1, const char* str2);
  137. static bool StringEndsWith(const char* str1, const char* str2);
  138. /**
  139. * Returns a pointer to the last occurence of str2 in str1
  140. */
  141. static const char* FindLastString(const char* str1, const char* str2);
  142. /**
  143. * Make a duplicate of the string similar to the strdup C function
  144. * but use new to create the 'new' string, so one can use
  145. * 'delete' to remove it. Returns 0 if the input is empty.
  146. */
  147. static char* DuplicateString(const char* str);
  148. /**
  149. * Return the string cropped to a given length by removing chars in the
  150. * center of the string and replacing them with an ellipsis (...)
  151. */
  152. static kwsys_stl::string CropString(const kwsys_stl::string&,size_t max_len);
  153. /** split a path by separator into an array of strings, default is /.
  154. If isPath is true then the string is treated like a path and if
  155. s starts with a / then the first element of the returned array will
  156. be /, so /foo/bar will be [/, foo, bar]
  157. */
  158. static kwsys_stl::vector<String> SplitString(const char* s, char separator = '/',
  159. bool isPath = false);
  160. /**
  161. * Perform a case-independent string comparison
  162. */
  163. static int Strucmp(const char *s1, const char *s2);
  164. /**
  165. * Convert a string in __DATE__ or __TIMESTAMP__ format into a time_t.
  166. * Return false on error, true on success
  167. */
  168. static bool ConvertDateMacroString(const char *str, time_t *tmt);
  169. static bool ConvertTimeStampMacroString(const char *str, time_t *tmt);
  170. /**
  171. * Split a string on its newlines into multiple lines
  172. * Return false only if the last line stored had no newline
  173. */
  174. static bool Split(const char* s, kwsys_stl::vector<kwsys_stl::string>& l);
  175. static bool Split(const char* s, kwsys_stl::vector<kwsys_stl::string>& l, char separator);
  176. /**
  177. * Return string with space added between capitalized words
  178. * (i.e. EatMyShorts becomes Eat My Shorts )
  179. * (note that IEatShorts becomes IEat Shorts)
  180. */
  181. static kwsys_stl::string AddSpaceBetweenCapitalizedWords(
  182. const kwsys_stl::string&);
  183. /**
  184. * Append two or more strings and produce new one.
  185. * Programmer must 'delete []' the resulting string, which was allocated
  186. * with 'new'.
  187. * Return 0 if inputs are empty or there was an error
  188. */
  189. static char* AppendStrings(
  190. const char* str1, const char* str2);
  191. static char* AppendStrings(
  192. const char* str1, const char* str2, const char* str3);
  193. /**
  194. * Estimate the length of the string that will be produced
  195. * from printing the given format string and arguments. The
  196. * returned length will always be at least as large as the string
  197. * that will result from printing.
  198. * WARNING: since va_arg is called to iterate of the argument list,
  199. * you will not be able to use this 'ap' anymore from the beginning.
  200. * It's up to you to call va_end though.
  201. */
  202. static int EstimateFormatLength(const char *format, va_list ap);
  203. /**
  204. * Escape specific characters in 'str'.
  205. */
  206. static kwsys_stl::string EscapeChars(
  207. const char *str, const char *chars_to_escape, char escape_char = '\\');
  208. /** -----------------------------------------------------------------
  209. * Filename Manipulation Routines
  210. * -----------------------------------------------------------------
  211. */
  212. /**
  213. * Replace Windows file system slashes with Unix-style slashes.
  214. */
  215. static void ConvertToUnixSlashes(kwsys_stl::string& path);
  216. /**
  217. * For windows this calls ConvertToWindowsOutputPath and for unix
  218. * it calls ConvertToUnixOutputPath
  219. */
  220. static kwsys_stl::string ConvertToOutputPath(const char*);
  221. /**
  222. * Convert the path to a string that can be used in a unix makefile.
  223. * double slashes are removed, and spaces are escaped.
  224. */
  225. static kwsys_stl::string ConvertToUnixOutputPath(const char*);
  226. /**
  227. * Convert the path to string that can be used in a windows project or
  228. * makefile. Double slashes are removed if they are not at the start of
  229. * the string, the slashes are converted to windows style backslashes, and
  230. * if there are spaces in the string it is double quoted.
  231. */
  232. static kwsys_stl::string ConvertToWindowsOutputPath(const char*);
  233. /**
  234. * Return true if a file exists in the current directory.
  235. * If isFile = true, then make sure the file is a file and
  236. * not a directory. If isFile = false, then return true
  237. * if it is a file or a directory.
  238. */
  239. static bool FileExists(const char* filename, bool isFile);
  240. static bool FileExists(const char* filename);
  241. /**
  242. * Converts Cygwin path to Win32 path. Uses dictionary container for
  243. * caching and calls to cygwin_conv_to_win32_path from Cygwin dll
  244. * for actual translation. Returns true on success, else false.
  245. */
  246. #ifdef __CYGWIN__
  247. static bool PathCygwinToWin32(const char *path, char *win32_path);
  248. #endif
  249. /**
  250. * Return file length
  251. */
  252. static unsigned long FileLength(const char *filename);
  253. /**
  254. Change the modification time or create a file
  255. */
  256. static bool Touch(const char* filename, bool create);
  257. /**
  258. * Compare file modification times.
  259. * Return true for successful comparison and false for error.
  260. * When true is returned, result has -1, 0, +1 for
  261. * f1 older, same, or newer than f2.
  262. */
  263. static bool FileTimeCompare(const char* f1, const char* f2,
  264. int* result);
  265. /**
  266. * Get the file extension (including ".") needed for an executable
  267. * on the current platform ("" for unix, ".exe" for Windows).
  268. */
  269. static const char* GetExecutableExtension();
  270. /**
  271. * Given a path that exists on a windows machine, return the
  272. * actuall case of the path as it was created. If the file
  273. * does not exist path is returned unchanged. This does nothing
  274. * on unix but return path.
  275. */
  276. static kwsys_stl::string GetActualCaseForPath(const char* path);
  277. /**
  278. * Given the path to a program executable, get the directory part of
  279. * the path with the file stripped off. If there is no directory
  280. * part, the empty string is returned.
  281. */
  282. static kwsys_stl::string GetProgramPath(const char*);
  283. static bool SplitProgramPath(const char* in_name,
  284. kwsys_stl::string& dir,
  285. kwsys_stl::string& file,
  286. bool errorReport = true);
  287. /**
  288. * Given argv[0] for a unix program find the full path to a running
  289. * executable. argv0 can be null for windows WinMain programs
  290. * in this case GetModuleFileName will be used to find the path
  291. * to the running executable. If argv0 is not a full path,
  292. * then this will try to find the full path. If the path is not
  293. * found false is returned, if found true is returned. An error
  294. * message of the attempted paths is stored in errorMsg.
  295. * exeName is the name of the executable.
  296. * buildDir is a possibly null path to the build directory.
  297. * installPrefix is a possibly null pointer to the install directory.
  298. */
  299. static bool FindProgramPath(const char* argv0,
  300. kwsys_stl::string& pathOut,
  301. kwsys_stl::string& errorMsg,
  302. const char* exeName = 0,
  303. const char* buildDir = 0,
  304. const char* installPrefix = 0);
  305. /**
  306. * Given a path to a file or directory, convert it to a full path.
  307. * This collapses away relative paths relative to the cwd argument
  308. * (which defaults to the current working directory). The full path
  309. * is returned.
  310. */
  311. static kwsys_stl::string CollapseFullPath(const char* in_relative);
  312. static kwsys_stl::string CollapseFullPath(const char* in_relative,
  313. const char* in_base);
  314. /**
  315. * Get the real path for a given path, removing all symlinks. In
  316. * the event of an error (non-existent path, permissions issue,
  317. * etc.) the original path is returned.
  318. */
  319. static kwsys_stl::string GetRealPath(const char* path);
  320. /**
  321. * Split a path name into its root component and the rest of the
  322. * path. The root component is one of the following:
  323. * "/" = UNIX full path
  324. * "c:/" = Windows full path (can be any drive letter)
  325. * "c:" = Windows drive-letter relative path (can be any drive letter)
  326. * "//" = Network path
  327. * "~/" = Home path for current user
  328. * "~u/" = Home path for user 'u'
  329. * "" = Relative path
  330. *
  331. * A pointer to the rest of the path after the root component is
  332. * returned. The root component is stored in the "root" string if
  333. * given.
  334. */
  335. static const char* SplitPathRootComponent(const char* p,
  336. kwsys_stl::string* root=0);
  337. /**
  338. * Split a path name into its basic components. The first component
  339. * always exists and is the root returned by SplitPathRootComponent.
  340. * The remaining components form the path. If there is a trailing
  341. * slash then the last component is the empty string. The
  342. * components can be recombined as "c[0]c[1]/c[2]/.../c[n]" to
  343. * produce the original path. Home directory references are
  344. * automatically expanded if expand_home_dir is true and this
  345. * platform supports them.
  346. */
  347. static void SplitPath(const char* p,
  348. kwsys_stl::vector<kwsys_stl::string>& components,
  349. bool expand_home_dir = true);
  350. /**
  351. * Join components of a path name into a single string. See
  352. * SplitPath for the format of the components.
  353. */
  354. static kwsys_stl::string JoinPath(
  355. const kwsys_stl::vector<kwsys_stl::string>& components);
  356. static kwsys_stl::string JoinPath(
  357. kwsys_stl::vector<kwsys_stl::string>::const_iterator first,
  358. kwsys_stl::vector<kwsys_stl::string>::const_iterator last);
  359. /**
  360. * Compare a path or components of a path.
  361. */
  362. static bool ComparePath(const char* c1, const char* c2);
  363. /**
  364. * Return path of a full filename (no trailing slashes)
  365. */
  366. static kwsys_stl::string GetFilenamePath(const kwsys_stl::string&);
  367. /**
  368. * Return file name of a full filename (i.e. file name without path)
  369. */
  370. static kwsys_stl::string GetFilenameName(const kwsys_stl::string&);
  371. /**
  372. * Split a program from its arguments and handle spaces in the paths
  373. */
  374. static void SplitProgramFromArgs(
  375. const char* path,
  376. kwsys_stl::string& program, kwsys_stl::string& args);
  377. /**
  378. * Return longest file extension of a full filename (dot included)
  379. */
  380. static kwsys_stl::string GetFilenameExtension(const kwsys_stl::string&);
  381. /**
  382. * Return shortest file extension of a full filename (dot included)
  383. */
  384. static kwsys_stl::string GetFilenameLastExtension(
  385. const kwsys_stl::string& filename);
  386. /**
  387. * Return file name without extension of a full filename
  388. */
  389. static kwsys_stl::string GetFilenameWithoutExtension(
  390. const kwsys_stl::string&);
  391. /**
  392. * Return file name without its last (shortest) extension
  393. */
  394. static kwsys_stl::string GetFilenameWithoutLastExtension(
  395. const kwsys_stl::string&);
  396. /**
  397. * Return whether the path represents a full path (not relative)
  398. */
  399. static bool FileIsFullPath(const char*);
  400. /**
  401. * For windows return the short path for the given path,
  402. * Unix just a pass through
  403. */
  404. static bool GetShortPath(const char* path, kwsys_stl::string& result);
  405. /**
  406. * Read line from file. Make sure to get everything. Due to a buggy stream
  407. * library on the HP and another on Mac OS X, we need this very carefully
  408. * written version of getline. Returns true if any data were read before the
  409. * end-of-file was reached. If the has_newline argument is specified, it will
  410. * be true when the line read had a newline character.
  411. */
  412. static bool GetLineFromStream(kwsys_ios::istream& istr,
  413. kwsys_stl::string& line,
  414. bool* has_newline=0,
  415. long sizeLimit=-1);
  416. /**
  417. * Get the parent directory of the directory or file
  418. */
  419. static kwsys_stl::string GetParentDirectory(const char* fileOrDir);
  420. /**
  421. * Check if the given file or directory is in subdirectory of dir
  422. */
  423. static bool IsSubDirectory(const char* fileOrDir, const char* dir);
  424. /** -----------------------------------------------------------------
  425. * File Manipulation Routines
  426. * -----------------------------------------------------------------
  427. */
  428. /**
  429. * Make a new directory if it is not there. This function
  430. * can make a full path even if none of the directories existed
  431. * prior to calling this function.
  432. */
  433. static bool MakeDirectory(const char* path);
  434. /**
  435. * Copy the source file to the destination file only
  436. * if the two files differ.
  437. */
  438. static bool CopyFileIfDifferent(const char* source,
  439. const char* destination);
  440. /**
  441. * Compare the contents of two files. Return true if different
  442. */
  443. static bool FilesDiffer(const char* source, const char* destination);
  444. /**
  445. * Return true if the two files are the same file
  446. */
  447. static bool SameFile(const char* file1, const char* file2);
  448. /**
  449. * Copy a file.
  450. */
  451. static bool CopyFileAlways(const char* source, const char* destination);
  452. /**
  453. * Copy a file. If the "always" argument is true the file is always
  454. * copied. If it is false, the file is copied only if it is new or
  455. * has changed.
  456. */
  457. static bool CopyAFile(const char* source, const char* destination,
  458. bool always = true);
  459. /**
  460. * Copy content directory to another directory with all files and
  461. * subdirectories. If the "always" argument is true all files are
  462. * always copied. If it is false, only files that have changed or
  463. * are new are copied.
  464. */
  465. static bool CopyADirectory(const char* source, const char* destination,
  466. bool always = true);
  467. /**
  468. * Remove a file
  469. */
  470. static bool RemoveFile(const char* source);
  471. /**
  472. * Remove a directory
  473. */
  474. static bool RemoveADirectory(const char* source);
  475. /**
  476. * Get the maximum full file path length
  477. */
  478. static size_t GetMaximumFilePathLength();
  479. /**
  480. * Find a file in the system PATH, with optional extra paths
  481. */
  482. static kwsys_stl::string FindFile(
  483. const char* name,
  484. const kwsys_stl::vector<kwsys_stl::string>& path =
  485. kwsys_stl::vector<kwsys_stl::string>(),
  486. bool no_system_path = false);
  487. /**
  488. * Find a directory in the system PATH, with optional extra paths
  489. */
  490. static kwsys_stl::string FindDirectory(
  491. const char* name,
  492. const kwsys_stl::vector<kwsys_stl::string>& path =
  493. kwsys_stl::vector<kwsys_stl::string>(),
  494. bool no_system_path = false);
  495. /**
  496. * Find an executable in the system PATH, with optional extra paths
  497. */
  498. static kwsys_stl::string FindProgram(
  499. const char* name,
  500. const kwsys_stl::vector<kwsys_stl::string>& path =
  501. kwsys_stl::vector<kwsys_stl::string>(),
  502. bool no_system_path = false);
  503. static kwsys_stl::string FindProgram(
  504. const kwsys_stl::vector<kwsys_stl::string>& names,
  505. const kwsys_stl::vector<kwsys_stl::string>& path =
  506. kwsys_stl::vector<kwsys_stl::string>(),
  507. bool no_system_path = false);
  508. /**
  509. * Find a library in the system PATH, with optional extra paths
  510. */
  511. static kwsys_stl::string FindLibrary(
  512. const char* name,
  513. const kwsys_stl::vector<kwsys_stl::string>& path);
  514. /**
  515. * Return true if the file is a directory
  516. */
  517. static bool FileIsDirectory(const char* name);
  518. /**
  519. * Return true if the file is a symlink
  520. */
  521. static bool FileIsSymlink(const char* name);
  522. /**
  523. * Return true if the file has a given signature (first set of bytes)
  524. */
  525. static bool FileHasSignature(
  526. const char* filename, const char *signature, long offset = 0);
  527. /**
  528. * Attempt to detect and return the type of a file.
  529. * Up to 'length' bytes are read from the file, if more than 'percent_bin' %
  530. * of the bytes are non-textual elements, the file is considered binary,
  531. * otherwise textual. Textual elements are bytes in the ASCII [0x20, 0x7E]
  532. * range, but also \\n, \\r, \\t.
  533. * The algorithm is simplistic, and should probably check for usual file
  534. * extensions, 'magic' signature, unicode, etc.
  535. */
  536. enum FileTypeEnum
  537. {
  538. FileTypeUnknown,
  539. FileTypeBinary,
  540. FileTypeText
  541. };
  542. static SystemTools::FileTypeEnum DetectFileType(
  543. const char* filename,
  544. unsigned long length = 256,
  545. double percent_bin = 0.05);
  546. /**
  547. * Create a symbolic link if the platform supports it. Returns whether
  548. * creation succeded.
  549. */
  550. static bool CreateSymlink(const char* origName, const char* newName);
  551. /**
  552. * Read the contents of a symbolic link. Returns whether reading
  553. * succeded.
  554. */
  555. static bool ReadSymlink(const char* newName, kwsys_stl::string& origName);
  556. /**
  557. * Try to locate the file 'filename' in the directory 'dir'.
  558. * If 'filename' is a fully qualified filename, the basename of the file is
  559. * used to check for its existence in 'dir'.
  560. * If 'dir' is not a directory, GetFilenamePath() is called on 'dir' to
  561. * get its directory first (thus, you can pass a filename as 'dir', as
  562. * a convenience).
  563. * 'filename_found' is assigned the fully qualified name/path of the file
  564. * if it is found (not touched otherwise).
  565. * If 'try_filename_dirs' is true, try to find the file using the
  566. * components of its path, i.e. if we are looking for c:/foo/bar/bill.txt,
  567. * first look for bill.txt in 'dir', then in 'dir'/bar, then in 'dir'/foo/bar
  568. * etc.
  569. * Return true if the file was found, false otherwise.
  570. */
  571. static bool LocateFileInDir(const char *filename,
  572. const char *dir,
  573. kwsys_stl::string& filename_found,
  574. int try_filename_dirs = 0);
  575. /** compute the relative path from local to remote. local must
  576. be a directory. remote can be a file or a directory.
  577. Both remote and local must be full paths. Basically, if
  578. you are in directory local and you want to access the file in remote
  579. what is the relative path to do that. For example:
  580. /a/b/c/d to /a/b/c1/d1 -> ../../c1/d1
  581. from /usr/src to /usr/src/test/blah/foo.cpp -> test/blah/foo.cpp
  582. */
  583. static kwsys_stl::string RelativePath(const char* local, const char* remote);
  584. /**
  585. * Return file's modified time
  586. */
  587. static long int ModifiedTime(const char* filename);
  588. /**
  589. * Return file's creation time (Win32: works only for NTFS, not FAT)
  590. */
  591. static long int CreationTime(const char* filename);
  592. /**
  593. * Get and set permissions of the file.
  594. */
  595. static bool GetPermissions(const char* file, mode_t& mode);
  596. static bool SetPermissions(const char* file, mode_t mode);
  597. /** -----------------------------------------------------------------
  598. * Time Manipulation Routines
  599. * -----------------------------------------------------------------
  600. */
  601. /** Get current time in seconds since Posix Epoch (Jan 1, 1970). */
  602. static double GetTime();
  603. /**
  604. * Get current date/time
  605. */
  606. static kwsys_stl::string GetCurrentDateTime(const char* format);
  607. /** -----------------------------------------------------------------
  608. * Registry Manipulation Routines
  609. * -----------------------------------------------------------------
  610. */
  611. /**
  612. * Specify access to the 32-bit or 64-bit application view of
  613. * registry values. The default is to match the currently running
  614. * binary type.
  615. */
  616. enum KeyWOW64 { KeyWOW64_Default, KeyWOW64_32, KeyWOW64_64 };
  617. /**
  618. * Read a registry value
  619. */
  620. static bool ReadRegistryValue(const char *key, kwsys_stl::string &value,
  621. KeyWOW64 view = KeyWOW64_Default);
  622. /**
  623. * Write a registry value
  624. */
  625. static bool WriteRegistryValue(const char *key, const char *value,
  626. KeyWOW64 view = KeyWOW64_Default);
  627. /**
  628. * Delete a registry value
  629. */
  630. static bool DeleteRegistryValue(const char *key,
  631. KeyWOW64 view = KeyWOW64_Default);
  632. /** -----------------------------------------------------------------
  633. * Environment Manipulation Routines
  634. * -----------------------------------------------------------------
  635. */
  636. /**
  637. * Add the paths from the environment variable PATH to the
  638. * string vector passed in. If env is set then the value
  639. * of env will be used instead of PATH.
  640. */
  641. static void GetPath(kwsys_stl::vector<kwsys_stl::string>& path,
  642. const char* env=0);
  643. /**
  644. * Read an environment variable
  645. */
  646. static const char* GetEnv(const char* key);
  647. static bool GetEnv(const char* key, kwsys_stl::string& result);
  648. /** Put a string into the environment
  649. of the form var=value */
  650. static bool PutEnv(const char* env);
  651. /** Remove a string from the environment.
  652. Input is of the form "var" or "var=value" (value is ignored). */
  653. static bool UnPutEnv(const char* env);
  654. /**
  655. * Get current working directory CWD
  656. */
  657. static kwsys_stl::string GetCurrentWorkingDirectory(bool collapse =true);
  658. /**
  659. * Change directory to the directory specified
  660. */
  661. static int ChangeDirectory(const char* dir);
  662. /**
  663. * Get the result of strerror(errno)
  664. */
  665. static kwsys_stl::string GetLastSystemError();
  666. /**
  667. * When building DEBUG with MSVC, this enables a hook that prevents
  668. * error dialogs from popping up if the program is being run from
  669. * DART.
  670. */
  671. static void EnableMSVCDebugHook();
  672. /**
  673. * Get the width of the terminal window. The code may or may not work, so
  674. * make sure you have some resonable defaults prepared if the code returns
  675. * some bogus size.
  676. */
  677. static int GetTerminalWidth();
  678. /**
  679. * Add an entry in the path translation table.
  680. */
  681. static void AddTranslationPath(const char * dir, const char * refdir);
  682. /**
  683. * If dir is different after CollapseFullPath is called,
  684. * Then insert it into the path translation table
  685. */
  686. static void AddKeepPath(const char* dir);
  687. /**
  688. * Update path by going through the Path Translation table;
  689. */
  690. static void CheckTranslationPath(kwsys_stl::string & path);
  691. /**
  692. * Delay the execution for a specified amount of time specified
  693. * in miliseconds
  694. */
  695. static void Delay(unsigned int msec);
  696. /**
  697. * Get the operating system name and version
  698. * This is implemented for Win32 only for the moment
  699. */
  700. static kwsys_stl::string GetOperatingSystemNameAndVersion();
  701. /**
  702. * Convert windows-style arguments given as a command-line string
  703. * into more traditional argc/argv arguments.
  704. * Note that argv[0] will be assigned the executable name using
  705. * the GetModuleFileName() function.
  706. */
  707. static void ConvertWindowsCommandLineToUnixArguments(
  708. const char *cmd_line, int *argc, char ***argv);
  709. /** -----------------------------------------------------------------
  710. * URL Manipulation Routines
  711. * -----------------------------------------------------------------
  712. */
  713. /**
  714. * Parse a character string :
  715. * protocol://dataglom
  716. * and fill protocol as appropriate.
  717. * Return false if the URL does not have the required form, true otherwise.
  718. */
  719. static bool ParseURLProtocol( const kwsys_stl::string& URL,
  720. kwsys_stl::string& protocol,
  721. kwsys_stl::string& dataglom );
  722. /**
  723. * Parse a string (a URL without protocol prefix) with the form:
  724. * protocol://[[username[':'password]'@']hostname[':'dataport]]'/'[datapath]
  725. * and fill protocol, username, password, hostname, dataport, and datapath
  726. * when values are found.
  727. * Return true if the string matches the format; false otherwise.
  728. */
  729. static bool ParseURL( const kwsys_stl::string& URL,
  730. kwsys_stl::string& protocol,
  731. kwsys_stl::string& username,
  732. kwsys_stl::string& password,
  733. kwsys_stl::string& hostname,
  734. kwsys_stl::string& dataport,
  735. kwsys_stl::string& datapath );
  736. private:
  737. /**
  738. * Allocate the stl map that serve as the Path Translation table.
  739. */
  740. static void ClassInitialize();
  741. /**
  742. * Deallocate the stl map that serve as the Path Translation table.
  743. */
  744. static void ClassFinalize();
  745. /**
  746. * This method prevents warning on SGI
  747. */
  748. SystemToolsManager* GetSystemToolsManager()
  749. {
  750. return &SystemToolsManagerInstance;
  751. }
  752. /**
  753. * Find a filename (file or directory) in the system PATH, with
  754. * optional extra paths.
  755. */
  756. static kwsys_stl::string FindName(
  757. const char* name,
  758. const kwsys_stl::vector<kwsys_stl::string>& path =
  759. kwsys_stl::vector<kwsys_stl::string>(),
  760. bool no_system_path = false);
  761. /**
  762. * Path translation table from dir to refdir
  763. * Each time 'dir' will be found it will be replace by 'refdir'
  764. */
  765. static SystemToolsTranslationMap *TranslationMap;
  766. static SystemToolsTranslationMap *LongPathMap;
  767. #ifdef __CYGWIN__
  768. static SystemToolsTranslationMap *Cyg2Win32Map;
  769. #endif
  770. friend class SystemToolsManager;
  771. };
  772. } // namespace @KWSYS_NAMESPACE@
  773. /* Undefine temporary macros. */
  774. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  775. # undef kwsys_stl
  776. # undef kwsys_ios
  777. #endif
  778. #endif