ENH: change the search path order (if several Tcl/Tk are installed, the "current...
[cmake.git] / Source / cmSystemTools.h
blob7a8c109e18c715bfa35883850e1f8d97b9ddef73
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmSystemTools.h,v $
5 Language: C++
6 Date: $Date: 2002-07-15 13:45:47 $
7 Version: $Revision: 1.65 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #ifndef cmSystemTools_h
18 #define cmSystemTools_h
20 #include "cmStandardIncludes.h"
22 class cmMakefile;
24 /** \class cmSystemTools
25 * \brief A collection of useful functions for CMake.
27 * cmSystemTools is a class that provides helper functions
28 * for the CMake build system.
30 class cmSystemTools
32 public:
33 /**
34 * Make a new directory if it is not there. This function
35 * can make a full path even if none of the directories existed
36 * prior to calling this function.
38 static bool MakeDirectory(const char* path);
40 /**
41 * Replace replace all occurances of the string in
42 * the source string.
44 static void ReplaceString(std::string& source,
45 const char* replace,
46 const char* with);
48 /** Expand out any arguements in the vector that have ; separated
49 * strings into multiple arguements. A new vector is created
50 * containing the expanded versions of all arguments in argsIn.
52 static void ExpandListArguments(std::vector<std::string> const& argsIn,
53 std::vector<std::string>& argsOut);
55 /**
56 * Read a registry value
58 static bool ReadRegistryValue(const char *key, std::string &value);
60 /**
61 * Write a registry value
63 static bool WriteRegistryValue(const char *key, const char *value);
65 /**
66 * Delete a registry value
68 static bool DeleteRegistryValue(const char *key);
70 /**
71 * Look for and replace registry values in a string
73 static void ExpandRegistryValues(std::string& source);
75 /**
76 * Return a capitalized string (i.e the first letter is uppercased, all other
77 * are lowercased).
79 static std::string Capitalized(const std::string&);
81 /**
82 * Return a lower case string
84 static std::string LowerCase(const std::string&);
86 /**
87 * Return a lower case string
89 static std::string UpperCase(const std::string&);
91 /**
92 * Replace Windows file system slashes with Unix-style slashes.
94 static void ConvertToUnixSlashes(std::string& path);
96 /**
97 * Platform independent escape spaces, unix uses backslash,
98 * windows double quotes the string.
100 static std::string EscapeSpaces(const char* str);
102 ///! Escape quotes in a string.
103 static std::string EscapeQuotes(const char* str);
106 * For windows this calles ConvertToWindowsOutputPath and for unix
107 * it calls ConvertToUnixOutputPath
109 static std::string ConvertToOutputPath(const char*);
111 ///! Return true if a file exists in the current directory.
112 static bool FileExists(const char* filename);
115 * Read a CMake command (or function) from an input file. This
116 * returns the name of the function and a list of its
117 * arguments. The last argument is the name of the file that
118 * the ifstream points to, and is used for debug info only.
120 static bool ParseFunction(std::ifstream&,
121 std::string& name,
122 std::vector<std::string>& arguments,
123 const char* filename, bool& parseError);
126 * Extract white-space separated arguments from a string.
127 * Double quoted strings are accepted with spaces.
128 * This is called by ParseFunction.
130 static void GetArguments(std::string& line,
131 std::vector<std::string>& arguments);
134 * Given a string, replace any escape sequences with the corresponding
135 * characters.
137 static std::string RemoveEscapes(const char*);
141 * Add the paths from the environment variable PATH to the
142 * string vector passed in.
144 static void GetPath(std::vector<std::string>& path);
147 * Get the file extension (including ".") needed for an executable
148 * on the current platform ("" for unix, ".exe" for Windows).
150 static const char* GetExecutableExtension();
152 typedef void (*ErrorCallback)(const char*, const char*, bool&);
154 * Set the function used by GUI's to display error messages
155 * Function gets passed: message as a const char*,
156 * title as a const char*, and a reference to bool that when
157 * set to false, will disable furthur messages (cancel).
159 static void SetErrorCallback(ErrorCallback f);
162 * Display an error message.
164 static void Error(const char* m, const char* m2=0,
165 const char* m3=0, const char* m4=0);
168 * Display a message.
170 static void Message(const char* m, const char* title=0);
172 ///! Return true if there was an error at any point.
173 static bool GetErrorOccuredFlag()
175 return cmSystemTools::s_ErrorOccured;
178 ///! Set the error occured flag back to false
179 static void ResetErrorOccuredFlag()
181 cmSystemTools::s_ErrorOccured = false;
185 * Copy the source file to the destination file only
186 * if the two files differ.
188 static bool CopyFileIfDifferent(const char* source,
189 const char* destination);
191 ///! Compare the contents of two files. Return true if different.
192 static bool FilesDiffer(const char* source,
193 const char* destination);
194 ///! return true if the two files are the same file
195 static bool SameFile(const char* file1, const char* file2);
197 ///! Copy a file.
198 static void cmCopyFile(const char* source,
199 const char* destination);
201 ///! Remove a file.
202 static bool RemoveFile(const char* source);
204 /**
205 * does a string indicate a true or on value ? This is not the same
206 * as ifdef.
208 static bool IsOn(const char* val);
210 /**
211 * does a string indicate a false or off value ? Note that this is
212 * not the same as !IsOn(...) because there are a number of
213 * ambiguous values such as "/usr/local/bin" a path will result in
214 * IsON and IsOff both returning false. Note that the special path
215 * NOTFOUND or IGNORE will cause IsOff to return true.
217 static bool IsOff(const char* val);
219 ///! Find a file in the system PATH, with optional extra paths.
220 static std::string FindFile(const char* name,
221 const std::vector<std::string>& path= std::vector<std::string>());
223 ///! Find an executable in the system PATH, with optional extra paths.
224 static std::string FindProgram(const char* name,
225 const std::vector<std::string>& path = std::vector<std::string>(),
226 bool no_system_path = false);
228 ///! Find a library in the system PATH, with optional extra paths.
229 static std::string FindLibrary(const char* name,
230 const std::vector<std::string>& path,
231 const cmMakefile *makefile = 0);
233 ///! return true if the file is a directory.
234 static bool FileIsDirectory(const char* name);
235 static void Glob(const char *directory, const char *regexp,
236 std::vector<std::string>& files);
237 static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
239 static std::string GetCurrentWorkingDirectory();
240 static std::string GetProgramPath(const char*);
241 static void SplitProgramPath(const char* in_name,
242 std::string& dir,
243 std::string& file);
244 static std::string CollapseFullPath(const char*);
246 ///! return path of a full filename (no trailing slashes).
247 static std::string GetFilenamePath(const std::string&);
249 ///! return file name of a full filename (i.e. file name without path).
250 static std::string GetFilenameName(const std::string&);
252 ///! return file extension of a full filename (dot included).
253 static std::string GetFilenameExtension(const std::string&);
255 ///! return file name without extension of a full filename.
256 static std::string GetFilenameWithoutExtension(const std::string&);
258 ///! return file name without its last (shortest) extension.
259 static std::string GetFilenameWithoutLastExtension(const std::string&);
261 static long int ModifiedTime(const char* filename);
264 * Run an executable command and put the stdout in output.
265 * A temporary file is created in the binaryDir for storing the
266 * output because windows does not have popen.
268 * If verbose is false, no user-viewable output from the program
269 * being run will be generated.
271 static bool RunCommand(const char* command, std::string& output, const char* directory = 0,
272 bool verbose = true);
273 static bool RunCommand(const char* command, std::string& output,
274 int &retVal, const char* directory = 0, bool verbose = true);
276 ///! for windows return the short path for the given path, unix just a pass through
277 static bool GetShortPath(const char* path, std::string& result);
279 ///! change directory the the directory specified
280 static int ChangeDirectory(const char* dir);
282 static void EnableMessages() { s_DisableMessages = false; }
283 static void DisableMessages() { s_DisableMessages = true; }
284 static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }
285 static void EnableRunCommandOutput() {s_DisableRunCommandOutput = false; }
286 protected:
287 // these two functions can be called from ConvertToOutputPath
289 * Convert the path to a string that can be used in a unix makefile.
290 * double slashes are removed, and spaces are escaped.
292 static std::string ConvertToUnixOutputPath(const char*);
295 * Convert the path to string that can be used in a windows project or
296 * makefile. Double slashes are removed if they are not at the start of
297 * the string, the slashes are converted to windows style backslashes, and
298 * if there are spaces in the string it is double quoted.
300 static std::string ConvertToWindowsOutputPath(const char*);
303 private:
304 static bool s_ErrorOccured;
305 static bool s_DisableMessages;
306 static bool s_DisableRunCommandOutput;
307 static ErrorCallback s_ErrorCallback;
311 #endif