Line data Source code
1 : #include "MiscInfoFile.h"
2 : #include "MiscInfoFile.h"
3 : //
4 : // Created by nmill on 10/13/2023.
5 : //
6 :
7 : #include <fstream>
8 : #include "MiscInfoFile.h"
9 : #include <string>
10 : #include <sstream>
11 : #include "str_utils.h"
12 : #include "../../Constants.h"
13 : #include "timestamp.h"
14 : #include "Logger.h"
15 : #include "../parse_error.h"
16 : #include "../invalid_odb_error.h"
17 : #include "../../Text/Utf8Sanitizer.h"
18 :
19 : using namespace std::chrono;
20 :
21 : namespace Odb::Lib::FileModel::Design
22 : {
23 93 : bool MiscInfoFile::Parse(std::filesystem::path path)
24 : {
25 93 : std::ifstream infoFile;
26 93 : int lineNumber = 0;
27 93 : std::string line;
28 :
29 : try
30 : {
31 93 : if (!OdbFile::Parse(path))
32 : {
33 0 : auto message = "\"misc\" directory does not exist: [" + path.string() + "]";
34 0 : throw invalid_odb_error(message);
35 0 : }
36 :
37 93 : auto infoFilePath = path / "info";
38 93 : if (!std::filesystem::exists(infoFilePath))
39 : {
40 3 : auto message = "misc/info file does not exist: [" + infoFilePath.string() + "]";
41 3 : throw invalid_odb_error(message);
42 3 : }
43 :
44 90 : infoFile.open(infoFilePath, std::ios::in);
45 90 : if (!infoFile.is_open())
46 : {
47 0 : auto message = "unable to open misc/info file: [" + infoFilePath.string() + "]";
48 0 : throw invalid_odb_error(message);
49 0 : }
50 :
51 990 : while (std::getline(infoFile, line))
52 : {
53 900 : lineNumber++;
54 :
55 : // trim whitespace from beginning and end of line
56 900 : Utils::str_trim(line);
57 900 : if (!line.empty())
58 : {
59 900 : std::stringstream lineStream(line);
60 900 : if (line.find(Constants::COMMENT_TOKEN) == 0)
61 : {
62 : // comment line
63 : }
64 : else
65 : {
66 : // attribute line
67 900 : std::string attribute;
68 900 : std::string value;
69 :
70 900 : if (!std::getline(lineStream, attribute, '=')) return false;
71 : // attribute value may be blank?
72 :
73 900 : if (!std::getline(lineStream, value))
74 : {
75 0 : if (!attributeValueIsOptional(attribute))
76 : {
77 0 : logwarn("misc/info file: no value for non-optional attribute: " + attribute);
78 : }
79 : }
80 :
81 900 : Utils::str_trim(attribute);
82 900 : Utils::str_trim(value);
83 :
84 1800 : if (attribute == "PRODUCT_MODEL_NAME" ||
85 900 : attribute == "product_model_name")
86 : {
87 0 : m_productModelName = value;
88 : }
89 1710 : else if (attribute == "JOB_NAME" ||
90 810 : attribute == "job_name")
91 : {
92 90 : m_jobName = value;
93 : }
94 1620 : else if (attribute == "odb_version_major" ||
95 810 : attribute == "ODB_VERSION_MAJOR")
96 : {
97 90 : m_odbVersionMajor = value;
98 : }
99 1440 : else if (attribute == "odb_version_minor" ||
100 720 : attribute == "ODB_VERSION_MINOR")
101 : {
102 90 : m_odbVersionMinor = value;
103 : }
104 1260 : else if (attribute == "odb_source" ||
105 630 : attribute == "ODB_SOURCE")
106 : {
107 90 : m_odbSource = value;
108 : }
109 1080 : else if (attribute == "creation_date" ||
110 540 : attribute == "CREATION_DATE")
111 : {
112 : //std::istringstream iss(value);
113 : //// yyyymmdd.hhmmss
114 : //iss >> std::chrono::parse("%Y%m%d.%H%M%S", m_creationDateDate);
115 180 : m_creationDateDate = Utils::parse_timestamp(value, "%Y%m%d.%H%M%S");
116 :
117 : #if defined(_DEBUG)
118 :
119 : auto createdDate = Utils::make_timestamp(m_creationDateDate);
120 : std::stringstream ss;
121 : ss << "value: " << value << ", parsed createdDate: " << createdDate;
122 : loginfo(ss.str());
123 : #endif // _DEBUG
124 : }
125 900 : else if (attribute == "save_date" ||
126 450 : attribute == "SAVE_DATE")
127 : {
128 : //// yyyymmdd.hhmmss
129 : //std::istringstream(value) >> std::chrono::parse("%Y%m%d.%H%M%S", m_saveDate);
130 180 : m_saveDate = Utils::parse_timestamp(value, "%Y%m%d.%H%M%S");
131 : #if defined(_DEBUG)
132 : auto saveDate = Utils::make_timestamp(m_saveDate);
133 :
134 : std::stringstream ss;
135 : ss << "value: " << value << ", parsed saveDate: " << saveDate;
136 : loginfo(ss.str());
137 : #endif // _DEBUG
138 : }
139 720 : else if (attribute == "save_app" ||
140 360 : attribute == "SAVE_APP")
141 : {
142 90 : m_saveApp = value;
143 : }
144 540 : else if (attribute == "save_user" ||
145 270 : attribute == "SAVE_USER")
146 : {
147 90 : m_saveUser = value;
148 : }
149 360 : else if (attribute == "units" ||
150 180 : attribute == "UNITS")
151 : {
152 90 : m_units = value;
153 : }
154 180 : else if (attribute == "max_uid" ||
155 90 : attribute == "MAX_UID")
156 : {
157 90 : m_maxUniqueId = std::stoi(value);
158 : }
159 : else
160 : {
161 : // unknown attribute
162 :
163 : // DO NOT fail parsing on unknown attributes- log instead
164 : //return false;
165 :
166 0 : parse_info pi(m_path, line, attribute, lineNumber);
167 0 : logwarn(pi.toString("unrecognized line in misc/info file:"));
168 0 : }
169 900 : }
170 900 : }
171 : }
172 :
173 90 : infoFile.close();
174 93 : }
175 3 : catch (invalid_odb_error& ioe)
176 : {
177 3 : parse_info pi(m_path, line, lineNumber);
178 3 : const auto m = pi.toString();
179 6 : logexception_msg(ioe, m);
180 : // cleanup file
181 3 : infoFile.close();
182 3 : throw ioe;
183 9 : }
184 0 : catch (std::exception& e)
185 : {
186 0 : parse_info pi(m_path, line, lineNumber);
187 0 : const auto m = pi.toString();
188 0 : logexception_msg(e, m);
189 : // cleanup file
190 0 : infoFile.close();
191 0 : throw e;
192 0 : }
193 :
194 90 : return true;
195 96 : }
196 :
197 0 : bool MiscInfoFile::Save(std::ostream& os)
198 : {
199 0 : os << PRODUCT_MODEL_NAME_KEY << "=" << m_productModelName << std::endl;
200 0 : os << JOB_NAME_KEY << "=" << m_jobName << std::endl;
201 0 : os << ODB_VERSION_MAJOR_KEY << "=" << m_odbVersionMajor << std::endl;
202 0 : os << ODB_VERSION_MINOR_KEY << "=" << m_odbVersionMinor << std::endl;
203 0 : os << ODB_SOURCE_KEY << "=" << m_odbSource << std::endl;
204 0 : os << CREATION_DATE_KEY << "=" << Utils::make_timestamp(m_creationDateDate) << std::endl;
205 0 : os << SAVE_DATE_KEY << "=" << Utils::make_timestamp(m_saveDate) << std::endl;
206 0 : os << SAVE_APP_KEY << "=" << m_saveApp << std::endl;
207 0 : os << SAVE_USER_KEY << "=" << m_saveUser << std::endl;
208 0 : os << UNITS_KEY << "=" << m_units << std::endl;
209 0 : os << MAX_UID_KEY << "=" << m_maxUniqueId << std::endl;
210 :
211 0 : return true;
212 : }
213 :
214 0 : /*static*/ inline bool MiscInfoFile::attributeValueIsOptional(const std::string& attribute)
215 : {
216 0 : for (const auto& optionalAttribute : OPTIONAL_ATTRIBUTES)
217 : {
218 0 : if (attribute == optionalAttribute)
219 : {
220 0 : return true;
221 : }
222 : }
223 0 : return false;
224 : }
225 :
226 95 : std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> MiscInfoFile::to_protobuf() const
227 : {
228 95 : std::unique_ptr<Odb::Lib::Protobuf::MiscInfoFile> pMiscInfoFileMessage(new Odb::Lib::Protobuf::MiscInfoFile);
229 190 : pMiscInfoFileMessage->set_jobname(Odb::Lib::Text::ToUtf8(m_jobName));
230 190 : pMiscInfoFileMessage->set_productmodelname(Odb::Lib::Text::ToUtf8(m_productModelName));
231 190 : pMiscInfoFileMessage->set_odbversionmajor(Odb::Lib::Text::ToUtf8(m_odbVersionMajor));
232 190 : pMiscInfoFileMessage->set_odbversionminor(Odb::Lib::Text::ToUtf8(m_odbVersionMinor));
233 190 : pMiscInfoFileMessage->set_odbsource(Odb::Lib::Text::ToUtf8(m_odbSource));
234 95 : auto seconds = std::chrono::duration_cast<std::chrono::seconds>(m_creationDateDate.time_since_epoch()).count();
235 95 : pMiscInfoFileMessage->mutable_creationdatedate()->set_seconds(seconds);
236 95 : pMiscInfoFileMessage->mutable_creationdatedate()->set_nanos(0);
237 95 : seconds = std::chrono::duration_cast<std::chrono::seconds>(m_saveDate.time_since_epoch()).count();
238 95 : pMiscInfoFileMessage->mutable_savedate()->set_seconds(seconds);
239 95 : pMiscInfoFileMessage->mutable_savedate()->set_nanos(0);
240 190 : pMiscInfoFileMessage->set_saveapp(Odb::Lib::Text::ToUtf8(m_saveApp));
241 190 : pMiscInfoFileMessage->set_saveuser(Odb::Lib::Text::ToUtf8(m_saveUser));
242 190 : pMiscInfoFileMessage->set_units(Odb::Lib::Text::ToUtf8(m_units));
243 95 : pMiscInfoFileMessage->set_maxuniqueid(m_maxUniqueId);
244 95 : return pMiscInfoFileMessage;
245 0 : }
246 :
247 7 : void MiscInfoFile::from_protobuf(const Odb::Lib::Protobuf::MiscInfoFile& message)
248 : {
249 7 : m_jobName = message.jobname();
250 7 : m_productModelName = message.productmodelname();
251 7 : m_odbVersionMajor = message.odbversionmajor();
252 7 : m_odbVersionMinor = message.odbversionminor();
253 7 : m_odbSource = message.odbsource();
254 : //m_creationDateDate = system_clock::from_time_t(message.creationdatedate().seconds());
255 7 : m_creationDateDate = std::chrono::system_clock::time_point(std::chrono::seconds(message.creationdatedate().seconds()));
256 7 : m_saveDate = std::chrono::system_clock::time_point(std::chrono::seconds(message.savedate().seconds()));
257 7 : m_saveApp = message.saveapp();
258 7 : m_saveUser = message.saveuser();
259 7 : m_units = message.units();
260 7 : m_maxUniqueId = message.maxuniqueid();
261 7 : }
262 :
263 111 : MiscInfoFile::MiscInfoFile()
264 111 : : m_maxUniqueId((unsigned int)-1)
265 : {
266 111 : }
267 :
268 0 : std::string MiscInfoFile::GetProductModelName() const
269 : {
270 0 : return m_productModelName;
271 : }
272 :
273 0 : std::string MiscInfoFile::GetJobName() const
274 : {
275 0 : return m_jobName;
276 : }
277 :
278 0 : std::string MiscInfoFile::GetOdbVersionMajor() const
279 : {
280 0 : return m_odbVersionMajor;
281 : }
282 :
283 0 : std::string MiscInfoFile::GetOdbVersionMinor() const
284 : {
285 0 : return m_odbVersionMinor;
286 : }
287 :
288 0 : std::string MiscInfoFile::GetOdbSource() const
289 : {
290 0 : return m_odbSource;
291 : }
292 :
293 0 : system_clock::time_point MiscInfoFile::GetCreationDate() const
294 : {
295 0 : return m_creationDateDate;
296 : }
297 :
298 0 : system_clock::time_point MiscInfoFile::GetSaveDate() const
299 : {
300 0 : return m_saveDate;
301 : }
302 :
303 0 : std::string MiscInfoFile::GetSaveApp() const
304 : {
305 0 : return m_saveApp;
306 : }
307 :
308 0 : std::string MiscInfoFile::GetSaveUser() const
309 : {
310 0 : return m_saveUser;
311 : }
312 :
313 0 : std::string MiscInfoFile::GetUnits() const
314 : {
315 0 : return m_units;
316 : }
317 :
318 0 : unsigned int MiscInfoFile::GetMaxUniqueId() const
319 : {
320 0 : return m_maxUniqueId;
321 : }
322 : }
|