1 #include "MiscInfoFile.h"
2 #include "MiscInfoFile.h"
8 #include "MiscInfoFile.h"
11 #include "str_utils.h"
12 #include "../../Constants.h"
13 #include "timestamp.h"
15 #include "../parse_error.h"
16 #include "../invalid_odb_error.h"
18 using namespace std::chrono;
20 namespace Odb::Lib::FileModel::Design
22 bool MiscInfoFile::Parse(std::filesystem::path path)
24 std::ifstream infoFile;
30 if (!OdbFile::Parse(path))
32 auto message =
"\"misc\" directory does not exist: [" + path.string() +
"]";
33 throw invalid_odb_error(message);
36 auto infoFilePath = path /
"info";
37 if (!std::filesystem::exists(infoFilePath))
39 auto message =
"misc/info file does not exist: [" + infoFilePath.string() +
"]";
40 throw invalid_odb_error(message);
43 infoFile.open(infoFilePath, std::ios::in);
44 if (!infoFile.is_open())
46 auto message =
"unable to open misc/info file: [" + infoFilePath.string() +
"]";
47 throw invalid_odb_error(message);
50 while (std::getline(infoFile, line))
55 Utils::str_trim(line);
58 std::stringstream lineStream(line);
59 if (line.find(Constants::COMMENT_TOKEN) == 0)
66 std::string attribute;
69 if (!std::getline(lineStream, attribute,
'='))
return false;
72 if (!std::getline(lineStream, value))
74 if (!attributeValueIsOptional(attribute))
76 logwarn(
"misc/info file: no value for non-optional attribute: " + attribute);
80 Utils::str_trim(attribute);
81 Utils::str_trim(value);
83 if (attribute ==
"PRODUCT_MODEL_NAME" ||
84 attribute ==
"product_model_name")
86 m_productModelName = value;
88 else if (attribute ==
"JOB_NAME" ||
89 attribute ==
"job_name")
93 else if (attribute ==
"odb_version_major" ||
94 attribute ==
"ODB_VERSION_MAJOR")
96 m_odbVersionMajor = value;
98 else if (attribute ==
"odb_version_minor" ||
99 attribute ==
"ODB_VERSION_MINOR")
101 m_odbVersionMinor = value;
103 else if (attribute ==
"odb_source" ||
104 attribute ==
"ODB_SOURCE")
108 else if (attribute ==
"creation_date" ||
109 attribute ==
"CREATION_DATE")
114 m_creationDateDate = Utils::parse_timestamp(value,
"%Y%m%d.%H%M%S");
118 auto createdDate = Utils::make_timestamp(m_creationDateDate);
119 std::stringstream ss;
120 ss <<
"value: " << value <<
", parsed createdDate: " << createdDate;
124 else if (attribute ==
"save_date" ||
125 attribute ==
"SAVE_DATE")
129 m_saveDate = Utils::parse_timestamp(value,
"%Y%m%d.%H%M%S");
131 auto saveDate = Utils::make_timestamp(m_saveDate);
133 std::stringstream ss;
134 ss <<
"value: " << value <<
", parsed saveDate: " << saveDate;
138 else if (attribute ==
"save_app" ||
139 attribute ==
"SAVE_APP")
143 else if (attribute ==
"save_user" ||
144 attribute ==
"SAVE_USER")
148 else if (attribute ==
"units" ||
149 attribute ==
"UNITS")
153 else if (attribute ==
"max_uid" ||
154 attribute ==
"MAX_UID")
156 m_maxUniqueId = std::stoi(value);
165 parse_info pi(m_path, line, attribute, lineNumber);
166 logwarn(pi.toString(
"unrecognized line in misc/info file:"));
174 catch (invalid_odb_error& ioe)
176 parse_info pi(m_path, line, lineNumber);
177 const auto m = pi.toString();
178 logexception_msg(ioe, m);
183 catch (std::exception& e)
185 parse_info pi(m_path, line, lineNumber);
186 const auto m = pi.toString();
187 logexception_msg(e, m);
196 bool MiscInfoFile::Save(std::ostream& os)
198 os << PRODUCT_MODEL_NAME_KEY <<
"=" << m_productModelName << std::endl;
199 os << JOB_NAME_KEY <<
"=" << m_jobName << std::endl;
200 os << ODB_VERSION_MAJOR_KEY <<
"=" << m_odbVersionMajor << std::endl;
201 os << ODB_VERSION_MINOR_KEY <<
"=" << m_odbVersionMinor << std::endl;
202 os << ODB_SOURCE_KEY <<
"=" << m_odbSource << std::endl;
203 os << CREATION_DATE_KEY <<
"=" << Utils::make_timestamp(m_creationDateDate) << std::endl;
204 os << SAVE_DATE_KEY <<
"=" << Utils::make_timestamp(m_saveDate) << std::endl;
205 os << SAVE_APP_KEY <<
"=" << m_saveApp << std::endl;
206 os << SAVE_USER_KEY <<
"=" << m_saveUser << std::endl;
207 os << UNITS_KEY <<
"=" << m_units << std::endl;
208 os << MAX_UID_KEY <<
"=" << m_maxUniqueId << std::endl;
213 inline bool MiscInfoFile::attributeValueIsOptional(
const std::string& attribute)
215 for (
const auto& optionalAttribute : OPTIONAL_ATTRIBUTES)
217 if (attribute == optionalAttribute)
225 std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> MiscInfoFile::to_protobuf()
const
227 std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> pMiscInfoFileMessage(
new Odb::Lib::Protobuf::MiscInfoFile);
228 pMiscInfoFileMessage->set_jobname(m_jobName);
229 pMiscInfoFileMessage->set_productmodelname(m_productModelName);
230 pMiscInfoFileMessage->set_odbversionmajor(m_odbVersionMajor);
231 pMiscInfoFileMessage->set_odbversionminor(m_odbVersionMinor);
232 pMiscInfoFileMessage->set_odbsource(m_odbSource);
233 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(m_creationDateDate.time_since_epoch()).count();
234 pMiscInfoFileMessage->mutable_creationdatedate()->set_seconds(seconds);
235 pMiscInfoFileMessage->mutable_creationdatedate()->set_nanos(0);
236 seconds = std::chrono::duration_cast<std::chrono::seconds>(m_saveDate.time_since_epoch()).count();
237 pMiscInfoFileMessage->mutable_savedate()->set_seconds(seconds);
238 pMiscInfoFileMessage->mutable_savedate()->set_nanos(0);
239 pMiscInfoFileMessage->set_saveapp(m_saveApp);
240 pMiscInfoFileMessage->set_saveuser(m_saveUser);
241 pMiscInfoFileMessage->set_units(m_units);
242 pMiscInfoFileMessage->set_maxuniqueid(m_maxUniqueId);
243 return pMiscInfoFileMessage;
246 void MiscInfoFile::from_protobuf(
const Odb::Lib::Protobuf::MiscInfoFile& message)
248 m_jobName = message.jobname();
249 m_productModelName = message.productmodelname();
250 m_odbVersionMajor = message.odbversionmajor();
251 m_odbVersionMinor = message.odbversionminor();
252 m_odbSource = message.odbsource();
254 m_creationDateDate = std::chrono::system_clock::time_point(std::chrono::seconds(message.creationdatedate().seconds()));
255 m_saveDate = std::chrono::system_clock::time_point(std::chrono::seconds(message.savedate().seconds()));
256 m_saveApp = message.saveapp();
257 m_saveUser = message.saveuser();
258 m_units = message.units();
259 m_maxUniqueId = message.maxuniqueid();
262 MiscInfoFile::MiscInfoFile()
263 : m_maxUniqueId((unsigned int)-1)
267 std::string MiscInfoFile::GetProductModelName()
const
269 return m_productModelName;
272 std::string MiscInfoFile::GetJobName()
const
277 std::string MiscInfoFile::GetOdbVersionMajor()
const
279 return m_odbVersionMajor;
282 std::string MiscInfoFile::GetOdbVersionMinor()
const
284 return m_odbVersionMinor;
287 std::string MiscInfoFile::GetOdbSource()
const
292 system_clock::time_point MiscInfoFile::GetCreationDate()
const
294 return m_creationDateDate;
297 system_clock::time_point MiscInfoFile::GetSaveDate()
const
302 std::string MiscInfoFile::GetSaveApp()
const
307 std::string MiscInfoFile::GetSaveUser()
const
312 std::string MiscInfoFile::GetUnits()
const
317 unsigned int MiscInfoFile::GetMaxUniqueId()
const
319 return m_maxUniqueId;