Ninja
|
00001 // Copyright 2011 Google Inc. All Rights Reserved. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // http://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 00015 #ifndef NINJA_UTIL_H_ 00016 #define NINJA_UTIL_H_ 00017 00018 #ifdef _WIN32 00019 #include "win32port.h" 00020 #else 00021 #include <stdint.h> 00022 #endif 00023 00024 #include <string> 00025 #include <vector> 00026 using namespace std; 00027 00028 #ifdef _MSC_VER 00029 #define NORETURN __declspec(noreturn) 00030 #else 00031 #define NORETURN __attribute__((noreturn)) 00032 #endif 00033 00034 /// Log a fatal message and exit. 00035 NORETURN void Fatal(const char* msg, ...); 00036 00037 /// Log a warning message. 00038 void Warning(const char* msg, ...); 00039 00040 /// Log an error message. 00041 void Error(const char* msg, ...); 00042 00043 /// Canonicalize a path like "foo/../bar.h" into just "bar.h". 00044 bool CanonicalizePath(string* path, string* err); 00045 00046 bool CanonicalizePath(char* path, size_t* len, string* err); 00047 00048 /// Read a file to a string (in text mode: with CRLF conversion 00049 /// on Windows). 00050 /// Returns -errno and fills in \a err on error. 00051 int ReadFile(const string& path, string* contents, string* err); 00052 00053 /// Mark a file descriptor to not be inherited on exec()s. 00054 void SetCloseOnExec(int fd); 00055 00056 /// Given a misspelled string and a list of correct spellings, returns 00057 /// the closest match or NULL if there is no close enough match. 00058 const char* SpellcheckStringV(const string& text, 00059 const vector<const char*>& words); 00060 00061 /// Like SpellcheckStringV, but takes a NULL-terminated list. 00062 const char* SpellcheckString(const char* text, ...); 00063 00064 /// Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm). 00065 string StripAnsiEscapeCodes(const string& in); 00066 00067 /// @return the number of processors on the machine. Useful for an initial 00068 /// guess for how many jobs to run in parallel. @return 0 on error. 00069 int GetProcessorCount(); 00070 00071 /// @return the load average of the machine. A negative value is returned 00072 /// on error. 00073 double GetLoadAverage(); 00074 00075 /// Elide the given string @a str with '...' in the middle if the length 00076 /// exceeds @a width. 00077 string ElideMiddle(const string& str, size_t width); 00078 00079 /// Truncates a file to the given size. 00080 bool Truncate(const string& path, size_t size, string* err); 00081 00082 #ifdef _MSC_VER 00083 #define snprintf _snprintf 00084 #define fileno _fileno 00085 #define unlink _unlink 00086 #define chdir _chdir 00087 #define strtoull _strtoui64 00088 #define getcwd _getcwd 00089 #define PATH_MAX _MAX_PATH 00090 #endif 00091 00092 #ifdef _WIN32 00093 /// Convert the value returned by GetLastError() into a string. 00094 string GetLastErrorString(); 00095 00096 /// Calls Fatal() with a function name and GetLastErrorString. 00097 NORETURN void Win32Fatal(const char* function); 00098 #endif 00099 00100 #endif // NINJA_UTIL_H_