avoid exceptions
This commit is contained in:
60
mission/memory/NvmParameterBase.cpp
Normal file
60
mission/memory/NvmParameterBase.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include <mission/memory/NvmParameterBase.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "fsfw/filesystem/HasFileSystemIF.h"
|
||||
#include "fsfw/serviceinterface/ServiceInterface.h"
|
||||
|
||||
NVMParameterBase::NVMParameterBase(std::string fullName) : fullName(fullName) {}
|
||||
|
||||
NVMParameterBase::NVMParameterBase() {}
|
||||
|
||||
ReturnValue_t NVMParameterBase::readJsonFile() {
|
||||
std::error_code e;
|
||||
if (std::filesystem::exists(fullName, e)) {
|
||||
// Read JSON file content into object
|
||||
std::ifstream i(fullName);
|
||||
try {
|
||||
i >> json;
|
||||
} catch (nlohmann::json::exception& e) {
|
||||
sif::warning << "Reading JSON file failed with error " << e.what() << std::endl;
|
||||
return returnvalue::FAILED;
|
||||
}
|
||||
return returnvalue::OK;
|
||||
}
|
||||
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
|
||||
}
|
||||
|
||||
ReturnValue_t NVMParameterBase::writeJsonFile() {
|
||||
std::ofstream o(fullName);
|
||||
try {
|
||||
o << std::setw(4) << json << std::endl;
|
||||
} catch (nlohmann::json::exception& e) {
|
||||
sif::warning << "Writing JSON file failed with error " << e.what() << std::endl;
|
||||
return returnvalue::FAILED;
|
||||
}
|
||||
return returnvalue::OK;
|
||||
}
|
||||
|
||||
void NVMParameterBase::setFullName(std::string fullName) { this->fullName = fullName; }
|
||||
|
||||
std::string NVMParameterBase::getFullName() const { return fullName; }
|
||||
|
||||
bool NVMParameterBase::getJsonFileExists() {
|
||||
std::error_code e;
|
||||
return std::filesystem::exists(fullName, e);
|
||||
}
|
||||
|
||||
void NVMParameterBase::printKeys() const {
|
||||
sif::info << "Printing keys for JSON file " << fullName << std::endl;
|
||||
for (const auto& key : keys) {
|
||||
sif::info << key << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void NVMParameterBase::print() const {
|
||||
sif::info << "Printing JSON file " << fullName << std::endl;
|
||||
for (const auto& key : keys) {
|
||||
sif::info << key << ": " << json[key] << std::endl;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user