SystemTools.hxx.in 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. 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. * do a case-independent string comparison
  86. */
  87. static int Strucmp(const char *s1, const char *s2);
  88. /**
  89. * Replace Windows file system slashes with Unix-style slashes.
  90. */
  91. static void ConvertToUnixSlashes(kwsys_std::string& path);
  92. /**
  93. * Platform independent escape spaces, unix uses backslash,
  94. * windows double quotes the string.
  95. */
  96. static kwsys_std::string EscapeSpaces(const char* str);
  97. /** Escape quotes in a string. */
  98. static kwsys_std::string EscapeQuotes(const char* str);
  99. /**
  100. * For windows this calles ConvertToWindowsOutputPath and for unix
  101. * it calls ConvertToUnixOutputPath
  102. */
  103. static kwsys_std::string ConvertToOutputPath(const char*);
  104. /** Return true if a file exists in the current directory. */
  105. static bool FileExists(const char* filename);
  106. static unsigned long FileLength(const char *filename);
  107. /**
  108. * Add the paths from the environment variable PATH to the
  109. * string vector passed in.
  110. */
  111. static void GetPath(kwsys_std::vector<kwsys_std::string>& path);
  112. /**
  113. * Get the file extension (including ".") needed for an executable
  114. * on the current platform ("" for unix, ".exe" for Windows).
  115. */
  116. static const char* GetExecutableExtension();
  117. /**
  118. * Copy the source file to the destination file only
  119. * if the two files differ.
  120. */
  121. static bool CopyFileIfDifferent(const char* source,
  122. const char* destination);
  123. ///! Compare the contents of two files. Return true if different.
  124. static bool FilesDiffer(const char* source,
  125. const char* destination);
  126. ///! return true if the two files are the same file
  127. static bool SameFile(const char* file1, const char* file2);
  128. ///! Copy a file.
  129. static bool CopyFileAlways(const char* source, const char* destination);
  130. ///! Remove a file.
  131. static bool RemoveFile(const char* source);
  132. ///! Find a file in the system PATH, with optional extra paths.
  133. static kwsys_std::string FindFile(const char* name,
  134. const kwsys_std::vector<kwsys_std::string>& path= kwsys_std::vector<kwsys_std::string>());
  135. ///! Find an executable in the system PATH, with optional extra paths.
  136. static kwsys_std::string FindProgram(const char* name,
  137. const kwsys_std::vector<kwsys_std::string>& path = kwsys_std::vector<kwsys_std::string>(),
  138. bool no_system_path = false);
  139. ///! Find a library in the system PATH, with optional extra paths.
  140. static kwsys_std::string FindLibrary(const char* name,
  141. const kwsys_std::vector<kwsys_std::string>& path);
  142. ///! return true if the file is a directory.
  143. static bool FileIsDirectory(const char* name);
  144. static void Glob(const char *directory, const char *regexp,
  145. kwsys_std::vector<kwsys_std::string>& files);
  146. static void GlobDirs(const char *fullPath, kwsys_std::vector<kwsys_std::string>& files);
  147. /**
  148. * Try to find a list of files that match the "simple" globbing
  149. * expression. At this point in time the globbing expressions have
  150. * to be in form: /directory/partial_file_name*. The * character has
  151. * to be at the end of the string and it does not support ?
  152. * []... The optional argument type specifies what kind of files you
  153. * want to find. 0 means all files, -1 means directories, 1 means
  154. * files only. This method returns true if search was succesfull.
  155. */
  156. static bool SimpleGlob(const kwsys_std::string& glob, kwsys_std::vector<kwsys_std::string>& files,
  157. int type = 0);
  158. static kwsys_std::string GetCurrentWorkingDirectory();
  159. /**
  160. * Given the path to a program executable, get the directory part of
  161. * the path with the file stripped off. If there is no directory
  162. * part, the empty string is returned.
  163. */
  164. static kwsys_std::string GetProgramPath(const char*);
  165. static bool SplitProgramPath(const char* in_name,
  166. kwsys_std::string& dir,
  167. kwsys_std::string& file,
  168. bool errorReport = true);
  169. /**
  170. * Given a path to a file or directory, convert it to a full path.
  171. * This collapses away relative paths relative to the cwd argument
  172. * (which defaults to the current working directory). The full path
  173. * is returned.
  174. */
  175. static kwsys_std::string CollapseFullPath(const char* in_relative);
  176. static kwsys_std::string CollapseFullPath(const char* in_relative,
  177. const char* in_base);
  178. ///! return path of a full filename (no trailing slashes).
  179. static kwsys_std::string GetFilenamePath(const kwsys_std::string&);
  180. ///! return file name of a full filename (i.e. file name without path).
  181. static kwsys_std::string GetFilenameName(const kwsys_std::string&);
  182. ///! Split a program from its arguments and handle spaces in the paths.
  183. static void SplitProgramFromArgs(const char* path,
  184. kwsys_std::string& program, kwsys_std::string& args);
  185. ///! return file extension of a full filename (dot included).
  186. static kwsys_std::string GetFilenameExtension(const kwsys_std::string&);
  187. ///! return file name without extension of a full filename.
  188. static kwsys_std::string GetFilenameWithoutExtension(const kwsys_std::string&);
  189. ///! return file name without its last (shortest) extension.
  190. static kwsys_std::string GetFilenameWithoutLastExtension(const kwsys_std::string&);
  191. /** Return whether the path represents a full path (not relative). */
  192. static bool FileIsFullPath(const char*);
  193. static long int ModifiedTime(const char* filename);
  194. ///! for windows return the short path for the given path, unix just a pass through
  195. static bool GetShortPath(const char* path, kwsys_std::string& result);
  196. ///! change directory the the directory specified
  197. static int ChangeDirectory(const char* dir);
  198. /** Split a string on its newlines into multiple lines. Returns
  199. false only if the last line stored had no newline. */
  200. static bool Split(const char* s, kwsys_std::vector<kwsys_std::string>& l);
  201. static kwsys_std::string GetCurrentDateTime(const char* format);
  202. /** Get the result of strerror(errno). */
  203. static kwsys_std::string GetLastSystemError();
  204. /** When building DEBUG with MSVC, this enables a hook that prevents
  205. * error dialogs from popping up if the program is being run from
  206. * DART.
  207. */
  208. static void EnableMSVCDebugHook();
  209. /**
  210. * Read line from file. Make sure to get everything. Due to a buggy stream
  211. * library on the HP and another on Mac OSX, we need this very carefully
  212. * written version of getline. Returns true if any data were read before the
  213. * end-of-file was reached.
  214. */
  215. static bool GetLineFromStream(kwsys_std::istream& istr, kwsys_std::string& line);
  216. protected:
  217. // these two functions can be called from ConvertToOutputPath
  218. /**
  219. * Convert the path to a string that can be used in a unix makefile.
  220. * double slashes are removed, and spaces are escaped.
  221. */
  222. static kwsys_std::string ConvertToUnixOutputPath(const char*);
  223. /**
  224. * Convert the path to string that can be used in a windows project or
  225. * makefile. Double slashes are removed if they are not at the start of
  226. * the string, the slashes are converted to windows style backslashes, and
  227. * if there are spaces in the string it is double quoted.
  228. */
  229. static kwsys_std::string ConvertToWindowsOutputPath(const char*);
  230. };
  231. } // namespace @KWSYS_NAMESPACE@
  232. #endif