Line data Source code
1 : //
2 : // Created by nmill on 10/13/2023.
3 : //
4 :
5 : #include "AttrListFile.h"
6 : #include <string>
7 : #include <fstream>
8 : #include "../parse_error.h"
9 : #include "../invalid_odb_error.h"
10 : #include "Logger.h"
11 : #include "str_utils.h"
12 : #include "../../Constants.h"
13 : #include <ArchiveExtractor.h>
14 : #include "../../Text/Utf8Sanitizer.h"
15 :
16 : namespace Odb::Lib::FileModel::Design
17 : {
18 9814 : AttrListFile::AttrListFile()
19 9814 : : m_directory("")
20 9814 : , m_path("")
21 39256 : , m_units("")
22 : {
23 9814 : }
24 :
25 0 : std::string AttrListFile::GetUnits() const
26 : {
27 0 : return m_units;
28 : }
29 :
30 7044 : const AttrListFile::AttributeMap& AttrListFile::GetAttributes() const
31 : {
32 7044 : return m_attributesByName;
33 : }
34 :
35 9253 : bool Lib::FileModel::Design::AttrListFile::Parse(std::filesystem::path directory)
36 : {
37 9253 : std::ifstream attrListFile;
38 9253 : int lineNumber = 0;
39 9253 : std::string line;
40 :
41 : try
42 : {
43 9253 : m_directory = directory;
44 :
45 37012 : loginfo("checking for extraction...");
46 :
47 9253 : std::filesystem::path attrListFilePath;
48 22319 : for (const std::string attrListFilename : ATTRLIST_FILENAMES)
49 : {
50 18506 : loginfo("trying attrlist file: [" + attrListFilename + "]...");
51 :
52 9253 : attrListFilePath = Utils::ArchiveExtractor::getUncompressedFilePath(m_directory, attrListFilename);
53 9253 : if (exists(attrListFilePath) && is_regular_file(attrListFilePath))
54 : {
55 10880 : loginfo("found attrlist file: [" + attrListFilePath.string() + "]");
56 5440 : break;
57 : }
58 9253 : }
59 :
60 9253 : m_path = attrListFilePath;
61 :
62 37012 : loginfo("any extraction complete, parsing data...");
63 :
64 9253 : if (!std::filesystem::exists(m_path))
65 : {
66 3813 : auto message = "attrlist file does not exist: [" + m_directory.string() + "]";
67 7626 : logwarn(message);
68 3813 : return true;
69 : //throw invalid_odb_error(message.c_str());
70 3813 : }
71 5440 : else if (!std::filesystem::is_regular_file(m_path))
72 : {
73 0 : auto message = "attrlist is not a file: [" + m_path.string() + "]";
74 0 : throw invalid_odb_error(message.c_str());
75 0 : }
76 :
77 5440 : attrListFile.open(m_path.string(), std::ios::in);
78 5440 : if (!attrListFile.is_open())
79 : {
80 0 : auto message = "unable to open attrlist file: [" + m_path.string() + "]";
81 0 : throw invalid_odb_error(message.c_str());
82 0 : }
83 :
84 199984 : while (std::getline(attrListFile, line))
85 : {
86 194544 : lineNumber++;
87 :
88 : // trim whitespace from beginning and end of line
89 194544 : Utils::str_trim(line);
90 194544 : if (!line.empty())
91 : {
92 194544 : std::stringstream lineStream(line);
93 : //char firstChar = line[0];
94 :
95 194544 : if (line.find(Constants::COMMENT_TOKEN) == 0)
96 : {
97 : // comment line
98 : }
99 194544 : else if (line.find(Constants::UNITS_TOKEN) == 0)
100 : {
101 : // units line
102 5440 : std::string token;
103 5440 : if (!std::getline(lineStream, token, '='))
104 : {
105 0 : throw_parse_error(m_path, line, token, lineNumber);
106 : }
107 5440 : else if (!std::getline(lineStream, token, '='))
108 : {
109 0 : throw_parse_error(m_path, line, token, lineNumber);
110 : }
111 :
112 5440 : m_units = token;
113 5440 : }
114 : else
115 : {
116 : // attribute line
117 189104 : std::string attribute;
118 189104 : std::string value;
119 :
120 189104 : if (!std::getline(lineStream, attribute, '='))
121 : {
122 0 : throw_parse_error(m_path, line, "", lineNumber);
123 : }
124 :
125 189104 : if (!std::getline(lineStream, value))
126 : {
127 52110 : if (!attributeValueIsOptional(attribute))
128 : {
129 0 : logwarn("attrlist file: no value for non-optional attribute: " + attribute);
130 : }
131 :
132 : }
133 :
134 189104 : Utils::str_trim(attribute);
135 189104 : Utils::str_trim(value);
136 :
137 189104 : m_attributesByName[attribute] = value;
138 189104 : }
139 194544 : }
140 : }
141 :
142 5440 : attrListFile.close();
143 9253 : }
144 0 : catch (parse_error& pe)
145 : {
146 0 : auto m = pe.toString("Parse Error:");
147 0 : logerror(m);
148 0 : attrListFile.close();
149 0 : throw pe;
150 0 : }
151 0 : catch (std::exception& e)
152 : {
153 0 : parse_info pi(m_path, line, lineNumber);
154 0 : const auto m = pi.toString();
155 0 : logexception_msg(e, m);
156 0 : attrListFile.close();
157 0 : throw e;
158 0 : }
159 :
160 5440 : return true;
161 9253 : }
162 :
163 7860 : std::unique_ptr<Odb::Lib::Protobuf::AttrListFile> Lib::FileModel::Design::AttrListFile::to_protobuf() const
164 : {
165 7860 : auto message = std::make_unique<Odb::Lib::Protobuf::AttrListFile>();
166 :
167 15720 : message->set_directory(Odb::Lib::Text::ToUtf8(m_directory.string()));
168 15720 : message->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
169 15720 : message->set_units(Odb::Lib::Text::ToUtf8(m_units));
170 :
171 230310 : for (const auto& kvAttribute : m_attributesByName)
172 : {
173 222450 : (*message->mutable_attributesbyname())[Odb::Lib::Text::ToUtf8(kvAttribute.first)] = Odb::Lib::Text::ToUtf8(kvAttribute.second);
174 : }
175 :
176 7860 : return message;
177 :
178 0 : }
179 :
180 543 : void Lib::FileModel::Design::AttrListFile::from_protobuf(const Odb::Lib::Protobuf::AttrListFile& message)
181 : {
182 543 : m_directory = message.directory();
183 543 : m_path = message.path();
184 543 : m_units = message.units();
185 :
186 20291 : for (const auto& kvAttribute : message.attributesbyname())
187 : {
188 9874 : m_attributesByName[kvAttribute.first] = kvAttribute.second;
189 : }
190 543 : }
191 :
192 52110 : bool Lib::FileModel::Design::AttrListFile::attributeValueIsOptional(const std::string& attributeName) const
193 : {
194 : //for (const auto& optionalAttribute : OPTIONAL_ATTRIBUTES)
195 : //{
196 : // if (attributeName == optionalAttribute)
197 : // {
198 : // return true;
199 : // }
200 : //}
201 : //return false;
202 :
203 : // all atrributes are "optional", i.e. value is not required
204 52110 : return true;
205 : }
206 :
207 0 : bool AttrListFile::Save(std::ostream& os)
208 : {
209 0 : os << Constants::UNITS_TOKEN << " = " << m_units << std::endl;
210 0 : for (const auto& kvAttribute : m_attributesByName)
211 : {
212 0 : os << kvAttribute.first << " = " << kvAttribute.second << std::endl;
213 : }
214 :
215 0 : return true;
216 : }
217 : } // namespace Odb::Lib::FileModel::Design
|