31 #ifndef HELPERFUNCTIONS_H_ 32 #define HELPERFUNCTIONS_H_ 34 #include "../Variable.h" 45 #include <unordered_set> 69 static int32_t
compareConstant(
const std::string &s1,
const std::string &s2);
143 static std::string
getTimeString(std::string format, int64_t time = 0);
164 static std::string
getUuid1(
bool useRandomNodeId =
false);
182 static inline std::string &
ltrim(std::string &s) {
183 s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
195 static inline std::string &
rtrim(std::string &s) {
196 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
208 static inline std::string &
trim(std::string &s) {
219 static inline std::string &
toLower(std::string &s) {
220 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
231 static inline std::string &
toUpper(std::string &s) {
232 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
243 if (s.empty())
return 0;
244 const unsigned char *pS = (
const unsigned char *)s.c_str();
246 while (*pS) len += (*pS++ & 0xc0) != 0x80;
258 static std::string
utf8Substring(
const std::string &s, uint32_t start, uint32_t length) {
260 if (length == 0)
return "";
261 uint32_t c, i, ix, q, min = (unsigned)std::string::npos, max = (
unsigned)std::string::npos;
262 for (q = 0, i = 0, ix = s.length(); i < ix; i++, q++) {
263 if (q == start) min = i;
264 if (q <= start + length || length == std::string::npos) max = i;
266 c = (
unsigned char)s[i];
267 if (c >= 0 && c <= 127) i += 0;
268 else if ((c & 0xE0) == 0xC0) i += 1;
269 else if ((c & 0xF0) == 0xE0) i += 2;
270 else if ((c & 0xF8) == 0xF0) i += 3;
273 if (q <= start + length || length == (
unsigned)std::string::npos) max = i;
274 if (min == (
unsigned)std::string::npos || max == (unsigned)std::string::npos)
return "";
275 return s.substr(min, max);
286 static std::string &
stringReplace(std::string &haystack,
const std::string &search,
const std::string &replace);
297 static std::string &
regexReplace(std::string &haystack,
const std::string &search,
const std::string &replace,
bool ignoreCase);
308 static std::pair<std::string, std::string>
splitFirst(std::string
string,
char delimiter);
319 static std::pair<std::string, std::string>
splitLast(std::string
string,
char delimiter);
329 static std::vector<std::string>
splitAll(std::string
string,
char delimiter);
340 return !(isalpha(c) || isdigit(c) || (c ==
'_') || (c ==
'-'));
352 static bool isAlphaNumeric(std::string &s, std::unordered_set<char> additionalCharacters = std::unordered_set<char>()) {
357 [&](
const char c) {
return !(isalpha(c) || isdigit(c) || additionalCharacters.find(c) != additionalCharacters.end()); }
370 static std::string
stripNonAlphaNumeric(
const std::string &s,
const std::unordered_set<char> &whitelist = std::unordered_set<char>());
415 static void memcpyBigEndian(
char *to,
const char *from,
const uint32_t &length);
424 static void memcpyBigEndian(uint8_t *to,
const uint8_t *from,
const uint32_t &length);
432 static void memcpyBigEndian(int32_t &to,
const std::vector<uint8_t> &from);
441 static void memcpyBigEndian(std::vector<uint8_t> &to,
const int32_t &from);
449 static void memcpyBigEndian(int64_t &to,
const std::vector<uint8_t> &from);
458 static void memcpyBigEndian(std::vector<uint8_t> &to,
const int64_t &from);
466 static std::vector<uint8_t>
hexToBin(
const std::string &data);
475 static std::string
getHexString(
const uint8_t *buffer, uint32_t size);
484 static std::string
getHexString(
const char *buffer, uint32_t size);
492 static std::string
getHexString(
const std::vector<char> &data);
500 static std::string
getHexString(
const std::string &data);
508 static std::string
getHexString(
const std::vector<uint8_t> &data);
516 static std::string
getHexString(
const std::vector<uint16_t> &data);
525 static std::string
getHexString(int32_t number, int32_t width = -1);
534 static std::string
getHexString(uint32_t number, int32_t width = -1);
543 static std::string
getHexString(int64_t number, int32_t width = -1);
552 static std::string
getHexString(uint64_t number, int32_t width = -1);
568 static std::vector<char>
getBinary(
const std::string &hexString);
577 static std::vector<uint8_t>
getUBinary(
const std::string &hexString);
588 static std::vector<uint8_t> &
getUBinary(
const std::string &hexString, uint32_t size, std::vector<uint8_t> &
binary);
597 static std::vector<uint8_t>
getUBinary(
const std::vector<uint8_t> &hexData);
613 static uid_t
userId(
const std::string &username);
621 static gid_t
groupId(
const std::string &groupname);
651 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);
656 static void *
memrchr(
const void *s,
int c,
size_t n);
661 static const std::array<int32_t, 23> _asciiToBinaryTable;
666 static const std::array<int32_t, 16> _binaryToASCIITable;
671 static bool isBigEndian();
static std::string & stringReplace(std::string &haystack, const std::string &search, const std::string &replace)
Replaces substrings within a string.
Definition: HelperFunctions.cpp:442
static std::pair< std::string, std::string > splitLast(std::string string, char delimiter)
Splits a string at the last occurrence of a delimiter.
Definition: HelperFunctions.cpp:470
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 ...
Definition: HelperFunctions.cpp:372
static std::string getTimeString(int64_t time=0)
Gets the current time as a string like "08/27/14 14:13:53.471".
Definition: HelperFunctions.cpp:91
static bool getBigEndian()
Returns the endianess of the system.
Definition: HelperFunctions.cpp:744
static std::vector< uint8_t > hexToBin(const std::string &data)
Converts a hex string to a byte array.
Definition: HelperFunctions.cpp:488
static PVariable xml2variable(const xml_node *node, bool &isDataNode)
Definition: HelperFunctions.cpp:239
static std::string getUuid1(bool useRandomNodeId=false)
Calculates a version 1 UUID (see RFC 4122) including a time stamp in 100-nanosecond intervals...
Definition: HelperFunctions.cpp:155
static std::pair< std::string, std::string > splitFirst(std::string string, char delimiter)
Splits a string at the first occurrence of a delimiter.
Definition: HelperFunctions.cpp:463
static gid_t groupId(const std::string &groupname)
Gets the GID of a group.
Definition: HelperFunctions.cpp:732
static std::string getHexString(const uint8_t *buffer, uint32_t size)
Converts a byte array to a hex string.
Definition: HelperFunctions.cpp:502
static std::string & rtrim(std::string &s)
Right trims a string.
Definition: HelperFunctions.h:195
static std::string & ltrim(std::string &s)
Left trims a string.
Definition: HelperFunctions.h:182
static std::string getUuid4()
Calculates a version 4 UUID (see RFC 4122).
Definition: HelperFunctions.cpp:200
Definition: BaseLib.cpp:34
static int64_t getLocalTime()
Gets the current local time as unix time stamp in milliseconds.
Definition: HelperFunctions.cpp:66
static int64_t getTimezoneOffset()
Definition: HelperFunctions.cpp:59
static bool isNotAlphaNumeric(char c)
Checks if a character is not alphanumeric ('_' and '-' are also regarded alphanumeric).
Definition: HelperFunctions.h:339
static bool isAlphaNumeric(std::string &s, std::unordered_set< char > additionalCharacters=std::unordered_set< char >())
Checks if a string is alphanumeric.
Definition: HelperFunctions.h:352
static char getHexChar(int32_t nibble)
Converts a nibble to a hex character.
Definition: HelperFunctions.cpp:497
static std::string & trim(std::string &s)
Trims a string.
Definition: HelperFunctions.h:208
static int32_t compareConstant(const std::string &s1, const std::string &s2)
Compares two strings in constant time.
Definition: HelperFunctions.cpp:48
static bool isShortCliCommand(const std::string &command)
Checks if a command is a short CLI command.
Definition: HelperFunctions.cpp:769
static int64_t getTimeSeconds()
Gets the current unix time stamp in seconds.
Definition: HelperFunctions.cpp:85
std::shared_ptr< Variable > PVariable
Definition: PhysicalInterfaceSettings.h:41
static std::string & regexReplace(std::string &haystack, const std::string &search, const std::string &replace, bool ignoreCase)
Replaces substrings within a string using regex.
Definition: HelperFunctions.cpp:454
This class provides functions to make your life easier.
Definition: HelperFunctions.h:55
static std::string getGNUTLSCertVerificationError(uint32_t errorCode)
Converts GNUTLS certificate verification error codes to human readable error messages.
Definition: HelperFunctions.cpp:758
static int32_t getRandomNumber(int32_t min, int32_t max)
Generates a random integer.
Definition: HelperFunctions.cpp:353
static std::string utf8Substring(const std::string &s, uint32_t start, uint32_t length)
Returns an UTF-8 substring.
Definition: HelperFunctions.h:258
static std::vector< uint8_t > getRandomBytes(uint32_t size)
Generates a random byte vector.
Definition: HelperFunctions.cpp:360
static std::string stripNonPrintable(const std::string &s)
Strips all non printable characters from a string.
Definition: HelperFunctions.cpp:230
static uid_t userId(const std::string &username)
Gets the UID of a user.
Definition: HelperFunctions.cpp:720
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)...
Definition: HelperFunctions.cpp:221
static std::string & toLower(std::string &s)
Converts all characters of a string to lower case.
Definition: HelperFunctions.h:219
static int64_t getTime()
Gets the current unix time stamp in milliseconds.
Definition: HelperFunctions.cpp:73
static size_t utf8StringSize(const std::string &s)
Returns the size of a UTF-8 string.
Definition: HelperFunctions.h:242
static std::vector< std::string > splitAll(std::string string, char delimiter)
Splits a string at all occurrences of a delimiter.
Definition: HelperFunctions.cpp:477
static int32_t getFileLastModifiedTime(const std::string &filename)
Gets the last modified time of a file.
static std::vector< uint8_t > getUBinary(const std::string &hexString)
Converts a hex string to an unsigned char vector.
Definition: HelperFunctions.cpp:648
static int64_t getTimeNanoseconds()
Gets the current unix time stamp in microseconds.
Definition: HelperFunctions.cpp:81
static void * memrchr(const void *s, int c, size_t n)
Reverse memchr().
Definition: HelperFunctions.cpp:804
This class represents root of the DOM hierarchy.
Definition: rapidxml.h:729
Class representing a node of XML document.
Definition: rapidxml.h:539
static std::string getBinaryString(const std::string &hexString)
Converts a hex string to a binary array of type string.
Definition: HelperFunctions.cpp:620
static std::vector< char > getBinary(const std::string &hexString)
Converts a hex string to a char vector.
Definition: HelperFunctions.cpp:592
static void variable2xml(xml_document *doc, xml_node *parentNode, const PVariable &variable)
Definition: HelperFunctions.cpp:320
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.
Definition: HelperFunctions.cpp:776
static int64_t getTimeMicroseconds()
Gets the current unix time stamp in microseconds.
Definition: HelperFunctions.cpp:77
static std::string getTimeUuid()
Calculates a Homegear-specific UUID including a time stamp in 100-nanosecond intervals.
Definition: HelperFunctions.cpp:130
static std::string & toUpper(std::string &s)
Converts all characters of a string to upper case.
Definition: HelperFunctions.h:231
HelperFunctions()
Dummy constructor for backwards compatibility.
Definition: HelperFunctions.cpp:45