cmSystemTools.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #ifndef cmSystemTools_h
  33. #define cmSystemTools_h
  34. #include "cmStandardIncludes.h"
  35. /** \class cmSystemTools
  36. * \brief A collection of useful functions for CMake.
  37. *
  38. * cmSystemTools is a class that provides helper functions
  39. * for the CMake build system.
  40. */
  41. class cmSystemTools
  42. {
  43. public:
  44. /**
  45. * Make a new directory if it is not there. This function
  46. * can make a full path even if none of the directories existed
  47. * prior to calling this function.
  48. */
  49. static bool MakeDirectory(const char* path);
  50. /**
  51. * Replace replace all occurances of the string in
  52. * the source string.
  53. */
  54. static void ReplaceString(std::string& source,
  55. const char* replace,
  56. const char* with);
  57. /**
  58. * Look for and replace registry values in a string
  59. */
  60. static void ExpandRegistryValues(std::string& source);
  61. /**
  62. * make sure on windows that paths with // are converted to \\
  63. */
  64. static std::string HandleNetworkPaths(const char*);
  65. /**
  66. * Return a string equivalent to the input string, but with all " " replaced
  67. * with "\ " to escape the spaces.
  68. */
  69. static std::string EscapeSpaces(const char*);
  70. /**
  71. * Return a string equivalent to the input string, but with all " replaced
  72. * with \" to escape the quote
  73. */
  74. static std::string EscapeQuotes(const char*);
  75. /**
  76. * Return a capitalized string (i.e the first letter is uppercased, all other
  77. * are lowercased).
  78. */
  79. static std::string Capitalized(const std::string&);
  80. /**
  81. * Return a lower case string
  82. */
  83. static std::string LowerCase(const std::string&);
  84. /**
  85. * Return a lower case string
  86. */
  87. static std::string UpperCase(const std::string&);
  88. /**
  89. * Replace Windows file system slashes with Unix-style slashes.
  90. */
  91. static const char *ConvertToUnixSlashes(std::string& path);
  92. /**
  93. * Replace Unix file system slashes with Windows-style slashes
  94. */
  95. static const char *ConvertToWindowsSlashes(std::string& path);
  96. /**
  97. * Replace Unix file system slashes with Windows-style slashes and
  98. * remove any duplicate \\ slashes to clean the path.
  99. */
  100. static const char *ConvertToWindowsSlashesAndCleanUp(std::string& path);
  101. ///! Return true if a file exists in the current directory.
  102. static bool FileExists(const char* filename);
  103. /**
  104. * Read a CMake command (or function) from an input file. This
  105. * returns the name of the function and a list of its
  106. * arguments. The last argument is the name of the file that
  107. * the ifstream points to, and is used for debug info only.
  108. */
  109. static bool ParseFunction(std::ifstream&,
  110. std::string& name,
  111. std::vector<std::string>& arguments,
  112. const char* filename, bool& parseError);
  113. /**
  114. * Extract white-space separated arguments from a string.
  115. * Double quoted strings are accepted with spaces.
  116. * This is called by ParseFunction.
  117. */
  118. static void GetArguments(std::string& line,
  119. std::vector<std::string>& arguments);
  120. /**
  121. * Given a string, replace any escape sequences with the corresponding
  122. * characters.
  123. */
  124. static std::string RemoveEscapes(const char*);
  125. /**
  126. * Add the paths from the environment variable PATH to the
  127. * string vector passed in.
  128. */
  129. static void GetPath(std::vector<std::string>& path);
  130. /**
  131. * Get the file extension (including ".") needed for an executable
  132. * on the current platform ("" for unix, ".exe" for Windows).
  133. */
  134. static const char* GetExecutableExtension();
  135. typedef void (*ErrorCallback)(const char*, const char*, bool&);
  136. /**
  137. * Set the function used by GUI's to display error messages
  138. * Function gets passed: message as a const char*,
  139. * title as a const char*, and a reference to bool that when
  140. * set to false, will disable furthur messages (cancel).
  141. */
  142. static void SetErrorCallback(ErrorCallback f);
  143. /**
  144. * Display an error message.
  145. */
  146. static void Error(const char* m, const char* m2=0,
  147. const char* m3=0, const char* m4=0);
  148. /**
  149. * Display a message.
  150. */
  151. static void Message(const char* m, const char* title=0);
  152. ///! Return true if there was an error at any point.
  153. static bool GetErrorOccuredFlag()
  154. {
  155. return cmSystemTools::s_ErrorOccured;
  156. }
  157. ///! Set the error occured flag back to false
  158. static void ResetErrorOccuredFlag()
  159. {
  160. cmSystemTools::s_ErrorOccured = false;
  161. }
  162. /**
  163. * Copy the source file to the destination file only
  164. * if the two files differ.
  165. */
  166. static void CopyFileIfDifferent(const char* source,
  167. const char* destination);
  168. ///! Compare the contents of two files. Return true if different.
  169. static bool FilesDiffer(const char* source,
  170. const char* destination);
  171. ///! return true if the two files are the same file
  172. static bool SameFile(const char* file1, const char* file2);
  173. ///! Copy a file.
  174. static void cmCopyFile(const char* source,
  175. const char* destination);
  176. ///! Remove a file.
  177. static void RemoveFile(const char* source);
  178. /**
  179. * does a string indicate a true or on value ? This is not the same
  180. * as ifdef.
  181. */
  182. static bool IsOn(const char* val);
  183. /**
  184. * does a string indicate a false or off value ? Note that this is
  185. * not the same as !IsOn(...) because there are a number of
  186. * ambiguous values such as "/usr/local/bin" a path will result in
  187. * IsON and IsOff both returning false. Note that the special path
  188. * NOTFOUND or IGNORE will cause IsOff to return true.
  189. */
  190. static bool IsOff(const char* val);
  191. ///! Find a file in the system PATH, with optional extra paths.
  192. static std::string FindFile(const char* name,
  193. const std::vector<std::string>& path= std::vector<std::string>());
  194. ///! Find an executable in the system PATH, with optional extra paths.
  195. static std::string FindProgram(const char* name,
  196. const std::vector<std::string>& path= std::vector<std::string>());
  197. ///! Find a library in the system PATH, with optional extra paths.
  198. static std::string FindLibrary(const char* name,
  199. const std::vector<std::string>& path);
  200. ///! return true if the file is a directory.
  201. static bool FileIsDirectory(const char* name);
  202. static void Glob(const char *directory, const char *regexp,
  203. std::vector<std::string>& files);
  204. static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
  205. static std::string GetCurrentWorkingDirectory();
  206. static std::string GetProgramPath(const char*);
  207. static void SplitProgramPath(const char* in_name,
  208. std::string& dir,
  209. std::string& file);
  210. static std::string CollapseFullPath(const char*);
  211. /**
  212. * all file path prefixes matching \arg from will be replaced by
  213. * \arg to. Affects the return value of CollapseFullPath and
  214. * GetCurrentWorkingDirectory.
  215. */
  216. static void AddPathTranslation( const std::string& from, const std::string& to );
  217. ///! Apply the current path translations to \arg path.
  218. static void ApplyPathTranslation( std::string& path );
  219. ///! return path of a full filename (no trailing slashes).
  220. static std::string GetFilenamePath(const std::string&);
  221. ///! return file name of a full filename (i.e. file name without path).
  222. static std::string GetFilenameName(const std::string&);
  223. ///! return file extension of a full filename (dot included).
  224. static std::string GetFilenameExtension(const std::string&);
  225. static std::string GetFilenameShortestExtension(const std::string&);
  226. ///! return file name without extension of a full filename.
  227. static std::string GetFilenameNameWithoutExtension(const std::string&);
  228. static long int ModifiedTime(const char* filename);
  229. /**
  230. * Run an executable command and put the stdout in output.
  231. * A temporary file is created in the binaryDir for storing the
  232. * output because windows does not have popen.
  233. *
  234. * If verbose is false, no user-viewable output from the program
  235. * being run will be generated.
  236. */
  237. static bool RunCommand(const char* command, std::string& output,
  238. bool verbose = true);
  239. static bool RunCommand(const char* command, std::string& output,
  240. int &retVal, bool verbose = true);
  241. ///! change directory the the directory specified
  242. static int ChangeDirectory(const char* dir);
  243. static void EnableMessages() { s_DisableMessages = false; }
  244. static void DisableMessages() { s_DisableMessages = true; }
  245. static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }
  246. static void EnableRunCommandOutput() {s_DisableRunCommandOutput = false; }
  247. private:
  248. static bool s_ErrorOccured;
  249. static bool s_DisableMessages;
  250. static bool s_DisableRunCommandOutput;
  251. static ErrorCallback s_ErrorCallback;
  252. typedef std::map<cmStdString, cmStdString> PathMap;
  253. static PathMap s_PathMap;
  254. };
  255. #endif