class for Ip and Mac

This commit is contained in:
Erik Frojdh
2019-04-02 15:24:25 +02:00
parent df2d67d90d
commit b198b50377
10 changed files with 204 additions and 138 deletions

View File

@ -2,6 +2,8 @@
#include <string>
#include <vector>
#include <cassert>
#include <cstring>
namespace sls {
@ -14,10 +16,18 @@ Still this is better than strcpy and a buffer overflow...
*/
template <size_t array_size>
void strcpy_safe(char (&destination)[array_size], const char *source) {
assert(array_size > strlen(source));
strncpy(destination, source, array_size-1);
destination[array_size - 1] = '\0';
}
template <size_t array_size>
void strcpy_safe(char (&destination)[array_size], const std::string& source) {
assert(array_size > source.size());
strncpy(destination, source.c_str(), array_size-1);
destination[array_size - 1] = '\0';
}
/*
Removes all occurrences of the specified char from a c string
Templated on array size to ensure no access after buffer limits.
@ -51,6 +61,12 @@ Concatenate strings using + if the strings are different
*/
std::string concatenateIfDifferent(const std::vector<std::string> &container);
/*
Concatenate vector of things with str method using + if the strings are different
*/
template<typename T>
std::string concatenateIfDifferent(const std::vector<T> &container);
/*
Convert an ip address string to a string in hex format. (removing dots)
*/