UI::Util Namespace Reference

Namespace for ui-utilcpp. More...

## Namespaces

namespace | Http
| Namespace for all HTTP related code.
namespace | Sys
| Namespace for system/library calls.

## Data Structures

class | Cap
| C++ encapsulation for libcap's 'cap_t'. More...
class | CapScope
| Helper to enable effective capabilities safely for a scope. More...
class | Exception
| Generic exception class for namespace UI::Util. More...
class | CodeException
| Adding code facility to Exception. More...
class | PIDFile
| Write and keep pid file during lifetime. More...
class | AutoRemoveFile
| Guarantuee to run "remove(2)" on path in destructor. More...
class | CFileStream
| Simple exception-save FILE abstraction. More...
class | FileDescriptor
| File Descriptor Holder Class. More...
class | File
| File representation. More...
class | PosixFileMutex
| Mutex For Posix Advisory File Locking. More...
class | BSDFileMutex
| Mutex For BSD Advisory File Locking. More...
class | FsInfo
| File system information abstraction. More...
class | CLOption
| Most general class for a command line option. Use this class for a option without argument. More...
class | CLOptionArg
| Class representing a command line option with mandatory argument. More...
class | CLOptionOptArg
| Class representing a command line option with optional argument. More...
class | GetOpt
| Abstraction class for GNU getopt_long(3). More...
class | auto_base
| Base adapter class to hold a pointer that can't be freed using standard "delete". More...
class | auto_cvec
| Control freeing of C vector pointers via delete[]. More...
class | auto_free
| Control freeing of memory via std::free. More...
class | MutexLock
| Mutex Lock class; an object of this class will guard a scope. More...
class | RealPath
| Get canonical absolute path name (mutex-protected). More...
class | EUIDSwap
| Helper class to get a scope executed with some other effective uid. More...
class | PosixRegex
| Wrapper class for POSIX.2 regex functions. More...
class | QuotaInfo
| Quota information abstraction. More...
class | FQuotaInfo
| Variant of QuotaInfo from any file in FS. More...
class | Conversion
| Holder class for conversion results. More...
class | StdFreeConversion
| Conversion w/ std::free call on the supplied C-String. More...
class | StringConversion
| Conversion w/ std::string holder. More...
class | Converter
| Factory for Conversions. More...
class | LibRecodeConverter
| Converter based on librecode (see there). More...
class | IConvConverter
| Character set converter, using libc's "iconv" directly. More...
class | URLConverter
| Converter for URL-Encoding. More...
class | Cpp2Base64Converter
| Converter for Base64 encoding, using CommonCpp functions. More...
class | Rfc2047Converter
| Converter charEncoding -> rfc2047 (MIME). More...
class | Rfc3490Utf8Converter
| Converter UTF-8 <-> rfc3490 (IDN). More...
class | IdnEMailConverter
| Converter from an-char-encoded string with randomly placed email-adresse -> the same string w/ domain parts magically replaced with IDN-encoding. More...
class | Recoder
| Meta converter class: converter switcher and generic interface. More...
class | RecoderCache
| This can be used to cache recoder instances. More...
class | Socket
| Socket abstraction. More...
class | INetSocket
| INet Socket. More...
class | UnixSocket
| Unix Socket. More...
class | FDTypeBuf
| Template IO stream buffer for all file descriptors types. More...
class | FDTypeStream
| IO stream for file descriptors. More...
class | SocketPair
| socketpair(2) abstraction. More...
class | SysLogMono
| C++ Abstraction of syslog(3) for mono threaded applications. More...
class | SysLogMonoSingleton
| Singleton class holding one SysLogMono object. More...
class | AlphaNumericKey
| Class using boost::mt19937 as random engine. More...
class | CStrArray
| Helper to construct/use C-String arrays ("char **"), occasionally needed by some c functions. More...
class | XConversion
| a class to convert between different systems. More...
class | ProcessThread
| Simple encapsulation for any process based threading system. More...
class | ForkThread
| Simple encapsulation of fork(2) using common thread syntax. More...
class | RealTimeStamp
| RealTimeStamp class encapsulating "gettimeofday". More...
class | ScopeRealTime
| Log time used for a scope. More...

## Typedefs

typedef std::ios_base &(* | StreamModifier) (std::ios_base &)
| Stream modifier function (is there a std:: type??).
Shortcuts for often used types and convenience stream operators.
typedef std::vector< std::string > | StrVec
typedef std::list< std::string > | StrList
typedef std::map< std::string, std::string > | StrMap

## Functions

std::string | getenv (std::string const &name)
| Like getenv(3), but returns empty string when env is missing.
std::string | guessConfFile (std::string const &id, std::string const &suffix=".conf")
| Guess a config file (/etc/id.conf or ~/.id.conf).
void | fileCopy (std::string const &src, std::string const &dest)
| File copy from file names.
bool | fileExists (std::string const &fName)
| Check if a file exists using stat(2).
time_t | fileModificationTime (std::string const &path)
| Get the modification time for a file.
std::string | realpath (std::string const &path)
| Get canonical absolute path name.
template<typename ElType>
void | delAnySeqContainer (std::vector< ElType * > &l) throw ()
| Run "delete" on all vector elements, and clear the vector. *>.
template<typename ElType>
void | freeAnySeqContainer (std::vector< ElType * > &l) throw ()
| Run "std::free" on all vector elements, and clear the vector. *>.
template<typename ElType>
void | delStringMap (std::map< std::string, ElType * > &l) throw ()
| Utility to delete any map<string, anytype>.
template<typename K>
std::list< K >::iterator | reverse_lfind (std::list< K > &l, K const &key)
| Reverse "find" for lists.
std::string | strVec2Str (StrVec const &strVec, std::string const &sep)
std::string | strerror (int const &errNo=errno)
| Save wrapper for ::strerror. Always use this instead of ::strerror directly.
std::string | getlineCRLF (std::istream &s)
| Like std::getline, but also rips of trailing "CR" when line break was CRLF.
std::string | asciiCAPS (std::string const &in, bool const upper=true)
| ASCII CAPS converter for strings (using std::tolower|upper).
std::string & | str2Ascii (std::string &s)
| Replace all non-ASCII characters to '?' in string.
char * | strdup (char const *s)
| BSD style "strdup" implementation (is not C standard). See strdup(3).
StrVec | strtok (std::string const &s, std::string const &delim, std::string const &prefix, std::string const &postfix)
StrVec | strtoks (std::string const &s, std::string const &delims, std::string const &prefix, std::string const &postfix)
std::string | istream2String (std::istream &f, int const blockSize=1024)
| Istream-to-string converter.
std::string | md5sum (std::string const &data)
template<typename T>
T | Default ()
| Defaults values for some types via template specialization.
template<>
int | Default< int > ()
| Default-Specialising.
template<>
float | Default< float > ()
| Default-Specialising.
template<>
double | Default< double > ()
| Default-Specialising.
template<>
long int | Default< long int > ()
| Default-Specialising.
template<>
bool | Default< bool > ()
| Default-Specialising.
template<>
std::string | Default< std::string > ()
| Default-Specialising.
template<typename out_type>
std::pair< out_type, bool > | eato (std::string const &t, StreamModifier const m=0)
| String-to-anything converter with error handling.
template<typename out_type>
out_type | ato (std::string const &t, StreamModifier const m=0)
| String-to-anything converter with default values on error.
template<typename in_type>
std::pair< std::string, bool > | etos (in_type const &t, StreamModifier const m=0, int w=0, char f=' ')
| Anything-to-string converter with error handling.
template<typename C>
C | strtok (std::string const &s, std::string const &delim=",", std::string const &prefix="", std::string const &postfix="")
| Generate any STL container of strings from a token string.
template<typename C>
C | strtoks (std::string const &s, std::string const &delims=",", std::string const &prefix="", std::string const &postfix="")
| Like strtok, but you can use a list of one-char delimiters (delims in strtoks) instead of a fixed delimiter string (delim in strtok).
template<typename C>
std::string | join (std::string const &expr, C const &list)
| join a list,vector,set, ... with the connector string
template<typename Byte>
std::vector< Byte > | istream2Vector (std::istream &f, int const blockSize=1024)
| Read any stream into any byte vector.
void | fd2DevNull (int const &fd, mode_t const &mode)
| Close fd and reopen on "/dev/null" using mode.
void | daemonize (bool closeStdFds=true, bool changeToRootdir=true, bool resetUMask=true)
| Use fork(2) to daemonize current program in a new session id.
long int | getTimeValSec ()
| Get "sec"-part from gettimeofday(2).
long int | getTimeValUSec ()
| Get "usec"-part from gettimeofday(2).
unsigned int | nanosleep (unsigned int seconds, long int nanoseconds)
| No Signal Sleep: Using POSIX nanosleep(2).
unsigned int | nssleep (unsigned int seconds)
| No Signal Seconds Sleep: Using POSIX nanosleep(2).
unsigned int | nsnsleep (long int nanoseconds)
| No Signal Nano Sleep.
std::ostream & | operator<< (std::ostream &os, RealTimeStamp const &rt)
| Streaming operator for RealTimeStamp.
Check whether token is in tokens.
bool | isToken (std::string const &token, StrVec const &tokensVec, int const match)
bool | isToken (std::string const &token, std::string const &tokens, std::string const &delim, int const match)
Mass string replace.
std::string & | strrpl (std::string &source, std::string const &token, std::string const &reptoken)
std::string | strrpl (std::string const &source, std::string const &token, std::string const &reptoken)
Anything-to-string converter with empty string on error. @see etos.
template<typename in_type>
std::string | tos (in_type const &t, StreamModifier const m=0, int w=0, char f=' ')
template<typename in_type>
std::string | tos (in_type const &t, int w, char f=' ')

## Generate arbitrary length pseudo random keys with alphanumeric ASCII characters.

std::string | genAlphaNumericKey (int len=8)
| For compatibility only.

## Detailed Description

Namespace for ui-utilcpp.

## Function Documentation

## ato()

template<typename out_type>

out_type UI::Util::ato | ( | std::string const & | t,
|  | StreamModifier const | m = 0 )

String-to-anything converter with default values on error.

See also
  eato.

Examples
  Regex.cpp.

References Default(), and eato().

Referenced by UI::Util::CmdLine::CmdLine::run(), and UI::Util::Http::StatusLine::StatusLine().

## daemonize()

void UI::Util::daemonize | ( | bool | closeStdFds = true,
|  | bool | changeToRootdir = true,
|  | bool | resetUMask = true )

Use fork(2) to daemonize current program in a new session id.

Parameters

  closeStdFds | Whether to close fd 0,1 and 2 (and re-open them to /dev/null). This is recommended for all production daemons.
  changeToRootdir | Will do a chdir(2) to "/". This is to avoid all errors due to a later remove/change of the cwd.
  resetUMask | Resets umask to "0".

See also
  fork(2), setsid(2), chdir(2), umask(2).

References fd2DevNull().

## delAnySeqContainer()

template<typename ElType>

void UI::Util::delAnySeqContainer | ( | std::vector< ElType * > & | l | ) |
throw | ( | ) |  |  |

Run "delete" on all vector elements, and clear the vector. *>.

I.e. empty whole vector && call destructor for each ElType * - element.

Referenced by UI::Util::GetOpt::~GetOpt().

## delStringMap()

template<typename ElType>

void UI::Util::delStringMap | ( | std::map< std::string, ElType * > & | l | ) |
throw | ( | ) |  |  |

Utility to delete any map<string, anytype>.

I.e. empty whole map && call destuctor for each ElType * - element.

## eato()

template<typename out_type>

std::pair< out_type, bool > UI::Util::eato | ( | std::string const & | t,
|  | StreamModifier const | m = 0 )

String-to-anything converter with error handling.

Parameters

  t | String to convert.
  m | Optional stream modifier like std::hex.

Returns
  Pair with conversion result and success; if false, result's value is undefinded.

Referenced by ato().

## etos()

template<typename in_type>

std::pair< std::string, bool > UI::Util::etos | ( | in_type const & | t,
|  | StreamModifier const | m = 0,
|  | int | w = 0,
|  | char | f = ' ' )

Anything-to-string converter with error handling.

Parameters

  t | Value to convert.
  m | Optional stream modifier like std::hex.
  w | Argument for std::setw() modifier.
  f | Argument for std::setfill() modifier.

Returns
  Pair with conversion result and success; if false, result's value is undefinded.

## fileExists()

bool UI::Util::fileExists | ( | std::string const & | fName | ) |

Check if a file exists using stat(2).

Parameters

  fName | File name to check.

Returns
  true, if file exists, else false.

## fileModificationTime()

time_t UI::Util::fileModificationTime | ( | std::string const & | path | ) |

Get the modification time for a file.

Parameters

  path | File to check.

Returns
  time of last modification.

## freeAnySeqContainer()

template<typename ElType>

void UI::Util::freeAnySeqContainer | ( | std::vector< ElType * > & | l | ) |
throw | ( | ) |  |  |

Run "std::free" on all vector elements, and clear the vector. *>.

I.e. empty whole vector && call destructor for each ElType * - element.

## istream2String()

std::string UI::Util::istream2String | ( | std::istream & | f,
|  | int const | blockSize = 1024 )

Istream-to-string converter.

Note
  Works for "binary"-files as well, but istream2Vector might be a better choice.

## istream2Vector()

template<typename Byte>

std::vector< Byte > UI::Util::istream2Vector | ( | std::istream & | f,
|  | int const | blockSize = 1024 )

Read any stream into any byte vector.

Note
  std::istream::read() is not const, so the stream reference can't be const.

## join()

template<typename C>

std::string UI::Util::join | ( | std::string const & | expr,
|  | C const & | list )

join a list,vector,set, ... with the connector string

e.g. the string list 'a', 'b', 'c', 'd' joined with ',' will return 'a,b,c,d'

As C needs to supply a C::const_iterator C.begin() C.end() and ++opertator e.g. std::vector<std::string>

## nanosleep()

unsigned int UI::Util::nanosleep | ( | unsigned int | seconds,
|  | long int | nanoseconds )

No Signal Sleep: Using POSIX nanosleep(2).

See also
  nanosleep(2), POSIX.1.

Returns
  Return values of POSIX nanosleep(2) call (0 for no error).

Referenced by nsnsleep(), and nssleep().

## nsnsleep()

unsigned int UI::Util::nsnsleep | ( | long int | nanoseconds | ) |

No Signal Nano Sleep.

See also
  nanosleep.

References nanosleep().

## nssleep()

unsigned int UI::Util::nssleep | ( | unsigned int | seconds | ) |

No Signal Seconds Sleep: Using POSIX nanosleep(2).

You may use this as drop-in replacement for sleep(3) if you do not want any signals to be raised.

See also
  nanosleep.

Examples
  Threads.cpp.

References nanosleep().

## realpath()

std::string UI::Util::realpath | ( | std::string const & | path | ) |

Get canonical absolute path name.

This effectively works like realpath(3), wrapped up for C++, w/ exception error handling.

See also
  realpath(3)

Bug
  realpath(3) is "broken by design". We need to create a char buffer of size MAX_PATH each time we call this function.

## reverse_lfind()

template<typename K>

std::list< K >::iterator UI::Util::reverse_lfind | ( | std::list< K > & | l,
|  | K const & | key )

Reverse "find" for lists.

Note
  Why doesn't "find(list.rbegin(), list.rend(), key)" work?

Parameters

  l | The list to search.
  key | The key to find.

Returns
  Iterator to found key, or l.end() if not found.

## strtok()

template<typename C>

C UI::Util::strtok | ( | std::string const & | s,
|  | std::string const & | delim = ",",
|  | std::string const & | prefix = "",
|  | std::string const & | postfix = "" )

Generate any STL container of strings from a token string.

Note
  There is a StrVec shortcut for this template. Doxygen seems to show only its syntax.

See also
  Text.hpp source header for the template syntax.