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