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 NORETURN   __attribute__((noreturn))
 

Functions

bool CanonicalizePath (string *path, string *err)
 Canonicalize a path like "foo/../bar.h" into just "bar.h". More...
 
bool CanonicalizePath (char *path, size_t *len, string *err)
 
string ElideMiddle (const 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 string &input, string *result)
 Appends |input| to |*result|, escaping according to the whims of either Bash, or Win32's CommandLineToArgvW(). More...
 
void GetWin32EscapedString (const string &input, string *result)
 
int ReadFile (const string &path, string *contents, 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 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 is no close enough match. More...
 
string StripAnsiEscapeCodes (const string &in)
 Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm). More...
 
bool Truncate (const string &path, size_t size, string *err)
 Truncates a file to the given size. More...
 
void Warning (const char *msg,...)
 Log a warning message. More...
 

Macro Definition Documentation

#define NORETURN   __attribute__((noreturn))

Definition at line 31 of file util.h.

Function Documentation

bool CanonicalizePath ( string *  path,
string *  err 
)

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

Definition at line 88 of file util.cc.

References CanonicalizePath(), and METRIC_RECORD.

Referenced by CanonicalizePath(), Builder::ExtractDeps(), ImplicitDepLoader::LoadDepFile(), main(), IncludesNormalize::Normalize(), ManifestParser::ParseDefault(), ManifestParser::ParseEdge(), and TEST().

bool CanonicalizePath ( char *  path,
size_t *  len,
string *  err 
)

Definition at line 100 of file util.cc.

References Fatal(), and METRIC_RECORD.

string ElideMiddle ( const string &  str,
size_t  width 
)

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

Definition at line 435 of file util.cc.

Referenced by LinePrinter::Print(), and TEST().

void Error ( const char *  msg,
  ... 
)
NORETURN void Fatal ( const char *  msg,
  ... 
)
double GetLoadAverage ( )
Returns
the load average of the machine. A negative value is returned on error.

Definition at line 424 of file util.cc.

Referenced by RealCommandRunner::CanRunMore(), and 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 407 of file util.cc.

References GetLoadAverage().

void GetShellEscapedString ( const string &  input,
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.

Definition at line 220 of file util.cc.

References StringNeedsShellEscaping().

Referenced by EdgeEnv::MakePathList(), and TEST().

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

Definition at line 247 of file util.cc.

References StringNeedsWin32Escaping().

Referenced by EdgeEnv::MakePathList(), and TEST().

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

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

Returns -errno and fills in err on error.

Definition at line 282 of file util.cc.

Referenced by main(), MSVCHelperMain(), RealFileReader::ReadFile(), RealDiskInterface::ReadFile(), CLWrapper::Run(), VirtualFileSystem::Tick(), DiskInterface::~DiskInterface(), ManifestParser::FileReader::~FileReader(), and RealDiskInterface::~RealDiskInterface().

void SetCloseOnExec ( int  fd)

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

Definition at line 304 of file util.cc.

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

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

Like SpellcheckStringV, but takes a NULL-terminated list.

Definition at line 341 of file util.cc.

References Fatal(), and SpellcheckStringV().

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 is no close enough match.

Definition at line 322 of file util.cc.

References EditDistance().

Referenced by SpellcheckString().

string StripAnsiEscapeCodes ( const string &  in)

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

Definition at line 384 of file util.cc.

References islatinalpha().

Referenced by BuildStatus::BuildEdgeFinished(), and TEST().

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

Truncates a file to the given size.

Definition at line 447 of file util.cc.

Referenced by DepsLog::Load().

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

Log a warning message.

Definition at line 70 of file util.cc.

Referenced by State::AddOut(), CheckNinjaVersion(), CreateWin32MiniDump(), and IncludesNormalize::Normalize().