OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
AttributeLookupTable.cpp
Go to the documentation of this file.
2#include <sstream>
3#include <charconv>
4
6{
7 const std::map<std::string, std::string>& AttributeLookupTable::GetAttributeLookupTable() const
8 {
10 }
11
12 bool AttributeLookupTable::ParseAttributeLookupTable(const std::string& attributeLookupTableString, std::optional<unsigned int>& parsedId)
13 {
14 parsedId.reset();
15 std::stringstream ss(attributeLookupTableString);
16 std::string token;
17
18 // skip the content before the first semicolon
19 // a failed read here means the attribute string is absent entirely: the
20 // attribute field is optional on ODB++ records, so that is not an error
21 if (!std::getline(ss, token, ';'))
22 return true;
23
24 // attributes
25 if (std::getline(ss, token, ';'))
26 {
27 std::stringstream attributesStream(token);
28 std::string attributeAssignment;
29 while (std::getline(attributesStream, attributeAssignment, ','))
30 {
31 if (!attributeAssignment.empty())
32 {
33 std::string name;
34 std::string value;
35
36 std::stringstream aa_ss(attributeAssignment);
37 if (attributeAssignment.find("=") != std::string::npos)
38 {
39 if (!std::getline(aa_ss, name, '=')) return false;
40 if (!std::getline(aa_ss, value)) return false;
41 }
42 else
43 {
44 if (!std::getline(aa_ss, name)) return false;
45 }
46
47 m_attributeLookupTable[name] = value;
48 }
49 }
50 }
51
52 // third section: the record unique identifier ("ID=<uid>")
53 // The ODB++ id is a product-model-wide UID, not an attribute: it is returned to
54 // the caller via parsedId instead of being inserted into the attribute map. The
55 // literal "ID" key is deliberately gone from the map from here on -- the UID
56 // lives in the typed `id` field of the owning record (decision D3, 2026-09-15).
57 if (std::getline(ss, token, ';'))
58 {
59 std::string name;
60 std::string value;
61
62 std::stringstream token_ss(token);
63 if (token.find("=") != std::string::npos)
64 {
65 if (!std::getline(token_ss, name, '=')) return false;
66 if (!std::getline(token_ss, value)) return false;
67
68 if (name == "ID")
69 {
70 // strict numeric parse: std::from_chars rejects partial values
71 // ("5abc", "0x10" -- std::stoul would have accepted their numeric
72 // prefixes), negative values ("-5" wraps under std::stoul on
73 // platforms with a 32-bit unsigned long) and out-of-range values
74 // alike; a rejected value leaves the id unset rather than failing
75 // the whole record parse. An empty value ("ID=") never reaches
76 // here: the getline guard above fails the parse like any other
77 // malformed name=value pair, matching the attribute section's
78 // behavior.
79 unsigned int idValue = 0;
80 const auto* const valueEnd = value.data() + value.size();
81 const auto result = std::from_chars(value.data(), valueEnd, idValue);
82 if (result.ec == std::errc() && result.ptr == valueEnd)
83 {
84 parsedId = idValue;
85 }
86 }
87 else
88 {
89 // out-of-spec third-section name: keep it as an attribute rather
90 // than silently dropping data
91 m_attributeLookupTable[name] = value;
92 }
93 }
94 }
95
96 return true;
97 }
98}
std::map< std::string, std::string > m_attributeLookupTable
const std::map< std::string, std::string > & GetAttributeLookupTable() const
bool ParseAttributeLookupTable(const std::string &attributeLookupTableString, std::optional< unsigned int > &parsedId)