8#include "../../Text/Utf8Sanitizer.h"
20 m_layersByName.clear();
21 m_netlistsByName.clear();
40 return m_attrListFile;
55 if (!std::filesystem::exists(m_path))
return false;
56 else if (!std::filesystem::is_directory(m_path))
return false;
58 m_name = std::filesystem::path(m_path).filename().string();
60 loginfo(
"Parsing step directory: " + m_name +
"...");
62 auto layersPath = m_path /
"layers";
63 if (!ParseLayerFiles(layersPath))
return false;
65 auto netlistsPath = m_path /
"netlists";
66 if (!ParseNetlistFiles(netlistsPath))
return false;
68 auto edaPath = m_path /
"eda";
69 if (!ParseEdaDataFiles(edaPath))
return false;
71 auto attrListPath = m_path;
72 if (!ParseAttrListFile(attrListPath))
return false;
74 auto profilePath = m_path;
75 if (!ParseProfileFile(profilePath))
return false;
77 auto stepHdrPath = m_path;
78 if (!ParseStepHdrFile(stepHdrPath))
return false;
80 loginfo(
"Parsing step directory: " + m_name +
" complete");
85 bool StepDirectory::ParseLayerFiles(std::filesystem::path layersPath)
87 loginfo(
"Parsing layer directories...");
89 if (!std::filesystem::exists(layersPath))
return false;
90 else if (!std::filesystem::is_directory(layersPath))
return false;
92 for (
auto& d : std::filesystem::directory_iterator(layersPath))
94 if (std::filesystem::is_directory(d))
96 auto pLayer = std::make_shared<LayerDirectory>(d.path());
99 loginfo(
"Parsing layer: " + pLayer->GetName() +
" complete");
101 m_layersByName[pLayer->GetName()] = pLayer;
110 loginfo(
"Parsing layer directories complete");
115 bool StepDirectory::ParseEdaDataFiles(std::filesystem::path edaPath)
117 loginfo(
"Parsing eda/data file...");
119 if (!std::filesystem::exists(edaPath))
return false;
120 else if (!std::filesystem::is_directory(edaPath))
return false;
123 auto success = m_edaData.
Parse(edaPath);
125 loginfo(
"Parsing eda/data file complete");
130 bool StepDirectory::ParseAttrListFile(std::filesystem::path attrListFileDirectory)
132 loginfo(
"Parsing attrlist file...");
134 if (!std::filesystem::exists(attrListFileDirectory))
return false;
135 if (!std::filesystem::is_directory(attrListFileDirectory))
return false;
138 auto success = m_attrListFile.
Parse(attrListFileDirectory);
140 loginfo(
"Parsing attrlist file complete");
145 bool StepDirectory::ParseProfileFile(std::filesystem::path profileFileDirectory)
147 loginfo(
"Parsing profile file...");
149 if (!std::filesystem::exists(profileFileDirectory))
return false;
150 if (!std::filesystem::is_directory(profileFileDirectory))
return false;
153 auto success = m_profileFile.
Parse(profileFileDirectory, PROFILE_FILENAME);
155 loginfo(
"Parsing profile file complete");
160 bool StepDirectory::ParseStepHdrFile(std::filesystem::path stepHdrFileDirectory)
162 loginfo(
"Parsing stephdr file...");
164 if (!std::filesystem::exists(stepHdrFileDirectory))
return false;
165 if (!std::filesystem::is_directory(stepHdrFileDirectory))
return false;
168 auto success = m_stepHdrFile.
Parse(stepHdrFileDirectory);
170 loginfo(
"Parsing stephdr file complete");
177 auto stepDir = directory / m_name;
178 if (!create_directory(stepDir))
return false;
181 auto edaPath = stepDir /
"eda";
182 if (!create_directory(edaPath))
return false;
183 std::ofstream edaDataFile(edaPath /
"data");
184 if (!m_edaData.
Save(edaDataFile))
return false;
188 std::ofstream attrlistFile(stepDir /
"attrlist");
189 if (!m_attrListFile.
Save(attrlistFile))
return false;
190 attrlistFile.close();
193 std::ofstream profileFile(stepDir /
"profile");
194 if (!m_profileFile.
Save(profileFile))
return false;
198 std::ofstream stephdrFile(stepDir /
"stephdr");
199 if (!m_stepHdrFile.
Save(stephdrFile))
return false;
203 auto layersPath = stepDir /
"layers";
204 if (!create_directory(layersPath))
return false;
205 for (
auto& kvLayer : m_layersByName)
207 if (!kvLayer.second->Save(layersPath))
return false;
211 auto netlistsPath = stepDir /
"netlists";
212 if (!create_directory(netlistsPath))
return false;
213 for (
auto& kvNetlist : m_netlistsByName)
215 if (!kvNetlist.second->Save(netlistsPath))
return false;
223 std::unique_ptr<Odb::Lib::Protobuf::StepDirectory> pStepDirectoryMessage(
new Odb::Lib::Protobuf::StepDirectory);
224 FillProtobufMessage(*pStepDirectoryMessage,
false);
225 return pStepDirectoryMessage;
230 std::unique_ptr<Odb::Lib::Protobuf::StepDirectory> pStepDirectoryMessage(
new Odb::Lib::Protobuf::StepDirectory);
231 FillProtobufMessage(*pStepDirectoryMessage,
true);
232 return pStepDirectoryMessage;
235 void StepDirectory::FillProtobufMessage(Odb::Lib::Protobuf::StepDirectory& stepDirectoryMessage,
bool layerSkeletons)
const
239 stepDirectoryMessage.mutable_edadatafile()->CopyFrom(*m_edaData.
to_protobuf());
240 stepDirectoryMessage.mutable_attrlistfile()->CopyFrom(*m_attrListFile.
to_protobuf());
241 stepDirectoryMessage.mutable_profilefile()->CopyFrom(*m_profileFile.
to_protobuf());
242 stepDirectoryMessage.mutable_stephdrfile()->CopyFrom(*m_stepHdrFile.
to_protobuf());
244 for (
const auto& kvNetlistFile : m_netlistsByName)
246 (*stepDirectoryMessage.mutable_netlistsbyname())[
Odb::Lib::Text::ToUtf8(kvNetlistFile.first)] = *kvNetlistFile.second->to_protobuf();
249 for (
const auto& kvLayerDirectoryRecord : m_layersByName)
258 *kvLayerDirectoryRecord.second->to_protobuf_skeleton();
263 *kvLayerDirectoryRecord.second->to_protobuf();
270 m_name = message.name();
271 m_path = message.path();
277 for (
const auto& kvNetlistFile : message.netlistsbyname())
279 auto pNetlistFile = std::make_shared<NetlistFile>(std::filesystem::path(kvNetlistFile.second.path()));
280 pNetlistFile->from_protobuf(kvNetlistFile.second);
281 m_netlistsByName[kvNetlistFile.first] = pNetlistFile;
284 for (
const auto& kvLayerDirectoryRecord : message.layersbyname())
286 auto pLayerDirectory = std::make_shared<LayerDirectory>(std::filesystem::path(kvLayerDirectoryRecord.second.path()));
287 pLayerDirectory->from_protobuf(kvLayerDirectoryRecord.second);
288 m_layersByName[kvLayerDirectoryRecord.first] = pLayerDirectory;
292 bool StepDirectory::ParseNetlistFiles(std::filesystem::path netlistsPath)
294 loginfo(
"Parsing netlist files...");
296 std::size_t netListDirectoriesFound = 0;
298 if (std::filesystem::exists(netlistsPath))
300 if (std::filesystem::is_directory(netlistsPath))
303 for (
auto& d : std::filesystem::directory_iterator(netlistsPath))
305 if (std::filesystem::is_directory(d))
307 netListDirectoriesFound++;
309 auto pNetlist = std::make_shared<NetlistFile>(d.path());
310 if (pNetlist->Parse())
312 m_netlistsByName[pNetlist->GetName()] = pNetlist;
317 logerror(
"Failed to parse netlist directory: " + pNetlist->GetName());
324 if (netListDirectoriesFound == 0)
326 logwarn(
"No netlist directories found");
328 else if (netListDirectoriesFound == m_netlistsByName.size())
330 loginfo(
"netlist directories parsed successfully");
339 if (findIt != m_layersByName.end())
341 return &(findIt->second->GetComponentsFile());
352 if (findIt != m_layersByName.end())
354 return &(findIt->second->GetComponentsFile());
bool Parse(std::filesystem::path directory)
void from_protobuf(const Odb::Lib::Protobuf::AttrListFile &message) override
std::unique_ptr< Odb::Lib::Protobuf::AttrListFile > to_protobuf() const override
bool Save(std::ostream &os) override
static constexpr const char * TOP_COMPONENTS_LAYER_NAME
static constexpr const char * BOTTOM_COMPONENTS_LAYER_NAME
bool Save(std::ostream &os) override
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile &message) override
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile > to_protobuf() const override
bool Parse(std::filesystem::path path)
bool Parse(std::filesystem::path directory, const std::string &alternateFilename="")
void from_protobuf(const Odb::Lib::Protobuf::FeaturesFile &message) override
bool Save(std::ostream &os) override
std::unique_ptr< Odb::Lib::Protobuf::FeaturesFile > to_protobuf() const override
std::map< std::string, std::shared_ptr< LayerDirectory > > StringMap
std::map< std::string, std::shared_ptr< NetlistFile > > StringMap
std::unique_ptr< Odb::Lib::Protobuf::StepDirectory > to_protobuf_skeleton() const
void from_protobuf(const Odb::Lib::Protobuf::StepDirectory &message) override
std::unique_ptr< Odb::Lib::Protobuf::StepDirectory > to_protobuf() const override
std::filesystem::path GetPath()
const ComponentsFile * GetTopComponentsFile() const
StepDirectory(std::filesystem::path path)
const FeaturesFile & GetProfileFile() const
const StepHdrFile & GetStepHdrFile() const
const ComponentsFile * GetBottomComponentsFile() const
const NetlistFile::StringMap & GetNetlistsByName() const
const LayerDirectory::StringMap & GetLayersByName() const
const AttrListFile & GetAttrListFile() const
const EdaDataFile & GetEdaDataFile() const
bool Save(const std::filesystem::path &directory) override
std::unique_ptr< Odb::Lib::Protobuf::StepHdrFile > to_protobuf() const override
bool Save(std::ostream &os) override
void from_protobuf(const Odb::Lib::Protobuf::StepHdrFile &message) override
bool Parse(std::filesystem::path path) override
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.