SystemTools.hxx.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 <sys/types.h>
  18. #if defined( _MSC_VER )
  19. typedef unsigned short mode_t;
  20. #endif
  21. /* Define these macros temporarily to keep the code readable. */
  22. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  23. # define kwsys_stl @KWSYS_NAMESPACE@_stl
  24. # define kwsys_ios @KWSYS_NAMESPACE@_ios
  25. #endif
  26. namespace @KWSYS_NAMESPACE@
  27. {
  28. /** \class SystemTools
  29. * \brief A collection of useful platform-independent system functions.
  30. */
  31. class @KWSYS_NAMESPACE@_EXPORT SystemTools
  32. {
  33. public:
  34. /**
  35. * Replace symbols in str that are not valid in C identifiers as
  36. * defined by the 1999 standard, ie. anything except [A-Za-z0-9_].
  37. * They are replaced with `_' and if the first character is a digit
  38. * then an underscore is prepended. Note that this can produce
  39. * identifiers that the standard reserves (_[A-Z].* and __.*).
  40. */
  41. static kwsys_stl::string MakeCindentifier(const char* s);
  42. /**
  43. * Make a new directory if it is not there. This function
  44. * can make a full path even if none of the directories existed
  45. * prior to calling this function.
  46. */
  47. static bool MakeDirectory(const char* path);
  48. /**
  49. * Get current time as a double. On certain platforms this will
  50. * return higher resolution than seconds:
  51. * (1) gettimeofday() -- resolution in microseconds
  52. * (2) ftime() -- resolution in milliseconds
  53. * (3) time() -- resolution in seconds
  54. */
  55. static double GetTime();
  56. /**
  57. * Replace replace all occurances of the string in
  58. * the source string.
  59. */
  60. static void ReplaceString(kwsys_stl::string& source,
  61. const char* replace,
  62. const char* with);
  63. /**
  64. * Read a registry value
  65. */
  66. static bool ReadRegistryValue(const char *key, kwsys_stl::string &value);
  67. /**
  68. * Write a registry value
  69. */
  70. static bool WriteRegistryValue(const char *key, const char *value);
  71. /**
  72. * Delete a registry value
  73. */
  74. static bool DeleteRegistryValue(const char *key);
  75. /**
  76. * Return a capitalized string (i.e the first letter is uppercased,
  77. * all other are lowercased).
  78. */
  79. static kwsys_stl::string Capitalized(const kwsys_stl::string&);
  80. /**
  81. * Return a lower case string
  82. */
  83. static kwsys_stl::string LowerCase(const kwsys_stl::string&);
  84. /**
  85. * Return a lower case string
  86. */
  87. static kwsys_stl::string UpperCase(const kwsys_stl::string&);
  88. /**
  89. * do a case-independent string comparison
  90. */
  91. static int Strucmp(const char *s1, const char *s2);
  92. /**
  93. * Replace Windows file system slashes with Unix-style slashes.
  94. */
  95. static void ConvertToUnixSlashes(kwsys_stl::string& path);
  96. /**
  97. * For windows this calles ConvertToWindowsOutputPath and for unix
  98. * it calls ConvertToUnixOutputPath
  99. */
  100. static kwsys_stl::string ConvertToOutputPath(const char*);
  101. /** Return true if a file exists in the current directory. */
  102. static bool FileExists(const char* filename);
  103. static unsigned long FileLength(const char *filename);
  104. /**
  105. * Add the paths from the environment variable PATH to the
  106. * string vector passed in. If env is set then the value
  107. * of env will be used instead of PATH.
  108. */
  109. static void GetPath(kwsys_stl::vector<kwsys_stl::string>& path, const char* env=0);
  110. /** Read an environment variable. */
  111. static const char* GetEnv(const char* key);
  112. static bool GetEnv(const char* key, kwsys_stl::string& result);
  113. /**
  114. * Get the file extension (including ".") needed for an executable
  115. * on the current platform ("" for unix, ".exe" for Windows).
  116. */
  117. static const char* GetExecutableExtension();
  118. /**
  119. * Copy the source file to the destination file only
  120. * if the two files differ.
  121. */
  122. static bool CopyFileIfDifferent(const char* source,
  123. const char* destination);
  124. ///! Compare the contents of two files. Return true if different.
  125. static bool FilesDiffer(const char* source,
  126. const char* destination);
  127. ///! return true if the two files are the same file
  128. static bool SameFile(const char* file1, const char* file2);
  129. ///! Copy a file.
  130. static bool CopyFileAlways(const char* source, const char* destination);
  131. ///! Remove a file.
  132. static bool RemoveFile(const char* source);
  133. ///! Remove a directory
  134. static bool RemoveADirectory(const char* source);
  135. ///! Find a file in the system PATH, with optional extra paths.
  136. static kwsys_stl::string FindFile(const char* name,
  137. const kwsys_stl::vector<kwsys_stl::string>& path= kwsys_stl::vector<kwsys_stl::string>());
  138. ///! Find an executable in the system PATH, with optional extra paths.
  139. static kwsys_stl::string FindProgram(const char* name,
  140. const kwsys_stl::vector<kwsys_stl::string>& path = kwsys_stl::vector<kwsys_stl::string>(),
  141. bool no_system_path = false);
  142. ///! Find a library in the system PATH, with optional extra paths.
  143. static kwsys_stl::string FindLibrary(const char* name,
  144. const kwsys_stl::vector<kwsys_stl::string>& path);
  145. ///! return true if the file is a directory.
  146. static bool FileIsDirectory(const char* name);
  147. static kwsys_stl::string GetCurrentWorkingDirectory();
  148. /**
  149. * Given the path to a program executable, get the directory part of
  150. * the path with the file stripped off. If there is no directory
  151. * part, the empty string is returned.
  152. */
  153. static kwsys_stl::string GetProgramPath(const char*);
  154. static bool SplitProgramPath(const char* in_name,
  155. kwsys_stl::string& dir,
  156. kwsys_stl::string& file,
  157. bool errorReport = true);
  158. /**
  159. * Given argv[0] for a unix program find the full path to a running
  160. * executable. argv0 can be null for windows WinMain programs
  161. * in this case GetModuleFileName will be used to find the path
  162. * to the running executable. If argv0 is not a full path,
  163. * then this will try to find the full path. If the path is not
  164. * found false is returned, if found true is returned. An error
  165. * message of the attempted paths is stored in errorMsg.
  166. * exeName is the name of the executable.
  167. * buildDir is a possibly null path to the build directory.
  168. * installPrefix is a possibly null pointer to the install directory.
  169. */
  170. static bool FindProgramPath(const char* argv0,
  171. kwsys_stl::string& pathOut,
  172. kwsys_stl::string& errorMsg,
  173. const char* exeName = 0,
  174. const char* buildDir = 0,
  175. const char* installPrefix = 0);
  176. /**
  177. * Given a path to a file or directory, convert it to a full path.
  178. * This collapses away relative paths relative to the cwd argument
  179. * (which defaults to the current working directory). The full path
  180. * is returned.
  181. */
  182. static kwsys_stl::string CollapseFullPath(const char* in_relative);
  183. static kwsys_stl::string CollapseFullPath(const char* in_relative,
  184. const char* in_base);
  185. ///! return path of a full filename (no trailing slashes).
  186. static kwsys_stl::string GetFilenamePath(const kwsys_stl::string&);
  187. ///! return file name of a full filename (i.e. file name without path).
  188. static kwsys_stl::string GetFilenameName(const kwsys_stl::string&);
  189. ///! Split a program from its arguments and handle spaces in the paths.
  190. static void SplitProgramFromArgs(const char* path,
  191. kwsys_stl::string& program, kwsys_stl::string& args);
  192. ///! return longest file extension of a full filename (dot included).
  193. static kwsys_stl::string GetFilenameExtension(const kwsys_stl::string&);
  194. ///! return shortest file extension of a full filename (dot included).
  195. static kwsys_stl::string GetFilenameLastExtension(const kwsys_stl::string& filename);
  196. ///! return file name without extension of a full filename.
  197. static kwsys_stl::string GetFilenameWithoutExtension(const kwsys_stl::string&);
  198. ///! return file name without its last (shortest) extension.
  199. static kwsys_stl::string GetFilenameWithoutLastExtension(const kwsys_stl::string&);
  200. /** Return whether the path represents a full path (not relative). */
  201. static bool FileIsFullPath(const char*);
  202. static long int ModifiedTime(const char* filename);
  203. ///! for windows return the short path for the given path, unix just a pass through
  204. static bool GetShortPath(const char* path, kwsys_stl::string& result);
  205. ///! change directory the the directory specified
  206. static int ChangeDirectory(const char* dir);
  207. /** Split a string on its newlines into multiple lines. Returns
  208. false only if the last line stored had no newline. */
  209. static bool Split(const char* s, kwsys_stl::vector<kwsys_stl::string>& l);
  210. static kwsys_stl::string GetCurrentDateTime(const char* format);
  211. /** Get the result of strerror(errno). */
  212. static kwsys_stl::string GetLastSystemError();
  213. /** When building DEBUG with MSVC, this enables a hook that prevents
  214. * error dialogs from popping up if the program is being run from
  215. * DART.
  216. */
  217. static void EnableMSVCDebugHook();
  218. /**
  219. * Read line from file. Make sure to get everything. Due to a buggy stream
  220. * library on the HP and another on Mac OSX, we need this very carefully
  221. * written version of getline. Returns true if any data were read before the
  222. * end-of-file was reached. If the has_newline argument is specified, it will
  223. * be true when the line read had a newline character.
  224. */
  225. static bool GetLineFromStream(kwsys_ios::istream& istr, kwsys_stl::string& line,
  226. bool* has_newline=0);
  227. /**
  228. * Get the width of the terminal window. The code may or may not work, so
  229. * make sure you have some resonable defaults prepared if the code returns
  230. * some bogus size.
  231. */
  232. static int GetTerminalWidth();
  233. /**
  234. * Add an entry in the path translation table.
  235. */
  236. static void AddTranslationPath(const char * dir, const char * refdir);
  237. /**
  238. * If dir is different after CollapseFullPath is called,
  239. * Then insert it into the path translation table
  240. */
  241. static void AddKeepPath(const char* dir);
  242. /**
  243. * Update path by going through the Path Translation table;
  244. */
  245. static void CheckTranslationPath(kwsys_stl::string & path);
  246. /**
  247. * Get and set permissions of the file.
  248. */
  249. static bool GetPermissions(const char* file, mode_t& mode);
  250. static bool SetPermissions(const char* file, mode_t mode);
  251. protected:
  252. // these two functions can be called from ConvertToOutputPath
  253. /**
  254. * Convert the path to a string that can be used in a unix makefile.
  255. * double slashes are removed, and spaces are escaped.
  256. */
  257. static kwsys_stl::string ConvertToUnixOutputPath(const char*);
  258. /**
  259. * Convert the path to string that can be used in a windows project or
  260. * makefile. Double slashes are removed if they are not at the start of
  261. * the string, the slashes are converted to windows style backslashes, and
  262. * if there are spaces in the string it is double quoted.
  263. */
  264. static kwsys_stl::string ConvertToWindowsOutputPath(const char*);
  265. private:
  266. /**
  267. * Path translation table from dir to refdir
  268. * Each time 'dir' will be found it will be replace by 'refdir'
  269. */
  270. static kwsys_stl::map<kwsys_stl::string,kwsys_stl::string> TranslationMap;
  271. };
  272. } // namespace @KWSYS_NAMESPACE@
  273. /* Undefine temporary macros. */
  274. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  275. # undef kwsys_stl
  276. # undef kwsys_ios
  277. #endif
  278. #endif