LCOV - code coverage report
Current view: top level - OdbDesignLib/FileModel/Design - ComponentsFile.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 77.7 % 435 338
Test Date: 2026-11-21 03:51:50 Functions: 62.5 % 24 15
Legend: Lines: hit not hit

            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         3224 :         ComponentsFile::ComponentsFile()
      21         3224 :                 : m_id((unsigned int)-1)
      22         3224 :                 , m_side(BoardSide::BsNone)
      23              :         {
      24         3224 :         }
      25              : 
      26         2672 :         ComponentsFile::~ComponentsFile()
      27              :         {
      28         2672 :                 m_attributeNames.clear();
      29         2672 :                 m_attributeTextValues.clear();
      30         2672 :                 m_componentRecords.clear();
      31         2672 :                 m_bomDescriptionRecordsByCpn.clear();
      32         2672 :         }
      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        62592 :         ComponentsFile::ComponentRecord::~ComponentRecord()
     193              :         {
     194        62592 :                 m_toeprintRecords.clear();
     195        62592 :                 m_propertyRecords.clear();
     196        62592 :         }
     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         3056 :         bool ComponentsFile::Parse(std::filesystem::path directory)
     298              :         {
     299         3056 :                 std::ifstream componentsFile;
     300         3056 :                 int lineNumber = 0;
     301         3056 :                 std::string line;
     302              : 
     303              :                 try
     304              :                 {
     305         3056 :                         m_directory = directory;
     306              : 
     307         3056 :                         m_layerName = m_directory.filename().string();
     308         6018 :                         if (m_layerName == TOP_COMPONENTS_LAYER_NAME ||
     309         2962 :                                 m_layerName == BOTTOM_COMPONENTS_LAYER_NAME)
     310              :                         {
     311          188 :                                 m_side = m_layerName == TOP_COMPONENTS_LAYER_NAME ?
     312              :                                         BoardSide::Top :
     313              :                                         BoardSide::Bottom;
     314              :                         }
     315              :                         else
     316              :                         {
     317              :                                 // not a components layer
     318         2868 :                                 return true;
     319              :                         }
     320              : 
     321          752 :                         loginfo("checking for extraction...");
     322              : 
     323          188 :                         std::filesystem::path componentsFilePath;
     324          376 :                         for (const std::string componentsFilename : COMPONENTS_FILENAMES)
     325              :                         {
     326          376 :                                 loginfo("trying components file: [" + componentsFilename + "]...");
     327              : 
     328          188 :                                 componentsFilePath = Utils::ArchiveExtractor::getUncompressedFilePath(m_directory, componentsFilename);
     329          188 :                                 if (exists(componentsFilePath) && is_regular_file(componentsFilePath))
     330              :                                 {                       
     331          376 :                                         loginfo("found components file: [" + componentsFilePath.string() + "]");
     332          188 :                                         break;
     333              :                                 }
     334          188 :                         }       
     335              : 
     336          188 :                         m_path = componentsFilePath;                    
     337              : 
     338          752 :                         loginfo("any extraction complete, parsing data...");
     339              : 
     340          188 :                         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          188 :                         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          188 :                         componentsFile.open(m_path.string(), std::ios::in);
     352          188 :                         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          188 :                         std::shared_ptr<ComponentRecord> pCurrentComponentRecord;
     359          188 :                         std::shared_ptr<BomDescriptionRecord> pCurrentBomDescriptionRecord;
     360              :                         
     361      2584322 :                         while (std::getline(componentsFile, line))
     362              :                         {
     363      2584134 :                                 lineNumber++;
     364              : 
     365              :                                 // trim whitespace from beginning and end of line
     366      2584134 :                                 Utils::str_trim(line);
     367      2584134 :                                 if (!line.empty())
     368              :                                 {
     369      2583758 :                                         std::stringstream lineStream(line);
     370              :                                         //char firstChar = line[0];                     
     371              : 
     372      2583758 :                                         if (line.find(COMMENT_TOKEN) == 0)
     373              :                                         {
     374              :                                                 // comment line
     375              :                                         }
     376      2413934 :                                         else if (line.find(UNITS_TOKEN) == 0)
     377              :                                         {
     378              :                                                 // units line
     379          188 :                                                 std::string token;
     380          188 :                                                 if (!std::getline(lineStream, token, '='))
     381              :                                                 {
     382            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     383              :                                                 }
     384          188 :                                                 else if (!std::getline(lineStream, token, '='))
     385              :                                                 {
     386            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     387              :                                                 }
     388              : 
     389          188 :                                                 m_units = token;
     390          188 :                                         }
     391      2413746 :                                         else if (line.find(ID_TOKEN) == 0)
     392              :                                         {
     393          188 :                                                 std::string token;
     394          188 :                                                 if (!std::getline(lineStream, token, '='))
     395              :                                                 {
     396            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     397              :                                                 }
     398          188 :                                                 else if (!std::getline(lineStream, token, '='))
     399              :                                                 {
     400            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     401              :                                                 }
     402          188 :                                                 m_id = std::stoul(token);
     403          188 :                                         }
     404      2413558 :                                         else if (line.find(ATTRIBUTE_NAME_TOKEN) == 0)
     405              :                                         {
     406              :                                                 // component attribute name line        
     407          520 :                                                 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          520 :                                                 if (!std::getline(lineStream, token, ' '))
     410              :                                                 {
     411            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     412              :                                                 }
     413          520 :                                                 else if (!std::getline(lineStream, token, ' '))
     414              :                                                 {
     415            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     416              :                                                 }
     417          520 :                                                 m_attributeNames.push_back(token);
     418          520 :                                         }
     419      2413038 :                                         else if (line.find(ATTRIBUTE_VALUE_TOKEN) == 0)
     420              :                                         {
     421              :                                                 // component attribute text string values       
     422          324 :                                                 std::string token;
     423          324 :                                                 if (!std::getline(lineStream, token, ' '))
     424              :                                                 {
     425            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     426              :                                                 }
     427          324 :                                                 else if (!std::getline(lineStream, token, ' '))
     428              :                                                 {
     429            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     430              :                                                 }
     431          324 :                                                 m_attributeTextValues.push_back(token);
     432          324 :                                         }
     433      2412714 :                                         else if (line.find(ComponentRecord::RECORD_TOKEN) == 0)
     434              :                                         {
     435              :                                                 // component record line
     436        72066 :                                                 std::string token;
     437              : 
     438        72066 :                                                 lineStream >> token;
     439        72066 :                                                 if (token != ComponentRecord::RECORD_TOKEN)
     440              :                                                 {
     441            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     442              :                                                 }
     443              : 
     444        72066 :                                                 if (pCurrentBomDescriptionRecord != nullptr)
     445              :                                                 {
     446              :                                                         // BomDescriptionRecord end (i.e. MPN) line
     447        24840 :                                                         m_bomDescriptionRecordsByCpn[pCurrentBomDescriptionRecord->cpn] = pCurrentBomDescriptionRecord;
     448        24840 :                                                         pCurrentBomDescriptionRecord.reset();
     449              :                                                 }
     450              : 
     451              :                                                 // do we have a current component record?
     452        72066 :                                                 if (pCurrentComponentRecord != nullptr)
     453              :                                                 {
     454              :                                                         // finish it up and add it to the list
     455        71878 :                                                         m_componentRecords.push_back(pCurrentComponentRecord);
     456        71878 :                                                         pCurrentComponentRecord.reset();
     457              :                                                 }
     458              : 
     459              :                                                 // create a new current component record
     460        72066 :                                                 pCurrentComponentRecord = std::make_shared<ComponentRecord>();
     461              : 
     462        72066 :                                                 lineStream >> pCurrentComponentRecord->pkgRef;
     463        72066 :                                                 lineStream >> pCurrentComponentRecord->locationX;
     464        72066 :                                                 lineStream >> pCurrentComponentRecord->locationY;
     465        72066 :                                                 lineStream >> pCurrentComponentRecord->rotation;
     466              : 
     467              :                                                 char mirror;
     468        72066 :                                                 lineStream >> mirror;
     469        72066 :                                                 pCurrentComponentRecord->mirror = (mirror == 'M');
     470              : 
     471        72066 :                                                 lineStream >> pCurrentComponentRecord->compName;
     472        72066 :                                                 lineStream >> pCurrentComponentRecord->partName;
     473              :                                                 //lineStream >> pCurrentComponentRecord->attributes;
     474              : 
     475        72066 :                                                 if (m_componentRecords.size() > UINT_MAX)
     476              :                                                 {
     477            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);                                                     
     478              :                                                 }
     479              :                                                 
     480        72066 :                                                 pCurrentComponentRecord->index = static_cast<unsigned int>(m_componentRecords.size());
     481              : 
     482        72066 :                                                 std::string attrIdString;
     483        72066 :                                                 lineStream >> attrIdString;
     484              : 
     485              :                                                 // Log component height parsing start
     486        72066 :                                                 ComponentHeightTracer::instance().logParseStart(
     487        72066 :                                                         pCurrentComponentRecord->compName,
     488        72066 :                                                         pCurrentComponentRecord->pkgRef,
     489              :                                                         attrIdString);
     490              : 
     491        72066 :                                                 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       144132 :                                                 ComponentHeightTracer::instance().logParseResult(
     498        72066 :                                                         pCurrentComponentRecord->compName,
     499        72066 :                                                         pCurrentComponentRecord->pkgRef,
     500        72066 :                                                         pCurrentComponentRecord->GetAttributeLookupTable(),
     501        72066 :                                                         m_attributeNames);                                              
     502        72066 :                                         }
     503      2340648 :                                         else if (line.find(PropertyRecord::RECORD_TOKEN) == 0)
     504              :                                         {
     505              :                                                 // component property record line
     506      1927950 :                                                 std::string token;
     507      1927950 :                                                 if (!(lineStream >> token))
     508              :                                                 {
     509            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     510              :                                                 }
     511              : 
     512      1927950 :                                                 if (token != PropertyRecord::RECORD_TOKEN)
     513              :                                                 {
     514            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     515              :                                                 }
     516              : 
     517      1927950 :                                                 auto pPropertyRecord = std::make_shared<PropertyRecord>();
     518              : 
     519      1927950 :                                                 if (!(lineStream >> pPropertyRecord->name))
     520              :                                                 {
     521            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     522              :                                                 }
     523              : 
     524      1927950 :                                                 if (!(lineStream >> token))
     525              :                                                 {
     526            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     527              :                                                 }
     528              : 
     529      1927950 :                                                 if (!token.empty())
     530              :                                                 {
     531              :                                                         // handle case where the beginning of the value, after the opening single-quote, is whitespace...
     532      1927950 :                                                         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      1927950 :                                                         if (token.size() > 0 && token[0] == '\'')
     542              :                                                         {
     543              :                                                                 // remove leading quote                                                 
     544      1927950 :                                                                 token.erase(0, 1);
     545              :                                                         }
     546              : 
     547      1927950 :                                                         if (token.size() > 0 && token[token.size() - 1] == '\'')
     548              :                                                         {
     549              :                                                                 // remove trailing quote
     550      1669080 :                                                                 token.erase(token.size() - 1);
     551              :                                                         }
     552              :                                                         
     553      1927950 :                                                         Utils::str_trim(token);
     554              :                                                 }       
     555              : 
     556      1927950 :                                                 pPropertyRecord->value = token;
     557              : 
     558              :                                                 double f;
     559      1955004 :                                                 while (lineStream >> f)
     560              :                                                 {
     561        27054 :                                                         pPropertyRecord->floatValues.push_back(f);
     562              :                                                 }
     563              : 
     564      1927950 :                                                 pCurrentComponentRecord->m_propertyRecords.push_back(pPropertyRecord);
     565      1927950 :                                         }
     566       412698 :                                         else if (line.find(ComponentRecord::ToeprintRecord::RECORD_TOKEN) == 0)
     567              :                                         {
     568              :                                                 // component toeprint record line
     569       251130 :                                                 std::string token;
     570       251130 :                                                 lineStream >> token;
     571       251130 :                                                 if (token != ComponentRecord::ToeprintRecord::RECORD_TOKEN)
     572              :                                                 {
     573            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     574              :                                                 }
     575              : 
     576       251130 :                                                 auto pToeprintRecord = std::make_shared<ComponentRecord::ToeprintRecord>();
     577       251130 :                                                 lineStream >> pToeprintRecord->pinNumber;
     578       251130 :                                                 lineStream >> pToeprintRecord->locationX;
     579       251130 :                                                 lineStream >> pToeprintRecord->locationY;
     580       251130 :                                                 lineStream >> pToeprintRecord->rotation;
     581              : 
     582              :                                                 char mirror;
     583       251130 :                                                 lineStream >> mirror;
     584       251130 :                                                 pToeprintRecord->mirror = (mirror == 'M' || mirror == 'm');
     585              : 
     586       251130 :                                                 lineStream >> pToeprintRecord->netNumber;
     587       251130 :                                                 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       251130 :                                                 lineStream >> pToeprintRecord->subnetNumber;
     600       251130 :                                                 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       251130 :                                                 lineStream >> pToeprintRecord->name;
     613              : 
     614       251130 :                                                 pCurrentComponentRecord->m_toeprintRecords.push_back(pToeprintRecord);
     615       251130 :                                         }
     616       161568 :                                         else if (line.find(ComponentsFile::BomDescriptionRecord::CPN_RECORD_TOKEN) == 0)
     617              :                                         {
     618              :                                                 // BomDescriptionRecord start (i.e. CPN) line
     619              : 
     620        24912 :                                                 std::string token;
     621        24912 :                                                 lineStream >> token;
     622        24912 :                                                 if (token != ComponentsFile::BomDescriptionRecord::CPN_RECORD_TOKEN)
     623              :                                                 {
     624            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     625              :                                                 }
     626              : 
     627        24912 :                                                 if (pCurrentBomDescriptionRecord != nullptr)
     628              :                                                 {
     629            0 :                                                         m_bomDescriptionRecordsByCpn[pCurrentBomDescriptionRecord->cpn] = pCurrentBomDescriptionRecord;
     630            0 :                                                         pCurrentBomDescriptionRecord.reset();
     631              :                                                 }
     632              : 
     633        24912 :                                                 pCurrentBomDescriptionRecord = std::make_shared<ComponentsFile::BomDescriptionRecord>();                                                                          
     634        24912 :                                         }
     635       136656 :                                         else if (line.find(ComponentsFile::BomDescriptionRecord::IPN_RECORD_TOKEN) == 0)
     636              :                                         {
     637        24912 :                                                 std::string token;
     638        24912 :                                                 lineStream >> token;
     639        24912 :                                                 if (token != ComponentsFile::BomDescriptionRecord::IPN_RECORD_TOKEN)
     640              :                                                 {
     641            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     642              :                                                 }
     643              : 
     644        24912 :                                                 if (pCurrentBomDescriptionRecord == nullptr)
     645              :                                                 {
     646            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     647              :                                                 }
     648              : 
     649        24912 :                                                 if (!(lineStream >> pCurrentBomDescriptionRecord->ipn))
     650              :                                                 {
     651              :                                                         // blank/ no IPN value allowed
     652              :                                                         //throw_parse_error(m_path, line, token, lineNumber);
     653              :                                                 }
     654        24912 :                                         }
     655       111744 :                                         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       111744 :                                         else if (line.find(ComponentsFile::BomDescriptionRecord::VPL_VND_RECORD_TOKEN) == 0)
     680              :                                         {
     681        21708 :                                                 std::string token;
     682        21708 :                                                 lineStream >> token;
     683        21708 :                                                 if (token != ComponentsFile::BomDescriptionRecord::VPL_VND_RECORD_TOKEN)
     684              :                                                 {
     685            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     686              :                                                 }
     687              : 
     688        21708 :                                                 if (pCurrentBomDescriptionRecord == nullptr)
     689              :                                                 {
     690            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     691              :                                                 }
     692              : 
     693        21708 :                                                 if (!(lineStream >> pCurrentBomDescriptionRecord->vpl_vnd))
     694              :                                                 {
     695            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     696              :                                                 }
     697        21708 :                                         }
     698        90036 :                                         else if (line.find(ComponentsFile::BomDescriptionRecord::VPL_MPN_RECORD_TOKEN) == 0)
     699              :                                         {
     700        21708 :                                                 std::string token;
     701        21708 :                                                 lineStream >> token;
     702        21708 :                                                 if (token != ComponentsFile::BomDescriptionRecord::VPL_MPN_RECORD_TOKEN)
     703              :                                                 {
     704            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     705              :                                                 }
     706              : 
     707        21708 :                                                 if (pCurrentBomDescriptionRecord == nullptr)
     708              :                                                 {
     709            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     710              :                                                 }
     711              : 
     712        21708 :                                                 if (!(lineStream >> pCurrentBomDescriptionRecord->vpl_mpn))
     713              :                                                 {
     714            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     715              :                                                 }
     716        21708 :                                         }
     717        68328 :                                         else if (line.find(ComponentsFile::BomDescriptionRecord::VND_RECORD_TOKEN) == 0)
     718              :                                         {
     719        21708 :                                                 std::string token;
     720        21708 :                                                 lineStream >> token;
     721        21708 :                                                 if (token != ComponentsFile::BomDescriptionRecord::VND_RECORD_TOKEN)
     722              :                                                 {
     723            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     724              :                                                 }
     725              : 
     726        21708 :                                                 if (pCurrentBomDescriptionRecord == nullptr)
     727              :                                                 {
     728            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     729              :                                                 }
     730              : 
     731        21708 :                                                 if (!(lineStream >> pCurrentBomDescriptionRecord->vnd))
     732              :                                                 {
     733            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     734              :                                                 }
     735        21708 :                                         }
     736        46620 :                                         else if (line.find(ComponentsFile::BomDescriptionRecord::MPN_RECORD_TOKEN) == 0)
     737              :                                         {
     738        21708 :                                                 std::string token;
     739        21708 :                                                 lineStream >> token;
     740        21708 :                                                 if (token != ComponentsFile::BomDescriptionRecord::MPN_RECORD_TOKEN)
     741              :                                                 {
     742            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     743              :                                                 }
     744              : 
     745        21708 :                                                 if (pCurrentBomDescriptionRecord == nullptr)
     746              :                                                 {
     747            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     748              :                                                 }
     749              : 
     750        21708 :                                                 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        21708 :                                         }                                       
     759      2583758 :                                 }
     760              :                         }
     761              : 
     762          188 :                         if (pCurrentBomDescriptionRecord != nullptr)
     763              :                         {
     764              :                                 // BomDescriptionRecord end (i.e. MPN) line
     765           72 :                                 m_bomDescriptionRecordsByCpn[pCurrentBomDescriptionRecord->cpn] = pCurrentBomDescriptionRecord;
     766           72 :                                 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          188 :                         if (pCurrentComponentRecord != nullptr)
     771              :                         {
     772              :                                 // finish it up and add it to the list
     773          188 :                                 m_componentRecords.push_back(pCurrentComponentRecord);
     774          188 :                                 pCurrentComponentRecord.reset();
     775              :                         }
     776              : 
     777          188 :                         componentsFile.close();                 
     778          188 :                 }
     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          188 :                 return true;
     796         3056 :         }
     797              : }
        

Generated by: LCOV version 2.0-1