1#include "MiscInfoFile.h"
2#include "MiscInfoFile.h"
8#include "MiscInfoFile.h"
12#include "../../Constants.h"
15#include "../parse_error.h"
16#include "../invalid_odb_error.h"
17#include "../../Text/Utf8Sanitizer.h"
19using namespace std::chrono;
21namespace Odb::Lib::FileModel::Design
23 bool MiscInfoFile::Parse(std::filesystem::path path)
25 std::ifstream infoFile;
31 if (!OdbFile::Parse(path))
33 auto message =
"\"misc\" directory does not exist: [" + path.string() +
"]";
34 throw invalid_odb_error(message);
37 auto infoFilePath = path /
"info";
38 if (!std::filesystem::exists(infoFilePath))
40 auto message =
"misc/info file does not exist: [" + infoFilePath.string() +
"]";
41 throw invalid_odb_error(message);
44 infoFile.open(infoFilePath, std::ios::in);
45 if (!infoFile.is_open())
47 auto message =
"unable to open misc/info file: [" + infoFilePath.string() +
"]";
48 throw invalid_odb_error(message);
51 while (std::getline(infoFile, line))
56 Utils::str_trim(line);
59 std::stringstream lineStream(line);
60 if (line.find(Constants::COMMENT_TOKEN) == 0)
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);
81 Utils::str_trim(attribute);
82 Utils::str_trim(value);
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")
115 m_creationDateDate = Utils::parse_timestamp(value,
"%Y%m%d.%H%M%S");
119 auto createdDate = Utils::make_timestamp(m_creationDateDate);
120 std::stringstream ss;
121 ss <<
"value: " << value <<
", parsed createdDate: " << createdDate;
125 else if (attribute ==
"save_date" ||
126 attribute ==
"SAVE_DATE")
130 m_saveDate = Utils::parse_timestamp(value,
"%Y%m%d.%H%M%S");
132 auto saveDate = Utils::make_timestamp(m_saveDate);
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);
166 parse_info pi(m_path, line, attribute, lineNumber);
167 logwarn(pi.toString(
"unrecognized line in misc/info file:"));
175 catch (invalid_odb_error& ioe)
177 parse_info pi(m_path, line, lineNumber);
178 const auto m = pi.toString();
179 logexception_msg(ioe, m);
184 catch (std::exception& e)
186 parse_info pi(m_path, line, lineNumber);
187 const auto m = pi.toString();
188 logexception_msg(e, m);
197 bool MiscInfoFile::Save(std::ostream& os)
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;
204 os << CREATION_DATE_KEY <<
"=" << Utils::make_timestamp(m_creationDateDate) << std::endl;
205 os << SAVE_DATE_KEY <<
"=" << Utils::make_timestamp(m_saveDate) << 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)
226 std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> MiscInfoFile::to_protobuf()
const
228 std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> pMiscInfoFileMessage(
new Odb::Lib::Protobuf::MiscInfoFile);
229 pMiscInfoFileMessage->set_jobname(Odb::Lib::Text::ToUtf8(m_jobName));
230 pMiscInfoFileMessage->set_productmodelname(Odb::Lib::Text::ToUtf8(m_productModelName));
231 pMiscInfoFileMessage->set_odbversionmajor(Odb::Lib::Text::ToUtf8(m_odbVersionMajor));
232 pMiscInfoFileMessage->set_odbversionminor(Odb::Lib::Text::ToUtf8(m_odbVersionMinor));
233 pMiscInfoFileMessage->set_odbsource(Odb::Lib::Text::ToUtf8(m_odbSource));
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);
240 pMiscInfoFileMessage->set_saveapp(Odb::Lib::Text::ToUtf8(m_saveApp));
241 pMiscInfoFileMessage->set_saveuser(Odb::Lib::Text::ToUtf8(m_saveUser));
242 pMiscInfoFileMessage->set_units(Odb::Lib::Text::ToUtf8(m_units));
243 pMiscInfoFileMessage->set_maxuniqueid(m_maxUniqueId);
244 return pMiscInfoFileMessage;
247 void MiscInfoFile::from_protobuf(
const Odb::Lib::Protobuf::MiscInfoFile& message)
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();
263 MiscInfoFile::MiscInfoFile()
264 : m_maxUniqueId((unsigned int)-1)
268 std::string MiscInfoFile::GetProductModelName()
const
270 return m_productModelName;
273 std::string MiscInfoFile::GetJobName()
const
278 std::string MiscInfoFile::GetOdbVersionMajor()
const
280 return m_odbVersionMajor;
283 std::string MiscInfoFile::GetOdbVersionMinor()
const
285 return m_odbVersionMinor;
288 std::string MiscInfoFile::GetOdbSource()
const
293 system_clock::time_point MiscInfoFile::GetCreationDate()
const
295 return m_creationDateDate;
298 system_clock::time_point MiscInfoFile::GetSaveDate()
const
303 std::string MiscInfoFile::GetSaveApp()
const
308 std::string MiscInfoFile::GetSaveUser()
const
313 std::string MiscInfoFile::GetUnits()
const
318 unsigned int MiscInfoFile::GetMaxUniqueId()
const
320 return m_maxUniqueId;