OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
ComponentsFile.cpp
Go to the documentation of this file.
1#include "ComponentsFile.h"
2#include <fstream>
3#include <sstream>
4#include <filesystem>
5#include "ArchiveExtractor.h"
6#include "../parse_error.h"
7#include <Logger.h>
8#include <exception>
9#include "str_utils.h"
10#include "../invalid_odb_error.h"
11#include <climits>
13#include "../../Text/Utf8Sanitizer.h"
14
15using namespace std::filesystem;
16
17
19{
21 : m_id((unsigned int)-1)
22 , m_side(BoardSide::BsNone)
23 {
24 }
25
27 {
28 m_attributeNames.clear();
29 m_attributeTextValues.clear();
30 m_componentRecords.clear();
31 m_bomDescriptionRecordsByCpn.clear();
32 }
33
34 std::string ComponentsFile::GetUnits() const
35 {
36 return m_units;
37 }
38
40 {
41 return m_side;
42 }
43
44 std::filesystem::path ComponentsFile::GetPath()
45 {
46 return m_path;
47 }
48
49 std::filesystem::path ComponentsFile::GetDirectory()
50 {
51 return m_directory;
52 }
53
54 std::string ComponentsFile::GetLayerName() const
55 {
56 return m_layerName;
57 }
58
60 {
61 return m_componentRecords;
62 }
63
64 const std::vector<std::string>& ComponentsFile::GetAttributeNames() const
65 {
66 return m_attributeNames;
67 }
68
69 const std::vector<std::string>& ComponentsFile::GetAttributeTextValues() const
70 {
71 return m_attributeTextValues;
72 }
73
75 {
76 return m_bomDescriptionRecordsByCpn;
77 }
78
79 std::unique_ptr<Odb::Lib::Protobuf::ComponentsFile> ComponentsFile::to_protobuf() const
80 {
81 std::unique_ptr<Odb::Lib::Protobuf::ComponentsFile> pComponentsFileMessage(new Odb::Lib::Protobuf::ComponentsFile);
82 pComponentsFileMessage->set_units(Odb::Lib::Text::ToUtf8(m_units));
83 pComponentsFileMessage->set_id(m_id);
84 pComponentsFileMessage->set_side(static_cast<Odb::Lib::Protobuf::BoardSide>(m_side));
85 pComponentsFileMessage->set_layername(Odb::Lib::Text::ToUtf8(m_layerName));
86 pComponentsFileMessage->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
87 pComponentsFileMessage->set_directory(Odb::Lib::Text::ToUtf8(m_directory.string()));
88
89 for (const auto& attributeName : m_attributeNames)
90 {
91 pComponentsFileMessage->add_attributenames(Odb::Lib::Text::ToUtf8(attributeName));
92 }
93
94 for (const auto& attributeTextValue : m_attributeTextValues)
95 {
96 pComponentsFileMessage->add_attributetextvalues(Odb::Lib::Text::ToUtf8(attributeTextValue));
97 }
98
99 for (const auto& pComponentRecord : m_componentRecords)
100 {
101 // Log component height serialization
103 pComponentRecord->compName,
104 pComponentRecord->pkgRef,
105 pComponentRecord->GetAttributeLookupTable(),
106 m_attributeNames);
107
108 auto pComponentRecordMessage = pComponentRecord->to_protobuf();
109 pComponentsFileMessage->add_componentrecords()->CopyFrom(*pComponentRecordMessage);
110 }
111
112 for (const auto& kvBomDescriptionRecord : m_bomDescriptionRecordsByCpn)
113 {
114 auto pBomDescriptionRecordMessage = kvBomDescriptionRecord.second->to_protobuf();
115 (*pComponentsFileMessage->mutable_bomdescriptionrecordsbycpn())[Odb::Lib::Text::ToUtf8(kvBomDescriptionRecord.first)] = *pBomDescriptionRecordMessage;
116 }
117
118 return pComponentsFileMessage;
119 }
120
121 void ComponentsFile::from_protobuf(const Odb::Lib::Protobuf::ComponentsFile& message)
122 {
123 m_units = message.units();
124 m_id = message.id();
125 m_side = static_cast<BoardSide>(message.side());
126 m_layerName = message.layername();
127 m_path = message.path();
128 m_directory = message.directory();
129
130 for (const auto& attributeName : message.attributenames())
131 {
132 m_attributeNames.push_back(attributeName);
133 }
134
135 for (const auto& attributeTextValue : message.attributetextvalues())
136 {
137 m_attributeTextValues.push_back(attributeTextValue);
138 }
139
140 for (const auto& componentRecordMessage : message.componentrecords())
141 {
142 auto pComponentRecord = std::make_shared<ComponentRecord>();
143 pComponentRecord->from_protobuf(componentRecordMessage);
144 m_componentRecords.push_back(pComponentRecord);
145 }
146
147 for (const auto& kvBomDescriptionRecord : message.bomdescriptionrecordsbycpn())
148 {
149 auto pBomDescriptionRecord = std::make_shared<BomDescriptionRecord>();
150 pBomDescriptionRecord->from_protobuf(kvBomDescriptionRecord.second);
151 m_bomDescriptionRecordsByCpn[kvBomDescriptionRecord.first] = pBomDescriptionRecord;
152 }
153 }
154
155 bool ComponentsFile::Save(std::ostream& os)
156 {
157 return true;
158 }
159
160 std::unique_ptr<Odb::Lib::Protobuf::ComponentsFile::BomDescriptionRecord> ComponentsFile::BomDescriptionRecord::to_protobuf() const
161 {
162 std::unique_ptr<Odb::Lib::Protobuf::ComponentsFile::BomDescriptionRecord> pBomDescriptionRecordMessage(new Odb::Lib::Protobuf::ComponentsFile::BomDescriptionRecord);
163 pBomDescriptionRecordMessage->set_cpn(Odb::Lib::Text::ToUtf8(cpn));
164 pBomDescriptionRecordMessage->set_pkg(Odb::Lib::Text::ToUtf8(pkg));
165 pBomDescriptionRecordMessage->set_ipn(Odb::Lib::Text::ToUtf8(ipn));
166 for (const auto& description : descriptions)
167 {
168 pBomDescriptionRecordMessage->add_descriptions()->assign(Odb::Lib::Text::ToUtf8(description));
169 }
170 pBomDescriptionRecordMessage->set_vpl_vnd(Odb::Lib::Text::ToUtf8(vpl_vnd));
171 pBomDescriptionRecordMessage->set_vpl_mpn(Odb::Lib::Text::ToUtf8(vpl_mpn));
172 pBomDescriptionRecordMessage->set_vnd(Odb::Lib::Text::ToUtf8(vnd));
173 pBomDescriptionRecordMessage->set_mpn(Odb::Lib::Text::ToUtf8(mpn));
174 return pBomDescriptionRecordMessage;
175 }
176
177 void ComponentsFile::BomDescriptionRecord::from_protobuf(const Odb::Lib::Protobuf::ComponentsFile::BomDescriptionRecord& message)
178 {
179 cpn = message.cpn();
180 pkg = message.pkg();
181 ipn = message.ipn();
182 for (const auto& description : message.descriptions())
183 {
184 descriptions.push_back(description);
185 }
186 vpl_vnd = message.vpl_vnd();
187 vpl_mpn = message.vpl_mpn();
188 vnd = message.vnd();
189 mpn = message.mpn();
190 }
191
193 {
194 m_toeprintRecords.clear();
195 m_propertyRecords.clear();
196 }
197
198 std::unique_ptr<Odb::Lib::Protobuf::ComponentsFile::ComponentRecord> ComponentsFile::ComponentRecord::to_protobuf() const
199 {
200 std::unique_ptr<Odb::Lib::Protobuf::ComponentsFile::ComponentRecord> pComponentRecordMessage(new Odb::Lib::Protobuf::ComponentsFile::ComponentRecord);
201 pComponentRecordMessage->set_pkgref(pkgRef);
202 pComponentRecordMessage->set_locationx(locationX);
203 pComponentRecordMessage->set_locationy(locationY);
204 pComponentRecordMessage->set_rotation(rotation);
205 pComponentRecordMessage->set_mirror(mirror);
206 pComponentRecordMessage->set_compname(Odb::Lib::Text::ToUtf8(compName));
207 pComponentRecordMessage->set_partname(Odb::Lib::Text::ToUtf8(partName));
208 //pComponentRecordMessage->set_attributes(attributes);
209 pComponentRecordMessage->set_index(index);
210 if (id.has_value())
211 {
212 pComponentRecordMessage->set_id(*id);
213 }
214
215 for (const auto& pPropertyRecord : m_propertyRecords)
216 {
217 auto pPropertyRecordMessage = pPropertyRecord->to_protobuf();
218 pComponentRecordMessage->add_propertyrecords()->CopyFrom(*pPropertyRecordMessage);
219 }
220
221 for (const auto& pToeprintRecord : m_toeprintRecords)
222 {
223 auto pToeprintRecordMessage = pToeprintRecord->to_protobuf();
224 pComponentRecordMessage->add_toeprintrecords()->CopyFrom(*pToeprintRecordMessage);
225 }
226
227 for (const auto& kvAttributeAssignment : m_attributeLookupTable)
228 {
229 (*pComponentRecordMessage->mutable_attributelookuptable())[Odb::Lib::Text::ToUtf8(kvAttributeAssignment.first)] = Odb::Lib::Text::ToUtf8(kvAttributeAssignment.second);
230 }
231
232 return pComponentRecordMessage;
233 }
234
235 void ComponentsFile::ComponentRecord::from_protobuf(const Odb::Lib::Protobuf::ComponentsFile::ComponentRecord& message)
236 {
237 pkgRef = message.pkgref();
238 locationX = message.locationx();
239 locationY = message.locationy();
240 rotation = message.rotation();
241 mirror = message.mirror();
242 compName = message.compname();
243 partName = message.partname();
244 //attributes = message.attributes();
245 index = message.index();
246 if (message.has_id())
247 {
248 id = message.id();
249 }
250
251 for (const auto& propertyRecordMessage : message.propertyrecords())
252 {
253 auto pPropertyRecord = std::make_shared<PropertyRecord>();
254 pPropertyRecord->from_protobuf(propertyRecordMessage);
255 m_propertyRecords.push_back(pPropertyRecord);
256 }
257
258 for (const auto& toeprintRecordMessage : message.toeprintrecords())
259 {
260 auto pToeprintRecord = std::make_shared<ToeprintRecord>();
261 pToeprintRecord->from_protobuf(toeprintRecordMessage);
262 m_toeprintRecords.push_back(pToeprintRecord);
263 }
264
265 for (const auto& kvAttributeAssignment : message.attributelookuptable())
266 {
267 m_attributeLookupTable[kvAttributeAssignment.first] = kvAttributeAssignment.second;
268 }
269 }
270
271 std::unique_ptr<Odb::Lib::Protobuf::ComponentsFile::ComponentRecord::ToeprintRecord> ComponentsFile::ComponentRecord::ToeprintRecord::to_protobuf() const
272 {
273 std::unique_ptr<Odb::Lib::Protobuf::ComponentsFile::ComponentRecord::ToeprintRecord> pToeprintRecordMessage(new Odb::Lib::Protobuf::ComponentsFile::ComponentRecord::ToeprintRecord);
274 pToeprintRecordMessage->set_pinnumber(pinNumber);
275 pToeprintRecordMessage->set_locationx(locationX);
276 pToeprintRecordMessage->set_locationy(locationY);
277 pToeprintRecordMessage->set_rotation(rotation);
278 pToeprintRecordMessage->set_mirror(mirror);
279 pToeprintRecordMessage->set_netnumber(netNumber);
280 pToeprintRecordMessage->set_subnetnumber(subnetNumber);
281 pToeprintRecordMessage->set_name(Odb::Lib::Text::ToUtf8(name));
282 return pToeprintRecordMessage;
283 }
284
285 void ComponentsFile::ComponentRecord::ToeprintRecord::from_protobuf(const Odb::Lib::Protobuf::ComponentsFile::ComponentRecord::ToeprintRecord& message)
286 {
287 pinNumber = message.pinnumber();
288 locationX = message.locationx();
289 locationY = message.locationy();
290 rotation = message.rotation();
291 mirror = message.mirror();
292 netNumber = message.netnumber();
293 subnetNumber = message.subnetnumber();
294 name = message.name();
295 }
296
297 bool ComponentsFile::Parse(std::filesystem::path directory)
298 {
299 std::ifstream componentsFile;
300 int lineNumber = 0;
301 std::string line;
302
303 try
304 {
305 m_directory = directory;
306
307 m_layerName = m_directory.filename().string();
308 if (m_layerName == TOP_COMPONENTS_LAYER_NAME ||
309 m_layerName == BOTTOM_COMPONENTS_LAYER_NAME)
310 {
311 m_side = m_layerName == TOP_COMPONENTS_LAYER_NAME ?
314 }
315 else
316 {
317 // not a components layer
318 return true;
319 }
320
321 loginfo("checking for extraction...");
322
323 std::filesystem::path componentsFilePath;
324 for (const std::string componentsFilename : COMPONENTS_FILENAMES)
325 {
326 loginfo("trying components file: [" + componentsFilename + "]...");
327
328 componentsFilePath = Utils::ArchiveExtractor::getUncompressedFilePath(m_directory, componentsFilename);
329 if (exists(componentsFilePath) && is_regular_file(componentsFilePath))
330 {
331 loginfo("found components file: [" + componentsFilePath.string() + "]");
332 break;
333 }
334 }
335
336 m_path = componentsFilePath;
337
338 loginfo("any extraction complete, parsing data...");
339
340 if (!std::filesystem::exists(m_path))
341 {
342 auto message = "components file does not exist: [" + m_path.string() + "]";
343 throw invalid_odb_error(message.c_str());
344 }
345 else if (!std::filesystem::is_regular_file(m_path))
346 {
347 auto message = "components is not a file: [" + m_path.string() + "]";
348 throw invalid_odb_error(message.c_str());
349 }
350
351 componentsFile.open(m_path.string(), std::ios::in);
352 if (!componentsFile.is_open())
353 {
354 auto message = "unable to open components file: [" + m_path.string() + "]";
355 throw invalid_odb_error(message.c_str());
356 }
357
358 std::shared_ptr<ComponentRecord> pCurrentComponentRecord;
359 std::shared_ptr<BomDescriptionRecord> pCurrentBomDescriptionRecord;
360
361 while (std::getline(componentsFile, line))
362 {
363 lineNumber++;
364
365 // trim whitespace from beginning and end of line
366 Utils::str_trim(line);
367 if (!line.empty())
368 {
369 std::stringstream lineStream(line);
370 //char firstChar = line[0];
371
372 if (line.find(COMMENT_TOKEN) == 0)
373 {
374 // comment line
375 }
376 else if (line.find(UNITS_TOKEN) == 0)
377 {
378 // units line
379 std::string token;
380 if (!std::getline(lineStream, token, '='))
381 {
382 throw_parse_error(m_path, line, token, lineNumber);
383 }
384 else if (!std::getline(lineStream, token, '='))
385 {
386 throw_parse_error(m_path, line, token, lineNumber);
387 }
388
389 m_units = token;
390 }
391 else if (line.find(ID_TOKEN) == 0)
392 {
393 std::string token;
394 if (!std::getline(lineStream, token, '='))
395 {
396 throw_parse_error(m_path, line, token, lineNumber);
397 }
398 else if (!std::getline(lineStream, token, '='))
399 {
400 throw_parse_error(m_path, line, token, lineNumber);
401 }
402 m_id = std::stoul(token);
403 }
404 else if (line.find(ATTRIBUTE_NAME_TOKEN) == 0)
405 {
406 // component attribute name line
407 std::string token;
408 // TODO: continue on failing line parse, to make a less strict/more robust parser (make a flag: enum ParseStrictness { strict, lax })
409 if (!std::getline(lineStream, token, ' '))
410 {
411 throw_parse_error(m_path, line, token, lineNumber);
412 }
413 else if (!std::getline(lineStream, token, ' '))
414 {
415 throw_parse_error(m_path, line, token, lineNumber);
416 }
417 m_attributeNames.push_back(token);
418 }
419 else if (line.find(ATTRIBUTE_VALUE_TOKEN) == 0)
420 {
421 // component attribute text string values
422 std::string token;
423 if (!std::getline(lineStream, token, ' '))
424 {
425 throw_parse_error(m_path, line, token, lineNumber);
426 }
427 else if (!std::getline(lineStream, token, ' '))
428 {
429 throw_parse_error(m_path, line, token, lineNumber);
430 }
431 m_attributeTextValues.push_back(token);
432 }
433 else if (line.find(ComponentRecord::RECORD_TOKEN) == 0)
434 {
435 // component record line
436 std::string token;
437
438 lineStream >> token;
440 {
441 throw_parse_error(m_path, line, token, lineNumber);
442 }
443
444 if (pCurrentBomDescriptionRecord != nullptr)
445 {
446 // BomDescriptionRecord end (i.e. MPN) line
447 m_bomDescriptionRecordsByCpn[pCurrentBomDescriptionRecord->cpn] = pCurrentBomDescriptionRecord;
448 pCurrentBomDescriptionRecord.reset();
449 }
450
451 // do we have a current component record?
452 if (pCurrentComponentRecord != nullptr)
453 {
454 // finish it up and add it to the list
455 m_componentRecords.push_back(pCurrentComponentRecord);
456 pCurrentComponentRecord.reset();
457 }
458
459 // create a new current component record
460 pCurrentComponentRecord = std::make_shared<ComponentRecord>();
461
462 lineStream >> pCurrentComponentRecord->pkgRef;
463 lineStream >> pCurrentComponentRecord->locationX;
464 lineStream >> pCurrentComponentRecord->locationY;
465 lineStream >> pCurrentComponentRecord->rotation;
466
467 char mirror;
468 lineStream >> mirror;
469 pCurrentComponentRecord->mirror = (mirror == 'M');
470
471 lineStream >> pCurrentComponentRecord->compName;
472 lineStream >> pCurrentComponentRecord->partName;
473 //lineStream >> pCurrentComponentRecord->attributes;
474
475 if (m_componentRecords.size() > UINT_MAX)
476 {
477 throw_parse_error(m_path, line, token, lineNumber);
478 }
479
480 pCurrentComponentRecord->index = static_cast<unsigned int>(m_componentRecords.size());
481
482 std::string attrIdString;
483 lineStream >> attrIdString;
484
485 // Log component height parsing start
487 pCurrentComponentRecord->compName,
488 pCurrentComponentRecord->pkgRef,
489 attrIdString);
490
491 if (!pCurrentComponentRecord->ParseAttributeLookupTable(attrIdString, pCurrentComponentRecord->id))
492 {
493 throw_parse_error(m_path, line, token, lineNumber);
494 }
495
496 // Log component height parsing result
498 pCurrentComponentRecord->compName,
499 pCurrentComponentRecord->pkgRef,
500 pCurrentComponentRecord->GetAttributeLookupTable(),
501 m_attributeNames);
502 }
503 else if (line.find(PropertyRecord::RECORD_TOKEN) == 0)
504 {
505 // component property record line
506 std::string token;
507 if (!(lineStream >> token))
508 {
509 throw_parse_error(m_path, line, token, lineNumber);
510 }
511
512 if (token != PropertyRecord::RECORD_TOKEN)
513 {
514 throw_parse_error(m_path, line, token, lineNumber);
515 }
516
517 auto pPropertyRecord = std::make_shared<PropertyRecord>();
518
519 if (!(lineStream >> pPropertyRecord->name))
520 {
521 throw_parse_error(m_path, line, token, lineNumber);
522 }
523
524 if (!(lineStream >> token))
525 {
526 throw_parse_error(m_path, line, token, lineNumber);
527 }
528
529 if (!token.empty())
530 {
531 // handle case where the beginning of the value, after the opening single-quote, is whitespace...
532 if (token == "'")
533 {
534 // eat the single-quote and get the next token, i.e. the actual value (w/ space in front)
535 if (!(lineStream >> token))
536 {
537 throw_parse_error(m_path, line, token, lineNumber);
538 }
539 }
540
541 if (token.size() > 0 && token[0] == '\'')
542 {
543 // remove leading quote
544 token.erase(0, 1);
545 }
546
547 if (token.size() > 0 && token[token.size() - 1] == '\'')
548 {
549 // remove trailing quote
550 token.erase(token.size() - 1);
551 }
552
553 Utils::str_trim(token);
554 }
555
556 pPropertyRecord->value = token;
557
558 double f;
559 while (lineStream >> f)
560 {
561 pPropertyRecord->floatValues.push_back(f);
562 }
563
564 pCurrentComponentRecord->m_propertyRecords.push_back(pPropertyRecord);
565 }
566 else if (line.find(ComponentRecord::ToeprintRecord::RECORD_TOKEN) == 0)
567 {
568 // component toeprint record line
569 std::string token;
570 lineStream >> token;
572 {
573 throw_parse_error(m_path, line, token, lineNumber);
574 }
575
576 auto pToeprintRecord = std::make_shared<ComponentRecord::ToeprintRecord>();
577 lineStream >> pToeprintRecord->pinNumber;
578 lineStream >> pToeprintRecord->locationX;
579 lineStream >> pToeprintRecord->locationY;
580 lineStream >> pToeprintRecord->rotation;
581
582 char mirror;
583 lineStream >> mirror;
584 pToeprintRecord->mirror = (mirror == 'M' || mirror == 'm');
585
586 lineStream >> pToeprintRecord->netNumber;
587 if (pToeprintRecord->netNumber == (unsigned int)-1)
588 {
589 // netNumber == -1
590 parse_info pi(m_path, line, lineNumber);
591 logdebug(pi.toString("Component Toeprint record with netNumber = -1"));
592
593 if (!m_allowToepintNetNumbersOfNegative1)
594 {
595 throw_parse_error(m_path, line, token, lineNumber);
596 }
597 }
598
599 lineStream >> pToeprintRecord->subnetNumber;
600 if (pToeprintRecord->subnetNumber == (unsigned int)-1)
601 {
602 // subnetNumber == -1
603 parse_info pi(m_path, line, lineNumber);
604 logdebug(pi.toString("Component Toeprint record with subnetNumber = -1"));
605
606 if (!m_allowToepintNetNumbersOfNegative1)
607 {
608 throw_parse_error(m_path, line, token, lineNumber);
609 }
610 }
611
612 lineStream >> pToeprintRecord->name;
613
614 pCurrentComponentRecord->m_toeprintRecords.push_back(pToeprintRecord);
615 }
617 {
618 // BomDescriptionRecord start (i.e. CPN) line
619
620 std::string token;
621 lineStream >> token;
623 {
624 throw_parse_error(m_path, line, token, lineNumber);
625 }
626
627 if (pCurrentBomDescriptionRecord != nullptr)
628 {
629 m_bomDescriptionRecordsByCpn[pCurrentBomDescriptionRecord->cpn] = pCurrentBomDescriptionRecord;
630 pCurrentBomDescriptionRecord.reset();
631 }
632
633 pCurrentBomDescriptionRecord = std::make_shared<ComponentsFile::BomDescriptionRecord>();
634 }
636 {
637 std::string token;
638 lineStream >> token;
640 {
641 throw_parse_error(m_path, line, token, lineNumber);
642 }
643
644 if (pCurrentBomDescriptionRecord == nullptr)
645 {
646 throw_parse_error(m_path, line, token, lineNumber);
647 }
648
649 if (!(lineStream >> pCurrentBomDescriptionRecord->ipn))
650 {
651 // blank/ no IPN value allowed
652 //throw_parse_error(m_path, line, token, lineNumber);
653 }
654 }
656 {
657 std::string token;
658 lineStream >> token;
660 {
661 throw_parse_error(m_path, line, token, lineNumber);
662 }
663
664 if (pCurrentBomDescriptionRecord == nullptr)
665 {
666 throw_parse_error(m_path, line, token, lineNumber);
667 }
668
669 std::string description;
670
671 if (!(lineStream >> description))
672 {
673 // allow blank/no description value
674 //throw_parse_error(m_path, line, token, lineNumber);
675 }
676
677 pCurrentBomDescriptionRecord->descriptions.push_back(description);
678 }
680 {
681 std::string token;
682 lineStream >> token;
684 {
685 throw_parse_error(m_path, line, token, lineNumber);
686 }
687
688 if (pCurrentBomDescriptionRecord == nullptr)
689 {
690 throw_parse_error(m_path, line, token, lineNumber);
691 }
692
693 if (!(lineStream >> pCurrentBomDescriptionRecord->vpl_vnd))
694 {
695 throw_parse_error(m_path, line, token, lineNumber);
696 }
697 }
699 {
700 std::string token;
701 lineStream >> token;
703 {
704 throw_parse_error(m_path, line, token, lineNumber);
705 }
706
707 if (pCurrentBomDescriptionRecord == nullptr)
708 {
709 throw_parse_error(m_path, line, token, lineNumber);
710 }
711
712 if (!(lineStream >> pCurrentBomDescriptionRecord->vpl_mpn))
713 {
714 throw_parse_error(m_path, line, token, lineNumber);
715 }
716 }
718 {
719 std::string token;
720 lineStream >> token;
722 {
723 throw_parse_error(m_path, line, token, lineNumber);
724 }
725
726 if (pCurrentBomDescriptionRecord == nullptr)
727 {
728 throw_parse_error(m_path, line, token, lineNumber);
729 }
730
731 if (!(lineStream >> pCurrentBomDescriptionRecord->vnd))
732 {
733 throw_parse_error(m_path, line, token, lineNumber);
734 }
735 }
737 {
738 std::string token;
739 lineStream >> token;
741 {
742 throw_parse_error(m_path, line, token, lineNumber);
743 }
744
745 if (pCurrentBomDescriptionRecord == nullptr)
746 {
747 throw_parse_error(m_path, line, token, lineNumber);
748 }
749
750 if (!(lineStream >> pCurrentBomDescriptionRecord->mpn))
751 {
752 throw_parse_error(m_path, line, token, lineNumber);
753 }
754
755 // BomDescriptionRecord end (i.e. MPN) line
756 //m_bomDescriptionRecordsByCpn[pCurrentBomDescriptionRecord->cpn] = pCurrentBomDescriptionRecord;
757 //pCurrentBomDescriptionRecord.reset();
758 }
759 }
760 }
761
762 if (pCurrentBomDescriptionRecord != nullptr)
763 {
764 // BomDescriptionRecord end (i.e. MPN) line
765 m_bomDescriptionRecordsByCpn[pCurrentBomDescriptionRecord->cpn] = pCurrentBomDescriptionRecord;
766 pCurrentBomDescriptionRecord.reset();
767 }
768
769 // do we have a current component record? (finish up the last record in the file- i.e. ran out of file)
770 if (pCurrentComponentRecord != nullptr)
771 {
772 // finish it up and add it to the list
773 m_componentRecords.push_back(pCurrentComponentRecord);
774 pCurrentComponentRecord.reset();
775 }
776
777 componentsFile.close();
778 }
779 catch (parse_error& pe)
780 {
781 auto m = pe.toString("Parse Error:");
782 logerror(m);
783 componentsFile.close();
784 throw pe;
785 }
786 catch (std::exception& e)
787 {
788 parse_info pi(m_path, line, lineNumber);
789 const auto m = pi.toString();
790 logexception_msg(e, m);
791 componentsFile.close();
792 throw e;
793 }
794
795 return true;
796 }
797}
#define logexception_msg(e, m)
Definition Logger.h:29
#define logerror(s)
Definition Logger.h:17
#define loginfo(s)
Definition Logger.h:14
#define logdebug(s)
Definition Logger.h:23
void logParseStart(const std::string &compName, unsigned int pkgRef, const std::string &attrIdString) const
void logParseResult(const std::string &compName, unsigned int pkgRef, const std::map< std::string, std::string > &lookupTable, const std::vector< std::string > &attributeNames) const
void logSerialization(const std::string &compName, unsigned int pkgRef, const std::map< std::string, std::string > &lookupTable, const std::vector< std::string > &attributeNames) const
static constexpr const char * TOP_COMPONENTS_LAYER_NAME
const ComponentRecord::Vector & GetComponentRecords() const
const BomDescriptionRecord::StringMap & GetBomDescriptionRecordsByCpn() const
static constexpr const char * BOTTOM_COMPONENTS_LAYER_NAME
const std::vector< std::string > & GetAttributeTextValues() const
const std::vector< std::string > & GetAttributeNames() const
std::unique_ptr< Odb::Lib::Protobuf::ComponentsFile > to_protobuf() const override
void from_protobuf(const Odb::Lib::Protobuf::ComponentsFile &message) override
bool Parse(std::filesystem::path directory)
std::string toString(const std::string &message="") const
static std::filesystem::path getUncompressedFilePath(const std::filesystem::path &directory, const std::string &filename)
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.
BoardSide
Definition enums.h:6
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
void from_protobuf(const Odb::Lib::Protobuf::ComponentsFile::BomDescriptionRecord &message) override
std::unique_ptr< Odb::Lib::Protobuf::ComponentsFile::BomDescriptionRecord > to_protobuf() const override
std::map< std::string, std::shared_ptr< BomDescriptionRecord > > StringMap
void from_protobuf(const Odb::Lib::Protobuf::ComponentsFile::ComponentRecord::ToeprintRecord &message) override
std::unique_ptr< Odb::Lib::Protobuf::ComponentsFile::ComponentRecord::ToeprintRecord > to_protobuf() const override
std::unique_ptr< Odb::Lib::Protobuf::ComponentsFile::ComponentRecord > to_protobuf() const override
void from_protobuf(const Odb::Lib::Protobuf::ComponentsFile::ComponentRecord &message) override
std::vector< std::shared_ptr< ComponentRecord > > Vector
static constexpr const char * RECORD_TOKEN
std::string toString(const std::string &message="") const
Definition parse_info.cpp:6