SystemTools.hxx.in 30 KB

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