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