cmSystemTools.h 10 KB

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