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 /// Appends |input| to |*result|, escaping according to the whims of either
49 /// Bash, or Win32's CommandLineToArgvW().
50 /// Appends the string directly to |result| without modification if we can
51 /// determine that it contains no problematic characters.
52 void GetShellEscapedString(const string& input, string* result);
53 void GetWin32EscapedString(const string& input, string* result);
54 
55 /// Read a file to a string (in text mode: with CRLF conversion
56 /// on Windows).
57 /// Returns -errno and fills in \a err on error.
58 int ReadFile(const string& path, string* contents, string* err);
59 
60 /// Mark a file descriptor to not be inherited on exec()s.
61 void SetCloseOnExec(int fd);
62 
63 /// Given a misspelled string and a list of correct spellings, returns
64 /// the closest match or NULL if there is no close enough match.
65 const char* SpellcheckStringV(const string& text,
66  const vector<const char*>& words);
67 
68 /// Like SpellcheckStringV, but takes a NULL-terminated list.
69 const char* SpellcheckString(const char* text, ...);
70 
71 /// Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
72 string StripAnsiEscapeCodes(const string& in);
73 
74 /// @return the number of processors on the machine. Useful for an initial
75 /// guess for how many jobs to run in parallel. @return 0 on error.
76 int GetProcessorCount();
77 
78 /// @return the load average of the machine. A negative value is returned
79 /// on error.
80 double GetLoadAverage();
81 
82 /// Elide the given string @a str with '...' in the middle if the length
83 /// exceeds @a width.
84 string ElideMiddle(const string& str, size_t width);
85 
86 /// Truncates a file to the given size.
87 bool Truncate(const string& path, size_t size, string* err);
88 
89 #ifdef _MSC_VER
90 #define snprintf _snprintf
91 #define fileno _fileno
92 #define unlink _unlink
93 #define chdir _chdir
94 #define strtoull _strtoui64
95 #define getcwd _getcwd
96 #define PATH_MAX _MAX_PATH
97 #endif
98 
99 #ifdef _WIN32
100 /// Convert the value returned by GetLastError() into a string.
101 string GetLastErrorString();
102 
103 /// Calls Fatal() with a function name and GetLastErrorString.
104 NORETURN void Win32Fatal(const char* function);
105 #endif
106 
107 #endif // NINJA_UTIL_H_
void GetWin32EscapedString(const string &input, string *result)
Definition: util.cc:247
const char * SpellcheckString(const char *text,...)
Like SpellcheckStringV, but takes a NULL-terminated list.
Definition: util.cc:341
void Error(const char *msg,...)
Log an error message.
Definition: util.cc:79
NORETURN void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:52
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:322
double GetLoadAverage()
Definition: util.cc:424
bool CanonicalizePath(string *path, string *err)
Canonicalize a path like "foo/../bar.h" into just "bar.h".
Definition: util.cc:88
#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:447
void Warning(const char *msg,...)
Log a warning message.
Definition: util.cc:70
void GetShellEscapedString(const string &input, string *result)
Appends |input| to |*result|, escaping according to the whims of either Bash, or Win32's CommandLineT...
Definition: util.cc:220
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:282
int GetProcessorCount()
Definition: util.cc:407
string StripAnsiEscapeCodes(const string &in)
Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
Definition: util.cc:384
void SetCloseOnExec(int fd)
Mark a file descriptor to not be inherited on exec()s.
Definition: util.cc:304
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:435