SystemTools.hxx.in 33 KB

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