cmSystemTools.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 cmSystemTools_h
  14. #define cmSystemTools_h
  15. #include "cmStandardIncludes.h"
  16. /** \class cmSystemTools
  17. * \brief A collection of useful functions for CMake.
  18. *
  19. * cmSystemTools is a class that provides helper functions
  20. * for the CMake build system.
  21. */
  22. class cmSystemTools
  23. {
  24. public:
  25. /**
  26. * Make a new directory if it is not there. This function
  27. * can make a full path even if none of the directories existed
  28. * prior to calling this function.
  29. */
  30. static bool MakeDirectory(const char* path);
  31. /**
  32. * Replace replace all occurances of the string in
  33. * the source string.
  34. */
  35. static void ReplaceString(std::string& source,
  36. const char* replace,
  37. const char* with);
  38. /**
  39. * Look for and replace registry values in a string
  40. */
  41. static void ExpandRegistryValues(std::string& source);
  42. /**
  43. * make sure on windows that paths with // are converted to \\
  44. */
  45. static std::string HandleNetworkPaths(const char*);
  46. /**
  47. * Return a string equivalent to the input string, but with all " " replaced
  48. * with "\ " to escape the spaces.
  49. */
  50. static std::string EscapeSpaces(const char*);
  51. /**
  52. * Return a string equivalent to the input string, but with all " replaced
  53. * with \" to escape the quote
  54. */
  55. static std::string EscapeQuotes(const char*);
  56. /**
  57. * Return a capitalized string (i.e the first letter is uppercased, all other
  58. * are lowercased).
  59. */
  60. static std::string Capitalized(const std::string&);
  61. /**
  62. * Return a lower case string
  63. */
  64. static std::string LowerCase(const std::string&);
  65. /**
  66. * Return a lower case string
  67. */
  68. static std::string UpperCase(const std::string&);
  69. /**
  70. * Replace Windows file system slashes with Unix-style slashes.
  71. */
  72. static const char *ConvertToUnixSlashes(std::string& path);
  73. /**
  74. * Replace Unix file system slashes with Windows-style slashes
  75. */
  76. static const char *ConvertToWindowsSlashes(std::string& path);
  77. /**
  78. * Replace Unix file system slashes with Windows-style slashes and
  79. * remove any duplicate \\ slashes to clean the path.
  80. */
  81. static const char *ConvertToWindowsSlashesAndCleanUp(std::string& path);
  82. ///! Return true if a file exists in the current directory.
  83. static bool FileExists(const char* filename);
  84. /**
  85. * Read a CMake command (or function) from an input file. This
  86. * returns the name of the function and a list of its
  87. * arguments. The last argument is the name of the file that
  88. * the ifstream points to, and is used for debug info only.
  89. */
  90. static bool ParseFunction(std::ifstream&,
  91. std::string& name,
  92. std::vector<std::string>& arguments,
  93. const char* filename, bool& parseError);
  94. /**
  95. * Extract white-space separated arguments from a string.
  96. * Double quoted strings are accepted with spaces.
  97. * This is called by ParseFunction.
  98. */
  99. static void GetArguments(std::string& line,
  100. std::vector<std::string>& arguments);
  101. /**
  102. * Given a string, replace any escape sequences with the corresponding
  103. * characters.
  104. */
  105. static std::string RemoveEscapes(const char*);
  106. /**
  107. * Add the paths from the environment variable PATH to the
  108. * string vector passed in.
  109. */
  110. static void GetPath(std::vector<std::string>& path);
  111. /**
  112. * Get the file extension (including ".") needed for an executable
  113. * on the current platform ("" for unix, ".exe" for Windows).
  114. */
  115. static const char* GetExecutableExtension();
  116. typedef void (*ErrorCallback)(const char*, const char*, bool&);
  117. /**
  118. * Set the function used by GUI's to display error messages
  119. * Function gets passed: message as a const char*,
  120. * title as a const char*, and a reference to bool that when
  121. * set to false, will disable furthur messages (cancel).
  122. */
  123. static void SetErrorCallback(ErrorCallback f);
  124. /**
  125. * Display an error message.
  126. */
  127. static void Error(const char* m, const char* m2=0,
  128. const char* m3=0, const char* m4=0);
  129. /**
  130. * Display a message.
  131. */
  132. static void Message(const char* m, const char* title=0);
  133. ///! Return true if there was an error at any point.
  134. static bool GetErrorOccuredFlag()
  135. {
  136. return cmSystemTools::s_ErrorOccured;
  137. }
  138. ///! Set the error occured flag back to false
  139. static void ResetErrorOccuredFlag()
  140. {
  141. cmSystemTools::s_ErrorOccured = false;
  142. }
  143. /**
  144. * Copy the source file to the destination file only
  145. * if the two files differ.
  146. */
  147. static bool CopyFileIfDifferent(const char* source,
  148. const char* destination);
  149. ///! Compare the contents of two files. Return true if different.
  150. static bool FilesDiffer(const char* source,
  151. const char* destination);
  152. ///! return true if the two files are the same file
  153. static bool SameFile(const char* file1, const char* file2);
  154. ///! Copy a file.
  155. static void cmCopyFile(const char* source,
  156. const char* destination);
  157. ///! Remove a file.
  158. static bool RemoveFile(const char* source);
  159. /**
  160. * does a string indicate a true or on value ? This is not the same
  161. * as ifdef.
  162. */
  163. static bool IsOn(const char* val);
  164. /**
  165. * does a string indicate a false or off value ? Note that this is
  166. * not the same as !IsOn(...) because there are a number of
  167. * ambiguous values such as "/usr/local/bin" a path will result in
  168. * IsON and IsOff both returning false. Note that the special path
  169. * NOTFOUND or IGNORE will cause IsOff to return true.
  170. */
  171. static bool IsOff(const char* val);
  172. ///! Find a file in the system PATH, with optional extra paths.
  173. static std::string FindFile(const char* name,
  174. const std::vector<std::string>& path= std::vector<std::string>());
  175. ///! Find an executable in the system PATH, with optional extra paths.
  176. static std::string FindProgram(const char* name,
  177. const std::vector<std::string>& path= std::vector<std::string>());
  178. ///! Find a library in the system PATH, with optional extra paths.
  179. static std::string FindLibrary(const char* name,
  180. const std::vector<std::string>& path);
  181. ///! return true if the file is a directory.
  182. static bool FileIsDirectory(const char* name);
  183. static void Glob(const char *directory, const char *regexp,
  184. std::vector<std::string>& files);
  185. static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
  186. static std::string GetCurrentWorkingDirectory();
  187. static std::string GetProgramPath(const char*);
  188. static void SplitProgramPath(const char* in_name,
  189. std::string& dir,
  190. std::string& file);
  191. static std::string CollapseFullPath(const char*);
  192. ///! return path of a full filename (no trailing slashes).
  193. static std::string GetFilenamePath(const std::string&);
  194. ///! return file name of a full filename (i.e. file name without path).
  195. static std::string GetFilenameName(const std::string&);
  196. ///! return file extension of a full filename (dot included).
  197. static std::string GetFilenameExtension(const std::string&);
  198. static std::string GetFilenameShortestExtension(const std::string&);
  199. ///! return file name without extension of a full filename.
  200. static std::string GetFilenameNameWithoutExtension(const std::string&);
  201. static long int ModifiedTime(const char* filename);
  202. /**
  203. * Run an executable command and put the stdout in output.
  204. * A temporary file is created in the binaryDir for storing the
  205. * output because windows does not have popen.
  206. *
  207. * If verbose is false, no user-viewable output from the program
  208. * being run will be generated.
  209. */
  210. static bool RunCommand(const char* command, std::string& output,
  211. bool verbose = true);
  212. static bool RunCommand(const char* command, std::string& output,
  213. int &retVal, bool verbose = true);
  214. ///! change directory the the directory specified
  215. static int ChangeDirectory(const char* dir);
  216. static void EnableMessages() { s_DisableMessages = false; }
  217. static void DisableMessages() { s_DisableMessages = true; }
  218. static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }
  219. static void EnableRunCommandOutput() {s_DisableRunCommandOutput = false; }
  220. private:
  221. static bool s_ErrorOccured;
  222. static bool s_DisableMessages;
  223. static bool s_DisableRunCommandOutput;
  224. static ErrorCallback s_ErrorCallback;
  225. };
  226. #endif