5 #include "AttrListFile.h"
8 #include "../parse_error.h"
9 #include "../invalid_odb_error.h"
11 #include "str_utils.h"
12 #include "../../Constants.h"
13 #include <ArchiveExtractor.h>
15 namespace Odb::Lib::FileModel::Design
17 AttrListFile::AttrListFile()
24 std::string AttrListFile::GetUnits()
const
29 const AttrListFile::AttributeMap& AttrListFile::GetAttributes()
const
31 return m_attributesByName;
34 bool Lib::FileModel::Design::AttrListFile::Parse(std::filesystem::path directory)
36 std::ifstream attrListFile;
42 m_directory = directory;
44 loginfo(
"checking for extraction...");
46 std::filesystem::path attrListFilePath;
47 for (
const std::string attrListFilename : ATTRLIST_FILENAMES)
49 loginfo(
"trying attrlist file: [" + attrListFilename +
"]...");
51 attrListFilePath = Utils::ArchiveExtractor::getUncompressedFilePath(m_directory, attrListFilename);
52 if (exists(attrListFilePath) && is_regular_file(attrListFilePath))
54 loginfo(
"found attrlist file: [" + attrListFilePath.string() +
"]");
59 m_path = attrListFilePath;
61 loginfo(
"any extraction complete, parsing data...");
63 if (!std::filesystem::exists(m_path))
65 auto message =
"attrlist file does not exist: [" + m_directory.string() +
"]";
70 else if (!std::filesystem::is_regular_file(m_path))
72 auto message =
"attrlist is not a file: [" + m_path.string() +
"]";
73 throw invalid_odb_error(message.c_str());
76 attrListFile.open(m_path.string(), std::ios::in);
77 if (!attrListFile.is_open())
79 auto message =
"unable to open attrlist file: [" + m_path.string() +
"]";
80 throw invalid_odb_error(message.c_str());
83 while (std::getline(attrListFile, line))
88 Utils::str_trim(line);
91 std::stringstream lineStream(line);
94 if (line.find(Constants::COMMENT_TOKEN) == 0)
98 else if (line.find(Constants::UNITS_TOKEN) == 0)
102 if (!std::getline(lineStream, token,
'='))
104 throw_parse_error(m_path, line, token, lineNumber);
106 else if (!std::getline(lineStream, token,
'='))
108 throw_parse_error(m_path, line, token, lineNumber);
116 std::string attribute;
119 if (!std::getline(lineStream, attribute,
'='))
121 throw_parse_error(m_path, line,
"", lineNumber);
124 if (!std::getline(lineStream, value))
126 if (!attributeValueIsOptional(attribute))
128 logwarn(
"attrlist file: no value for non-optional attribute: " + attribute);
133 Utils::str_trim(attribute);
134 Utils::str_trim(value);
136 m_attributesByName[attribute] = value;
141 attrListFile.close();
143 catch (parse_error& pe)
145 auto m = pe.toString(
"Parse Error:");
147 attrListFile.close();
150 catch (std::exception& e)
152 parse_info pi(m_path, line, lineNumber);
153 const auto m = pi.toString();
154 logexception_msg(e, m);
155 attrListFile.close();
162 std::unique_ptr<Odb::Lib::Protobuf::AttrListFile> Lib::FileModel::Design::AttrListFile::to_protobuf()
const
164 auto message = std::make_unique<Odb::Lib::Protobuf::AttrListFile>();
166 message->set_directory(m_directory.string());
167 message->set_path(m_path.string());
168 message->set_units(m_units);
170 for (
const auto& kvAttribute : m_attributesByName)
172 (*message->mutable_attributesbyname())[kvAttribute.first] = kvAttribute.second;
179 void Lib::FileModel::Design::AttrListFile::from_protobuf(
const Odb::Lib::Protobuf::AttrListFile& message)
181 m_directory = message.directory();
182 m_path = message.path();
183 m_units = message.units();
185 for (
const auto& kvAttribute : message.attributesbyname())
187 m_attributesByName[kvAttribute.first] = kvAttribute.second;
191 bool Lib::FileModel::Design::AttrListFile::attributeValueIsOptional(
const std::string& attributeName)
const
206 bool AttrListFile::Save(std::ostream& os)
208 os << Constants::UNITS_TOKEN <<
" = " << m_units << std::endl;
209 for (
const auto& kvAttribute : m_attributesByName)
211 os << kvAttribute.first <<
" = " << kvAttribute.second << std::endl;