LCOV - code coverage report
Current view: top level - OdbDesignLib/FileModel/Design - StepHdrFile.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 47.3 % 201 95
Test Date: 2026-11-19 04:56:22 Functions: 50.0 % 8 4
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "StepHdrFile.h"
       2              : #include "../invalid_odb_error.h"
       3              : #include "str_utils.h"
       4              : #include "../../Constants.h"
       5              : #include "../parse_error.h"
       6              : #include <Logger.h>
       7              : #include "../../Text/Utf8Sanitizer.h"
       8              : 
       9              : namespace Odb::Lib::FileModel::Design
      10              : {
      11           88 :         StepHdrFile::~StepHdrFile()
      12              :         {
      13           88 :                 m_stepRepeatRecords.clear();
      14           88 :         }
      15              : 
      16           95 :         bool StepHdrFile::Parse(std::filesystem::path path)
      17              :         {
      18           95 :         std::ifstream stepHdrFile;
      19           95 :         int lineNumber = 0;
      20           95 :         std::string line;
      21              : 
      22              :         try
      23              :         {
      24           95 :             if (!OdbFile::Parse(path)) return false;
      25              : 
      26           95 :             auto stepHdrFilePath = path / "stephdr";
      27           95 :             if (!std::filesystem::exists(stepHdrFilePath))
      28              :             {
      29            0 :                 auto message = "stephdr file does not exist: [" + stepHdrFilePath.string() + "]";
      30            0 :                 throw invalid_odb_error(message.c_str());
      31            0 :             }
      32              : 
      33           95 :             stepHdrFile.open(stepHdrFilePath, std::ios::in);
      34           95 :             if (!stepHdrFile.is_open())
      35              :             {
      36            0 :                 auto message = "unable to open stephdr file: [" + stepHdrFilePath.string() + "]";
      37            0 :                 throw invalid_odb_error(message.c_str());
      38            0 :             }
      39              : 
      40           95 :             m_path = stepHdrFilePath;
      41              : 
      42           95 :             std::shared_ptr<StepRepeatRecord> pCurrentStepRepeatRecord;
      43           95 :             bool openBraceFound = false;
      44              : 
      45         1268 :             while (std::getline(stepHdrFile, line))
      46              :             {
      47         1173 :                 lineNumber++;
      48              :                 // trim whitespace from beginning and end of line
      49         1173 :                 Utils::str_trim(line);
      50         1173 :                 if (!line.empty())
      51              :                 {
      52         1139 :                     std::stringstream lineStream(line);
      53         1139 :                     if (line.find(Constants::COMMENT_TOKEN) == 0)
      54              :                     {
      55              :                         // comment line
      56              :                     }
      57         1139 :                     else if (line.find(StepRepeatRecord::ARRAY_HEADER_TOKEN) == 0)
      58              :                     {
      59            0 :                         std::string token;
      60            0 :                         if (!(lineStream >> token))
      61              :                         {
      62            0 :                             throw_parse_error(m_path, line, token, lineNumber);
      63              :                         }
      64              : 
      65            0 :                         if (token != StepRepeatRecord::ARRAY_HEADER_TOKEN)
      66              :                         {
      67            0 :                             throw_parse_error(m_path, line, token, lineNumber);
      68              :                         }
      69              : 
      70            0 :                         if (lineStream >> token)
      71              :                         {
      72              :                             // open brace is at the end on the same line as the step array record open token
      73            0 :                             if (token == Constants::ARRAY_RECORD_OPEN_TOKEN)
      74              :                             {
      75            0 :                                 openBraceFound = true;
      76              : 
      77              :                                 // TODO: finish any existing previous step record
      78              :                                 // (same code as finding a close brace on an empty line)
      79              : 
      80              :                                 // open a new STEP-REPEAT array record
      81            0 :                                 pCurrentStepRepeatRecord = std::make_shared<StepRepeatRecord>();
      82              :                             }
      83              :                         }
      84            0 :                     }                    
      85         1139 :                     else if (line.find(Constants::ARRAY_RECORD_OPEN_TOKEN) == 0)
      86              :                     {
      87              :                         // TODO: how to determine if you are opening a step or layer array record?
      88              :                         // (maybe a boolean flag? stepArrayOpen = true/false)
      89              :                         // no current opening of a layer or step array record found yet
      90            0 :                         if (pCurrentStepRepeatRecord == nullptr)
      91              :                         {
      92            0 :                             throw_parse_error(m_path, line, "", lineNumber);
      93              :                         }
      94              : 
      95              :                         // found another open brace while still parsing after the previous record's open brace
      96            0 :                         if (openBraceFound)
      97              :                         {
      98            0 :                             throw_parse_error(m_path, line, "", lineNumber);
      99              :                         }
     100              : 
     101            0 :                         openBraceFound = true;
     102              : 
     103              :                         // TODO: finish any existing previous layer or step record
     104              :                         // (same code as finding a close brace on an empty line)
     105              : 
     106              :                         // open a new LAYER array record
     107              :                         //pCurrentLayerRecord = std::make_shared<LayerRecord>();
     108              :                     }
     109         1139 :                     else if (line.find(Constants::ARRAY_RECORD_CLOSE_TOKEN) == 0)
     110              :                     {
     111            0 :                         if (pCurrentStepRepeatRecord != nullptr && openBraceFound)
     112              :                         {
     113            0 :                             m_stepRepeatRecords.push_back(pCurrentStepRepeatRecord);
     114            0 :                             pCurrentStepRepeatRecord.reset();
     115            0 :                             openBraceFound = false;
     116              :                         }                       
     117              :                         else
     118              :                         {
     119              :                             // found a close brace but aren't currently parsing a step or layer array record
     120            0 :                             throw_parse_error(m_path, line, "", lineNumber);
     121              :                         }
     122              :                     }
     123              :                     else
     124              :                     {
     125              :                         // it should be a name-value pair line (i.e. element of a step or layer array)
     126         1139 :                         std::string attribute;
     127         1139 :                         std::string value;
     128              : 
     129         1139 :                         if (!std::getline(lineStream, attribute, '='))
     130              :                         {
     131            0 :                             throw_parse_error(m_path, line, attribute, lineNumber);
     132              :                         }
     133              : 
     134         1139 :                         if (std::getline(lineStream, value))
     135              :                         {
     136         1139 :                             Utils::str_trim(attribute);
     137         1139 :                             Utils::str_trim(value);
     138              : 
     139         1139 :                             if (pCurrentStepRepeatRecord != nullptr && openBraceFound)
     140              :                             {
     141            0 :                                 if (attribute == "NAME" || attribute == "name")
     142              :                                 {
     143            0 :                                     pCurrentStepRepeatRecord->name = value;
     144              :                                 }
     145            0 :                                 else if (attribute == "X" || attribute == "x")
     146              :                                 {
     147            0 :                                     pCurrentStepRepeatRecord->x = std::stof(value);
     148              :                                 }
     149            0 :                                 else if (attribute == "Y" || attribute == "y")
     150              :                                                                 {
     151            0 :                                                                         pCurrentStepRepeatRecord->y = std::stof(value);
     152              :                                                                 }
     153            0 :                                                                 else if (attribute == "DX" || attribute == "dx")
     154              :                                                                 {
     155            0 :                                                                         pCurrentStepRepeatRecord->dx = std::stof(value);
     156              :                                                                 }
     157            0 :                                                                 else if (attribute == "DY" || attribute == "dy")
     158              :                                                                 {
     159            0 :                                                                         pCurrentStepRepeatRecord->dy = std::stof(value);
     160              :                                                                 }
     161            0 :                                                                 else if (attribute == "NX" || attribute == "nx")
     162              :                                                                 {
     163            0 :                                                                         pCurrentStepRepeatRecord->nx = std::stoi(value);
     164              :                                                                 }
     165            0 :                                                                 else if (attribute == "NY" || attribute == "ny")
     166              :                                                                 {
     167            0 :                                                                         pCurrentStepRepeatRecord->ny = std::stoi(value);
     168              :                                                                 }
     169            0 :                                                                 else if (attribute == "ANGLE" || attribute == "angle")
     170              :                                                                 {
     171            0 :                                                                         pCurrentStepRepeatRecord->angle = std::stof(value);
     172              :                                                                 }
     173            0 :                                 else if (attribute == "FLIP" || attribute == "flip")
     174              :                                 {
     175            0 :                                     if (value == "YES" || value == "yes")
     176              :                                     {
     177            0 :                                                                                 pCurrentStepRepeatRecord->flip = true;
     178              :                                                                         }
     179            0 :                                     else if (value == "NO" || value == "no")
     180              :                                     {
     181            0 :                                                                                 pCurrentStepRepeatRecord->flip = false;
     182              :                                                                         }
     183              :                                     else
     184              :                                     {
     185            0 :                                                                                 throw_parse_error(m_path, line, attribute, lineNumber);
     186              :                                                                         }
     187              :                                 }
     188            0 :                                 else if (attribute == "MIRROR" || attribute == "mirror")
     189              :                                 {
     190            0 :                                     if (value == "YES" || value == "yes")
     191              :                                     {
     192            0 :                                         pCurrentStepRepeatRecord->mirror = true;
     193              :                                     }
     194            0 :                                     else if (value == "NO" || value == "no")
     195              :                                     {
     196            0 :                                         pCurrentStepRepeatRecord->mirror = false;
     197              :                                     }
     198              :                                     else
     199              :                                     {
     200            0 :                                         throw_parse_error(m_path, line, attribute, lineNumber);
     201              :                                     }
     202              :                                 }
     203              :                                 else
     204              :                                 {
     205            0 :                                     throw_parse_error(m_path, line, attribute, lineNumber);
     206              :                                 }
     207              :                             }
     208              :                             else
     209              :                             {                               
     210         1139 :                                 if (attribute == "X_DATUM" || attribute == "x_datum")
     211              :                                 {
     212           95 :                                     xDatum = std::stof(value);
     213              :                                 }
     214         1044 :                                 else if (attribute == "Y_DATUM" || attribute == "y_datum")
     215              :                                 {
     216           95 :                                     yDatum = stof(value);
     217              :                                 }
     218          949 :                                 else if (attribute == "ID" || attribute == "id")
     219              :                                                                 {
     220           95 :                                                                         id = (unsigned int)std::stoul(value);
     221              :                                                                 }
     222          854 :                                                                 else if (attribute == "X_ORIGIN" || attribute == "x_origin")
     223              :                                                                 {
     224           95 :                                                                         xOrigin = std::stof(value);
     225              :                                                                 }
     226          759 :                                                                 else if (attribute == "Y_ORIGIN" || attribute == "y_origin")
     227              :                                                                 {
     228           95 :                                                                         yOrigin = std::stof(value);
     229              :                                                                 }
     230          664 :                                                                 else if (attribute == "TOP_ACTIVE" || attribute == "top_active")
     231              :                                                                 {
     232           95 :                                                                         topActive = std::stof(value);
     233              :                                                                 }
     234          569 :                                                                 else if (attribute == "BOTTOM_ACTIVE" || attribute == "bottom_active")
     235              :                                                                 {
     236           95 :                                                                         bottomActive = std::stof(value);
     237              :                                                                 }
     238          474 :                                                                 else if (attribute == "RIGHT_ACTIVE" || attribute == "right_active")
     239              :                                                                 {
     240           95 :                                                                         rightActive = std::stof(value);
     241              :                                                                 }
     242          379 :                                                                 else if (attribute == "LEFT_ACTIVE" || attribute == "left_active")
     243              :                                                                 {
     244           95 :                                                                         leftActive = std::stof(value);
     245              :                                                                 }
     246          284 :                                                                 else if (attribute == "AFFECTING_BOM" || attribute == "affecting_bom")
     247              :                                                                 {
     248           94 :                                     affectingBom = value;
     249              :                                                                 }
     250          190 :                                                                 else if (attribute == "AFFECTING_BOM_CHANGED" || attribute == "affecting_bom_changed")
     251              :                                                                 {
     252           94 :                                                                         affectingBomChanged = std::stoi(value);
     253              :                                                                 }
     254           96 :                                 else if (attribute == "UNITS" || attribute == "units")
     255              :                                 {
     256           95 :                                     m_units = value;
     257              :                                 }
     258            1 :                                 else if (attribute.find("ONLINE_") == 0)
     259              :                                 {
     260            1 :                                     m_onlineValues[attribute] = value;
     261              :                                 }
     262              :                                 else
     263              :                                 {
     264            0 :                                     throw_parse_error(m_path, line, attribute, lineNumber);
     265              :                                 }
     266              :                             }
     267              :                         }
     268              :                         //else if (!attributeValueIsOptional(attribute))
     269              :                         //{
     270              :                         //    logwarn("matrix/matrix file: no value for non-optional attribute: " + attribute);
     271              :                         //}
     272         1139 :                     }
     273         1139 :                 }
     274              :             }
     275              : 
     276           95 :             stepHdrFile.close();
     277           95 :         }
     278            0 :         catch (parse_error& pe)
     279              :         {
     280            0 :             auto m = pe.toString("Parse Error:");
     281            0 :             logerror(m);
     282              :             // cleanup file
     283            0 :             stepHdrFile.close();
     284            0 :             throw pe;
     285            0 :         }
     286            0 :         catch (invalid_odb_error& ioe)
     287              :         {
     288            0 :             parse_info pi(m_path, line, lineNumber);
     289            0 :             const auto m = pi.toString();
     290            0 :             logexception_msg(ioe, m);
     291              :             // cleanup file
     292            0 :             stepHdrFile.close();
     293            0 :             throw ioe;
     294            0 :         }
     295              : 
     296           95 :         return true;
     297           95 :         }
     298              : 
     299           98 :         std::unique_ptr<Odb::Lib::Protobuf::StepHdrFile> StepHdrFile::to_protobuf() const
     300              :         {
     301           98 :                 auto message = std::make_unique<Odb::Lib::Protobuf::StepHdrFile>();
     302           98 :                 message->set_xdatum(xDatum);
     303           98 :                 message->set_ydatum(yDatum);
     304           98 :                 message->set_id(id);
     305           98 :                 message->set_xorigin(xOrigin);
     306           98 :                 message->set_yorigin(yOrigin);
     307           98 :                 message->set_topactive(topActive);
     308           98 :                 message->set_bottomactive(bottomActive);
     309           98 :                 message->set_leftactive(leftActive);
     310           98 :                 message->set_rightactive(rightActive);
     311          196 :                 message->set_affectingbom(Odb::Lib::Text::ToUtf8(affectingBom));
     312           98 :                 message->set_affectingbomchanged(affectingBomChanged);
     313              : 
     314           98 :                 for (const auto& stepRepeatRecord : m_stepRepeatRecords)
     315              :                 {
     316            0 :                         auto stepRepeatRecordPtr = message->add_steprepeatrecords();
     317            0 :                         stepRepeatRecordPtr->CopyFrom(*stepRepeatRecord->to_protobuf());
     318              :                 }
     319              : 
     320           99 :         for (const auto& kvOnlineValue : m_onlineValues)
     321              :         {
     322            1 :             (*message->mutable_onlinevalues())[Odb::Lib::Text::ToUtf8(kvOnlineValue.first)] = Odb::Lib::Text::ToUtf8(kvOnlineValue.second);
     323              :         }
     324              : 
     325           98 :                 return message;
     326            0 :         }
     327              : 
     328            8 :         void StepHdrFile::from_protobuf(const Odb::Lib::Protobuf::StepHdrFile& message)
     329              :         {
     330            8 :                 xDatum = message.xdatum();
     331            8 :                 yDatum = message.ydatum();
     332            8 :                 id = message.id();
     333            8 :                 xOrigin = message.xorigin();
     334            8 :                 yOrigin = message.yorigin();
     335            8 :                 topActive = message.topactive();
     336            8 :                 bottomActive = message.bottomactive();
     337            8 :                 leftActive = message.leftactive();
     338            8 :                 rightActive = message.rightactive();
     339            8 :                 affectingBom = message.affectingbom();
     340            8 :                 affectingBomChanged = message.affectingbomchanged();            
     341              :                 
     342            8 :                 for (const auto& stepRepeatRecord : message.steprepeatrecords())
     343              :                 {
     344            0 :                         auto stepRepeatRecordPtr = std::make_unique<StepRepeatRecord>();
     345            0 :                         stepRepeatRecordPtr->from_protobuf(stepRepeatRecord);
     346            0 :                         m_stepRepeatRecords.push_back(std::move(stepRepeatRecordPtr));
     347            0 :                 }
     348              : 
     349            8 :         for (const auto& kvOnlineValue : message.onlinevalues())
     350              :         {
     351            0 :             m_onlineValues[kvOnlineValue.first] = kvOnlineValue.second;
     352              :         }
     353            8 :         }
     354              : 
     355            0 :     bool StepHdrFile::Save(std::ostream& os)
     356              :     {
     357            0 :         return true;
     358              :     }
     359              : 
     360            0 :         std::unique_ptr<Odb::Lib::Protobuf::StepHdrFile::StepRepeatRecord> StepHdrFile::StepRepeatRecord::to_protobuf() const
     361              :         {
     362            0 :                 auto message = std::make_unique<Odb::Lib::Protobuf::StepHdrFile::StepRepeatRecord>();
     363            0 :                 message->set_name(Odb::Lib::Text::ToUtf8(name));
     364            0 :                 message->set_x(x);
     365            0 :                 message->set_y(y);
     366            0 :                 message->set_dx(dx);
     367            0 :                 message->set_dy(dy);
     368            0 :                 message->set_nx(nx);
     369            0 :                 message->set_ny(ny);
     370            0 :                 message->set_angle(angle);
     371            0 :         message->set_flip(flip);
     372            0 :         message->set_mirror(mirror);
     373            0 :                 return message;
     374            0 :         }
     375              : 
     376            0 :         void StepHdrFile::StepRepeatRecord::from_protobuf(const Odb::Lib::Protobuf::StepHdrFile::StepRepeatRecord& message)
     377              :         {
     378            0 :                 name = message.name();
     379            0 :                 x = message.x();
     380            0 :                 y = message.y();
     381            0 :                 dx = message.dx();
     382            0 :                 dy = message.dy();
     383            0 :                 nx = message.nx();
     384            0 :                 ny = message.ny();
     385            0 :         flip = message.flip();
     386            0 :         mirror = message.mirror();
     387            0 :                 angle = message.angle();
     388            0 :         }
     389              : }
        

Generated by: LCOV version 2.0-1