1 #include "MiscInfoFile.h"
7 #include "MiscInfoFile.h"
10 #include "str_utils.h"
11 #include "../../Constants.h"
12 #include "timestamp.h"
14 #include "../parse_error.h"
15 #include "../invalid_odb_error.h"
17 using namespace std::chrono;
19 namespace Odb::Lib::FileModel::Design
21 bool MiscInfoFile::Parse(std::filesystem::path path)
23 std::ifstream infoFile;
29 if (!OdbFile::Parse(path))
31 auto message =
"\"misc\" directory does not exist: [" + path.string() +
"]";
32 throw invalid_odb_error(message);
35 auto infoFilePath = path /
"info";
36 if (!std::filesystem::exists(infoFilePath))
38 auto message =
"misc/info file does not exist: [" + infoFilePath.string() +
"]";
39 throw invalid_odb_error(message);
42 infoFile.open(infoFilePath, std::ios::in);
43 if (!infoFile.is_open())
45 auto message =
"unable to open misc/info file: [" + infoFilePath.string() +
"]";
46 throw invalid_odb_error(message);
49 while (std::getline(infoFile, line))
54 Utils::str_trim(line);
57 std::stringstream lineStream(line);
58 if (line.find(Constants::COMMENT_TOKEN) == 0)
65 std::string attribute;
68 if (!std::getline(lineStream, attribute,
'='))
return false;
71 if (!std::getline(lineStream, value))
73 if (!attributeValueIsOptional(attribute))
75 logwarn(
"misc/info file: no value for non-optional attribute: " + attribute);
79 Utils::str_trim(attribute);
80 Utils::str_trim(value);
82 if (attribute ==
"PRODUCT_MODEL_NAME" ||
83 attribute ==
"product_model_name")
85 m_productModelName = value;
87 else if (attribute ==
"JOB_NAME" ||
88 attribute ==
"job_name")
92 else if (attribute ==
"odb_version_major" ||
93 attribute ==
"ODB_VERSION_MAJOR")
95 m_odbVersionMajor = value;
97 else if (attribute ==
"odb_version_minor" ||
98 attribute ==
"ODB_VERSION_MINOR")
100 m_odbVersionMinor = value;
102 else if (attribute ==
"odb_source" ||
103 attribute ==
"ODB_SOURCE")
107 else if (attribute ==
"creation_date" ||
108 attribute ==
"CREATION_DATE")
113 m_creationDateDate = Utils::parse_timestamp(value,
"%Y%m%d.%H%M%S");
117 auto createdDate = Utils::make_timestamp(m_creationDateDate);
118 std::stringstream ss;
119 ss <<
"value: " << value <<
", parsed createdDate: " << createdDate;
123 else if (attribute ==
"save_date" ||
124 attribute ==
"SAVE_DATE")
128 m_saveDate = Utils::parse_timestamp(value,
"%Y%m%d.%H%M%S");
130 auto saveDate = Utils::make_timestamp(m_saveDate);
132 std::stringstream ss;
133 ss <<
"value: " << value <<
", parsed saveDate: " << saveDate;
137 else if (attribute ==
"save_app" ||
138 attribute ==
"SAVE_APP")
142 else if (attribute ==
"save_user" ||
143 attribute ==
"SAVE_USER")
147 else if (attribute ==
"units" ||
148 attribute ==
"UNITS")
152 else if (attribute ==
"max_uid" ||
153 attribute ==
"MAX_UID")
155 m_maxUniqueId = std::stoi(value);
164 parse_info pi(m_path, line, attribute, lineNumber);
165 logwarn(pi.toString(
"unrecognized line in misc/info file:"));
173 catch (invalid_odb_error& ioe)
175 parse_info pi(m_path, line, lineNumber);
176 const auto m = pi.toString();
177 logexception_msg(ioe, m);
182 catch (std::exception& e)
184 parse_info pi(m_path, line, lineNumber);
185 const auto m = pi.toString();
186 logexception_msg(e, m);
195 inline bool MiscInfoFile::attributeValueIsOptional(
const std::string& attribute)
197 for (
const auto& optionalAttribute : OPTIONAL_ATTRIBUTES)
199 if (attribute == optionalAttribute)
207 std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> MiscInfoFile::to_protobuf()
const
209 std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> pMiscInfoFileMessage(
new Odb::Lib::Protobuf::MiscInfoFile);
210 pMiscInfoFileMessage->set_jobname(m_jobName);
211 pMiscInfoFileMessage->set_productmodelname(m_productModelName);
212 pMiscInfoFileMessage->set_odbversionmajor(m_odbVersionMajor);
213 pMiscInfoFileMessage->set_odbversionminor(m_odbVersionMinor);
214 pMiscInfoFileMessage->set_odbsource(m_odbSource);
215 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(m_creationDateDate.time_since_epoch()).count();
216 pMiscInfoFileMessage->mutable_creationdatedate()->set_seconds(seconds);
217 pMiscInfoFileMessage->mutable_creationdatedate()->set_nanos(0);
218 seconds = std::chrono::duration_cast<std::chrono::seconds>(m_saveDate.time_since_epoch()).count();
219 pMiscInfoFileMessage->mutable_savedate()->set_seconds(seconds);
220 pMiscInfoFileMessage->mutable_savedate()->set_nanos(0);
221 pMiscInfoFileMessage->set_saveapp(m_saveApp);
222 pMiscInfoFileMessage->set_saveuser(m_saveUser);
223 pMiscInfoFileMessage->set_units(m_units);
224 pMiscInfoFileMessage->set_maxuniqueid(m_maxUniqueId);
225 return pMiscInfoFileMessage;
228 void MiscInfoFile::from_protobuf(
const Odb::Lib::Protobuf::MiscInfoFile& message)
230 m_jobName = message.jobname();
231 m_productModelName = message.productmodelname();
232 m_odbVersionMajor = message.odbversionmajor();
233 m_odbVersionMinor = message.odbversionminor();
234 m_odbSource = message.odbsource();
236 m_creationDateDate = std::chrono::system_clock::time_point(std::chrono::seconds(message.creationdatedate().seconds()));
237 m_saveDate = std::chrono::system_clock::time_point(std::chrono::seconds(message.savedate().seconds()));
238 m_saveApp = message.saveapp();
239 m_saveUser = message.saveuser();
240 m_units = message.units();
241 m_maxUniqueId = message.maxuniqueid();
244 MiscInfoFile::MiscInfoFile()
245 : m_maxUniqueId((unsigned int)-1)
249 std::string MiscInfoFile::GetProductModelName()
const
251 return m_productModelName;
254 std::string MiscInfoFile::GetJobName()
const
259 std::string MiscInfoFile::GetOdbVersionMajor()
const
261 return m_odbVersionMajor;
264 std::string MiscInfoFile::GetOdbVersionMinor()
const
266 return m_odbVersionMinor;
269 std::string MiscInfoFile::GetOdbSource()
const
274 system_clock::time_point MiscInfoFile::GetCreationDate()
const
276 return m_creationDateDate;
279 system_clock::time_point MiscInfoFile::GetSaveDate()
const
284 std::string MiscInfoFile::GetSaveApp()
const
289 std::string MiscInfoFile::GetSaveUser()
const
294 std::string MiscInfoFile::GetUnits()
const
299 unsigned int MiscInfoFile::GetMaxUniqueId()
const
301 return m_maxUniqueId;