1 #include "StepDirectory.h"
3 #include "LayerDirectory.h"
10 namespace Odb::Lib::FileModel::Design
12 StepDirectory::StepDirectory(std::filesystem::path path)
17 StepDirectory::~StepDirectory()
19 m_layersByName.clear();
20 m_netlistsByName.clear();
23 std::string StepDirectory::GetName()
28 std::filesystem::path StepDirectory::GetPath()
33 const EdaDataFile& StepDirectory::GetEdaDataFile()
const {
return m_edaData; }
34 const LayerDirectory::StringMap& StepDirectory::GetLayersByName()
const {
return m_layersByName; }
35 const NetlistFile::StringMap& StepDirectory::GetNetlistsByName()
const {
return m_netlistsByName; }
37 const AttrListFile& StepDirectory::GetAttrListFile()
const
39 return m_attrListFile;
42 const FeaturesFile& StepDirectory::GetProfileFile()
const
47 const StepHdrFile& StepDirectory::GetStepHdrFile()
const
52 bool StepDirectory::Parse()
54 if (!std::filesystem::exists(m_path))
return false;
55 else if (!std::filesystem::is_directory(m_path))
return false;
57 m_name = std::filesystem::path(m_path).filename().string();
59 loginfo(
"Parsing step directory: " + m_name +
"...");
61 auto layersPath = m_path /
"layers";
62 if (!ParseLayerFiles(layersPath))
return false;
64 auto netlistsPath = m_path /
"netlists";
65 if (!ParseNetlistFiles(netlistsPath))
return false;
67 auto edaPath = m_path /
"eda";
68 if (!ParseEdaDataFiles(edaPath))
return false;
70 auto attrListPath = m_path;
71 if (!ParseAttrListFile(attrListPath))
return false;
73 auto profilePath = m_path;
74 if (!ParseProfileFile(profilePath))
return false;
76 auto stepHdrPath = m_path;
77 if (!ParseStepHdrFile(stepHdrPath))
return false;
79 loginfo(
"Parsing step directory: " + m_name +
" complete");
84 bool StepDirectory::ParseLayerFiles(std::filesystem::path layersPath)
86 loginfo(
"Parsing layer directories...");
88 if (!std::filesystem::exists(layersPath))
return false;
89 else if (!std::filesystem::is_directory(layersPath))
return false;
91 for (
auto& d : std::filesystem::directory_iterator(layersPath))
93 if (std::filesystem::is_directory(d))
95 auto pLayer = std::make_shared<LayerDirectory>(d.path());
98 loginfo(
"Parsing layer: " + pLayer->GetName() +
" complete");
100 m_layersByName[pLayer->GetName()] = pLayer;
109 loginfo(
"Parsing layer directories complete");
114 bool StepDirectory::ParseEdaDataFiles(std::filesystem::path edaPath)
116 loginfo(
"Parsing eda/data file...");
118 if (!std::filesystem::exists(edaPath))
return false;
119 else if (!std::filesystem::is_directory(edaPath))
return false;
122 auto success = m_edaData.Parse(edaPath);
124 loginfo(
"Parsing eda/data file complete");
129 bool StepDirectory::ParseAttrListFile(std::filesystem::path attrListFileDirectory)
131 loginfo(
"Parsing attrlist file...");
133 if (!std::filesystem::exists(attrListFileDirectory))
return false;
134 if (!std::filesystem::is_directory(attrListFileDirectory))
return false;
137 auto success = m_attrListFile.Parse(attrListFileDirectory);
139 loginfo(
"Parsing attrlist file complete");
144 bool StepDirectory::ParseProfileFile(std::filesystem::path profileFileDirectory)
146 loginfo(
"Parsing profile file...");
148 if (!std::filesystem::exists(profileFileDirectory))
return false;
149 if (!std::filesystem::is_directory(profileFileDirectory))
return false;
152 auto success = m_profileFile.Parse(profileFileDirectory, PROFILE_FILENAME);
154 loginfo(
"Parsing profile file complete");
159 bool StepDirectory::ParseStepHdrFile(std::filesystem::path stepHdrFileDirectory)
161 loginfo(
"Parsing stephdr file...");
163 if (!std::filesystem::exists(stepHdrFileDirectory))
return false;
164 if (!std::filesystem::is_directory(stepHdrFileDirectory))
return false;
167 auto success = m_stepHdrFile.Parse(stepHdrFileDirectory);
169 loginfo(
"Parsing stephdr file complete");
174 bool StepDirectory::Save(
const std::filesystem::path& directory)
176 auto stepDir = directory / m_name;
177 if (!create_directory(stepDir))
return false;
180 auto edaPath = stepDir /
"eda";
181 if (!create_directory(edaPath))
return false;
182 std::ofstream edaDataFile(edaPath /
"data");
183 if (!m_edaData.Save(edaDataFile))
return false;
187 std::ofstream attrlistFile(stepDir /
"attrlist");
188 if (!m_attrListFile.Save(attrlistFile))
return false;
189 attrlistFile.close();
192 std::ofstream profileFile(stepDir /
"profile");
193 if (!m_profileFile.Save(profileFile))
return false;
197 std::ofstream stephdrFile(stepDir /
"stephdr");
198 if (!m_stepHdrFile.Save(stephdrFile))
return false;
202 auto layersPath = stepDir /
"layers";
203 if (!create_directory(layersPath))
return false;
204 for (
auto& kvLayer : m_layersByName)
206 if (!kvLayer.second->Save(layersPath))
return false;
210 auto netlistsPath = stepDir /
"netlists";
211 if (!create_directory(netlistsPath))
return false;
212 for (
auto& kvNetlist : m_netlistsByName)
214 if (!kvNetlist.second->Save(netlistsPath))
return false;
220 std::unique_ptr<Odb::Lib::Protobuf::StepDirectory> StepDirectory::to_protobuf()
const
222 std::unique_ptr<Odb::Lib::Protobuf::StepDirectory> pStepDirectoryMessage(
new Odb::Lib::Protobuf::StepDirectory);
223 pStepDirectoryMessage->set_name(m_name);
224 pStepDirectoryMessage->set_path(m_path.string());
225 pStepDirectoryMessage->mutable_edadatafile()->CopyFrom(*m_edaData.to_protobuf());
226 pStepDirectoryMessage->mutable_attrlistfile()->CopyFrom(*m_attrListFile.to_protobuf());
227 pStepDirectoryMessage->mutable_profilefile()->CopyFrom(*m_profileFile.to_protobuf());
228 pStepDirectoryMessage->mutable_stephdrfile()->CopyFrom(*m_stepHdrFile.to_protobuf());
230 for (
const auto& kvNetlistFile : m_netlistsByName)
232 (*pStepDirectoryMessage->mutable_netlistsbyname())[kvNetlistFile.first] = *kvNetlistFile.second->to_protobuf();
235 for (
const auto& kvLayerDirectoryRecord : m_layersByName)
237 (*pStepDirectoryMessage->mutable_layersbyname())[kvLayerDirectoryRecord.first] = *kvLayerDirectoryRecord.second->to_protobuf();
240 return pStepDirectoryMessage;
243 void StepDirectory::from_protobuf(
const Odb::Lib::Protobuf::StepDirectory& message)
245 m_name = message.name();
246 m_path = message.path();
247 m_edaData.from_protobuf(message.edadatafile());
248 m_attrListFile.from_protobuf(message.attrlistfile());
249 m_profileFile.from_protobuf(message.profilefile());
250 m_stepHdrFile.from_protobuf(message.stephdrfile());
252 for (
const auto& kvNetlistFile : message.netlistsbyname())
254 auto pNetlistFile = std::make_shared<NetlistFile>(std::filesystem::path(kvNetlistFile.second.path()));
255 pNetlistFile->from_protobuf(kvNetlistFile.second);
256 m_netlistsByName[kvNetlistFile.first] = pNetlistFile;
259 for (
const auto& kvLayerDirectoryRecord : message.layersbyname())
261 auto pLayerDirectory = std::make_shared<LayerDirectory>(std::filesystem::path(kvLayerDirectoryRecord.second.path()));
262 pLayerDirectory->from_protobuf(kvLayerDirectoryRecord.second);
263 m_layersByName[kvLayerDirectoryRecord.first] = pLayerDirectory;
267 bool StepDirectory::ParseNetlistFiles(std::filesystem::path netlistsPath)
269 loginfo(
"Parsing netlist files...");
271 std::size_t netListDirectoriesFound = 0;
273 if (std::filesystem::exists(netlistsPath))
275 if (std::filesystem::is_directory(netlistsPath))
278 for (
auto& d : std::filesystem::directory_iterator(netlistsPath))
280 if (std::filesystem::is_directory(d))
282 netListDirectoriesFound++;
284 auto pNetlist = std::make_shared<NetlistFile>(d.path());
285 if (pNetlist->Parse())
287 m_netlistsByName[pNetlist->GetName()] = pNetlist;
292 logerror(
"Failed to parse netlist directory: " + pNetlist->GetName());
299 if (netListDirectoriesFound == 0)
301 logwarn(
"No netlist directories found");
303 else if (netListDirectoriesFound == m_netlistsByName.size())
305 loginfo(
"netlist directories parsed successfully");
311 const ComponentsFile* StepDirectory::GetTopComponentsFile()
const
313 auto findIt = m_layersByName.find(ComponentsFile::TOP_COMPONENTS_LAYER_NAME);
314 if (findIt != m_layersByName.end())
316 return &(findIt->second->GetComponentsFile());
324 const ComponentsFile* StepDirectory::GetBottomComponentsFile()
const
326 auto findIt = m_layersByName.find(ComponentsFile::BOTTOM_COMPONENTS_LAYER_NAME);
327 if (findIt != m_layersByName.end())
329 return &(findIt->second->GetComponentsFile());