dodo  0.0.1
A C++ library to create containerized Linux services
dodo::common Namespace Reference

Common and utility interfaces. More...

Data Structures

class  Application
 Red tape wrapper class for applications, from command line to services. More...
 
class  Bytes
 An array of Octets with size elements. More...
 
class  Cache
 Simple thread-safe Cache template for arbitrary Key-Value pairs. More...
 
class  Config
 Singleton interface to a (read-only) deployment configuration, combining data from the deployment configuration file, environment variables and some operating system attributes such as the CPU count, RAM and virtual memory size. More...
 
class  DataCrypt
 Interface to encrypt and decrypt Bytes data to/from a secure string. More...
 
class  DebugObject
 Interface to objects that support dumping their state to a string. More...
 
class  Exception
 An Exception is thrown in exceptional circumstances, and its occurrence should generally imply that the program should stop, as it has entered a state it was never designed to handle. More...
 
class  Logger
 A Logger interface. More...
 
class  Puts
 Helper class to write strings in stream format, eg. More...
 
class  StopWatch
 StopWatch timing class. More...
 
class  SystemError
 Linux system error primitive to provide a consistent interface to Linux error codes. More...
 
class  SystemException
 Descending from Exception, exceptions based on a dodo::common::SystemError code. More...
 
class  UnitTest
 Unit test assitence class. More...
 

Typedefs

typedef uint8_t Octet
 Octet aka 'byte'. More...
 

Functions

bool availableFileSpace (const std::string &path, size_t &avail)
 Return true when the free space could be determined, and set in avail. More...
 
std::string bio2String (BIO *bio)
 Convert the data contents of an OpenSSL BIO to a std::string. More...
 
void closeLibrary ()
 Close the common library. More...
 
bool directoryExists (const std::string &path)
 Return true when the directory exists. More...
 
bool directoryWritable (const std::string &path)
 Return true when the directory exists and is writable to the caller. More...
 
void dumpBinaryData (std::ostream &out, const std::string &s, size_t width)
 Dump binary data to a std::ostream. More...
 
std::vector< std::string > escapedSplit (const std::string &src, std::set< char > escape, char delimiter=' ')
 Split a string into substrings by delimiter - unless the delimiter is escaped. More...
 
std::string escapeJSON (const std::string &s)
 Escape a JSOn string. More...
 
bool fileReadAccess (const std::string &path)
 Return true when the file exists and the calling user has read access. More...
 
bool fileReadInt (const std::string &file, int &i)
 Read from a file, expecting it to contain a (signed) int. More...
 
std::string fileReadString (const std::string &filename)
 Read the file as a single string. More...
 
std::vector< std::string > fileReadStrings (const std::string &filename)
 Read the file as vector of strings. More...
 
std::vector< std::string > fileReadStrings (const std::string &filename, const std::regex &exp)
 Read the file as vector of strings, return only regexp matches. More...
 
std::string formatDateTimeUTC (const struct timeval &tv)
 Return a datetime string in UTC (2020-07-01T20:14:36.442929Z) More...
 
size_t getFileSize (const std::string &path)
 Return the size of the file. More...
 
double getSecondDiff (struct timeval &t1, struct timeval &t2)
 Return difference in seconds as a double. More...
 
std::string getSSLErrors (char terminator)
 Get all OpenSSL errors as a single string, and clear their error state. More...
 
void initLibrary ()
 Initialize the common library. More...
 
std::vector< std::string > split (const std::string &src, char delimiter=' ')
 Split a string into substrings. More...
 
std::string strASCII (char c)
 Return ASCII representation of char. More...
 
size_t writeSSLErrors (std::ostream &out, char terminator)
 Write OpenSSL errors occurred in this thread to ostream, and clear their error state. More...
 
template<typename T >
YAML_read_key (const YAML::Node &node, const std::string &key)
 Template function to check existence and read YAML values of arbitrary type. More...
 
template bool YAML_read_key< bool > (const YAML::Node &, const std::string &)
 Instantiate template YAML_read_key for bool. More...
 
template double YAML_read_key< double > (const YAML::Node &, const std::string &)
 Instantiate template YAML_read_key for double. More...
 
template int YAML_read_key< int > (const YAML::Node &, const std::string &)
 Instantiate template YAML_read_key for int. More...
 
template long YAML_read_key< long > (const YAML::Node &, const std::string &)
 Instantiate template YAML_read_key for long. More...
 
template size_t YAML_read_key< size_t > (const YAML::Node &, const std::string &)
 Instantiate template YAML_read_key for size_t. More...
 
template std::string YAML_read_key< std::string > (const YAML::Node &, const std::string &)
 Instantiate template YAML_read_key for std::string. More...
 
template uint16_t YAML_read_key< uint16_t > (const YAML::Node &, const std::string &)
 Instantiate template YAML_read_key for uint16_t. More...
 
template unsigned int YAML_read_key< unsigned int > (const YAML::Node &, const std::string &)
 Instantiate template YAML_read_key for unsigned int. More...
 
template<typename T >
YAML_read_key_default (const YAML::Node &node, const std::string &key, const T &default_value)
 Template function to check existence and read YAML values of arbitrary type. More...
 
template bool YAML_read_key_default< bool > (const YAML::Node &, const std::string &, const bool &)
 Instantiate template YAML_read_key for bool. More...
 
template double YAML_read_key_default< double > (const YAML::Node &, const std::string &, const double &)
 Instantiate template YAML_read_key for double. More...
 
template int YAML_read_key_default< int > (const YAML::Node &, const std::string &, const int &)
 Instantiate template YAML_read_key for int. More...
 
template long YAML_read_key_default< long > (const YAML::Node &, const std::string &, const long &)
 Instantiate template YAML_read_key for long. More...
 
template size_t YAML_read_key_default< size_t > (const YAML::Node &, const std::string &, const size_t &)
 Instantiate template YAML_read_key for size_t. More...
 
template std::string YAML_read_key_default< std::string > (const YAML::Node &, const std::string &, const std::string &)
 Instantiate template YAML_read_key for std::string. More...
 
template unsigned int YAML_read_key_default< unsigned int > (const YAML::Node &, const std::string &, const unsigned int &)
 Instantiate template YAML_read_key for unsigned int. More...
 

Detailed Description

Common and utility interfaces.

Typedef Documentation

◆ Octet

typedef uint8_t dodo::common::Octet

Octet aka 'byte'.

Definition at line 34 of file bytes.hpp.

Function Documentation

◆ availableFileSpace()

bool dodo::common::availableFileSpace ( const std::string &  path,
size_t &  avail 
)

Return true when the free space could be determined, and set in avail.

Parameters
pathThe path on which to check.
availThe available free space.
Returns
False if the free space could not be determined (for example, when the file does not exist).

Definition at line 249 of file util.cpp.

◆ bio2String()

std::string dodo::common::bio2String ( BIO *  bio)

Convert the data contents of an OpenSSL BIO to a std::string.

Parameters
bioThe source BIO.
Returns
The string representation of the BIO contents.

Definition at line 132 of file util.cpp.

Referenced by dodo::network::X509Certificate::getIssuer(), dodo::network::X509CertificateSigningRequest::getSubject(), and dodo::network::X509Certificate::getSubject().

Here is the caller graph for this function:

◆ closeLibrary()

void dodo::common::closeLibrary ( )

Close the common library.

Definition at line 58 of file common.hpp.

Referenced by dodo::closeLibrary().

Here is the caller graph for this function:

◆ directoryExists()

bool dodo::common::directoryExists ( const std::string &  path)

Return true when the directory exists.

Parameters
pathThe path to the directory.
Returns
True when the directory exists.

Definition at line 230 of file util.cpp.

Referenced by dodo::common::Logger::getFileParams().

Here is the caller graph for this function:

◆ directoryWritable()

bool dodo::common::directoryWritable ( const std::string &  path)

Return true when the directory exists and is writable to the caller.

Parameters
pathThe path to the directory.
Returns
True when the directory exists and is writable to the caller.

Definition at line 239 of file util.cpp.

Referenced by dodo::common::Logger::getFileParams().

Here is the caller graph for this function:

◆ dumpBinaryData()

void dodo::common::dumpBinaryData ( std::ostream &  out,
const std::string &  s,
size_t  width 
)

Dump binary data to a std::ostream.

Parameters
outThe std::stream to dump to.
sThe char stream.
widthThe max character width of the output.

Definition at line 105 of file util.cpp.

References strASCII().

Here is the call graph for this function:

◆ escapedSplit()

std::vector<std::string> dodo::common::escapedSplit ( const std::string &  src,
std::set< char >  escape,
char  delimiter = ' ' 
)
inline

Split a string into substrings by delimiter - unless the delimiter is escaped.

Passing ( 'one:two$:three', '$', ':' ) splits into ['one','two:three']. Passing ( 'one:t

$wo
hree', '

', ':' ) splits into ['one','t$$wo','three']. So the escape character only has effect when followed by a delimiter, otherwise taken as input.

Parameters
srcThe string to split from.
escapeThe escape characters to use
delimiterThe delimiter to use.
Returns
A vector of strings.

Definition at line 157 of file util.hpp.

Referenced by dodo::network::X509Common::parseIdentity().

Here is the caller graph for this function:

◆ escapeJSON()

std::string dodo::common::escapeJSON ( const std::string &  s)

Escape a JSOn string.

Parameters
sThe JSON string to escape.
Returns
The escaped string.

Definition at line 197 of file util.cpp.

◆ fileReadAccess()

bool dodo::common::fileReadAccess ( const std::string &  path)

Return true when the file exists and the calling user has read access.

Parameters
pathThe path to the file.
Returns
True when the file exists and can be read.

Definition at line 220 of file util.cpp.

Referenced by dodo::common::Application::detectHostType().

Here is the caller graph for this function:

◆ fileReadInt()

bool dodo::common::fileReadInt ( const std::string &  file,
int &  i 
)

Read from a file, expecting it to contain a (signed) int.

Parameters
fileThe file name.
iThe int read.
Returns
False if the read or conversion failed.

Definition at line 39 of file util.cpp.

Referenced by dodo::network::TCPListener::construct().

Here is the caller graph for this function:

◆ fileReadString()

std::string dodo::common::fileReadString ( const std::string &  filename)

Read the file as a single string.

Exceptions
Oopswhen the file cannot be read.
Parameters
filenamethe file to read from.
Returns
the file contents as a string.

Definition at line 161 of file util.cpp.

References throw_Exception.

Referenced by dodo::common::Config::checkConfig().

Here is the caller graph for this function:

◆ fileReadStrings() [1/2]

std::vector< std::string > dodo::common::fileReadStrings ( const std::string &  filename)

Read the file as vector of strings.

Exceptions
Oopswhen the file cannot be read.
Parameters
filenamethe file to read from.
Returns
the file as a vector of strings

Definition at line 170 of file util.cpp.

References throw_Exception.

Referenced by dodo::common::Application::detectHostType().

Here is the caller graph for this function:

◆ fileReadStrings() [2/2]

std::vector< std::string > dodo::common::fileReadStrings ( const std::string &  filename,
const std::regex &  exp 
)

Read the file as vector of strings, return only regexp matches.

Exceptions
Oopswhen the file cannot be read.
Parameters
filenamethe file to read from.
expthe regex that must match.
Returns
the file as a vector of strings.

Definition at line 183 of file util.cpp.

References throw_Exception.

◆ formatDateTimeUTC()

std::string dodo::common::formatDateTimeUTC ( const struct timeval &  tv)

Return a datetime string in UTC (2020-07-01T20:14:36.442929Z)

Parameters
tvThe tv time struct.
Returns
The formatted datetime.

Definition at line 262 of file util.cpp.

Referenced by dodo::common::Logger::checkRotate(), and dodo::common::Logger::formatMessage().

Here is the caller graph for this function:

◆ getFileSize()

size_t dodo::common::getFileSize ( const std::string &  path)

Return the size of the file.

If the file does not exist or is otherwise inaccessible, returns 0.

Parameters
pathThe path of the file.
Returns
The file size

Definition at line 256 of file util.cpp.

Referenced by dodo::common::Logger::checkRotate(), and dodo::common::Logger::getFileParams().

Here is the caller graph for this function:

◆ getSecondDiff()

double dodo::common::getSecondDiff ( struct timeval &  t1,
struct timeval &  t2 
)
inline

Return difference in seconds as a double.

t2 must be > t1 for the return value to be positive.

Parameters
t1The earlier time.
t2The later time.
Returns
the difference in seconds from t1 to t2.

Definition at line 126 of file util.hpp.

Referenced by dodo::network::TCPServer::getIdleSeconds(), dodo::threads::Thread::getRunTime(), dodo::threads::Thread::getSnapDiffTime(), dodo::network::TCPListener::logStats(), and dodo::network::TCPServer::run().

Here is the caller graph for this function:

◆ getSSLErrors()

std::string dodo::common::getSSLErrors ( char  terminator)

◆ initLibrary()

void dodo::common::initLibrary ( )

Initialize the common library.

Definition at line 48 of file common.hpp.

References throw_Exception.

Referenced by dodo::initLibrary().

Here is the caller graph for this function:

◆ split()

std::vector<std::string> dodo::common::split ( const std::string &  src,
char  delimiter = ' ' 
)
inline

Split a string into substrings.

Parameters
srcThe string to split from.
delimiterThe delimiter to use.
Returns
A vector of strings.

Definition at line 136 of file util.hpp.

Referenced by dodo::common::DataCrypt::decode(), and dodo::network::X509Common::parseIdentity().

Here is the caller graph for this function:

◆ strASCII()

std::string dodo::common::strASCII ( char  c)

Return ASCII representation of char.

Parameters
cThe char.
Returns
the ASCII representation.

Definition at line 50 of file util.cpp.

Referenced by dumpBinaryData().

Here is the caller graph for this function:

◆ writeSSLErrors()

size_t dodo::common::writeSSLErrors ( std::ostream &  out,
char  terminator 
)

Write OpenSSL errors occurred in this thread to ostream, and clear their error state.

Parameters
outThe std::ostream to write to.
terminatorThe char to use to separate lines.
Returns
The number of SSL errors written.

Definition at line 140 of file util.cpp.

Referenced by getSSLErrors().

Here is the caller graph for this function:

◆ YAML_read_key()

template<typename T >
T dodo::common::YAML_read_key ( const YAML::Node &  node,
const std::string &  key 
)

Template function to check existence and read YAML values of arbitrary type.

The key-value pair must must exist under node.

Parameters
nodeThe YAML::Node containing the key-value pair
keyThe YAML::Node name.
Returns
the value of type T
Exceptions
common::Exceptionwhen the node does not exist.

Definition at line 276 of file util.cpp.

References throw_Exception.

◆ YAML_read_key< bool >()

template bool dodo::common::YAML_read_key< bool > ( const YAML::Node &  ,
const std::string &   
)

Instantiate template YAML_read_key for bool.

Returns
the value as a bool

Referenced by dodo::network::TLSContext::TLSContext().

Here is the caller graph for this function:

◆ YAML_read_key< double >()

template double dodo::common::YAML_read_key< double > ( const YAML::Node &  ,
const std::string &   
)

Instantiate template YAML_read_key for double.

Returns
the value as a double

◆ YAML_read_key< int >()

template int dodo::common::YAML_read_key< int > ( const YAML::Node &  ,
const std::string &   
)

Instantiate template YAML_read_key for int.

Returns
the value as an int

◆ YAML_read_key< long >()

template long dodo::common::YAML_read_key< long > ( const YAML::Node &  ,
const std::string &   
)

Instantiate template YAML_read_key for long.

Returns
the value as a long

◆ YAML_read_key< size_t >()

template size_t dodo::common::YAML_read_key< size_t > ( const YAML::Node &  ,
const std::string &   
)

Instantiate template YAML_read_key for size_t.

Returns
the value as a size_t

◆ YAML_read_key< std::string >()

template std::string dodo::common::YAML_read_key< std::string > ( const YAML::Node &  ,
const std::string &   
)

Instantiate template YAML_read_key for std::string.

Returns
the value as a std::string

◆ YAML_read_key< uint16_t >()

template uint16_t dodo::common::YAML_read_key< uint16_t > ( const YAML::Node &  ,
const std::string &   
)

Instantiate template YAML_read_key for uint16_t.

Returns
the value as a uint16_t

◆ YAML_read_key< unsigned int >()

template unsigned int dodo::common::YAML_read_key< unsigned int > ( const YAML::Node &  ,
const std::string &   
)

Instantiate template YAML_read_key for unsigned int.

Returns
the value as an unsigned int

◆ YAML_read_key_default()

template<typename T >
T dodo::common::YAML_read_key_default ( const YAML::Node &  node,
const std::string &  key,
const T &  default_value 
)

Template function to check existence and read YAML values of arbitrary type.

The key-value pair must must exist under node.

Parameters
nodeThe YAML::Node containing the key-value pair
keyThe YAML::Node name.
default_valueThe default value to assign when the key is missing
Returns
the value of type T

Definition at line 331 of file util.cpp.

◆ YAML_read_key_default< bool >()

template bool dodo::common::YAML_read_key_default< bool > ( const YAML::Node &  ,
const std::string &  ,
const bool &   
)

Instantiate template YAML_read_key for bool.

Returns
the value as a bool

Referenced by dodo::network::TCPListener::Params::Params().

Here is the caller graph for this function:

◆ YAML_read_key_default< double >()

template double dodo::common::YAML_read_key_default< double > ( const YAML::Node &  ,
const std::string &  ,
const double &   
)

Instantiate template YAML_read_key for double.

Returns
the value as a double

Referenced by dodo::network::TCPListener::Params::Params().

Here is the caller graph for this function:

◆ YAML_read_key_default< int >()

template int dodo::common::YAML_read_key_default< int > ( const YAML::Node &  ,
const std::string &  ,
const int &   
)

Instantiate template YAML_read_key for int.

Returns
the value as an int

Referenced by dodo::network::TCPListener::Params::Params().

Here is the caller graph for this function:

◆ YAML_read_key_default< long >()

template long dodo::common::YAML_read_key_default< long > ( const YAML::Node &  ,
const std::string &  ,
const long &   
)

Instantiate template YAML_read_key for long.

Returns
the value as a long

◆ YAML_read_key_default< size_t >()

template size_t dodo::common::YAML_read_key_default< size_t > ( const YAML::Node &  ,
const std::string &  ,
const size_t &   
)

Instantiate template YAML_read_key for size_t.

Returns
the value as a size_t

Referenced by dodo::network::TCPListener::Params::Params().

Here is the caller graph for this function:

◆ YAML_read_key_default< std::string >()

template std::string dodo::common::YAML_read_key_default< std::string > ( const YAML::Node &  ,
const std::string &  ,
const std::string &   
)

Instantiate template YAML_read_key for std::string.

Returns
the value as a std::string

◆ YAML_read_key_default< unsigned int >()

template unsigned int dodo::common::YAML_read_key_default< unsigned int > ( const YAML::Node &  ,
const std::string &  ,
const unsigned int &   
)

Instantiate template YAML_read_key for unsigned int.

Returns
the value as an unsigned int