Ninja
util.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NINJA_UTIL_H_
16 #define NINJA_UTIL_H_
17 
18 #ifdef _WIN32
19 #include "win32port.h"
20 #else
21 #include <stdint.h>
22 #endif
23 
24 #include <string>
25 #include <vector>
26 using namespace std;
27 
28 #ifdef _MSC_VER
29 #define NORETURN __declspec(noreturn)
30 #else
31 #define NORETURN __attribute__((noreturn))
32 #endif
33 
34 /// Log a fatal message and exit.
35 NORETURN void Fatal(const char* msg, ...);
36 
37 /// Log a warning message.
38 void Warning(const char* msg, ...);
39 
40 /// Log an error message.
41 void Error(const char* msg, ...);
42 
43 /// Canonicalize a path like "foo/../bar.h" into just "bar.h".
44 bool CanonicalizePath(string* path, string* err);
45 
46 bool CanonicalizePath(char* path, size_t* len, string* err);
47 
48 /// Read a file to a string (in text mode: with CRLF conversion
49 /// on Windows).
50 /// Returns -errno and fills in \a err on error.
51 int ReadFile(const string& path, string* contents, string* err);
52 
53 /// Mark a file descriptor to not be inherited on exec()s.
54 void SetCloseOnExec(int fd);
55 
56 /// Given a misspelled string and a list of correct spellings, returns
57 /// the closest match or NULL if there is no close enough match.
58 const char* SpellcheckStringV(const string& text,
59  const vector<const char*>& words);
60 
61 /// Like SpellcheckStringV, but takes a NULL-terminated list.
62 const char* SpellcheckString(const char* text, ...);
63 
64 /// Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
65 string StripAnsiEscapeCodes(const string& in);
66 
67 /// @return the number of processors on the machine. Useful for an initial
68 /// guess for how many jobs to run in parallel. @return 0 on error.
69 int GetProcessorCount();
70 
71 /// @return the load average of the machine. A negative value is returned
72 /// on error.
73 double GetLoadAverage();
74 
75 /// Elide the given string @a str with '...' in the middle if the length
76 /// exceeds @a width.
77 string ElideMiddle(const string& str, size_t width);
78 
79 /// Truncates a file to the given size.
80 bool Truncate(const string& path, size_t size, string* err);
81 
82 #ifdef _MSC_VER
83 #define snprintf _snprintf
84 #define fileno _fileno
85 #define unlink _unlink
86 #define chdir _chdir
87 #define strtoull _strtoui64
88 #define getcwd _getcwd
89 #define PATH_MAX _MAX_PATH
90 #endif
91 
92 #ifdef _WIN32
93 /// Convert the value returned by GetLastError() into a string.
94 string GetLastErrorString();
95 
96 /// Calls Fatal() with a function name and GetLastErrorString.
97 NORETURN void Win32Fatal(const char* function);
98 #endif
99 
100 #endif // NINJA_UTIL_H_
const char * SpellcheckString(const char *text,...)
Like SpellcheckStringV, but takes a NULL-terminated list.
Definition: util.cc:237
void Error(const char *msg,...)
Log an error message.
Definition: util.cc:78
NORETURN void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:51
const char * SpellcheckStringV(const string &text, const vector< const char * > &words)
Given a misspelled string and a list of correct spellings, returns the closest match or NULL if there...
Definition: util.cc:218
double GetLoadAverage()
Definition: util.cc:340
bool CanonicalizePath(string *path, string *err)
Canonicalize a path like "foo/../bar.h" into just "bar.h".
Definition: util.cc:87
#define NORETURN
Definition: util.h:31
bool Truncate(const string &path, size_t size, string *err)
Truncates a file to the given size.
Definition: util.cc:363
void Warning(const char *msg,...)
Log a warning message.
Definition: util.cc:69
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
Definition: util.cc:178
int GetProcessorCount()
Definition: util.cc:328
string StripAnsiEscapeCodes(const string &in)
Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
Definition: util.cc:280
void SetCloseOnExec(int fd)
Mark a file descriptor to not be inherited on exec()s.
Definition: util.cc:200
string ElideMiddle(const string &str, size_t width)
Elide the given string str with '...' in the middle if the length exceeds width.
Definition: util.cc:351