cmSystemTools.h 9.7 KB

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