SystemTools.hxx.in 14 KB

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