12#include "../../Constants.h"
15#include "../parse_error.h"
16#include "../invalid_odb_error.h"
17#include "../../Text/Utf8Sanitizer.h"
19using namespace std::chrono;
25 std::ifstream infoFile;
33 auto message =
"\"misc\" directory does not exist: [" + path.string() +
"]";
37 auto infoFilePath = path /
"info";
38 if (!std::filesystem::exists(infoFilePath))
40 auto message =
"misc/info file does not exist: [" + infoFilePath.string() +
"]";
44 infoFile.open(infoFilePath, std::ios::in);
45 if (!infoFile.is_open())
47 auto message =
"unable to open misc/info file: [" + infoFilePath.string() +
"]";
51 while (std::getline(infoFile, line))
59 std::stringstream lineStream(line);
67 std::string attribute;
70 if (!std::getline(lineStream, attribute,
'='))
return false;
73 if (!std::getline(lineStream, value))
75 if (!attributeValueIsOptional(attribute))
77 logwarn(
"misc/info file: no value for non-optional attribute: " + attribute);
84 if (attribute ==
"PRODUCT_MODEL_NAME" ||
85 attribute ==
"product_model_name")
87 m_productModelName = value;
89 else if (attribute ==
"JOB_NAME" ||
90 attribute ==
"job_name")
94 else if (attribute ==
"odb_version_major" ||
95 attribute ==
"ODB_VERSION_MAJOR")
97 m_odbVersionMajor = value;
99 else if (attribute ==
"odb_version_minor" ||
100 attribute ==
"ODB_VERSION_MINOR")
102 m_odbVersionMinor = value;
104 else if (attribute ==
"odb_source" ||
105 attribute ==
"ODB_SOURCE")
109 else if (attribute ==
"creation_date" ||
110 attribute ==
"CREATION_DATE")
120 std::stringstream ss;
121 ss <<
"value: " << value <<
", parsed createdDate: " << createdDate;
125 else if (attribute ==
"save_date" ||
126 attribute ==
"SAVE_DATE")
134 std::stringstream ss;
135 ss <<
"value: " << value <<
", parsed saveDate: " << saveDate;
139 else if (attribute ==
"save_app" ||
140 attribute ==
"SAVE_APP")
144 else if (attribute ==
"save_user" ||
145 attribute ==
"SAVE_USER")
149 else if (attribute ==
"units" ||
150 attribute ==
"UNITS")
154 else if (attribute ==
"max_uid" ||
155 attribute ==
"MAX_UID")
157 m_maxUniqueId = std::stoi(value);
184 catch (std::exception& e)
199 os << PRODUCT_MODEL_NAME_KEY <<
"=" << m_productModelName << std::endl;
200 os << JOB_NAME_KEY <<
"=" << m_jobName << std::endl;
201 os << ODB_VERSION_MAJOR_KEY <<
"=" << m_odbVersionMajor << std::endl;
202 os << ODB_VERSION_MINOR_KEY <<
"=" << m_odbVersionMinor << std::endl;
203 os << ODB_SOURCE_KEY <<
"=" << m_odbSource << std::endl;
206 os << SAVE_APP_KEY <<
"=" << m_saveApp << std::endl;
207 os << SAVE_USER_KEY <<
"=" << m_saveUser << std::endl;
208 os << UNITS_KEY <<
"=" << m_units << std::endl;
209 os << MAX_UID_KEY <<
"=" << m_maxUniqueId << std::endl;
214 inline bool MiscInfoFile::attributeValueIsOptional(
const std::string& attribute)
216 for (
const auto& optionalAttribute : OPTIONAL_ATTRIBUTES)
218 if (attribute == optionalAttribute)
228 std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> pMiscInfoFileMessage(
new Odb::Lib::Protobuf::MiscInfoFile);
234 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(m_creationDateDate.time_since_epoch()).count();
235 pMiscInfoFileMessage->mutable_creationdatedate()->set_seconds(seconds);
236 pMiscInfoFileMessage->mutable_creationdatedate()->set_nanos(0);
237 seconds = std::chrono::duration_cast<std::chrono::seconds>(m_saveDate.time_since_epoch()).count();
238 pMiscInfoFileMessage->mutable_savedate()->set_seconds(seconds);
239 pMiscInfoFileMessage->mutable_savedate()->set_nanos(0);
243 pMiscInfoFileMessage->set_maxuniqueid(m_maxUniqueId);
244 return pMiscInfoFileMessage;
249 m_jobName = message.jobname();
250 m_productModelName = message.productmodelname();
251 m_odbVersionMajor = message.odbversionmajor();
252 m_odbVersionMinor = message.odbversionminor();
253 m_odbSource = message.odbsource();
255 m_creationDateDate = std::chrono::system_clock::time_point(std::chrono::seconds(message.creationdatedate().seconds()));
256 m_saveDate = std::chrono::system_clock::time_point(std::chrono::seconds(message.savedate().seconds()));
257 m_saveApp = message.saveapp();
258 m_saveUser = message.saveuser();
259 m_units = message.units();
260 m_maxUniqueId = message.maxuniqueid();
264 : m_maxUniqueId((unsigned int)-1)
270 return m_productModelName;
280 return m_odbVersionMajor;
285 return m_odbVersionMinor;
295 return m_creationDateDate;
320 return m_maxUniqueId;
#define logexception_msg(e, m)
static const auto COMMENT_TOKEN
bool Parse(std::filesystem::path path) override
std::string GetSaveUser() const
std::string GetOdbVersionMajor() const
std::chrono::system_clock::time_point GetCreationDate() const
void from_protobuf(const Odb::Lib::Protobuf::MiscInfoFile &message) override
std::string GetOdbSource() const
std::unique_ptr< Odb::Lib::Protobuf::MiscInfoFile > to_protobuf() const override
std::string GetSaveApp() const
std::string GetJobName() const
bool Save(std::ostream &os) override
std::string GetProductModelName() const
std::string GetUnits() const
std::chrono::system_clock::time_point GetSaveDate() const
unsigned int GetMaxUniqueId() const
std::string GetOdbVersionMinor() const
std::filesystem::path m_path
virtual bool Parse(std::filesystem::path path)
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.
std::string & str_trim(std::string &s)
std::string make_timestamp()
system_clock::time_point parse_timestamp(const std::string ×tamp, const std::string &format)
std::string toString(const std::string &message="") const