OdbDesignLib
OdbDesign ODB++ Parsing Library
 
Loading...
Searching...
No Matches
SymbolsDirectory.cpp
1#include "SymbolsDirectory.h"
2#include "Logger.h"
3#include <filesystem>
4#include <string>
5#include "FeaturesFile.h"
6#include "AttrListFile.h"
7#include <memory>
8#include "../../Text/Utf8Sanitizer.h"
9
10namespace Odb::Lib::FileModel::Design
11{
12 SymbolsDirectory::SymbolsDirectory(const std::filesystem::path& path)
13 : m_name("")
14 , m_path(path)
15 {
16 }
17
18 bool SymbolsDirectory::Parse()
19 {
20
21 if (!std::filesystem::exists(m_path)) return false;
22 else if (!std::filesystem::is_directory(m_path)) return false;
23
24 m_name = std::filesystem::path(m_path).filename().string();
25
26 loginfo("Parsing symbols directory: " + m_name + "...");
27
28 if (!ParseFeaturesFile(m_path)) return false;
29 if (!ParseAttrListFile(m_path)) return false;
30
31 loginfo("Parsing symbols directory: " + m_name + " complete");
32
33 return true;
34 }
35
36 std::string SymbolsDirectory::GetName() const { return m_name; }
37 std::filesystem::path SymbolsDirectory::GetPath() const { return m_path; }
38
39 const FeaturesFile& SymbolsDirectory::GetFeaturesFile() const { return m_featuresFile; }
40 const AttrListFile& SymbolsDirectory::GetAttrListFile() const { return m_attrListFile; }
41
42 std::unique_ptr<Odb::Lib::Protobuf::SymbolsDirectory> SymbolsDirectory::to_protobuf() const
43 {
44 auto message = std::make_unique<Odb::Lib::Protobuf::SymbolsDirectory>();
45 message->set_name(Odb::Lib::Text::ToUtf8(m_name));
46 message->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
47 message->mutable_attrlistfile()->CopyFrom(*m_attrListFile.to_protobuf());
48 message->mutable_featurefile()->CopyFrom(*m_featuresFile.to_protobuf());
49 return message;
50 }
51
52 void SymbolsDirectory::from_protobuf(const Odb::Lib::Protobuf::SymbolsDirectory& message)
53 {
54 m_name = message.name();
55 m_path = message.path();
56 m_attrListFile.from_protobuf(message.attrlistfile());
57 m_featuresFile.from_protobuf(message.featurefile());
58 }
59
60 bool SymbolsDirectory::ParseFeaturesFile(const std::filesystem::path& directory)
61 {
62 return m_featuresFile.Parse(directory);
63 }
64
65 bool SymbolsDirectory::ParseAttrListFile(const std::filesystem::path& directory)
66 {
67 return m_attrListFile.Parse(directory);
68 }
69
70 bool SymbolsDirectory::Save(const std::filesystem::path& directory)
71 {
72 auto symbolDir = directory / m_name;
73 if (!create_directory(symbolDir)) return false;
74
75 //FeaturesFile m_featuresFile;
76 std::ofstream featuresFile(symbolDir / "features");
77 if (!featuresFile.is_open()) return false;
78 if (!m_featuresFile.Save(featuresFile)) return false;
79 featuresFile.close();
80
81 //AttrListFile m_attrListFile;
82 std::ofstream attrlistFile(symbolDir / "attrlist");
83 if (!attrlistFile.is_open()) return false;
84 if (!m_attrListFile.Save(attrlistFile)) return false;
85 attrlistFile.close();
86
87 return true;
88 }
89}