SystemTools.hxx.in 10 KB

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