cmSystemTools.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 cmMakefile;
  17. /** \class cmSystemTools
  18. * \brief A collection of useful functions for CMake.
  19. *
  20. * cmSystemTools is a class that provides helper functions
  21. * for the CMake build system.
  22. */
  23. class cmSystemTools
  24. {
  25. public:
  26. /**
  27. * Make a new directory if it is not there. This function
  28. * can make a full path even if none of the directories existed
  29. * prior to calling this function.
  30. */
  31. static bool MakeDirectory(const char* path);
  32. /**
  33. * Replace replace all occurances of the string in
  34. * the source string.
  35. */
  36. static void ReplaceString(std::string& source,
  37. const char* replace,
  38. const char* with);
  39. /** Expand out any arguements in the vector that have ; separated
  40. * strings into multiple arguements. A new vector is created
  41. * containing the expanded versions of all arguments in argsIn.
  42. */
  43. static void ExpandListArguments(std::vector<std::string> const& argsIn,
  44. std::vector<std::string>& argsOut);
  45. /**
  46. * Read a registry value
  47. */
  48. static bool ReadRegistryValue(const char *key, std::string &value);
  49. /**
  50. * Write a registry value
  51. */
  52. static bool WriteRegistryValue(const char *key, const char *value);
  53. /**
  54. * Delete a registry value
  55. */
  56. static bool DeleteRegistryValue(const char *key);
  57. /**
  58. * Look for and replace registry values in a string
  59. */
  60. static void ExpandRegistryValues(std::string& source);
  61. /**
  62. * Return a capitalized string (i.e the first letter is uppercased, all other
  63. * are lowercased).
  64. */
  65. static std::string Capitalized(const std::string&);
  66. /**
  67. * Return a lower case string
  68. */
  69. static std::string LowerCase(const std::string&);
  70. /**
  71. * Return a lower case string
  72. */
  73. static std::string UpperCase(const std::string&);
  74. /**
  75. * Replace Windows file system slashes with Unix-style slashes.
  76. */
  77. static void ConvertToUnixSlashes(std::string& path);
  78. /**
  79. * Platform independent escape spaces, unix uses backslash,
  80. * windows double quotes the string.
  81. */
  82. static std::string EscapeSpaces(const char* str);
  83. ///! Escape quotes in a string.
  84. static std::string EscapeQuotes(const char* str);
  85. /**
  86. * For windows this calles ConvertToWindowsOutputPath and for unix
  87. * it calls ConvertToUnixOutputPath
  88. */
  89. static std::string ConvertToOutputPath(const char*);
  90. ///! Return true if a file exists in the current directory.
  91. static bool FileExists(const char* filename);
  92. /**
  93. * Read a CMake command (or function) from an input file. This
  94. * returns the name of the function and a list of its
  95. * arguments. The last argument is the name of the file that
  96. * the ifstream points to, and is used for debug info only.
  97. */
  98. static bool ParseFunction(std::ifstream&,
  99. std::string& name,
  100. std::vector<std::string>& arguments,
  101. const char* filename, bool& parseError);
  102. /**
  103. * Extract white-space separated arguments from a string.
  104. * Double quoted strings are accepted with spaces.
  105. * This is called by ParseFunction.
  106. */
  107. static void GetArguments(std::string& line,
  108. std::vector<std::string>& arguments);
  109. /**
  110. * Given a string, replace any escape sequences with the corresponding
  111. * characters.
  112. */
  113. static std::string RemoveEscapes(const char*);
  114. /**
  115. * Add the paths from the environment variable PATH to the
  116. * string vector passed in.
  117. */
  118. static void GetPath(std::vector<std::string>& path);
  119. /**
  120. * Get the file extension (including ".") needed for an executable
  121. * on the current platform ("" for unix, ".exe" for Windows).
  122. */
  123. static const char* GetExecutableExtension();
  124. typedef void (*ErrorCallback)(const char*, const char*, bool&);
  125. /**
  126. * Set the function used by GUI's to display error messages
  127. * Function gets passed: message as a const char*,
  128. * title as a const char*, and a reference to bool that when
  129. * set to false, will disable furthur messages (cancel).
  130. */
  131. static void SetErrorCallback(ErrorCallback f);
  132. /**
  133. * Display an error message.
  134. */
  135. static void Error(const char* m, const char* m2=0,
  136. const char* m3=0, const char* m4=0);
  137. /**
  138. * Display a message.
  139. */
  140. static void Message(const char* m, const char* title=0);
  141. ///! Return true if there was an error at any point.
  142. static bool GetErrorOccuredFlag()
  143. {
  144. return cmSystemTools::s_ErrorOccured;
  145. }
  146. ///! Set the error occured flag back to false
  147. static void ResetErrorOccuredFlag()
  148. {
  149. cmSystemTools::s_ErrorOccured = false;
  150. }
  151. /**
  152. * Copy the source file to the destination file only
  153. * if the two files differ.
  154. */
  155. static bool CopyFileIfDifferent(const char* source,
  156. const char* destination);
  157. ///! Compare the contents of two files. Return true if different.
  158. static bool FilesDiffer(const char* source,
  159. const char* destination);
  160. ///! return true if the two files are the same file
  161. static bool SameFile(const char* file1, const char* file2);
  162. ///! Copy a file.
  163. static void cmCopyFile(const char* source,
  164. const char* destination);
  165. ///! Remove a file.
  166. static bool RemoveFile(const char* source);
  167. /**
  168. * does a string indicate a true or on value ? This is not the same
  169. * as ifdef.
  170. */
  171. static bool IsOn(const char* val);
  172. /**
  173. * does a string indicate a false or off value ? Note that this is
  174. * not the same as !IsOn(...) because there are a number of
  175. * ambiguous values such as "/usr/local/bin" a path will result in
  176. * IsON and IsOff both returning false. Note that the special path
  177. * NOTFOUND or IGNORE will cause IsOff to return true.
  178. */
  179. static bool IsOff(const char* val);
  180. ///! Find a file in the system PATH, with optional extra paths.
  181. static std::string FindFile(const char* name,
  182. const std::vector<std::string>& path= std::vector<std::string>());
  183. ///! Find an executable in the system PATH, with optional extra paths.
  184. static std::string FindProgram(const char* name,
  185. const std::vector<std::string>& path = std::vector<std::string>(),
  186. bool no_system_path = false);
  187. ///! Find a library in the system PATH, with optional extra paths.
  188. static std::string FindLibrary(const char* name,
  189. const std::vector<std::string>& path,
  190. const cmMakefile *makefile = 0);
  191. ///! return true if the file is a directory.
  192. static bool FileIsDirectory(const char* name);
  193. static void Glob(const char *directory, const char *regexp,
  194. std::vector<std::string>& files);
  195. static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
  196. static std::string GetCurrentWorkingDirectory();
  197. static std::string GetProgramPath(const char*);
  198. static void SplitProgramPath(const char* in_name,
  199. std::string& dir,
  200. std::string& file);
  201. static std::string CollapseFullPath(const char*);
  202. ///! return path of a full filename (no trailing slashes).
  203. static std::string GetFilenamePath(const std::string&);
  204. ///! return file name of a full filename (i.e. file name without path).
  205. static std::string GetFilenameName(const std::string&);
  206. ///! return file extension of a full filename (dot included).
  207. static std::string GetFilenameExtension(const std::string&);
  208. ///! return file name without extension of a full filename.
  209. static std::string GetFilenameWithoutExtension(const std::string&);
  210. ///! return file name without its last (shortest) extension.
  211. static std::string GetFilenameWithoutLastExtension(const std::string&);
  212. static long int ModifiedTime(const char* filename);
  213. /**
  214. * Run an executable command and put the stdout in output.
  215. * A temporary file is created in the binaryDir for storing the
  216. * output because windows does not have popen.
  217. *
  218. * If verbose is false, no user-viewable output from the program
  219. * being run will be generated.
  220. */
  221. static bool RunCommand(const char* command, std::string& output, const char* directory = 0,
  222. bool verbose = true);
  223. static bool RunCommand(const char* command, std::string& output,
  224. int &retVal, const char* directory = 0, bool verbose = true);
  225. ///! for windows return the short path for the given path, unix just a pass through
  226. static bool GetShortPath(const char* path, std::string& result);
  227. ///! change directory the the directory specified
  228. static int ChangeDirectory(const char* dir);
  229. static void EnableMessages() { s_DisableMessages = false; }
  230. static void DisableMessages() { s_DisableMessages = true; }
  231. static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }
  232. static void EnableRunCommandOutput() {s_DisableRunCommandOutput = false; }
  233. protected:
  234. // these two functions can be called from ConvertToOutputPath
  235. /**
  236. * Convert the path to a string that can be used in a unix makefile.
  237. * double slashes are removed, and spaces are escaped.
  238. */
  239. static std::string ConvertToUnixOutputPath(const char*);
  240. /**
  241. * Convert the path to string that can be used in a windows project or
  242. * makefile. Double slashes are removed if they are not at the start of
  243. * the string, the slashes are converted to windows style backslashes, and
  244. * if there are spaces in the string it is double quoted.
  245. */
  246. static std::string ConvertToWindowsOutputPath(const char*);
  247. private:
  248. static bool s_ErrorOccured;
  249. static bool s_DisableMessages;
  250. static bool s_DisableRunCommandOutput;
  251. static ErrorCallback s_ErrorCallback;
  252. };
  253. #endif