Ninja
Macros | Functions
util.h File Reference
#include <stdint.h>
#include <string>
#include <vector>
Include dependency graph for util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NINJA_FALLTHROUGH
 
#define NORETURN   __attribute__((noreturn))
 

Functions

bool CanonicalizePath (std::string *path, uint64_t *slash_bits, std::string *err)
 Canonicalize a path like "foo/../bar.h" into just "bar.h". More...
 
bool CanonicalizePath (char *path, size_t *len, uint64_t *slash_bits, std::string *err)
 
std::string ElideMiddle (const std::string &str, size_t width)
 Elide the given string str with '...' in the middle if the length exceeds width. More...
 
void Error (const char *msg,...)
 Log an error message. More...
 
NORETURN void Fatal (const char *msg,...)
 Log a fatal message and exit. More...
 
double GetLoadAverage ()
 
int GetProcessorCount ()
 
void GetShellEscapedString (const std::string &input, std::string *result)
 Appends |input| to |*result|, escaping according to the whims of either Bash, or Win32's CommandLineToArgvW(). More...
 
void GetWin32EscapedString (const std::string &input, std::string *result)
 
bool islatinalpha (int c)
 
int ReadFile (const std::string &path, std::string *contents, std::string *err)
 Read a file to a string (in text mode: with CRLF conversion on Windows). More...
 
void SetCloseOnExec (int fd)
 Mark a file descriptor to not be inherited on exec()s. More...
 
const char * SpellcheckString (const char *text,...)
 Like SpellcheckStringV, but takes a NULL-terminated list. More...
 
const char * SpellcheckStringV (const std::string &text, const std::vector< const char *> &words)
 Given a misspelled string and a list of correct spellings, returns the closest match or NULL if there is no close enough match. More...
 
std::string StripAnsiEscapeCodes (const std::string &in)
 Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm). More...
 
bool Truncate (const std::string &path, size_t size, std::string *err)
 Truncates a file to the given size. More...
 
void Warning (const char *msg,...)
 Log a warning message. More...
 

Macro Definition Documentation

◆ NINJA_FALLTHROUGH

#define NINJA_FALLTHROUGH

Definition at line 47 of file util.h.

Referenced by CanonicalizePath(), and MurmurHash2().

◆ NORETURN

#define NORETURN   __attribute__((noreturn))

Definition at line 30 of file util.h.

Function Documentation

◆ CanonicalizePath() [1/2]

bool CanonicalizePath ( std::string *  path,
uint64_t slash_bits,
std::string *  err 
)

Canonicalize a path like "foo/../bar.h" into just "bar.h".

|slash_bits| has bits set starting from lowest for a backslash that was normalized to a forward slash. (only used on Windows)

◆ CanonicalizePath() [2/2]

bool CanonicalizePath ( char *  path,
size_t *  len,
uint64_t slash_bits,
std::string *  err 
)

◆ ElideMiddle()

std::string ElideMiddle ( const std::string &  str,
size_t  width 
)

Elide the given string str with '...' in the middle if the length exceeds width.

◆ Error()

void Error ( const char *  msg,
  ... 
)

◆ Fatal()

NORETURN void Fatal ( const char *  msg,
  ... 
)

◆ GetLoadAverage()

double GetLoadAverage ( )
Returns
the load average of the machine. A negative value is returned on error.

Definition at line 589 of file util.cc.

Referenced by RealCommandRunner::CanRunMore().

◆ GetProcessorCount()

int GetProcessorCount ( )
Returns
the number of processors on the machine. Useful for an initial guess for how many jobs to run in parallel.
0 on error.

Definition at line 484 of file util.cc.

◆ GetShellEscapedString()

void GetShellEscapedString ( const std::string &  input,
std::string *  result 
)

Appends |input| to |*result|, escaping according to the whims of either Bash, or Win32's CommandLineToArgvW().

Appends the string directly to |result| without modification if we can determine that it contains no problematic characters.

◆ GetWin32EscapedString()

void GetWin32EscapedString ( const std::string &  input,
std::string *  result 
)

◆ islatinalpha()

bool islatinalpha ( int  c)

Definition at line 456 of file util.cc.

Referenced by StripAnsiEscapeCodes().

◆ ReadFile()

int ReadFile ( const std::string &  path,
std::string *  contents,
std::string *  err 
)

Read a file to a string (in text mode: with CRLF conversion on Windows).

Returns -errno and fills in err on error.

◆ SetCloseOnExec()

void SetCloseOnExec ( int  fd)

Mark a file descriptor to not be inherited on exec()s.

Definition at line 377 of file util.cc.

Referenced by BuildLog::OpenForWriteIfNeeded(), DepsLog::OpenForWriteIfNeeded(), and Subprocess::Start().

◆ SpellcheckString()

const char* SpellcheckString ( const char *  text,
  ... 
)

Like SpellcheckStringV, but takes a NULL-terminated list.

Definition at line 414 of file util.cc.

References SpellcheckStringV().

◆ SpellcheckStringV()

const char* SpellcheckStringV ( const std::string &  text,
const std::vector< const char *> &  words 
)

Given a misspelled string and a list of correct spellings, returns the closest match or NULL if there is no close enough match.

◆ StripAnsiEscapeCodes()

std::string StripAnsiEscapeCodes ( const std::string &  in)

Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).

◆ Truncate()

bool Truncate ( const std::string &  path,
size_t  size,
std::string *  err 
)

Truncates a file to the given size.

◆ Warning()

void Warning ( const char *  msg,
  ... 
)

Log a warning message.

Definition at line 77 of file util.cc.

Referenced by GraphViz::AddTarget(), CheckNinjaVersion(), and ManifestParser::ParseEdge().