/** * @file * * @author Max Lammers * @author Mamadou Babaei * * @section LICENSE * * Copyright (c) 2020 - 2025 SenseGlove * * @section DESCRIPTION * * String conversion, parsing and other utility stuff. Much more prominent in * C++. */ #pragma once #include #include #include #include #include "ExitCodes.hpp" #include "Platform.hpp" namespace SGConnect { class StringUtils; } /// Utility class to convert strings into usable values. class SGConnect::StringUtils { public: /// Split a std::string into a vector of std::strings by a delimiter static std::vector Split(const std::string& str, char delimiter); /// Convert a string into an integer value. static int32_t ToInt(const std::string& str); /// Convert a string into a decimal value. static float ToFloat(const std::string& str); static std::string ToString(EExitCode code); template static std::string FormatString(const std::string& format, const Args&... args) { int32_t size = std::snprintf(nullptr, 0, format.c_str(), args...) + 1;// extra space for '\0' if (size <= 0) { throw std::runtime_error("Error during formatting."); } std::unique_ptr buffer(new char[size]); std::snprintf(buffer.get(), size, format.c_str(), args...); return std::string{buffer.get(), buffer.get() + size - 1};// we don't want the '\0' inside } //Base64 Encoding - for firmware. Taken from https://github.com/tobiaslocker/base64 static std::string get_base64_chars(); static std::string CharV_to_base64(const std::vector& data); public: StringUtils() = delete; virtual ~StringUtils() = delete; };