SystemTools.hxx.in 9.9 KB

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