|
21 | 21 | #include <cstdint> |
22 | 22 | #include <limits> |
23 | 23 | #include <cstring> |
| 24 | +#include <map> |
| 25 | +#include <string> |
| 26 | +#include <variant> |
| 27 | +#include <cassert> |
24 | 28 | #include "exceptions.h" |
25 | 29 |
|
26 | | -// Little-endian helpers reused across modules |
| 30 | +// Utility functions for little-endian number reading and writing. |
| 31 | + |
27 | 32 | inline void append_u32_le(std::vector<uint8_t>& out, uint32_t v) { |
28 | 33 | out.push_back(static_cast<uint8_t>(v & 0xFF)); |
29 | 34 | out.push_back(static_cast<uint8_t>((v >> 8) & 0xFF)); |
@@ -69,6 +74,8 @@ inline uint32_t read_u32_le(const std::vector<uint8_t>& in, size_t offset) { |
69 | 74 | (static_cast<uint32_t>(in[offset + 3]) << 24); |
70 | 75 | } |
71 | 76 |
|
| 77 | +// Utility functions for splitting and joining byte vectors. |
| 78 | + |
72 | 79 | struct SplitBytesPair { |
73 | 80 | std::vector<uint8_t> leading; |
74 | 81 | std::vector<uint8_t> trailing; |
@@ -171,3 +178,61 @@ inline SplitBytesPair SplitWithLengthPrefix(const std::vector<uint8_t>& bytes) { |
171 | 178 |
|
172 | 179 | return result; |
173 | 180 | } |
| 181 | + |
| 182 | +// Utility functions for creating an AttributesMap |
| 183 | + |
| 184 | +// Common alias for converted encoding attributes used across modules. |
| 185 | +// Numeric values are captured as int32_t. |
| 186 | +using AttributesMap = std::map<std::string, std::variant<int32_t, bool, std::string>>; |
| 187 | + |
| 188 | +inline const std::string& GetRequiredAttribute( |
| 189 | + const std::map<std::string, std::string>& attributes, |
| 190 | + const std::string& key) { |
| 191 | + auto it = attributes.find(key); |
| 192 | + if (it == attributes.end()) { |
| 193 | + throw InvalidInputException("Required encoding attribute [" + key + "] is missing"); |
| 194 | + } |
| 195 | + return it->second; |
| 196 | +} |
| 197 | + |
| 198 | +inline int32_t AddIntAttribute( |
| 199 | + AttributesMap& out, |
| 200 | + const std::map<std::string, std::string>& attributes, |
| 201 | + const std::string& key) { |
| 202 | + const std::string& value = GetRequiredAttribute(attributes, key); |
| 203 | + try { |
| 204 | + int32_t value_int = static_cast<int32_t>(std::stol(value)); |
| 205 | + assert(value_int >= 0); |
| 206 | + out[key] = value_int; |
| 207 | + return value_int; |
| 208 | + } catch (const std::exception& e) { |
| 209 | + throw InvalidInputException( |
| 210 | + "Failed to convert [" + key + "] with value [" + value + "] to int: " + e.what()); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +inline bool AddBoolAttribute( |
| 215 | + AttributesMap& out, |
| 216 | + const std::map<std::string, std::string>& attributes, |
| 217 | + const std::string& key) { |
| 218 | + const std::string& value = GetRequiredAttribute(attributes, key); |
| 219 | + if (value == "true") { |
| 220 | + out[key] = true; |
| 221 | + return true; |
| 222 | + } else if (value == "false") { |
| 223 | + out[key] = false; |
| 224 | + return false; |
| 225 | + } else { |
| 226 | + throw InvalidInputException( |
| 227 | + "Failed to convert [" + key + "] with value [" + value + "] to bool"); |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +inline std::string AddStringAttribute( |
| 232 | + AttributesMap& out, |
| 233 | + const std::map<std::string, std::string>& attributes, |
| 234 | + const std::string& key) { |
| 235 | + const std::string& value = GetRequiredAttribute(attributes, key); |
| 236 | + out[key] = value; |
| 237 | + return value; |
| 238 | +} |
0 commit comments