Line data Source code
1 : #include "AttributeLookupTable.h"
2 : #include <sstream>
3 : #include <charconv>
4 :
5 : namespace Odb::Lib::FileModel::Design
6 : {
7 207945 : const std::map<std::string, std::string>& AttributeLookupTable::GetAttributeLookupTable() const
8 : {
9 207945 : return m_attributeLookupTable;
10 : }
11 :
12 24568734 : bool AttributeLookupTable::ParseAttributeLookupTable(const std::string& attributeLookupTableString, std::optional<unsigned int>& parsedId)
13 : {
14 24568734 : parsedId.reset();
15 24568734 : std::stringstream ss(attributeLookupTableString);
16 24568734 : 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 24568734 : if (!std::getline(ss, token, ';'))
22 1 : return true;
23 :
24 : // attributes
25 24568733 : if (std::getline(ss, token, ';'))
26 : {
27 24568507 : std::stringstream attributesStream(token);
28 24568507 : std::string attributeAssignment;
29 103511298 : while (std::getline(attributesStream, attributeAssignment, ','))
30 : {
31 54374284 : if (!attributeAssignment.empty())
32 : {
33 54374284 : std::string name;
34 54374284 : std::string value;
35 :
36 54374284 : std::stringstream aa_ss(attributeAssignment);
37 54374284 : if (attributeAssignment.find("=") != std::string::npos)
38 : {
39 49847014 : if (!std::getline(aa_ss, name, '=')) return false;
40 49847014 : if (!std::getline(aa_ss, value)) return false;
41 : }
42 : else
43 : {
44 4527270 : if (!std::getline(aa_ss, name)) return false;
45 : }
46 :
47 54374284 : m_attributeLookupTable[name] = value;
48 54374284 : }
49 : }
50 24568507 : }
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 24568733 : if (std::getline(ss, token, ';'))
58 : {
59 24493673 : std::string name;
60 24493673 : std::string value;
61 :
62 24493673 : std::stringstream token_ss(token);
63 24493673 : if (token.find("=") != std::string::npos)
64 : {
65 24493673 : if (!std::getline(token_ss, name, '=')) return false;
66 24493673 : if (!std::getline(token_ss, value)) return false;
67 :
68 24493673 : 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 24493672 : unsigned int idValue = 0;
80 24493672 : const auto* const valueEnd = value.data() + value.size();
81 24493672 : const auto result = std::from_chars(value.data(), valueEnd, idValue);
82 24493672 : if (result.ec == std::errc() && result.ptr == valueEnd)
83 : {
84 24493667 : 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 1 : m_attributeLookupTable[name] = value;
92 : }
93 : }
94 24493673 : }
95 :
96 24568733 : return true;
97 24568734 : }
98 : }
|