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