OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
LayerDirectory.cpp
Go to the documentation of this file.
1#include "LayerDirectory.h"
2#include "Logger.h"
3#include "../../Text/Utf8Sanitizer.h"
4
6{
7 LayerDirectory::LayerDirectory(std::filesystem::path path)
8 : m_path(path)
9 {
10 }
11
15
16 std::string LayerDirectory::GetName() const
17 {
18 return m_name;
19 }
20
21 std::filesystem::path LayerDirectory::GetPath() const
22 {
23 return m_path;
24 }
25
27 {
28 if (!std::filesystem::exists(m_path)) return false;
29 else if (!std::filesystem::is_directory(m_path)) return false;
30
31 m_name = std::filesystem::path(m_path).filename().string();
32
33 loginfo("Parsing layer directory: " + m_name + "...");
34
35 if (!ParseComponentsFile(m_path)) return false;
36 if (!ParseFeaturesFile(m_path)) return false;
37 if (!ParseAttrListFile(m_path)) return false;
38 if (!ParseToolsFile(m_path)) return false;
39
40 loginfo("Parsing layer directory: " + m_name + " complete");
41
42 return true;
43 }
44
45 bool LayerDirectory::ParseAttrListFile(std::filesystem::path directory)
46 {
47 return m_attrListFile.Parse(directory);
48 }
49
51 {
52 return m_componentsFile.Parse(directory);
53 }
54
56 {
57 return m_featuresFile.Parse(directory);
58 }
59
61 {
62 return m_toolsFile.Parse(directory);
63 }
64
66 {
67 return m_componentsFile;
68 }
69
71 {
72 return m_featuresFile;
73 }
74
76 {
77 return m_attrListFile;
78 }
79
81 {
82 return m_toolsFile;
83 }
84
85 std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> Odb::Lib::FileModel::Design::LayerDirectory::to_protobuf() const
86 {
87 std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> pLayerDirectoryMessage(new Odb::Lib::Protobuf::LayerDirectory);
88 FillProtobufMessage(*pLayerDirectoryMessage, /*includeFeatureRecords=*/true);
89 return pLayerDirectoryMessage;
90 }
91
92 std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> Odb::Lib::FileModel::Design::LayerDirectory::to_protobuf_skeleton() const
93 {
94 std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> pLayerDirectoryMessage(new Odb::Lib::Protobuf::LayerDirectory);
95 FillProtobufMessage(*pLayerDirectoryMessage, /*includeFeatureRecords=*/false);
96 return pLayerDirectoryMessage;
97 }
98
99 void Odb::Lib::FileModel::Design::LayerDirectory::FillProtobufMessage(Odb::Lib::Protobuf::LayerDirectory& layerDirectoryMessage, bool includeFeatureRecords) const
100 {
101 layerDirectoryMessage.set_name(Odb::Lib::Text::ToUtf8(m_name));
102 layerDirectoryMessage.set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
103 layerDirectoryMessage.mutable_components()->CopyFrom(*m_componentsFile.to_protobuf());
104 if (includeFeatureRecords)
105 {
106 layerDirectoryMessage.mutable_featurefile()->CopyFrom(*m_featuresFile.to_protobuf());
107 }
108 else
109 {
110 layerDirectoryMessage.mutable_featurefile()->CopyFrom(*m_featuresFile.to_protobuf_skeleton());
111 }
112 layerDirectoryMessage.mutable_attrlistfile()->CopyFrom(*m_attrListFile.to_protobuf());
113 layerDirectoryMessage.mutable_tools()->CopyFrom(*m_toolsFile.to_protobuf());
114 }
115
116 void Odb::Lib::FileModel::Design::LayerDirectory::from_protobuf(const Odb::Lib::Protobuf::LayerDirectory& message)
117 {
118 m_name = message.name();
119 m_path = message.path();
120 m_componentsFile.from_protobuf(message.components());
121 m_featuresFile.from_protobuf(message.featurefile());
122 m_attrListFile.from_protobuf(message.attrlistfile());
123 m_toolsFile.from_protobuf(message.tools());
124 }
125
126 bool LayerDirectory::Save(const std::filesystem::path& directory)
127 {
128 auto layerDir = directory / m_name;
129 if (!create_directory(layerDir)) return false;
130
131 //ComponentsFile m_componentsFile;
132 std::ofstream componentsFile(layerDir / "components");
133 if (!componentsFile.is_open()) return false;
134 if (!m_componentsFile.Save(componentsFile)) return false;
135 componentsFile.close();
136
137 //FeaturesFile m_featuresFile;
138 std::ofstream featuresFile(layerDir / "features");
139 if (!featuresFile.is_open()) return false;
140 if (!m_featuresFile.Save(featuresFile)) return false;
141 featuresFile.close();
142
143 //AttrListFile m_attrListFile;
144 std::ofstream attrlistFile(layerDir / "attrlist");
145 if (!attrlistFile.is_open()) return false;
146 if (!m_attrListFile.Save(attrlistFile)) return false;
147 attrlistFile.close();
148
149 //ToolsFile m_toolsFile;
150 std::ofstream toolsFile(layerDir / ToolsFile::TOOLS_FILENAME);
151 if (!toolsFile.is_open()) return false;
152 if (!m_toolsFile.Save(toolsFile)) return false;
153 toolsFile.close();
154
155 return true;
156 }
157}
#define loginfo(s)
Definition Logger.h:14
bool Parse(std::filesystem::path directory)
bool Save(std::ostream &os) override
bool Save(std::ostream &os) override
LayerDirectory(std::filesystem::path path)
bool ParseAttrListFile(std::filesystem::path directory)
bool Save(const std::filesystem::path &directory) override
bool ParseToolsFile(std::filesystem::path directory)
std::unique_ptr< Odb::Lib::Protobuf::LayerDirectory > to_protobuf_skeleton() const
const ComponentsFile & GetComponentsFile() const
void from_protobuf(const Odb::Lib::Protobuf::LayerDirectory &message) override
std::unique_ptr< Odb::Lib::Protobuf::LayerDirectory > to_protobuf() const override
const FeaturesFile & GetFeaturesFile() const
const AttrListFile & GetAttrListFile() const
bool ParseComponentsFile(std::filesystem::path directory)
bool ParseFeaturesFile(std::filesystem::path directory)
bool Save(std::ostream &os) override
static constexpr const char * TOOLS_FILENAME
Definition ToolsFile.h:100
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.