Line data Source code
1 : //
2 : // Created by nmill on 10/13/2023.
3 : //
4 :
5 : #include "MatrixFile.h"
6 : #include <fstream>
7 : #include "str_utils.h"
8 : #include <string>
9 : #include "../../Constants.h"
10 : #include <sstream>
11 : #include "../parse_error.h"
12 : #include <Logger.h>
13 : #include "../invalid_odb_error.h"
14 : #include "enums.pb.h"
15 : #include "../../enums.h"
16 : #include "../OdbFile.h"
17 : #include <memory>
18 : #include <ostream>
19 : #include "../../Text/Utf8Sanitizer.h"
20 :
21 : namespace Odb::Lib::FileModel::Design
22 : {
23 94 : MatrixFile::~MatrixFile()
24 : {
25 94 : m_layerRecords.clear();
26 94 : m_stepRecords.clear();
27 94 : }
28 :
29 4 : const MatrixFile::LayerRecord::Vector& MatrixFile::GetLayerRecords() const
30 : {
31 4 : return m_layerRecords;
32 : }
33 :
34 3 : const MatrixFile::StepRecord::Vector& MatrixFile::GetStepRecords() const
35 : {
36 3 : return m_stepRecords;
37 : }
38 :
39 90 : bool MatrixFile::Parse(std::filesystem::path path)
40 : {
41 90 : std::ifstream matrixFile;
42 90 : int lineNumber = 0;
43 90 : std::string line;
44 :
45 : try
46 : {
47 90 : if (!OdbFile::Parse(path)) return false;
48 :
49 90 : auto matrixFilePath = path / "matrix";
50 90 : if (!std::filesystem::exists(matrixFilePath))
51 : {
52 0 : auto message = "matrix/matrix file does not exist: [" + matrixFilePath.string() + "]";
53 0 : throw invalid_odb_error(message.c_str());
54 0 : }
55 :
56 90 : matrixFile.open(matrixFilePath, std::ios::in);
57 90 : if (!matrixFile.is_open())
58 : {
59 0 : auto message = "unable to open matrix/matrix file: [" + matrixFilePath.string() + "]";
60 0 : throw invalid_odb_error(message.c_str());
61 0 : }
62 :
63 90 : std::shared_ptr<StepRecord> pCurrentStepRecord;
64 90 : std::shared_ptr<LayerRecord> pCurrentLayerRecord;
65 90 : bool openBraceFound = false;
66 :
67 47102 : while (std::getline(matrixFile, line))
68 : {
69 47012 : lineNumber++;
70 : // trim whitespace from beginning and end of line
71 47012 : Utils::str_trim(line);
72 47012 : if (!line.empty())
73 : {
74 43974 : std::stringstream lineStream(line);
75 43974 : if (line.find(Constants::COMMENT_TOKEN) == 0)
76 : {
77 : // comment line
78 : }
79 43974 : else if (line.find(StepRecord::RECORD_TOKEN) == 0)
80 : {
81 90 : std::string token;
82 90 : if (!(lineStream >> token))
83 : {
84 0 : throw_parse_error(m_path, line, token, lineNumber);
85 : }
86 :
87 : // any existing previous step record
88 90 : if (token != StepRecord::RECORD_TOKEN || pCurrentStepRecord != nullptr)
89 : {
90 0 : throw_parse_error(m_path, line, token, lineNumber);
91 : }
92 :
93 : // open a new STEP array record
94 90 : pCurrentStepRecord = std::make_shared<StepRecord>();
95 :
96 90 : if (lineStream >> token)
97 : {
98 : // open brace is at the end on the same line
99 90 : if (token == Constants::ARRAY_RECORD_OPEN_TOKEN)
100 : {
101 90 : openBraceFound = true;
102 : }
103 : }
104 90 : }
105 43884 : else if (line.find(LayerRecord::RECORD_TOKEN) == 0)
106 : {
107 2884 : std::string token;
108 2884 : if (!(lineStream >> token))
109 : {
110 0 : throw_parse_error(m_path, line, token, lineNumber);
111 : }
112 :
113 : // any existing previous layer record
114 2884 : if (token != LayerRecord::RECORD_TOKEN || pCurrentLayerRecord != nullptr)
115 : {
116 0 : throw_parse_error(m_path, line, token, lineNumber);
117 : }
118 :
119 : // open a new LAYER array record
120 2884 : pCurrentLayerRecord = std::make_shared<LayerRecord>();
121 :
122 2884 : if (lineStream >> token)
123 : {
124 : // open brace is on same line
125 2884 : if (token == Constants::ARRAY_RECORD_OPEN_TOKEN)
126 : {
127 2884 : openBraceFound = true;
128 : }
129 : }
130 2884 : }
131 41000 : else if (line.find(Constants::ARRAY_RECORD_OPEN_TOKEN) == 0)
132 : {
133 : // TODO: how to determine if you are opening a step or layer array record?
134 : // (maybe a boolean flag? stepArrayOpen = true/false)
135 :
136 : // no current opening of a layer or step array record found yet
137 0 : if (pCurrentStepRecord == nullptr && pCurrentLayerRecord == nullptr)
138 : {
139 0 : throw_parse_error(m_path, line, "", lineNumber);
140 : }
141 :
142 : // found another open brace while still parsing after the previous record's open brace
143 0 : if (openBraceFound)
144 : {
145 0 : throw_parse_error(m_path, line, "", lineNumber);
146 : }
147 :
148 0 : openBraceFound = true;
149 :
150 : // TODO: finish any existing previous layer or step record
151 : // (same code as finding a close brace on an empty line)
152 :
153 : // open a new LAYER array record
154 : //pCurrentLayerRecord = std::make_shared<LayerRecord>();
155 : }
156 41000 : else if (line.find(Constants::ARRAY_RECORD_CLOSE_TOKEN) == 0)
157 : {
158 2974 : if (pCurrentStepRecord != nullptr && openBraceFound)
159 : {
160 90 : m_stepRecords.push_back(pCurrentStepRecord);
161 90 : pCurrentStepRecord.reset();
162 90 : openBraceFound = false;
163 : }
164 2884 : else if (pCurrentLayerRecord != nullptr && openBraceFound)
165 : {
166 2884 : m_layerRecords.push_back(pCurrentLayerRecord);
167 2884 : pCurrentLayerRecord.reset();
168 2884 : openBraceFound = false;
169 : }
170 : else
171 : {
172 : // found a close brace but aren't currently parsing a step or layer array record
173 0 : throw_parse_error(m_path, line, "", lineNumber);
174 : }
175 : }
176 : else
177 : {
178 : // it should be a name-value pair line (i.e. element of a step or layer array)
179 38026 : std::string attribute;
180 38026 : std::string value;
181 :
182 38026 : if (!std::getline(lineStream, attribute, '='))
183 : {
184 0 : throw_parse_error(m_path, line, attribute, lineNumber);
185 : }
186 :
187 38026 : if (std::getline(lineStream, value))
188 : {
189 21684 : Utils::str_trim(attribute);
190 21684 : Utils::str_trim(value);
191 :
192 21684 : if (pCurrentStepRecord != nullptr && openBraceFound)
193 : {
194 270 : if (attribute == StepRecord::COLUMN_KEY || attribute == "col")
195 : {
196 90 : pCurrentStepRecord->column = std::stoi(value);
197 : }
198 180 : else if (attribute == StepRecord::NAME_KEY || attribute == "name")
199 : {
200 90 : pCurrentStepRecord->name = value;
201 : }
202 90 : else if (attribute == StepRecord::ID_KEY || attribute == "id")
203 : {
204 90 : pCurrentStepRecord->id = static_cast<unsigned int>(std::stoul(value));
205 : }
206 : else
207 : {
208 0 : throw_parse_error(m_path, line, attribute, lineNumber);
209 : }
210 : }
211 21414 : else if (pCurrentLayerRecord != nullptr && openBraceFound)
212 : {
213 21414 : if (attribute == "ROW" || attribute == "row")
214 : {
215 2884 : pCurrentLayerRecord->row = std::stoi(value);
216 : }
217 18530 : else if (attribute == "NAME" || attribute == "name")
218 : {
219 2884 : pCurrentLayerRecord->name = value;
220 : }
221 15646 : else if (attribute == "ID" || attribute == "id")
222 : {
223 2884 : pCurrentLayerRecord->id = (unsigned int)std::stoul(value);
224 : }
225 12762 : else if (attribute == "TYPE" || attribute == "type")
226 : {
227 2884 : if (LayerRecord::typeMap.contains(value))
228 : {
229 2884 : pCurrentLayerRecord->type = LayerRecord::typeMap.getValue(value);
230 : }
231 : else
232 : {
233 0 : throw_parse_error(m_path, line, attribute, lineNumber);
234 : }
235 : }
236 9878 : else if (attribute == LayerRecord::CONTEXT_KEY || attribute == "context")
237 : {
238 2884 : if (LayerRecord::contextMap.contains(value))
239 : {
240 2884 : pCurrentLayerRecord->context = LayerRecord::contextMap.getValue(value);
241 : }
242 : else
243 : {
244 0 : throw_parse_error(m_path, line, attribute, lineNumber);
245 : }
246 : }
247 6994 : else if (attribute == "OLD_NAME" || attribute == "old_name")
248 : {
249 0 : pCurrentLayerRecord->oldName = value;
250 : }
251 6994 : else if (attribute == LayerRecord::POLARITY_KEY || attribute == "polarity")
252 : {
253 2884 : if (LayerRecord::polarityMap.contains(value))
254 : {
255 2884 : pCurrentLayerRecord->polarity = LayerRecord::polarityMap.getValue(value);
256 : }
257 : else
258 : {
259 0 : throw_parse_error(m_path, line, attribute, lineNumber);
260 : }
261 : }
262 4110 : else if (attribute == LayerRecord::DIELECTRIC_TYPE_KEY || attribute == "dielectric_type")
263 : {
264 406 : if (LayerRecord::dielectricTypeMap.contains(value))
265 : {
266 406 : pCurrentLayerRecord->dielectricType = LayerRecord::dielectricTypeMap.getValue(value);
267 : }
268 : else
269 : {
270 0 : throw_parse_error(m_path, line, attribute, lineNumber);
271 : }
272 : }
273 3704 : else if (attribute == "DIELECTRIC_NAME" || attribute == "dielectric_name")
274 : {
275 0 : pCurrentLayerRecord->dielectricName = value;
276 : }
277 3704 : else if (attribute == LayerRecord::FORM_KEY || attribute == "form")
278 : {
279 0 : if (LayerRecord::formMap.contains(value))
280 : {
281 0 : pCurrentLayerRecord->form = LayerRecord::formMap.getValue(value);
282 : }
283 : else
284 : {
285 0 : throw_parse_error(m_path, line, attribute, lineNumber);
286 : }
287 : }
288 3704 : else if (attribute == "CU_TOP" || attribute == "cu_top")
289 : {
290 0 : pCurrentLayerRecord->cuTop = std::stoul(value);
291 : }
292 3704 : else if (attribute == "CU_TOP" || attribute == "cu_top")
293 : {
294 0 : pCurrentLayerRecord->cuTop = std::stoul(value);
295 : }
296 3704 : else if (attribute == "CU_BOTTOM" || attribute == "cu_bottom")
297 : {
298 0 : pCurrentLayerRecord->cuBottom = std::stoul(value);
299 : }
300 3704 : else if (attribute == "REF" || attribute == "ref")
301 : {
302 0 : pCurrentLayerRecord->ref = std::stoul(value);
303 : }
304 3704 : else if (attribute == "START_NAME" || attribute == "start_name")
305 : {
306 282 : pCurrentLayerRecord->startName = value;
307 : }
308 3422 : else if (attribute == "END_NAME" || attribute == "end_name")
309 : {
310 282 : pCurrentLayerRecord->endName = value;
311 : }
312 3140 : else if (attribute == "ADD_TYPE" || attribute == "add_type")
313 : {
314 256 : pCurrentLayerRecord->addType = value;
315 : }
316 2884 : else if (attribute == "COLOR" || attribute == "color")
317 : {
318 2884 : if (!pCurrentLayerRecord->color.from_string(value))
319 : {
320 0 : throw_parse_error(m_path, line, value, lineNumber);
321 : }
322 : }
323 : else
324 : {
325 0 : throw_parse_error(m_path, line, attribute, lineNumber);
326 : }
327 : }
328 : else
329 : {
330 : // found a name-value pair but aren't currently parsing a step or layer array record
331 : //return false;
332 0 : throw_parse_error(m_path, line, attribute, lineNumber);
333 : }
334 : }
335 16342 : else if (!attributeValueIsOptional(attribute))
336 : {
337 0 : logwarn("matrix/matrix file: no value for non-optional attribute: " + attribute);
338 : }
339 38026 : }
340 43974 : }
341 : }
342 :
343 90 : matrixFile.close();
344 90 : }
345 0 : catch (parse_error& pe)
346 : {
347 0 : auto m = pe.toString("Parse Error:");
348 0 : logerror(m);
349 : // cleanup file
350 0 : matrixFile.close();
351 0 : throw pe;
352 0 : }
353 0 : catch (invalid_odb_error& ioe)
354 : {
355 0 : parse_info pi(m_path, line, lineNumber);
356 0 : const auto m = pi.toString();
357 0 : logexception_msg(ioe, m);
358 : // cleanup file
359 0 : matrixFile.close();
360 0 : throw ioe;
361 0 : }
362 :
363 90 : return true;
364 90 : }
365 :
366 96 : std::unique_ptr<Odb::Lib::Protobuf::MatrixFile> Odb::Lib::FileModel::Design::MatrixFile::to_protobuf() const
367 : {
368 96 : std::unique_ptr<Odb::Lib::Protobuf::MatrixFile> pMatrixFileMessage(new Odb::Lib::Protobuf::MatrixFile);
369 188 : for (const auto& stepRecord : m_stepRecords)
370 : {
371 92 : auto pStepRecordMessage = pMatrixFileMessage->add_steps();
372 92 : pStepRecordMessage->CopyFrom(*stepRecord->to_protobuf());
373 : }
374 2811 : for (const auto& layerRecord : m_layerRecords)
375 : {
376 2715 : auto pLayerRecordMessage = pMatrixFileMessage->add_layers();
377 2715 : pLayerRecordMessage->CopyFrom(*layerRecord->to_protobuf());
378 : }
379 96 : return pMatrixFileMessage;
380 :
381 0 : }
382 :
383 8 : void Odb::Lib::FileModel::Design::MatrixFile::from_protobuf(const Odb::Lib::Protobuf::MatrixFile& message)
384 : {
385 14 : for (const auto& stepRecord : message.steps())
386 : {
387 6 : auto pStepRecord = std::make_shared<StepRecord>();
388 6 : pStepRecord->from_protobuf(stepRecord);
389 6 : m_stepRecords.push_back(pStepRecord);
390 6 : }
391 215 : for (const auto& layerRecord : message.layers())
392 : {
393 207 : auto pLayerRecord = std::make_shared<LayerRecord>();
394 207 : pLayerRecord->from_protobuf(layerRecord);
395 207 : m_layerRecords.push_back(pLayerRecord);
396 207 : }
397 8 : }
398 :
399 0 : bool MatrixFile::Save(std::ostream& os)
400 : {
401 0 : for (const auto& stepRecord : m_stepRecords)
402 : {
403 0 : os << StepRecord::RECORD_TOKEN << " " << Constants::ARRAY_RECORD_OPEN_TOKEN << std::endl;
404 :
405 0 : os << '\t' << StepRecord::COLUMN_KEY << "=" << stepRecord->column << std::endl;
406 0 : os << '\t' << StepRecord::NAME_KEY << "=" << stepRecord->name << std::endl;
407 0 : os << '\t' << StepRecord::ID_KEY << "=" << stepRecord->id << std::endl;
408 :
409 0 : os << Constants::ARRAY_RECORD_CLOSE_TOKEN << std::endl;
410 0 : os << std::endl;
411 : }
412 :
413 0 : for (const auto& layerRecord : m_layerRecords)
414 : {
415 0 : os << LayerRecord::RECORD_TOKEN << " " << Constants::ARRAY_RECORD_OPEN_TOKEN << std::endl;
416 :
417 0 : os << '\t' << LayerRecord::ROW_KEY << "=" << layerRecord->row << std::endl;
418 0 : os << '\t' << LayerRecord::CONTEXT_KEY << "=" << LayerRecord::contextMap.getValue(layerRecord->context) << std::endl;
419 0 : os << '\t' << LayerRecord::TYPE_KEY << "=" << LayerRecord::typeMap.getValue(layerRecord->type) << std::endl;
420 0 : os << '\t' << LayerRecord::NAME_KEY << "=" << layerRecord->name << std::endl;
421 0 : os << '\t' << LayerRecord::POLARITY_KEY << "=" << LayerRecord::polarityMap.getValue(layerRecord->polarity) << std::endl;
422 0 : os << '\t' << LayerRecord::FORM_KEY << "=" << LayerRecord::formMap.getValue(layerRecord->form) << std::endl;
423 0 : os << '\t' << LayerRecord::DIELECTRIC_TYPE_KEY << "=" << LayerRecord::dielectricTypeMap.getValue(layerRecord->dielectricType) << std::endl;
424 0 : os << '\t' << LayerRecord::DIELECTRIC_NAME_KEY << "=" << layerRecord->dielectricName << std::endl;
425 0 : os << '\t' << LayerRecord::CU_TOP_KEY << "=";
426 0 : if (layerRecord->cuTop != (unsigned int)-1)
427 : {
428 0 : os << layerRecord->cuTop;
429 : }
430 0 : os << std::endl;
431 0 : os << '\t' << LayerRecord::CU_BOTTOM_KEY << "=";
432 0 : if (layerRecord->cuBottom != (unsigned int)-1)
433 : {
434 0 : os << layerRecord->cuBottom;
435 : }
436 0 : os << std::endl;
437 0 : os << '\t' << LayerRecord::REF_KEY << "=";
438 0 : if (layerRecord->ref != (unsigned int)-1)
439 : {
440 0 : os << layerRecord->ref;
441 : }
442 0 : os << std::endl;
443 0 : os << '\t' << LayerRecord::START_NAME_KEY << "=" << layerRecord->startName << std::endl;
444 0 : os << '\t' << LayerRecord::END_NAME_KEY << "=" << layerRecord->endName << std::endl;
445 0 : os << '\t' << LayerRecord::OLD_NAME_KEY << "=" << layerRecord->oldName << std::endl;
446 0 : os << '\t' << LayerRecord::ADD_TYPE_KEY << "=" << layerRecord->addType << std::endl;
447 0 : os << '\t' << LayerRecord::COLOR_KEY << "=" << layerRecord->color.to_string() << std::endl;
448 0 : os << '\t' << LayerRecord::ID_KEY << "=" << layerRecord->id << std::endl;
449 :
450 0 : os << Constants::ARRAY_RECORD_CLOSE_TOKEN << std::endl;
451 0 : os << std::endl;
452 : }
453 :
454 0 : return true;
455 : }
456 :
457 92 : std::unique_ptr<Odb::Lib::Protobuf::MatrixFile::StepRecord> Odb::Lib::FileModel::Design::MatrixFile::StepRecord::to_protobuf() const
458 : {
459 92 : std::unique_ptr<Odb::Lib::Protobuf::MatrixFile::StepRecord> pStepRecordMessage(new Odb::Lib::Protobuf::MatrixFile::StepRecord);
460 92 : pStepRecordMessage->set_column(column);
461 184 : pStepRecordMessage->set_name(Odb::Lib::Text::ToUtf8(name));
462 92 : pStepRecordMessage->set_id(id);
463 92 : return pStepRecordMessage;
464 0 : }
465 :
466 6 : void Odb::Lib::FileModel::Design::MatrixFile::StepRecord::from_protobuf(const Odb::Lib::Protobuf::MatrixFile::StepRecord& message)
467 : {
468 6 : column = message.column();
469 6 : name = message.name();
470 6 : id = message.id();
471 6 : }
472 :
473 2742 : std::unique_ptr<Odb::Lib::Protobuf::MatrixFile::LayerRecord> Odb::Lib::FileModel::Design::MatrixFile::LayerRecord::to_protobuf() const
474 : {
475 2742 : std::unique_ptr<Odb::Lib::Protobuf::MatrixFile::LayerRecord> pLayerRecordMessage(new Odb::Lib::Protobuf::MatrixFile::LayerRecord);
476 5484 : pLayerRecordMessage->set_addtype(Odb::Lib::Text::ToUtf8(addType));
477 2742 : pLayerRecordMessage->set_context(static_cast<Odb::Lib::Protobuf::MatrixFile::LayerRecord::Context>(context));
478 2742 : pLayerRecordMessage->set_cubottom(cuBottom);
479 2742 : pLayerRecordMessage->set_cutop(cuTop);
480 5484 : pLayerRecordMessage->set_dielectricname(Odb::Lib::Text::ToUtf8(dielectricName));
481 2742 : pLayerRecordMessage->set_dielectrictype(static_cast<Odb::Lib::Protobuf::MatrixFile::LayerRecord::DielectricType>(dielectricType));
482 5484 : pLayerRecordMessage->set_endname(Odb::Lib::Text::ToUtf8(endName));
483 2742 : pLayerRecordMessage->set_form(static_cast<Odb::Lib::Protobuf::MatrixFile::LayerRecord::Form>(form));
484 2742 : pLayerRecordMessage->set_id(id);
485 5484 : pLayerRecordMessage->set_name(Odb::Lib::Text::ToUtf8(name));
486 5484 : pLayerRecordMessage->set_oldname(Odb::Lib::Text::ToUtf8(oldName));
487 2742 : pLayerRecordMessage->set_polarity(static_cast<Odb::Lib::Protobuf::Polarity>(polarity));
488 2742 : pLayerRecordMessage->set_ref(ref);
489 2742 : pLayerRecordMessage->set_row(row);
490 5484 : pLayerRecordMessage->set_startname(Odb::Lib::Text::ToUtf8(startName));
491 2742 : pLayerRecordMessage->set_type(static_cast<Odb::Lib::Protobuf::MatrixFile::LayerRecord::Type>(type));
492 2742 : pLayerRecordMessage->mutable_color()->set_red(color.red);
493 2742 : pLayerRecordMessage->mutable_color()->set_green(color.green);
494 2742 : pLayerRecordMessage->mutable_color()->set_blue(color.blue);
495 2742 : pLayerRecordMessage->mutable_color()->set_nopreference(color.noPreference);
496 :
497 2742 : return pLayerRecordMessage;
498 0 : }
499 :
500 234 : void Odb::Lib::FileModel::Design::MatrixFile::LayerRecord::from_protobuf(const Odb::Lib::Protobuf::MatrixFile::LayerRecord& message)
501 : {
502 234 : addType = message.addtype();
503 234 : context = static_cast<Context>(message.context());
504 234 : cuBottom = message.cubottom();
505 234 : cuTop = message.cutop();
506 234 : dielectricName = message.dielectricname();
507 234 : dielectricType = static_cast<DielectricType>(message.dielectrictype());
508 234 : endName = message.endname();
509 234 : form = static_cast<Form>(message.form());
510 234 : id = message.id();
511 234 : name = message.name();
512 234 : oldName = message.oldname();
513 234 : polarity = static_cast<Polarity>(message.polarity());
514 234 : ref = message.ref();
515 234 : row = message.row();
516 234 : startName = message.startname();
517 234 : type = static_cast<Type>(message.type());
518 234 : if (message.has_color())
519 : {
520 234 : color.red = static_cast<decltype(color.red)>(message.color().red());
521 234 : color.green = static_cast<decltype(color.green)>(message.color().green());
522 234 : color.blue = static_cast<decltype(color.blue)>(message.color().blue());
523 234 : color.noPreference = message.color().nopreference();
524 : }
525 : // color unset: keep the parsed/default no-preference color rather
526 : // than overwriting it with an empty message's zeros
527 234 : }
528 : }
|