ccuty  1.0
 All Classes Namespaces Files Functions Enumerations Macros Pages
Classes | Functions
ccuty Namespace Reference

C++ Utilities. More...

Classes

class  Socket
 TCP/IP or UDP/Datagram socket. More...
 
class  ServerSocket
 TCP/IP server socket. More...
 
class  SocketBuffer
 Preserves record boundaries when exchanging messages between connected TCP/IP sockets. More...
 
struct  strdelim
 Delimiters for strsplit(), regsplit(), strtoken() and strtrim(). More...
 
class  stringset
 Set of strings, allows converting strings to numbers. More...
 
class  istringset
 Indexed set of strings, allows converting strings to numbers and vice-versa. More...
 
class  MacDesktop
 Class for managing the MacOSX desktop. More...
 

Functions

std::string basename (const std::string &path, bool with_extension=true)
 Returns filename component of path. More...
 
std::string dirname (const std::string &path)
 Returns directory component of path.
 
std::string dirslashed (const std::string &path)
 Adds a final / to a path if not already the case.
 
std::string extname (const std::string &path, bool with_dot=true)
 Returns extension of path. More...
 
std::string change_extname (const std::string &path, const std::string &ext)
 Changes the extension of the pathname (ext must start with a dot or be empty).
 
std::string join_paths (const std::string &left, const std::string &right)
 Creates a new path from 2 paths.
 
constexpr unsigned long long strid (const char *str, int last=32767, unsigned long long id=0)
 Returns a numeric ID from last characters that can be used in switch blocks. More...
 
unsigned long long strid (const std::string &str, int last=32767)
 Returns a numeric ID from last characters that can be used in switch blocks. More...
 
size_t strsplit (std::vector< std::string > &tokens, const std::string &str, ccuty::strdelim delimiters, int limit=0)
 Splits a string into a vector of tokens. More...
 
size_t strsplit (std::string &left, std::string &right, const std::string &str, ccuty::strdelim delimiters)
 Splits a string into two tokens. More...
 
size_t regsplit (std::vector< std::string > &tokens, const std::string &str, ccuty::strdelim delimiters, int limit=0)
 Splits a string into a vector of tokens using a regex. More...
 
bool strtoken (std::string &token, const std::string &str, ccuty::strdelim &delimiters)
 Retrieves the next token in a string. More...
 
void strtrim (std::string &str, ccuty::strdelim toremove)
 Removes beginning and trailing characters. More...
 
void strtrim (std::string &str)
 Removes beginning and trailing whitespaces.
 
void unquote (std::string &str)
 Removes beginning/trailing quotes and whitespaces.
 
std::string trimmed (const std::string &str)
 Returns a copy without beginning and trailing spaces.
 
std::string unquoted (const std::string &str)
 Returns a copy without beginning/trailing quotes and whitespaces.
 
int icompare (const std::string &a, const std::string &b)
 Compares two ASCII strings ignoring case (relies on tolower(int)).
 
bool iequal (const std::string &str1, const std::string &str2)
 Compares two ASCII strings ignoring case (relies on tolower(int)).
 
void lowerize (std::string &str)
 Lowercases an ASCII string (relies on tolower(int)).
 
void upperize (std::string &str)
 Uppercases an ASCII string (relies on tolower(int)).
 
std::string tolower (const std::string &str)
 Returns an lowercased copy of an ASCII string (relies on toupper(int)).
 
std::string toupper (const std::string &str)
 Returns an uppercased copy of an ASCII string (relies on tolower(int)).
 
bool from_string (bool &var, const std::string &str)
 Converts string to bool (returns true if the value could be successfully converted).
 
bool from_string (long &var, const std::string &str)
 Converts string to long (returns true if the value could be successfully converted).
 
bool from_string (unsigned long &var, const std::string &str)
 Converts string to unsigned long (returns true if the value could be successfully converted).
 
bool from_string (float &var, const std::string &str)
 Converts string to float (returns true if the value could be successfully converted).
 
bool from_string (double &var, const std::string &str)
 Converts string to double (returns true if the value could be successfully converted).
 
bool from_string (long double &var, const std::string &str)
 Converts string to long double (returns true if the value could be successfully converted).
 
int system (const std::string &command)
 Calls the C system() function.
 

Detailed Description

C++ Utilities.

Function Documentation

std::string ccuty::basename ( const std::string &  path,
bool  with_extension = true 
)
inline

Returns filename component of path.

Keeps filename extension if with_extension is true.

std::string ccuty::extname ( const std::string &  path,
bool  with_dot = true 
)
inline

Returns extension of path.

Keeps dot between extension if with_dot is true.

constexpr unsigned long long ccuty::strid ( const char *  str,
int  last = 32767,
unsigned long long  id = 0 
)
inline

Returns a numeric ID from last characters that can be used in switch blocks.

The returned ID relies on the last 8 characters of the string (or on the last 8 characters up to the last position if last is provided). Because this function is a constexpr, it is computed at compile time and can be used in switch blocks.

Example:

int getRGB(const std::string& color) {
switch (strid(color)) {
case strid("red"): return 0xf00;
case strid("green"): return 0x0f0;
case strid("blue"): return 0x00f;
default: return 0;
}
}
See Also
ccuty::strid(const std::string&) for C++ strings.
ccuty:stringset, which provides similar functionality but takes all characters into account.
unsigned long long ccuty::strid ( const std::string &  str,
int  last = 32767 
)
inline

Returns a numeric ID from last characters that can be used in switch blocks.

See Also
ccuty::strid(const char*)
size_t ccuty::strsplit ( std::vector< std::string > &  tokens,
const std::string &  str,
ccuty::strdelim  delimiters,
int  limit = 0 
)
inline

Splits a string into a vector of tokens.

Splits str into tokens that are stored in tokens. If greater than 0, limit is the maximum number of tokens and the last token contains the remaining text. delimiters specifies the delimiters. All whitespaces match if delimiters is the empty string. By default, the tokens are trimmed. See ccuty::strdelim for more options.

std::vector<std::string> tokens;
strsplit(tokens, "res = one + two", "=+"); // [1]
strsplit(tokens, "res one\ttwo", ""); // [2]
strsplit(tokens, "res one\ttwo", " "); // [3]
strsplit(tokens, "res = one + two", strdelim("=+").trim(false)); // [4]
strsplit(tokens, "res = one two", strdelim("=").spaces()); // [5]
  • [1] '=' and '+' are delimiters. Tokens are trimmed (the default). tokens = {"res", "one", "two"}
  • [2] all whitespaces are delimiters (including \t). tokens = {"res", "one", "two"}
  • [3] only ' ' is a delimiter. tokens = {"res", "one\ttwo"}
  • [4] same as [1] except that tokens are not trimmed. tokens = {" res ", " one ", " two" }
  • [5] '=' and whitespaces are delimiters. tokens = {"res", "one", "two"}
Returns
the number of tokens (i.e. tokens.size()).
See Also
ccuty::strdelim for more options.
size_t ccuty::strsplit ( std::string &  left,
std::string &  right,
const std::string &  str,
ccuty::strdelim  delimiters 
)
inline

Splits a string into two tokens.

Works as ccuty::strsplit() except that this function splits str into two tokens that are stored in left and right.

std::string left, right;
strsplit(left, right, " name : Bob", ":"); // [1]
strsplit(left, right, " name : Bob", strdelim(":").trim(false)); // [2]
  • [1] left contains "name" and right contains "Bob" (tokens are trimmed).
  • [2] left contains " name " and right contains " Bob" (tokens are not trimmed).
Returns
the number of tokens (e.g. 0, 1 or 2).
See Also
ccuty::strsplit() for examples and details.
size_t ccuty::regsplit ( std::vector< std::string > &  tokens,
const std::string &  str,
ccuty::strdelim  delimiters,
int  limit = 0 
)
inline

Splits a string into a vector of tokens using a regex.

Works as ccuty::strsplit() except that the delimiter is a regex.

std::vector<std::string> tokens;
regsplit(tokens, " res = one + two", "=|+"); // [1]
regsplit(tokens, " res = one + two", strdelim("=|+").trim(false)); // [2]
  • [1] tokens = "res" "one, "two" (tokens are trimmed). Note the | in "=|+" because the delimiter is a regex. - [2] _tokens_ = " res " " one " " two" (tokens are not trimmed).
Note
This function is slower than strsplit(). The strdelim::quotes() option has no effect.
See Also
ccuty::strsplit() for examples and details.
bool ccuty::strtoken ( std::string &  token,
const std::string &  str,
ccuty::strdelim delimiters 
)
inline

Retrieves the next token in a string.

Retrieves the next token token in string str. delimiters specifies the delimiters. All whitespaces match if delimiters is the empty string.

Returns
true if a token was found.
Note
delimiters is modified each time this function is called. strdelim::reset() must be called before using the same delimiters variable with another string.
See Also
ccuty::strdelim for more options.
void ccuty::strtrim ( std::string &  str,
ccuty::strdelim  toremove 
)
inline

Removes beginning and trailing characters.

Removes beginning and trailing characters that belongs to toremove. Removes all whitespaces if toremove is the empty string.

See Also
ccuty::strdelim for more options.