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 48095 : SymbolName::SymbolName()
13 48095 : : m_name("")
14 48095 : , m_unitType(UnitType::None)
15 48095 : , m_index(-1)
16 : {
17 48095 : }
18 :
19 48092 : std::string SymbolName::GetName() const
20 : {
21 48092 : 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 45731 : void SymbolName::ApplyDefaultUnitTypeIfNone(UnitType unitType)
35 : {
36 45731 : if (m_unitType == UnitType::None)
37 : {
38 34190 : m_unitType = unitType;
39 : }
40 45731 : }
41 :
42 45732 : bool SymbolName::Parse(const std::filesystem::path& path, const std::string& line, int lineNumber)
43 : {
44 45732 : std::stringstream lineStream(line);
45 45732 : std::string token;
46 :
47 : // $n (index)
48 45732 : if (!std::getline(lineStream, token, ' '))
49 : {
50 0 : throw_parse_error(path, line, token, lineNumber);
51 : }
52 :
53 45732 : if (!token.empty() && token[0] == '$')
54 : {
55 : try
56 : {
57 45732 : m_index = std::stoi(token.substr(1));
58 : // Reject negative index values
59 45730 : 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 45729 : 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 45729 : 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 45730 : if (std::getline(lineStream, token, ' '))
91 : {
92 11541 : switch (token[0])
93 : {
94 1 : case 'M':
95 1 : m_unitType = UnitType::Metric;
96 1 : break;
97 11540 : case 'I':
98 11540 : m_unitType = UnitType::Imperial;
99 11540 : break;
100 : }
101 : }
102 :
103 45730 : return true;
104 45734 : }
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 : }
|