CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
Config Class Reference

Parses and stores config values from custom file format. More...

#include <CCSDSUtils.h>

Public Types

using ConfigValue = std::variant< std::string, int, float, bool, std::vector< uint8_t > >
 

Public Member Functions

CCSDS::ResultBool load (const std::string &filename)
 Load config file.
 
template<typename T >
CCSDS::Result< T > get (const std::string &key) const
 Get value by key and type.
 
bool isKey (const std::string &key) const
 

Static Private Member Functions

static std::tuple< std::string, std::string, std::string > parseLine (const std::string &line)
 Parse a single line from config.
 
static CCSDS::ResultBuffer parseBytes (const std::string &valueStr)
 Parse string "[1,2,3]" into vector<uint8_t>
 

Private Attributes

std::unordered_map< std::string, ConfigValuevalues
 

Detailed Description

Parses and stores config values from custom file format.

Definition at line 145 of file CCSDSUtils.h.

Member Typedef Documentation

◆ ConfigValue

using Config::ConfigValue = std::variant<std::string, int, float, bool, std::vector<uint8_t> >

Definition at line 147 of file CCSDSUtils.h.

Member Function Documentation

◆ get()

template<typename T >
CCSDS::Result< T > Config::get ( const std::string &  key) const
inline

Get value by key and type.

Definition at line 154 of file CCSDSUtils.h.

154 {
155 auto it = values.find(key);
156 RET_IF_ERR_MSG(it == values.end(), CCSDS::ErrorCode::NO_DATA,"Config: No data found for key: " + key);
157 return std::get<T>(values.at(key));
158 }
#define RET_IF_ERR_MSG(condition, errorCode, message)
Macro to return an error with an error message if a condition is met.
std::unordered_map< std::string, ConfigValue > values
Definition CCSDSUtils.h:163
CCSDS::Result< T > get(const std::string &key) const
Get value by key and type.
Definition CCSDSUtils.h:154
@ NO_DATA
No data available.
Definition CCSDSResult.h:22
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isKey()

bool Config::isKey ( const std::string &  key) const

Definition at line 277 of file CCSDSUtils.cpp.

277 {
278 if (values.find(key) != values.end()) {
279 return true;
280 }
281 return false;
282}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ load()

CCSDS::ResultBool Config::load ( const std::string &  filename)

Load config file.

Definition at line 241 of file CCSDSUtils.cpp.

241 {
242 std::ifstream file(filename);
243 RET_IF_ERR_MSG(!file.is_open(),CCSDS::ErrorCode::CONFIG_FILE_ERROR, "Failed to open configuration file");
244
245 std::string line;
246 while (std::getline(file, line)) {
247 if (line.empty() || line.front() == '#') continue;
248
249 auto [key, type, valueStr] = parseLine(line);
250 RET_IF_ERR_MSG(key.empty() || type.empty(),CCSDS::ErrorCode::CONFIG_FILE_ERROR, "Failed to parse configuration file");
251 ConfigValue value;
252 if (type == "string") {
253 value = valueStr;
254 } else if (type == "int") {
255 int base = 10;
256 if (valueStr.size() > 2 && valueStr.substr(0, 2) == "0x") {
257 valueStr = valueStr.substr(2);
258 base = 16;
259 }
260 value = std::stoi(valueStr, nullptr, base);
261 } else if (type == "float") {
262 value = std::stof(valueStr);
263 } else if (type == "bool") {
264 value = (valueStr == "true" || valueStr == "1");
265 } else if (type == "bytes") {
266 ASSIGN_CP( value, parseBytes(valueStr) );
267 } else {
268 return CCSDS::Error{CCSDS::ErrorCode::CONFIG_FILE_ERROR, " unknown type: " + type};
269 }
270
271 values[key] = value;
272 }
273
274 return true;
275}
#define ASSIGN_CP(var, result)
Macro to assign a result value to a variable or return an error by copy.
Represents an error with both an error code and a message.
Definition CCSDSResult.h:43
std::variant< std::string, int, float, bool, std::vector< uint8_t > > ConfigValue
Definition CCSDSUtils.h:147
static CCSDS::ResultBuffer parseBytes(const std::string &valueStr)
Parse string "[1,2,3]" into vector<uint8_t>
static std::tuple< std::string, std::string, std::string > parseLine(const std::string &line)
Parse a single line from config.
@ CONFIG_FILE_ERROR
Definition CCSDSResult.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseBytes()

CCSDS::ResultBuffer Config::parseBytes ( const std::string &  valueStr)
staticprivate

Parse string "[1,2,3]" into vector<uint8_t>

Definition at line 301 of file CCSDSUtils.cpp.

301 {
302 std::vector<uint8_t> result;
303 RET_IF_ERR_MSG(valueStr.front() != '[' || valueStr.back() != ']', CCSDS::ErrorCode::CONFIG_FILE_ERROR, "Config: Invalid buffer formatting []");
304
305 std::string inner = valueStr.substr(1, valueStr.size() - 2);
306 std::stringstream ss(inner);
307 std::string token;
308
309 while (std::getline(ss, token, ',')) {
310 // Trim whitespace
311 token.erase(std::remove_if(token.begin(), token.end(), ::isspace), token.end());
312
313 try {
314 int base = 10;
315 if (token.size() > 2 && token.substr(0, 2) == "0x") {
316 token = token.substr(2);
317 base = 16;
318 }
319 result.push_back(static_cast<uint8_t>(std::stoi(token, nullptr, base)));
320 } catch (...) {
321 return CCSDS::Error{CCSDS::ErrorCode::CONFIG_FILE_ERROR, "Invalid byte value: " + token};
322 }
323 }
324 return result;
325}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseLine()

std::tuple< std::string, std::string, std::string > Config::parseLine ( const std::string &  line)
staticprivate

Parse a single line from config.

Definition at line 284 of file CCSDSUtils.cpp.

284 {
285 auto colonPos = line.find(':');
286 auto equalPos = line.find('=');
287
288 if (colonPos == std::string::npos || equalPos == std::string::npos || equalPos < colonPos)
289 return {"", "", ""};
290
291 std::string key = line.substr(0, colonPos);
292 std::string type = line.substr(colonPos + 1, equalPos - colonPos - 1);
293 std::string value = line.substr(equalPos + 1);
294
295 if (!value.empty() && value.front() == '"' && value.back() == '"')
296 value = value.substr(1, value.size() - 2);
297
298 return {key, type, value};
299}
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ values

std::unordered_map<std::string, ConfigValue> Config::values
private

Definition at line 163 of file CCSDSUtils.h.


The documentation for this class was generated from the following files: