OdbDesignLib
OdbDesign ODB++ Parsing Library
LayerDirectory.cpp
1 #include "LayerDirectory.h"
2 #include "Logger.h"
3 
4 namespace Odb::Lib::FileModel::Design
5 {
6  LayerDirectory::LayerDirectory(std::filesystem::path path)
7  : m_path(path)
8  {
9  }
10 
11  LayerDirectory::~LayerDirectory()
12  {
13  }
14 
15  std::string LayerDirectory::GetName() const
16  {
17  return m_name;
18  }
19 
20  std::filesystem::path LayerDirectory::GetPath() const
21  {
22  return m_path;
23  }
24 
25  bool LayerDirectory::Parse()
26  {
27  if (!std::filesystem::exists(m_path)) return false;
28  else if (!std::filesystem::is_directory(m_path)) return false;
29 
30  m_name = std::filesystem::path(m_path).filename().string();
31 
32  loginfo("Parsing layer directory: " + m_name + "...");
33 
34  if (!ParseComponentsFile(m_path)) return false;
35  if (!ParseFeaturesFile(m_path)) return false;
36  if (!ParseAttrListFile(m_path)) return false;
37 
38  loginfo("Parsing layer directory: " + m_name + " complete");
39 
40  return true;
41  }
42 
43  bool LayerDirectory::ParseAttrListFile(std::filesystem::path directory)
44  {
45  return m_attrListFile.Parse(directory);
46  }
47 
48  bool Odb::Lib::FileModel::Design::LayerDirectory::ParseComponentsFile(std::filesystem::path directory)
49  {
50  return m_componentsFile.Parse(directory);
51  }
52 
53  bool Odb::Lib::FileModel::Design::LayerDirectory::ParseFeaturesFile(std::filesystem::path directory)
54  {
55  return m_featuresFile.Parse(directory);
56  }
57 
58  const ComponentsFile& Odb::Lib::FileModel::Design::LayerDirectory::GetComponentsFile() const
59  {
60  return m_componentsFile;
61  }
62 
63  const FeaturesFile& LayerDirectory::GetFeaturesFile() const
64  {
65  return m_featuresFile;
66  }
67 
68  const AttrListFile& LayerDirectory::GetAttrListFile() const
69  {
70  return m_attrListFile;
71  }
72 
73  std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> Odb::Lib::FileModel::Design::LayerDirectory::to_protobuf() const
74  {
75  std::unique_ptr<Odb::Lib::Protobuf::LayerDirectory> pLayerDirectoryMessage(new Odb::Lib::Protobuf::LayerDirectory);
76  pLayerDirectoryMessage->set_name(m_name);
77  pLayerDirectoryMessage->set_path(m_path.string());
78  pLayerDirectoryMessage->mutable_components()->CopyFrom(*m_componentsFile.to_protobuf());
79  pLayerDirectoryMessage->mutable_featurefile()->CopyFrom(*m_featuresFile.to_protobuf());
80  pLayerDirectoryMessage->mutable_attrlistfile()->CopyFrom(*m_attrListFile.to_protobuf());
81  return pLayerDirectoryMessage;
82  }
83 
84  void Odb::Lib::FileModel::Design::LayerDirectory::from_protobuf(const Odb::Lib::Protobuf::LayerDirectory& message)
85  {
86  m_name = message.name();
87  m_path = message.path();
88  m_componentsFile.from_protobuf(message.components());
89  m_featuresFile.from_protobuf(message.featurefile());
90  m_attrListFile.from_protobuf(message.attrlistfile());
91  }
92 
93  bool LayerDirectory::Save(const std::filesystem::path& directory)
94  {
95  auto layerDir = directory / m_name;
96  if (!create_directory(layerDir)) return false;
97 
98  //ComponentsFile m_componentsFile;
99  std::ofstream componentsFile(layerDir / "components");
100  if (!componentsFile.is_open()) return false;
101  if (!m_componentsFile.Save(componentsFile)) return false;
102  componentsFile.close();
103 
104  //FeaturesFile m_featuresFile;
105  std::ofstream featuresFile(layerDir / "features");
106  if (!featuresFile.is_open()) return false;
107  if (!m_featuresFile.Save(featuresFile)) return false;
108  featuresFile.close();
109 
110  //AttrListFile m_attrListFile;
111  std::ofstream attrlistFile(layerDir / "attrlist");
112  if (!attrlistFile.is_open()) return false;
113  if (!m_attrListFile.Save(attrlistFile)) return false;
114  attrlistFile.close();
115 
116  return true;
117  }
118 }