Motion Master
|
Classes | |
class | ThreadSafeQueue |
A simple thread-safe FIFO queue. More... | |
Enumerations | |
enum class | Endianness { LITTLE , BIG } |
Specifies the byte order used when converting a byte sequence to an integer. More... | |
Functions | |
std::vector< std::vector< uint8_t > > | splitIntoParts (const std::vector< uint8_t > &input, size_t partSize) |
Splits a byte vector into parts of a specified maximum size. More... | |
std::vector< uint8_t > | zipData (const std::string &filename, const std::vector< uint8_t > &data) |
Compresses a block of data into a ZIP archive containing a single file. More... | |
std::unordered_map< std::string, std::vector< uint8_t > > | unzipData (const std::vector< uint8_t > &data) |
Unzips a ZIP archive from an in-memory byte buffer. More... | |
std::vector< uint8_t > | readBinaryFile (const std::string &filename) |
Reads the entire contents of a binary file into a byte vector. More... | |
std::string | readTextFile (const std::string &filename) |
Reads the entire content of a text file into a std::string. More... | |
std::string | joinStrings (const std::vector< std::string > &list, const std::string &delimiter) |
Joins a list of strings into a single string with a delimiter. More... | |
std::string | formatMacAddress (const std::string &originalMacAddress) |
Formats a MAC address string to ensure each component is two digits and uppercase. More... | |
std::string | extractIpAddress (const std::string &socketAddress) |
Extracts the IP address from a socket address string. More... | |
unsigned short | extractPort (const std::string &socketAddress) |
Extracts the port number from a socket address string. More... | |
template<typename T > | |
T | parseHex (const std::string &s) |
Parses a hexadecimal string into an unsigned integer of type T. More... | |
template<typename T > | |
T | toInteger (const std::vector< std::uint8_t > &data, std::size_t offset=0, Endianness endian=Endianness::LITTLE) |
Converts a byte sequence to an integer of type T with optional offset and endianness. More... | |
template<typename T > | |
bool | stringViewToNumber (std::string_view sv, T &result) |
Converts a string_view to a numeric type. More... | |
template<typename T > | |
std::vector< uint8_t > | toBytes (T value, bool bigEndian=false) |
Converts a trivially copyable value to a byte vector. More... | |
template<typename T > | |
T | fromBytes (const std::vector< uint8_t > &bytes, bool bigEndian=false) |
Reconstructs a value of type T from a byte vector. More... | |
std::string | makeParameterId (int index, int subindex) |
Formats the given index and subindex into a parameter identifier string. More... | |
std::string | toHex (const std::vector< uint8_t > &data) |
Converts a vector of bytes to a space-separated hexadecimal string. More... | |
template<typename T > | |
std::string | toHex (T value) |
Converts an integer value to its hexadecimal string representation. More... | |
bool | endsWith (const std::string &str, const std::string &suffix) |
Checks whether a string ends with the given suffix. More... | |
size_t | countStringsStartingWith (const std::vector< std::string > &strings, const std::string &prefix) |
Count how many strings in the list start with the given prefix. More... | |
std::vector< std::string > | split (const std::string &input, char delimiter) |
Splits a string into substrings based on a delimiter character. More... | |
int64_t | currentTimeMillis () |
Gets the current system time in milliseconds since the Unix epoch. More... | |
template<typename T > | |
constexpr T | linearScale (T value, T inMin, T inMax, T outMin, T outMax) |
|
strong |
|
inline |
Count how many strings in the list start with the given prefix.
strings | The vector of strings to search. |
prefix | The prefix to match. |
|
inline |
Gets the current system time in milliseconds since the Unix epoch.
Uses std::chrono to obtain the current time point from the system clock, converts it to milliseconds duration since epoch, and returns the count as a 64-bit integer.
|
inline |
Checks whether a string ends with the given suffix.
str | The full string to check. |
suffix | The suffix to compare against. |
std::string mm::core::util::extractIpAddress | ( | const std::string & | socketAddress | ) |
Extracts the IP address from a socket address string.
Parses a string of the form "IP:port" and returns the IP portion.
socketAddress | A socket address in the format "IP:port". |
std::invalid_argument | If the input string does not contain a colon. |
unsigned short mm::core::util::extractPort | ( | const std::string & | socketAddress | ) |
Extracts the port number from a socket address string.
Parses a string of the form "IP:port" and returns the port portion as an unsigned short.
socketAddress | A socket address in the format "IP:port". |
std::invalid_argument | If the input string does not contain a valid port. |
std::string mm::core::util::formatMacAddress | ( | const std::string & | originalMacAddress | ) |
Formats a MAC address string to ensure each component is two digits and uppercase.
This function takes a MAC address string in formats such as "a-b-c-d-e-f" or "a:b:c:d:e:f", and returns a standardized format like "0A:0B:0C:0D:0E:0F". It ensures that each component is two characters long (adding leading zeros if necessary), uses ':' as the delimiter, and converts all letters to uppercase.
originalMacAddress | The input MAC address string, using either ':' or '-' as delimiter. |
T mm::core::util::fromBytes | ( | const std::vector< uint8_t > & | bytes, |
bool | bigEndian = false |
||
) |
Reconstructs a value of type T from a byte vector.
Deserializes a value of trivially copyable type T from a std::vector<uint8_t>
containing its memory representation. Optionally interprets the byte vector as big-endian.
T | The type of the value to reconstruct. Must be trivially copyable. |
bytes | The byte vector representing the serialized value. |
bigEndian | If true, the byte vector is interpreted as big-endian. If false (default), little-endian order is assumed. |
std::invalid_argument | If the size of the byte vector does not match sizeof(T). |
toBytes<T>
). Be cautious with types containing padding or platform-specific layout. std::string mm::core::util::joinStrings | ( | const std::vector< std::string > & | list, |
const std::string & | delimiter | ||
) |
Joins a list of strings into a single string with a delimiter.
Concatenates all elements in the given vector of strings, inserting the specified delimiter between each element. If the list is empty, returns an empty string.
list | The vector of strings to join. |
delimiter | The delimiter to insert between each string. |
|
inlineconstexpr |
Maps a value from one range to another using linear interpolation.
T | Type of input and output values (int, float, double, etc.) |
value | Input value to be mapped |
inMin | Lower bound of input range |
inMax | Upper bound of input range |
outMin | Lower bound of output range |
outMax | Upper bound of output range |
|
inline |
Formats the given index and subindex into a parameter identifier string.
This function takes an index and a subindex and formats them into a string of the form "0xINDEX:SUBINDEX". The index is formatted as a 4-digit hexadecimal number, and the subindex is formatted as a 2-digit hexadecimal number.
index | The 16-bit index value (e.g., 0x2030). |
subindex | The 8-bit subindex value (e.g., 0x01). |
T mm::core::util::parseHex | ( | const std::string & | s | ) |
Parses a hexadecimal string into an unsigned integer of type T.
This function converts a hex string (e.g., "0x1A3F" or "1A3F") to an unsigned integer of the specified type T. It supports any unsigned integer type such as uint16_t, uint32_t, uint64_t, etc.
The function uses std::stoul
for types up to the size of unsigned long
, and std::stoull
for larger types (e.g., uint64_t
).
T | The unsigned integer type to parse to. Must be an unsigned integral type. |
s | The hexadecimal string to parse. |
std::invalid_argument | if the string is not a valid hex number. |
std::out_of_range | if the parsed value is out of range for type T. |
std::vector< uint8_t > mm::core::util::readBinaryFile | ( | const std::string & | filename | ) |
Reads the entire contents of a binary file into a byte vector.
Opens the file in binary mode and reads all bytes into a std::vector<uint8_t>.
filePath | The path to the binary file to read. |
std::runtime_error | If the file cannot be opened. |
std::string mm::core::util::readTextFile | ( | const std::string & | filename | ) |
Reads the entire content of a text file into a std::string.
filePath | The path to the text file to be read. |
std::runtime_error | If the file cannot be opened. |
|
inline |
Splits a string into substrings based on a delimiter character.
Parses the input string, splitting it at each occurrence of the delimiter, and returns a vector containing the resulting substrings in order.
input | The input string to split. |
delimiter | The character used as the delimiter to split the string. |
std::vector< std::vector< uint8_t > > mm::core::util::splitIntoParts | ( | const std::vector< uint8_t > & | input, |
size_t | partSize | ||
) |
Splits a byte vector into parts of a specified maximum size.
input | The input vector of bytes. |
partSize | The maximum size (in bytes) for each part. |
bool mm::core::util::stringViewToNumber | ( | std::string_view | sv, |
T & | result | ||
) |
Converts a string_view to a numeric type.
This function uses std::from_chars to convert a given std::string_view into a numeric value of type T. The conversion is done without allocating memory, and the function returns true if the conversion succeeds, false otherwise.
T | The numeric type to convert the string_view to (e.g., int, long, double). |
sv | The string_view representing the numeric string to be converted. |
result | The variable where the result of the conversion will be stored. |
std::vector< uint8_t > mm::core::util::toBytes | ( | T | value, |
bool | bigEndian = false |
||
) |
Converts a trivially copyable value to a byte vector.
Serializes the given value into a std::vector<uint8_t>
by copying its memory representation. Optionally converts to big-endian byte order.
T | The type of the value to convert. Must be trivially copyable. |
value | The value to convert to bytes. |
bigEndian | If true, the resulting byte vector will be in big-endian order. If false (default), little-endian order is used. |
|
inline |
Converts a vector of bytes to a space-separated hexadecimal string.
Formats each byte in the input vector as a two-digit hexadecimal value with a 0x
prefix. Bytes are separated by a single space.
Example: a vector containing {0xAB, 0xCD} will be formatted as "0xAB 0xCD".
data | The vector of bytes to format. |
|
inline |
Converts an integer value to its hexadecimal string representation.
This function template formats any integral value as a hexadecimal string, including a 0x
prefix, uppercase letters, and leading zeros based on the size of the type.
T | An integral type (e.g., int, uint32_t). |
value | The integer value to convert. |
T mm::core::util::toInteger | ( | const std::vector< std::uint8_t > & | data, |
std::size_t | offset = 0 , |
||
Endianness | endian = Endianness::LITTLE |
||
) |
Converts a byte sequence to an integer of type T with optional offset and endianness.
This function reads up to sizeof(T) bytes from the given data vector starting at the specified offset and interprets them as an integer of type T. If there are fewer than sizeof(T) bytes available, the missing high-order bytes are zero-padded.
Supported integer types include signed and unsigned types like int16_t, uint32_t, etc.
T | The target integral type to convert to (e.g., int32_t, uint16_t). |
data | A vector of bytes to extract the integer value from. |
offset | The starting index in the vector (default is 0). |
endian | The byte order used for conversion (default is Endianness::Little). |
std::unordered_map< std::string, std::vector< uint8_t > > mm::core::util::unzipData | ( | const std::vector< uint8_t > & | data | ) |
Unzips a ZIP archive from an in-memory byte buffer.
This function opens a ZIP archive from the provided raw data buffer, extracts all files inside it, and returns a mapping from filenames to their respective uncompressed file data.
data | A vector of bytes containing the ZIP archive data. |
std::runtime_error | If the ZIP archive cannot be opened, is empty, contains invalid entries, or any error occurs during extraction. |
std::vector< uint8_t > mm::core::util::zipData | ( | const std::string & | filename, |
const std::vector< uint8_t > & | data | ||
) |
Compresses a block of data into a ZIP archive containing a single file.
This function creates an in-memory ZIP archive and adds a single entry (file) to it, containing the provided raw data. It returns the entire ZIP archive as a byte vector.
filename | The name of the file entry to store inside the ZIP archive. |
data | The raw data to be compressed and stored in the ZIP archive. |
std::runtime_error | If any step of the ZIP creation fails (e.g., entry open, write, or stream copy). |