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