SystemTools.hxx.in 10 KB

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