libhomegear-base
0.7
Base library for Homegear and Homegear family modules.
|
This class provides functions to make your life easier. More...
#include <HelperFunctions.h>
Public Member Functions | |
HelperFunctions () | |
Dummy constructor for backwards compatibility. More... | |
Static Public Member Functions | |
static int32_t | compareConstant (const std::string &s1, const std::string &s2) |
Compares two strings in constant time. More... | |
static int64_t | getTimezoneOffset () |
static int64_t | getLocalTime () |
Gets the current local time as unix time stamp in milliseconds. More... | |
static int64_t | getTime () |
Gets the current unix time stamp in milliseconds. More... | |
static int64_t | getTimeMicroseconds () |
Gets the current unix time stamp in microseconds. More... | |
static int64_t | getTimeNanoseconds () |
Gets the current unix time stamp in microseconds. More... | |
static int64_t | getTimeSeconds () |
Gets the current unix time stamp in seconds. More... | |
static int32_t | getFileLastModifiedTime (const std::string &filename) |
Gets the last modified time of a file. More... | |
static std::string | getTimeString (int64_t time=0) |
Gets the current time as a string like "08/27/14 14:13:53.471". More... | |
static std::string | getTimeString (std::string format, int64_t time=0) |
Gets the current time as a string with custom formatting. More... | |
static std::string | getTimeUuid () |
Calculates a Homegear-specific UUID including a time stamp in 100-nanosecond intervals. More... | |
static std::string | getUuid1 (bool useRandomNodeId=false) |
Calculates a version 1 UUID (see RFC 4122) including a time stamp in 100-nanosecond intervals. More... | |
static std::string | getUuid4 () |
Calculates a version 4 UUID (see RFC 4122). More... | |
static std::string & | ltrim (std::string &s) |
Left trims a string. More... | |
static std::string & | rtrim (std::string &s) |
Right trims a string. More... | |
static std::string & | trim (std::string &s) |
Trims a string. More... | |
static std::string & | toLower (std::string &s) |
Converts all characters of a string to lower case. More... | |
static std::string & | toUpper (std::string &s) |
Converts all characters of a string to upper case. More... | |
static size_t | utf8StringSize (const std::string &s) |
Returns the size of a UTF-8 string. More... | |
static std::string | utf8Substring (const std::string &s, uint32_t start, uint32_t length) |
Returns an UTF-8 substring. More... | |
static std::string & | stringReplace (std::string &haystack, const std::string &search, const std::string &replace) |
Replaces substrings within a string. More... | |
static std::string & | regexReplace (std::string &haystack, const std::string &search, const std::string &replace, bool ignoreCase) |
Replaces substrings within a string using regex. More... | |
static std::pair< std::string, std::string > | splitFirst (std::string string, char delimiter) |
Splits a string at the first occurrence of a delimiter. More... | |
static std::pair< std::string, std::string > | splitLast (std::string string, char delimiter) |
Splits a string at the last occurrence of a delimiter. More... | |
static std::vector< std::string > | splitAll (std::string string, char delimiter) |
Splits a string at all occurrences of a delimiter. More... | |
static bool | isNotAlphaNumeric (char c) |
Checks if a character is not alphanumeric ('_' and '-' are also regarded alphanumeric). More... | |
static bool | isAlphaNumeric (std::string &s, std::unordered_set< char > additionalCharacters=std::unordered_set< char >()) |
Checks if a string is alphanumeric. More... | |
static std::string | stripNonAlphaNumeric (const std::string &s, const std::unordered_set< char > &whitelist=std::unordered_set< char >()) |
Strips all non alphanumeric characters from a string ('_' and '-' are also regarded alphanumeric). More... | |
static std::string | stripNonPrintable (const std::string &s) |
Strips all non printable characters from a string. More... | |
static PVariable | xml2variable (const xml_node *node, bool &isDataNode) |
static void | variable2xml (xml_document *doc, xml_node *parentNode, const PVariable &variable) |
static bool | getBigEndian () |
Returns the endianess of the system. More... | |
static int32_t | getRandomNumber (int32_t min, int32_t max) |
Generates a random integer. More... | |
static std::vector< uint8_t > | getRandomBytes (uint32_t size) |
Generates a random byte vector. More... | |
static void | memcpyBigEndian (char *to, const char *from, const uint32_t &length) |
Copies binary values from one memory location to another reversing the byte order when the system is little endian. More... | |
static void | memcpyBigEndian (uint8_t *to, const uint8_t *from, const uint32_t &length) |
Copies binary values from one memory location to another reversing the byte order when the system is little endian. More... | |
static void | memcpyBigEndian (int32_t &to, const std::vector< uint8_t > &from) |
Copies binary values from a vector to an integer reversing the byte order when the system is little endian. More... | |
static void | memcpyBigEndian (std::vector< uint8_t > &to, const int32_t &from) |
Copies binary values from an integer to a vector reversing the byte order when the system is little endian. More... | |
static void | memcpyBigEndian (int64_t &to, const std::vector< uint8_t > &from) |
Copies binary values from a vector to an integer reversing the byte order when the system is little endian. More... | |
static void | memcpyBigEndian (std::vector< uint8_t > &to, const int64_t &from) |
Copies binary values from an integer to a vector reversing the byte order when the system is little endian. More... | |
static std::vector< uint8_t > | hexToBin (const std::string &data) |
Converts a hex string to a byte array. More... | |
static std::string | getHexString (const uint8_t *buffer, uint32_t size) |
Converts a byte array to a hex string. More... | |
static std::string | getHexString (const char *buffer, uint32_t size) |
Converts a byte array to a hex string. More... | |
static std::string | getHexString (const std::vector< char > &data) |
Converts a byte array to a hex string. More... | |
static std::string | getHexString (const std::string &data) |
Converts a byte array to a hex string. More... | |
static std::string | getHexString (const std::vector< uint8_t > &data) |
Converts a byte array to a hex string. More... | |
static std::string | getHexString (const std::vector< uint16_t > &data) |
Converts an int16 array to a hex string. More... | |
static std::string | getHexString (int32_t number, int32_t width=-1) |
Converts an integer to a hex string. More... | |
static std::string | getHexString (uint32_t number, int32_t width=-1) |
Converts an integer to a hex string. More... | |
static std::string | getHexString (int64_t number, int32_t width=-1) |
Converts an integer to a hex string. More... | |
static std::string | getHexString (uint64_t number, int32_t width=-1) |
Converts an integer to a hex string. More... | |
static char | getHexChar (int32_t nibble) |
Converts a nibble to a hex character. More... | |
static std::vector< char > | getBinary (const std::string &hexString) |
Converts a hex string to a char vector. More... | |
static std::vector< uint8_t > | getUBinary (const std::string &hexString) |
Converts a hex string to an unsigned char vector. More... | |
static std::vector< uint8_t > & | getUBinary (const std::string &hexString, uint32_t size, std::vector< uint8_t > &binary) |
Converts a hex string to an unsigned char vector. More... | |
static std::vector< uint8_t > | getUBinary (const std::vector< uint8_t > &hexData) |
Converts an unsigned char vector filled with ASCII characters to an unsigned char vector. More... | |
static std::string | getBinaryString (const std::string &hexString) |
Converts a hex string to a binary array of type string. More... | |
static uid_t | userId (const std::string &username) |
Gets the UID of a user. More... | |
static gid_t | groupId (const std::string &groupname) |
Gets the GID of a group. More... | |
static std::string | getGNUTLSCertVerificationError (uint32_t errorCode) |
Converts GNUTLS certificate verification error codes to human readable error messages. More... | |
static bool | isShortCliCommand (const std::string &command) |
Checks if a command is a short CLI command. More... | |
static bool | checkCliCommand (const std::string &command, const std::string &longCommand, const std::string &shortCommand1, const std::string &shortCommand2, uint32_t minArgumentCount, std::vector< std::string > &arguments, bool &showHelp) |
Checks if a string contains a CLI command and checks and returns the arguments. More... | |
static void * | memrchr (const void *s, int c, size_t n) |
Reverse memchr(). More... | |
This class provides functions to make your life easier.
BaseLib::HelperFunctions::HelperFunctions | ( | ) |
Dummy constructor for backwards compatibility.
|
static |
Checks if a string contains a CLI command and checks and returns the arguments.
command | The CLI command. | |
longCommand | The long CLI command to search for at the beginning of command. | |
shortCommand1 | An optional short CLI command to search for at the beginning of command. | |
shortCommand2 | An optional second short CLI command to search for at the beginning of command. Set to empty string if not needed. | |
minArgumentCount | The minimum number of arguments. | |
[out] | arguments | A string vector which is filled with the arguments. |
[out] | showHelp | Set to true when help is requested. |
|
static |
Compares two strings in constant time.
s1 | |
s2 |
|
static |
Returns the endianess of the system.
|
static |
Converts a hex string to a char vector.
hexString | The string to convert. |
|
static |
Converts a hex string to a binary array of type string.
hexString | The hex string to convert. |
|
static |
Gets the last modified time of a file.
filename | The file to get the last modified time for. |
|
static |
Converts GNUTLS certificate verification error codes to human readable error messages.
errorCode | The GNUTLS certificate verification error code. |
|
static |
Converts a nibble to a hex character.
nibble | The nibble to convert. |
|
static |
Converts a byte array to a hex string.
buffer | The byte array to convert. |
size | The size of the buffer. |
|
static |
Converts a byte array to a hex string.
buffer | The byte array to convert. |
size | The size of the buffer. |
|
static |
Converts a byte array to a hex string.
data | The byte array to convert. |
|
static |
Converts a byte array to a hex string.
data | The byte array to convert. |
|
static |
Converts a byte array to a hex string.
data | The byte array to convert. |
|
static |
Converts an int16 array to a hex string.
data | The array to convert. |
|
static |
Converts an integer to a hex string.
number | The integer to convert. |
width | The minimal width of the hex string (default -1). If the hex string is smaller, it is prefixed with zeros. |
|
static |
Converts an integer to a hex string.
number | The integer to convert. |
width | The minimal width of the hex string (default -1). If the hex string is smaller, it is prefixed with zeros. |
|
static |
Converts an integer to a hex string.
number | The integer to convert. |
width | The minimal width of the hex string (default -1). If the hex string is smaller, it is prefixed with zeros. |
|
static |
Converts an integer to a hex string.
number | The integer to convert. |
width | The minimal width of the hex string (default -1). If the hex string is smaller, it is prefixed with zeros. |
|
static |
Gets the current local time as unix time stamp in milliseconds.
|
static |
Generates a random byte vector.
size | The number of bytes to generate. |
|
static |
Generates a random integer.
min | The minimal value of the random integer. |
max | The maximum value of the random integer. |
|
static |
Gets the current unix time stamp in milliseconds.
|
static |
Gets the current unix time stamp in microseconds.
|
static |
Gets the current unix time stamp in microseconds.
|
static |
Gets the current unix time stamp in seconds.
|
static |
Gets the current time as a string like "08/27/14 14:13:53.471".
time | The unix time stamp in milliseconds to get the time string for. If "0" the current time is returned. |
|
static |
Gets the current time as a string with custom formatting.
format | The format like described for strftime |
time | The unix time stamp in milliseconds to get the time string for. If "0" the current time is returned. |
|
static |
Calculates a Homegear-specific UUID including a time stamp in 100-nanosecond intervals.
|
static |
|
static |
Converts a hex string to an unsigned char vector.
hexString | The string to convert. |
|
static |
Converts a hex string to an unsigned char vector.
hexString | The string to convert. |
size | The maximum number of characters to convert. |
binary | The unsigned char vector to append the converted bytes to. Already existing elements will not be cleared. |
|
static |
Converts an unsigned char vector filled with ASCII characters to an unsigned char vector.
hexData | The vector with ASCII characters to convert. |
|
static |
Calculates a version 1 UUID (see RFC 4122) including a time stamp in 100-nanosecond intervals.
Calling this method is thread safe. Please note that UUIDs are only unique within one process. Collisions are impossible if the following conditions are met:
When the MAC address is not unique or a random node ID is used, collisions are not impossible but very improbable.
useRandomNodeId | When set to true a random node ID is generated with every start of the program (i. e. every initialization of the base library) and used instead of the computers MAC address. |
|
static |
Calculates a version 4 UUID (see RFC 4122).
Calling this method is thread safe. Please note that collisions are very improbable but not impossible:
|
static |
Gets the GID of a group.
groupname | The name of the group to get the GID for. |
|
static |
Converts a hex string to a byte array.
data | The hex string to convert. |
|
inlinestatic |
Checks if a string is alphanumeric.
s | The string to check. |
additionalCharacters | Additional characters to accept. |
|
inlinestatic |
Checks if a character is not alphanumeric ('_' and '-' are also regarded alphanumeric).
c | The character to check. |
|
static |
Checks if a command is a short CLI command.
command | The CLI command. |
|
inlinestatic |
|
static |
Copies binary values from one memory location to another reversing the byte order when the system is little endian.
[out] | to | The destination array. No memory is allocated, so make sure, the array is large enough. |
[in] | from | The source array. |
length | The number of bytes to copy. |
|
static |
Copies binary values from one memory location to another reversing the byte order when the system is little endian.
[out] | to | The destination array. No memory is allocated, so make sure, the array is large enough. |
[in] | from | The source array. |
length | The number of bytes to copy. |
|
static |
Copies binary values from a vector to an integer reversing the byte order when the system is little endian.
[out] | to | The destination integer. |
[in] | from | The source array. A length less than 4 bytes is allowed. |
|
static |
Copies binary values from an integer to a vector reversing the byte order when the system is little endian.
Note that leading zero bytes are trimmed, so 0x0000FFFF becomes {0xFF, 0xFF}.
[out] | to | The destination array. |
[in] | from | The source integer. |
|
static |
Copies binary values from a vector to an integer reversing the byte order when the system is little endian.
[out] | to | The destination integer. |
[in] | from | The source array. A length less than 4 bytes is allowed. |
|
static |
Copies binary values from an integer to a vector reversing the byte order when the system is little endian.
Note that leading zero bytes are trimmed, so 0x0000000000FFFFFF becomes {0xFF, 0xFF, 0xFF}.
[out] | to | The destination array. |
[in] | from | The source integer. |
|
static |
Reverse memchr().
Searches the initial n bytes of buffer s for the last occurrence of 'c'.
|
static |
Replaces substrings within a string using regex.
[in,out] | haystack | The string to search the substring in. |
regex | The regex to search. | |
replace | The substring to replace "search" with. | |
ignoreCase | Set to true, to ignore the case. |
|
inlinestatic |
|
static |
Splits a string at all occurrences of a delimiter.
string | The string to split. |
delimiter | The delimiter to split the string at. |
|
static |
Splits a string at the first occurrence of a delimiter.
string | The string to split. |
delimiter | The delimiter to split the string at. |
|
static |
Splits a string at the last occurrence of a delimiter.
string | The string to split. |
delimiter | The delimiter to split the string at. |
|
static |
Replaces substrings within a string.
[in,out] | haystack | The string to search the substring in. |
search | The substring to search. | |
replace | The substring to replace "search" with. |
|
static |
Strips all non alphanumeric characters from a string ('_' and '-' are also regarded alphanumeric).
s | The string to strip. |
whitelist | A list of characters not to be stripped. |
|
static |
Strips all non printable characters from a string.
s | The string to strip. |
|
inlinestatic |
Converts all characters of a string to lower case.
[in,out] | s | The string to convert all characters to lower case for. |
|
inlinestatic |
Converts all characters of a string to upper case.
[in,out] | s | The string to convert all characters to upper case for. |
|
inlinestatic |
|
static |
Gets the UID of a user.
username | The name of the user to get the UID for. |
|
inlinestatic |
Returns the size of a UTF-8 string.
This method is needed, because std::string::size() and std::string::length() return the size in bytes.
s | The string to get the size for. |
|
inlinestatic |
Returns an UTF-8 substring.
This method is needed, because std::string::substr() counts bytes and not characters.
s | The string to get the substring from. |
start | The start position of the substring. |
length | The substring size. |
|
static |
|
static |