cmSystemTools.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * Return a string equivalent to the input string, but with all " " replaced
  63. * with "\ " to escape the spaces.
  64. */
  65. static std::string EscapeSpaces(const char*);
  66. /**
  67. * Return a capitalized string (i.e the first letter is uppercased, all other
  68. * are lowercased).
  69. */
  70. static std::string Capitalized(const std::string&);
  71. /**
  72. * Replace Windows file system slashes with Unix-style slashes.
  73. */
  74. static void ConvertToUnixSlashes(std::string& path);
  75. ///! Return true if a file exists in the current directory.
  76. static bool FileExists(const char* filename);
  77. /**
  78. * Return the number of times the given expression occurs in the file
  79. * specified by the concatenation of dir/file.
  80. */
  81. static int Grep(const char* dir, const char* file, const char* expression);
  82. /**
  83. * Convert a path containing a cygwin drive specifier to its natural
  84. * equivalent.
  85. */
  86. static void ConvertCygwinPath(std::string& pathname);
  87. /**
  88. * Read a CMake command (or function) from an input file. This
  89. * returns the name of the function and a list of its
  90. * arguments.
  91. */
  92. static bool ParseFunction(std::ifstream&,
  93. std::string& name,
  94. std::vector<std::string>& arguments);
  95. /**
  96. * Extract white-space separated arguments from a string.
  97. * Double quoted strings are accepted with spaces.
  98. * This is called by ParseFunction.
  99. */
  100. static void GetArguments(std::string& line,
  101. std::vector<std::string>& arguments);
  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. /**
  113. * Display an error message.
  114. */
  115. static void Error(const char* m, const char* m2=0,
  116. const char* m3=0, const char* m4=0);
  117. /**
  118. * Display a message.
  119. */
  120. static void Message(const char* m, const char* title=0);
  121. ///! Return true if there was an error at any point.
  122. static bool GetErrorOccuredFlag()
  123. {
  124. return cmSystemTools::s_ErrorOccured;
  125. }
  126. /**
  127. * Copy the source file to the destination file only
  128. * if the two files differ.
  129. */
  130. static void CopyFileIfDifferent(const char* source,
  131. const char* destination);
  132. ///! Compare the contents of two files. Return true if different.
  133. static bool FilesDiffer(const char* source,
  134. const char* destination);
  135. ///! Copy a file.
  136. static void cmCopyFile(const char* source,
  137. const char* destination);
  138. ///! Remove a file.
  139. static void RemoveFile(const char* source);
  140. /**
  141. * does a string indicate a true or on value ? This is not the same
  142. * as ifdef.
  143. */
  144. static bool IsOn(const char* val);
  145. /**
  146. * does a string indicate a false or off value ? Note that this is
  147. * not the same as !IsOn(...) because there are a number of
  148. * ambiguous values such as "/usr/local/bin" a path will result in
  149. * IsON and IsOff both returning false. Note that the special path
  150. * NOTFOUND will cause IsOff to return true.
  151. */
  152. static bool IsOff(const char* val);
  153. ///! Find an executable in the system PATH, with optional extra paths.
  154. static std::string FindProgram(const char* name,
  155. const std::vector<std::string>& path= std::vector<std::string>());
  156. ///! Find a library in the system PATH, with optional extra paths.
  157. static std::string FindLibrary(const char* name,
  158. const std::vector<std::string>& path);
  159. ///! return true if the file is a directory.
  160. static bool FileIsDirectory(const char* name);
  161. static std::string GetCurrentWorkingDirectory();
  162. static std::string GetProgramPath(const char*);
  163. static void SplitProgramPath(const char* in_name,
  164. std::string& dir,
  165. std::string& file);
  166. static std::string CollapseFullPath(const char*);
  167. ///! return path of a full filename (no trailing slashes).
  168. static std::string GetFilenamePath(const std::string&);
  169. ///! return file name of a full filename (i.e. file name without path).
  170. static std::string GetFilenameName(const std::string&);
  171. ///! return file extension of a full filename (dot included).
  172. static std::string GetFilenameExtension(const std::string&);
  173. ///! return file name without extension of a full filename.
  174. static std::string GetFilenameNameWithoutExtension(const std::string&);
  175. static long int ModifiedTime(const char* filename);
  176. /**
  177. * Run an executable command and put the stdout in output.
  178. * A temporary file is created in the binaryDir for storing the
  179. * output because windows does not have popen.
  180. */
  181. static bool RunCommand(const char* command, std::string& output);
  182. ///! Generate a temporary file name
  183. static std::string TemporaryFileName();
  184. private:
  185. static bool s_ErrorOccured;
  186. };
  187. #endif