5#include "AttrListFile.h"
8#include "../parse_error.h"
9#include "../invalid_odb_error.h"
12#include "../../Constants.h"
13#include <ArchiveExtractor.h>
14#include "../../Text/Utf8Sanitizer.h"
16namespace Odb::Lib::FileModel::Design
18 AttrListFile::AttrListFile()
25 std::string AttrListFile::GetUnits()
const
30 const AttrListFile::AttributeMap& AttrListFile::GetAttributes()
const
32 return m_attributesByName;
35 bool Lib::FileModel::Design::AttrListFile::Parse(std::filesystem::path directory)
37 std::ifstream attrListFile;
43 m_directory = directory;
45 loginfo(
"checking for extraction...");
47 std::filesystem::path attrListFilePath;
48 for (
const std::string attrListFilename : ATTRLIST_FILENAMES)
50 loginfo(
"trying attrlist file: [" + attrListFilename +
"]...");
52 attrListFilePath = Utils::ArchiveExtractor::getUncompressedFilePath(m_directory, attrListFilename);
53 if (exists(attrListFilePath) && is_regular_file(attrListFilePath))
55 loginfo(
"found attrlist file: [" + attrListFilePath.string() +
"]");
60 m_path = attrListFilePath;
62 loginfo(
"any extraction complete, parsing data...");
64 if (!std::filesystem::exists(m_path))
66 auto message =
"attrlist file does not exist: [" + m_directory.string() +
"]";
71 else if (!std::filesystem::is_regular_file(m_path))
73 auto message =
"attrlist is not a file: [" + m_path.string() +
"]";
74 throw invalid_odb_error(message.c_str());
77 attrListFile.open(m_path.string(), std::ios::in);
78 if (!attrListFile.is_open())
80 auto message =
"unable to open attrlist file: [" + m_path.string() +
"]";
81 throw invalid_odb_error(message.c_str());
84 while (std::getline(attrListFile, line))
89 Utils::str_trim(line);
92 std::stringstream lineStream(line);
95 if (line.find(Constants::COMMENT_TOKEN) == 0)
99 else if (line.find(Constants::UNITS_TOKEN) == 0)
103 if (!std::getline(lineStream, token,
'='))
105 throw_parse_error(m_path, line, token, lineNumber);
107 else if (!std::getline(lineStream, token,
'='))
109 throw_parse_error(m_path, line, token, lineNumber);
117 std::string attribute;
120 if (!std::getline(lineStream, attribute,
'='))
122 throw_parse_error(m_path, line,
"", lineNumber);
125 if (!std::getline(lineStream, value))
127 if (!attributeValueIsOptional(attribute))
129 logwarn(
"attrlist file: no value for non-optional attribute: " + attribute);
134 Utils::str_trim(attribute);
135 Utils::str_trim(value);
137 m_attributesByName[attribute] = value;
142 attrListFile.close();
144 catch (parse_error& pe)
146 auto m = pe.toString(
"Parse Error:");
148 attrListFile.close();
151 catch (std::exception& e)
153 parse_info pi(m_path, line, lineNumber);
154 const auto m = pi.toString();
155 logexception_msg(e, m);
156 attrListFile.close();
163 std::unique_ptr<Odb::Lib::Protobuf::AttrListFile> Lib::FileModel::Design::AttrListFile::to_protobuf()
const
165 auto message = std::make_unique<Odb::Lib::Protobuf::AttrListFile>();
167 message->set_directory(Odb::Lib::Text::ToUtf8(m_directory.string()));
168 message->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
169 message->set_units(Odb::Lib::Text::ToUtf8(m_units));
171 for (
const auto& kvAttribute : m_attributesByName)
173 (*message->mutable_attributesbyname())[Odb::Lib::Text::ToUtf8(kvAttribute.first)] =
Odb::Lib::Text::ToUtf8(kvAttribute.second);
180 void Lib::FileModel::Design::AttrListFile::from_protobuf(
const Odb::Lib::Protobuf::AttrListFile& message)
182 m_directory = message.directory();
183 m_path = message.path();
184 m_units = message.units();
186 for (
const auto& kvAttribute : message.attributesbyname())
188 m_attributesByName[kvAttribute.first] = kvAttribute.second;
192 bool Lib::FileModel::Design::AttrListFile::attributeValueIsOptional(
const std::string& attributeName)
const
207 bool AttrListFile::Save(std::ostream& os)
209 os << Constants::UNITS_TOKEN <<
" = " << m_units << std::endl;
210 for (
const auto& kvAttribute : m_attributesByName)
212 os << kvAttribute.first <<
" = " << kvAttribute.second << std::endl;
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.