Line data Source code
1 : #include "LayerDirectory.h"
2 : #include "Logger.h"
3 : #include "../../Text/Utf8Sanitizer.h"
4 :
5 : namespace Odb::Lib::FileModel::Design
6 : {
7 3224 : LayerDirectory::LayerDirectory(std::filesystem::path path)
8 3224 : : m_path(path)
9 : {
10 3224 : }
11 :
12 2689 : LayerDirectory::~LayerDirectory()
13 : {
14 2689 : }
15 :
16 6114 : std::string LayerDirectory::GetName() const
17 : {
18 6114 : return m_name;
19 : }
20 :
21 0 : std::filesystem::path LayerDirectory::GetPath() const
22 : {
23 0 : return m_path;
24 : }
25 :
26 3056 : bool LayerDirectory::Parse()
27 : {
28 3056 : if (!std::filesystem::exists(m_path)) return false;
29 3056 : else if (!std::filesystem::is_directory(m_path)) return false;
30 :
31 3056 : m_name = std::filesystem::path(m_path).filename().string();
32 :
33 6112 : loginfo("Parsing layer directory: " + m_name + "...");
34 :
35 3056 : if (!ParseComponentsFile(m_path)) return false;
36 3056 : if (!ParseFeaturesFile(m_path)) return false;
37 3056 : if (!ParseAttrListFile(m_path)) return false;
38 3056 : if (!ParseToolsFile(m_path)) return false;
39 :
40 6112 : loginfo("Parsing layer directory: " + m_name + " complete");
41 :
42 3056 : return true;
43 : }
44 :
45 3056 : bool LayerDirectory::ParseAttrListFile(std::filesystem::path directory)
46 : {
47 3056 : return m_attrListFile.Parse(directory);
48 : }
49 :
50 3056 : bool Odb::Lib::FileModel::Design::LayerDirectory::ParseComponentsFile(std::filesystem::path directory)
51 : {
52 3056 : return m_componentsFile.Parse(directory);
53 : }
54 :
55 3056 : bool Odb::Lib::FileModel::Design::LayerDirectory::ParseFeaturesFile(std::filesystem::path directory)
56 : {
57 9168 : return m_featuresFile.Parse(directory);
58 : }
59 :
60 3056 : bool Odb::Lib::FileModel::Design::LayerDirectory::ParseToolsFile(std::filesystem::path directory)
61 : {
62 3056 : return m_toolsFile.Parse(directory);
63 : }
64 :
65 2754 : const ComponentsFile& Odb::Lib::FileModel::Design::LayerDirectory::GetComponentsFile() const
66 : {
67 2754 : return m_componentsFile;
68 : }
69 :
70 2830 : const FeaturesFile& LayerDirectory::GetFeaturesFile() const
71 : {
72 2830 : return m_featuresFile;
73 : }
74 :
75 2404 : const AttrListFile& LayerDirectory::GetAttrListFile() const
76 : {
77 2404 : return m_attrListFile;
78 : }
79 :
80 2473 : const ToolsFile& LayerDirectory::GetToolsFile() const
81 : {
82 2473 : return m_toolsFile;
83 : }
84 :
85 2312 : std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> Odb::Lib::FileModel::Design::LayerDirectory::to_protobuf() const
86 : {
87 2312 : std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> pLayerDirectoryMessage(new Odb::Lib::Protobuf::LayerDirectory);
88 2312 : FillProtobufMessage(*pLayerDirectoryMessage, /*includeFeatureRecords=*/true);
89 2312 : return pLayerDirectoryMessage;
90 0 : }
91 :
92 368 : std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> Odb::Lib::FileModel::Design::LayerDirectory::to_protobuf_skeleton() const
93 : {
94 368 : std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> pLayerDirectoryMessage(new Odb::Lib::Protobuf::LayerDirectory);
95 368 : FillProtobufMessage(*pLayerDirectoryMessage, /*includeFeatureRecords=*/false);
96 368 : return pLayerDirectoryMessage;
97 0 : }
98 :
99 2680 : void Odb::Lib::FileModel::Design::LayerDirectory::FillProtobufMessage(Odb::Lib::Protobuf::LayerDirectory& layerDirectoryMessage, bool includeFeatureRecords) const
100 : {
101 5360 : layerDirectoryMessage.set_name(Odb::Lib::Text::ToUtf8(m_name));
102 5360 : layerDirectoryMessage.set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
103 2680 : layerDirectoryMessage.mutable_components()->CopyFrom(*m_componentsFile.to_protobuf());
104 2680 : if (includeFeatureRecords)
105 : {
106 2312 : layerDirectoryMessage.mutable_featurefile()->CopyFrom(*m_featuresFile.to_protobuf());
107 : }
108 : else
109 : {
110 368 : layerDirectoryMessage.mutable_featurefile()->CopyFrom(*m_featuresFile.to_protobuf_skeleton());
111 : }
112 2680 : layerDirectoryMessage.mutable_attrlistfile()->CopyFrom(*m_attrListFile.to_protobuf());
113 2680 : layerDirectoryMessage.mutable_tools()->CopyFrom(*m_toolsFile.to_protobuf());
114 2680 : }
115 :
116 168 : void Odb::Lib::FileModel::Design::LayerDirectory::from_protobuf(const Odb::Lib::Protobuf::LayerDirectory& message)
117 : {
118 168 : m_name = message.name();
119 168 : m_path = message.path();
120 168 : m_componentsFile.from_protobuf(message.components());
121 168 : m_featuresFile.from_protobuf(message.featurefile());
122 168 : m_attrListFile.from_protobuf(message.attrlistfile());
123 168 : m_toolsFile.from_protobuf(message.tools());
124 168 : }
125 :
126 0 : bool LayerDirectory::Save(const std::filesystem::path& directory)
127 : {
128 0 : auto layerDir = directory / m_name;
129 0 : if (!create_directory(layerDir)) return false;
130 :
131 : //ComponentsFile m_componentsFile;
132 0 : std::ofstream componentsFile(layerDir / "components");
133 0 : if (!componentsFile.is_open()) return false;
134 0 : if (!m_componentsFile.Save(componentsFile)) return false;
135 0 : componentsFile.close();
136 :
137 : //FeaturesFile m_featuresFile;
138 0 : std::ofstream featuresFile(layerDir / "features");
139 0 : if (!featuresFile.is_open()) return false;
140 0 : if (!m_featuresFile.Save(featuresFile)) return false;
141 0 : featuresFile.close();
142 :
143 : //AttrListFile m_attrListFile;
144 0 : std::ofstream attrlistFile(layerDir / "attrlist");
145 0 : if (!attrlistFile.is_open()) return false;
146 0 : if (!m_attrListFile.Save(attrlistFile)) return false;
147 0 : attrlistFile.close();
148 :
149 : //ToolsFile m_toolsFile;
150 0 : std::ofstream toolsFile(layerDir / ToolsFile::TOOLS_FILENAME);
151 0 : if (!toolsFile.is_open()) return false;
152 0 : if (!m_toolsFile.Save(toolsFile)) return false;
153 0 : toolsFile.close();
154 :
155 0 : return true;
156 0 : }
157 : }
|