LCOV - code coverage report
Current view: top level - OdbDesignLib/FileModel/Design - SymbolName.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 89.1 % 55 49
Test Date: 2026-11-21 03:51:50 Functions: 100.0 % 8 8
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "SymbolName.h"
       2              : #include "../../Text/Utf8Sanitizer.h"
       3              : 
       4              : namespace Odb::Lib::FileModel::Design
       5              : {
       6              :         //SymbolName::SymbolName(const std::string& name, UnitType unitType)
       7              :         //      : m_name(name)
       8              :         //      , m_unitType(unitType)
       9              :         //{
      10              :         //}
      11              : 
      12        46686 :         SymbolName::SymbolName()
      13        46686 :                 : m_name("")
      14        46686 :                 , m_unitType(UnitType::None)
      15        46686 :                 , m_index(-1)
      16              :         {
      17        46686 :         }
      18              : 
      19        46683 :         std::string SymbolName::GetName() const
      20              :         {
      21        46683 :                 return m_name;
      22              :         }
      23              : 
      24          567 :         int SymbolName::GetIndex() const
      25              :         {
      26          567 :                 return m_index;
      27              :         }
      28              : 
      29          351 :         UnitType SymbolName::GetUnitType() const
      30              :         {
      31          351 :                 return m_unitType;
      32              :         }
      33              : 
      34        44322 :         void SymbolName::ApplyDefaultUnitTypeIfNone(UnitType unitType)
      35              :         {
      36        44322 :                 if (m_unitType == UnitType::None)
      37              :                 {
      38        33165 :                         m_unitType = unitType;
      39              :                 }
      40        44322 :         }
      41              : 
      42        44323 :         bool SymbolName::Parse(const std::filesystem::path& path, const std::string& line, int lineNumber)
      43              :         {
      44        44323 :                 std::stringstream lineStream(line);
      45        44323 :                 std::string token;
      46              : 
      47              :                 // $n (index)
      48        44323 :                 if (!std::getline(lineStream, token, ' '))
      49              :                 {
      50            0 :                         throw_parse_error(path, line, token, lineNumber);
      51              :                 }
      52              : 
      53        44323 :                 if (!token.empty() && token[0] == '$')
      54              :                 {
      55              :                         try
      56              :                         {
      57        44323 :                                 m_index = std::stoi(token.substr(1));
      58              :                                 // Reject negative index values
      59        44321 :                                 if (m_index < 0)
      60              :                                 {
      61            1 :                                         throw_parse_error(path, line, token, lineNumber);
      62              :                                 }
      63              :                         }
      64            2 :                         catch (const std::invalid_argument&)
      65              :                         {
      66              :                                 // Surface malformed index tokens instead of silently defaulting.
      67            1 :                                 throw_parse_error(path, line, token, lineNumber);
      68            1 :                         }
      69            0 :                         catch (const std::out_of_range&)
      70              :                         {
      71              :                                 // Surface index tokens that exceed integer range.
      72            0 :                                 throw_parse_error(path, line, token, lineNumber);
      73            0 :                         }
      74              :                         
      75              :                         // Get the name token after the index; it must be present and non-empty
      76        44320 :                         if (!std::getline(lineStream, token, ' ') || token.empty())
      77              :                         {
      78              :                                 // Missing or empty name after a valid index is treated as a parse error
      79            0 :                                 throw_parse_error(path, line, token, lineNumber);
      80              :                         }
      81              : 
      82        44320 :                         m_name = token;
      83              :                 }
      84              :                 else
      85              :                 {
      86              :                         // No index token, use the first token as the name
      87            1 :                         m_name = token;
      88              :                 }
      89              : 
      90        44321 :                 if (std::getline(lineStream, token, ' '))
      91              :                 {
      92        11157 :                         switch (token[0])
      93              :                         {
      94            1 :                         case 'M':
      95            1 :                                 m_unitType = UnitType::Metric;
      96            1 :                                 break;
      97        11156 :                         case 'I':
      98        11156 :                                 m_unitType = UnitType::Imperial;
      99        11156 :                                 break;
     100              :                         }
     101              :                 }
     102              : 
     103        44321 :                 return true;
     104        44325 :         }
     105              : 
     106        84620 :         std::unique_ptr<Odb::Lib::Protobuf::SymbolName> SymbolName::to_protobuf() const
     107              :         {
     108        84620 :                 auto message = std::make_unique<Odb::Lib::Protobuf::SymbolName>();
     109       169240 :                 message->set_name(Odb::Lib::Text::ToUtf8(m_name));
     110        84620 :                 message->set_unittype(static_cast<Odb::Lib::Protobuf::UnitType>(m_unitType));
     111        84620 :                 return message;
     112            0 :         }
     113              : 
     114         2363 :         void SymbolName::from_protobuf(const Odb::Lib::Protobuf::SymbolName& message)
     115              :         {
     116         2363 :                 m_name = message.name();
     117         2363 :                 m_unitType = static_cast<UnitType>(message.unittype());
     118         2363 :                 m_index = -1;
     119         2363 :         }
     120              : 
     121              :         //std::shared_ptr<SymbolName> SymbolName::Parse(const std::string& line)
     122              :         //{
     123              :         //      return nullptr;
     124              :         //}
     125              : }
        

Generated by: LCOV version 2.0-1