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