LCOV - code coverage report
Current view: top level - OdbDesignLib/FileModel/Design - StepDirectory.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 79.2 % 183 145
Test Date: 2026-11-21 03:51:50 Functions: 84.0 % 25 21
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "StepDirectory.h"
       2              : #include <filesystem>
       3              : #include "LayerDirectory.h"
       4              : #include <fstream>
       5              : #include <sstream>
       6              : #include "Logger.h"
       7              : #include <memory>
       8              : #include "../../Text/Utf8Sanitizer.h"
       9              : 
      10              : 
      11              : namespace Odb::Lib::FileModel::Design
      12              : {
      13          102 :     StepDirectory::StepDirectory(std::filesystem::path path)
      14          102 :         : m_path(path)
      15              :     {
      16          102 :     }
      17              : 
      18          102 :     StepDirectory::~StepDirectory()
      19              :     {
      20          102 :         m_layersByName.clear();
      21           84 :         m_netlistsByName.clear();
      22           84 :     }
      23              : 
      24           93 :     std::string StepDirectory::GetName()
      25              :     {
      26           93 :         return m_name;
      27              :     }
      28              : 
      29            0 :     std::filesystem::path StepDirectory::GetPath()
      30              :     {
      31            0 :         return m_path;
      32              :     }
      33              : 
      34          200 :     const EdaDataFile& StepDirectory::GetEdaDataFile() const { return m_edaData; }
      35          129 :     const LayerDirectory::StringMap& StepDirectory::GetLayersByName() const { return m_layersByName; }
      36           84 :     const NetlistFile::StringMap& StepDirectory::GetNetlistsByName() const { return m_netlistsByName; }
      37              : 
      38           82 :     const AttrListFile& StepDirectory::GetAttrListFile() const
      39              :     {
      40           82 :         return m_attrListFile;
      41              :     }
      42              : 
      43           82 :     const FeaturesFile& StepDirectory::GetProfileFile() const
      44              :     {
      45           82 :         return m_profileFile;
      46              :     }
      47              : 
      48            0 :     const StepHdrFile& StepDirectory::GetStepHdrFile() const
      49              :     {
      50            0 :         return m_stepHdrFile;
      51              :     }
      52              : 
      53           94 :     bool StepDirectory::Parse()
      54              :     {
      55           94 :         if (!std::filesystem::exists(m_path)) return false;
      56           94 :         else if (!std::filesystem::is_directory(m_path)) return false;
      57              : 
      58           94 :         m_name = std::filesystem::path(m_path).filename().string();
      59              : 
      60          188 :         loginfo("Parsing step directory: " + m_name + "...");
      61              : 
      62           94 :         auto layersPath = m_path / "layers";
      63           94 :         if (!ParseLayerFiles(layersPath)) return false;
      64              : 
      65           94 :         auto netlistsPath = m_path / "netlists";
      66           94 :         if (!ParseNetlistFiles(netlistsPath)) return false;
      67              : 
      68           94 :         auto edaPath = m_path / "eda";
      69           97 :         if (!ParseEdaDataFiles(edaPath)) return false;
      70              : 
      71           91 :         auto attrListPath = m_path;
      72           91 :         if (!ParseAttrListFile(attrListPath)) return false;
      73              : 
      74           91 :         auto profilePath = m_path;
      75           91 :         if (!ParseProfileFile(profilePath)) return false;
      76              : 
      77           91 :         auto stepHdrPath = m_path;
      78           91 :         if (!ParseStepHdrFile(stepHdrPath)) return false;
      79              : 
      80          182 :         loginfo("Parsing step directory: " + m_name + " complete");
      81              : 
      82           91 :         return true;
      83          100 :     }
      84              : 
      85           94 :     bool StepDirectory::ParseLayerFiles(std::filesystem::path layersPath)
      86              :     {
      87          376 :         loginfo("Parsing layer directories...");
      88              : 
      89           94 :         if (!std::filesystem::exists(layersPath)) return false;
      90           94 :         else if (!std::filesystem::is_directory(layersPath)) return false;
      91              : 
      92         3150 :         for (auto& d : std::filesystem::directory_iterator(layersPath))
      93              :         {
      94         3056 :             if (std::filesystem::is_directory(d))
      95              :             {
      96         3056 :                 auto pLayer = std::make_shared<LayerDirectory>(d.path());               
      97         3056 :                 if (pLayer->Parse())
      98              :                 {
      99         6112 :                     loginfo("Parsing layer: " + pLayer->GetName() + " complete");
     100              : 
     101         3056 :                     m_layersByName[pLayer->GetName()] = pLayer;
     102              :                 }
     103              :                 else
     104              :                 {
     105            0 :                     return false;
     106              :                 }
     107         3056 :             }
     108           94 :         }
     109              : 
     110          376 :         loginfo("Parsing layer directories complete");
     111              : 
     112           94 :         return true;
     113              :     }
     114              : 
     115           94 :     bool StepDirectory::ParseEdaDataFiles(std::filesystem::path edaPath)
     116              :     {
     117          376 :         loginfo("Parsing eda/data file...");
     118              : 
     119           94 :         if (!std::filesystem::exists(edaPath)) return false;
     120           94 :         else if (!std::filesystem::is_directory(edaPath)) return false;
     121              : 
     122              :         // parse nets and packages definitions      
     123           97 :         auto success =  m_edaData.Parse(edaPath);
     124              : 
     125          364 :         loginfo("Parsing eda/data file complete");
     126              : 
     127           91 :         return success;
     128              :     }
     129              : 
     130           91 :     bool StepDirectory::ParseAttrListFile(std::filesystem::path attrListFileDirectory)
     131              :     {
     132          364 :         loginfo("Parsing attrlist file...");
     133              : 
     134           91 :         if (!std::filesystem::exists(attrListFileDirectory)) return false;
     135           91 :         if (!std::filesystem::is_directory(attrListFileDirectory)) return false;
     136              : 
     137              :         // parse nets and packages definitions      
     138           91 :         auto success = m_attrListFile.Parse(attrListFileDirectory);
     139              : 
     140          364 :         loginfo("Parsing attrlist file complete");
     141              : 
     142           91 :         return success;        
     143              :     }
     144              : 
     145           91 :     bool StepDirectory::ParseProfileFile(std::filesystem::path profileFileDirectory)
     146              :     {
     147          364 :         loginfo("Parsing profile file...");
     148              : 
     149           91 :         if (!std::filesystem::exists(profileFileDirectory)) return false;
     150           91 :         if (!std::filesystem::is_directory(profileFileDirectory)) return false;
     151              : 
     152              :         // parse nets and packages definitions      
     153          182 :         auto success = m_profileFile.Parse(profileFileDirectory, PROFILE_FILENAME);
     154              : 
     155          364 :         loginfo("Parsing profile file complete");
     156              : 
     157           91 :         return success;
     158              :     }
     159              : 
     160           91 :     bool StepDirectory::ParseStepHdrFile(std::filesystem::path stepHdrFileDirectory)
     161              :     {
     162          364 :         loginfo("Parsing stephdr file...");
     163              : 
     164           91 :         if (!std::filesystem::exists(stepHdrFileDirectory)) return false;
     165           91 :         if (!std::filesystem::is_directory(stepHdrFileDirectory)) return false;
     166              : 
     167              :         // parse nets and packages definitions      
     168           91 :         auto success = m_stepHdrFile.Parse(stepHdrFileDirectory);
     169              : 
     170          364 :         loginfo("Parsing stephdr file complete");
     171              : 
     172           91 :         return success;
     173              :     }
     174              : 
     175            0 :     bool StepDirectory::Save(const std::filesystem::path& directory)
     176              :     {
     177            0 :         auto stepDir = directory / m_name;
     178            0 :         if (!create_directory(stepDir)) return false;
     179              : 
     180              :         // eda/data
     181            0 :         auto edaPath = stepDir / "eda";
     182            0 :         if (!create_directory(edaPath)) return false;
     183            0 :         std::ofstream edaDataFile(edaPath / "data");
     184            0 :         if (!m_edaData.Save(edaDataFile)) return false;
     185            0 :         edaDataFile.close();
     186              : 
     187              :         // attrlist
     188            0 :         std::ofstream attrlistFile(stepDir / "attrlist");
     189            0 :         if (!m_attrListFile.Save(attrlistFile)) return false;
     190            0 :         attrlistFile.close();
     191              : 
     192              :         // profile
     193            0 :         std::ofstream profileFile(stepDir / "profile");
     194            0 :         if (!m_profileFile.Save(profileFile)) return false;
     195            0 :         profileFile.close();
     196              : 
     197              :         // StepHdrFile
     198            0 :         std::ofstream stephdrFile(stepDir / "stephdr");
     199            0 :         if (!m_stepHdrFile.Save(stephdrFile)) return false;
     200            0 :         stephdrFile.close();
     201              : 
     202              :         // layers
     203            0 :         auto layersPath = stepDir / "layers";
     204            0 :         if (!create_directory(layersPath)) return false;
     205            0 :         for (auto& kvLayer : m_layersByName)
     206              :                 {
     207            0 :                         if (!kvLayer.second->Save(layersPath)) return false;
     208              :                 }
     209              :         
     210              :         // m_netlistsByName;
     211            0 :         auto netlistsPath = stepDir / "netlists";
     212            0 :         if (!create_directory(netlistsPath)) return false;
     213            0 :         for (auto& kvNetlist : m_netlistsByName)
     214              :                 {
     215            0 :                         if (!kvNetlist.second->Save(netlistsPath)) return false;
     216              :                 }
     217              : 
     218            0 :         return true;
     219            0 :     }
     220              : 
     221           80 :     std::unique_ptr<Odb::Lib::Protobuf::StepDirectory> StepDirectory::to_protobuf() const
     222              :     {
     223           80 :         std::unique_ptr<Odb::Lib::Protobuf::StepDirectory> pStepDirectoryMessage(new Odb::Lib::Protobuf::StepDirectory);
     224           80 :         FillProtobufMessage(*pStepDirectoryMessage, /*layerSkeletons=*/false);
     225           80 :         return pStepDirectoryMessage;
     226            0 :     }
     227              : 
     228           17 :     std::unique_ptr<Odb::Lib::Protobuf::StepDirectory> StepDirectory::to_protobuf_skeleton() const
     229              :     {
     230           17 :         std::unique_ptr<Odb::Lib::Protobuf::StepDirectory> pStepDirectoryMessage(new Odb::Lib::Protobuf::StepDirectory);
     231           17 :         FillProtobufMessage(*pStepDirectoryMessage, /*layerSkeletons=*/true);
     232           17 :         return pStepDirectoryMessage;
     233            0 :     }
     234              : 
     235           97 :     void StepDirectory::FillProtobufMessage(Odb::Lib::Protobuf::StepDirectory& stepDirectoryMessage, bool layerSkeletons) const
     236              :     {
     237          194 :         stepDirectoryMessage.set_name(Odb::Lib::Text::ToUtf8(m_name));
     238          194 :         stepDirectoryMessage.set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
     239           97 :         stepDirectoryMessage.mutable_edadatafile()->CopyFrom(*m_edaData.to_protobuf());
     240           97 :         stepDirectoryMessage.mutable_attrlistfile()->CopyFrom(*m_attrListFile.to_protobuf());
     241           97 :         stepDirectoryMessage.mutable_profilefile()->CopyFrom(*m_profileFile.to_protobuf());
     242           97 :         stepDirectoryMessage.mutable_stephdrfile()->CopyFrom(*m_stepHdrFile.to_protobuf());
     243              : 
     244          188 :         for (const auto& kvNetlistFile : m_netlistsByName)
     245              :         {
     246           91 :             (*stepDirectoryMessage.mutable_netlistsbyname())[Odb::Lib::Text::ToUtf8(kvNetlistFile.first)] = *kvNetlistFile.second->to_protobuf();
     247              :         }
     248              : 
     249         2777 :         for (const auto& kvLayerDirectoryRecord : m_layersByName)
     250              :         {
     251              :             // No conditional operator here: MSVC's lowering of
     252              :             // `cond ? f() : g()` for move-only prvalues (both returning
     253              :             // unique_ptr) synthesizes a copy of the deleted copy constructor
     254              :             // (C2280); GCC/Clang merge the prvalues per the standard.
     255         2680 :             if (layerSkeletons)
     256              :             {
     257          736 :                 (*stepDirectoryMessage.mutable_layersbyname())[Odb::Lib::Text::ToUtf8(kvLayerDirectoryRecord.first)] =
     258         1104 :                     *kvLayerDirectoryRecord.second->to_protobuf_skeleton();
     259              :             }
     260              :             else
     261              :             {
     262         4624 :                 (*stepDirectoryMessage.mutable_layersbyname())[Odb::Lib::Text::ToUtf8(kvLayerDirectoryRecord.first)] =
     263         6936 :                     *kvLayerDirectoryRecord.second->to_protobuf();
     264              :             }
     265              :         }
     266           97 :     }
     267              : 
     268            8 :     void StepDirectory::from_protobuf(const Odb::Lib::Protobuf::StepDirectory& message)
     269              :     {
     270            8 :         m_name = message.name();
     271            8 :                 m_path = message.path();
     272            8 :                 m_edaData.from_protobuf(message.edadatafile());
     273            8 :         m_attrListFile.from_protobuf(message.attrlistfile());
     274            8 :         m_profileFile.from_protobuf(message.profilefile());
     275            8 :         m_stepHdrFile.from_protobuf(message.stephdrfile());
     276              : 
     277           18 :         for (const auto& kvNetlistFile : message.netlistsbyname())
     278              :         {
     279            5 :             auto pNetlistFile = std::make_shared<NetlistFile>(std::filesystem::path(kvNetlistFile.second.path()));
     280            5 :             pNetlistFile->from_protobuf(kvNetlistFile.second);
     281            5 :             m_netlistsByName[kvNetlistFile.first] = pNetlistFile;
     282            5 :         }
     283              : 
     284          344 :         for (const auto& kvLayerDirectoryRecord : message.layersbyname())
     285              :         {
     286          168 :                         auto pLayerDirectory = std::make_shared<LayerDirectory>(std::filesystem::path(kvLayerDirectoryRecord.second.path()));
     287          168 :                         pLayerDirectory->from_protobuf(kvLayerDirectoryRecord.second);
     288          168 :                         m_layersByName[kvLayerDirectoryRecord.first] = pLayerDirectory;
     289          168 :                 }
     290            8 :     }
     291              : 
     292           94 :     bool StepDirectory::ParseNetlistFiles(std::filesystem::path netlistsPath)
     293              :     {
     294          376 :         loginfo("Parsing netlist files...");
     295              : 
     296           94 :         std::size_t netListDirectoriesFound = 0;
     297              : 
     298           94 :         if (std::filesystem::exists(netlistsPath))
     299              :         {
     300           94 :             if (std::filesystem::is_directory(netlistsPath))
     301              :             {                
     302              :                 // parse net name records
     303          188 :                 for (auto& d : std::filesystem::directory_iterator(netlistsPath))
     304              :                 {
     305           94 :                     if (std::filesystem::is_directory(d))
     306              :                     {
     307           94 :                         netListDirectoriesFound++;
     308              : 
     309           94 :                         auto pNetlist = std::make_shared<NetlistFile>(d.path());
     310           94 :                         if (pNetlist->Parse())
     311              :                         {
     312           94 :                             m_netlistsByName[pNetlist->GetName()] = pNetlist;
     313              :                         }
     314              :                         else
     315              :                         {
     316              :                             // pNetList will be freed when exiting the above scope
     317            0 :                             logerror("Failed to parse netlist directory: " + pNetlist->GetName());
     318              :                         }
     319           94 :                     }
     320           94 :                 }
     321              :             }
     322              :         }
     323              :        
     324           94 :         if (netListDirectoriesFound == 0)   // netlist dirs found, but none parsed successfully
     325              :         {      
     326            0 :             logwarn("No netlist directories found");
     327              :         }
     328           94 :         else if (netListDirectoriesFound == m_netlistsByName.size())
     329              :         {
     330          470 :                         loginfo("netlist directories parsed successfully");
     331              :                 }
     332              : 
     333           94 :         return true;
     334              :     }
     335              : 
     336          174 :     const ComponentsFile* StepDirectory::GetTopComponentsFile() const
     337              :     {
     338          348 :         auto findIt = m_layersByName.find(ComponentsFile::TOP_COMPONENTS_LAYER_NAME);
     339          174 :         if (findIt != m_layersByName.end())
     340              :         {
     341          174 :             return &(findIt->second->GetComponentsFile());
     342              :                 }
     343              :         else
     344              :         {
     345            0 :                         return nullptr;
     346              :                 }
     347              :     }
     348              : 
     349          174 :     const ComponentsFile* StepDirectory::GetBottomComponentsFile() const
     350              :     {
     351          348 :         auto findIt = m_layersByName.find(ComponentsFile::BOTTOM_COMPONENTS_LAYER_NAME);
     352          174 :         if (findIt != m_layersByName.end())
     353              :         {
     354          174 :             return &(findIt->second->GetComponentsFile());
     355              :         }
     356              :         else
     357              :         {
     358            0 :                         return nullptr;
     359              :                 }
     360              :     }
     361              : }
        

Generated by: LCOV version 2.0-1