LCOV - code coverage report
Current view: top level - src/esys/repo - pinsfile.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 66 71 93.0 %
Date: 2026-09-26 15:38:18 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /*!
       2             :  * \file esys/repo/pinsfile.cpp
       3             :  * \brief
       4             :  *
       5             :  * \cond
       6             :  * __legal_b__
       7             :  *
       8             :  * Copyright (c) 2026 Michel Gillet
       9             :  * Distributed under the MIT License.
      10             :  * (See accompanying file LICENSE.txt or
      11             :  * copy at https://opensource.org/licenses/MIT)
      12             :  *
      13             :  * __legal_e__
      14             :  * \endcond
      15             :  *
      16             :  */
      17             : 
      18             : #include "esys/repo/esysrepo_prec.h"
      19             : #include "esys/repo/pinsfile.h"
      20             : 
      21             : #include <nlohmann/json.hpp>
      22             : 
      23             : #include <boost/filesystem.hpp>
      24             : 
      25             : #include <fstream>
      26             : #include <iomanip>
      27             : 
      28             : using json = nlohmann::json;
      29             : 
      30             : namespace esys::repo
      31             : {
      32             : 
      33          86 : PinsFile::PinsFile() = default;
      34             : 
      35         172 : PinsFile::~PinsFile() = default;
      36             : 
      37          86 : void PinsFile::set_path(const std::string &path)
      38             : {
      39          86 :     m_path = path;
      40          86 : }
      41             : 
      42         177 : const std::string &PinsFile::get_path() const
      43             : {
      44         177 :     return m_path;
      45             : }
      46             : 
      47           2 : void PinsFile::set_pins(std::shared_ptr<RevisionPins> pins)
      48             : {
      49           2 :     m_pins = pins;
      50           2 : }
      51             : 
      52           7 : std::shared_ptr<RevisionPins> PinsFile::get_pins() const
      53             : {
      54           7 :     return m_pins;
      55             : }
      56             : 
      57         167 : std::shared_ptr<RevisionPins> PinsFile::get_or_new_pins()
      58             : {
      59         167 :     if (m_pins == nullptr) m_pins = std::make_shared<RevisionPins>();
      60         167 :     return m_pins;
      61             : }
      62             : 
      63          84 : Result PinsFile::open(const std::string &path)
      64             : {
      65          84 :     if (!path.empty()) set_path(path);
      66             : 
      67          84 :     auto pins = get_or_new_pins();
      68          84 :     pins->clear();
      69             : 
      70          84 :     if (get_path().empty()) return ESYSREPO_RESULT(ResultCode::EMPTY_PATH);
      71          84 :     if (!boost::filesystem::exists(get_path())) return ESYSREPO_RESULT(ResultCode::OK);
      72             : 
      73           3 :     std::ifstream fin(get_path());
      74           3 :     if (!fin.is_open()) return ESYSREPO_RESULT(ResultCode::ERROR_OPENING_FILE, get_path());
      75             : 
      76           3 :     json obj;
      77           3 :     try
      78             :     {
      79           3 :         fin >> obj;
      80             :     }
      81           0 :     catch (json::parse_error &)
      82             :     {
      83           0 :         return ESYSREPO_RESULT(ResultCode::CMDPIN_PINS_FILE_PARSE_ERROR, get_path());
      84           0 :     }
      85             : 
      86           3 :     if (!obj.contains("pins") || !obj["pins"].is_array()) return ESYSREPO_RESULT(ResultCode::OK);
      87             : 
      88          12 :     for (auto &item : obj["pins"])
      89             :     {
      90           3 :         if (!item.is_object()) continue;
      91           3 :         auto pin = std::make_shared<RevisionPin>();
      92           3 :         if (item.contains("selector"))
      93             :         {
      94           3 :             PinSelector selector = PinSelector::NOT_SET;
      95           3 :             if (!pin_selector_from_string(item["selector"].get<std::string>(), selector))
      96           0 :                 return ESYSREPO_RESULT(ResultCode::CMDPIN_PINS_FILE_PARSE_ERROR, get_path());
      97           3 :             pin->set_selector(selector);
      98             :         }
      99           6 :         if (item.contains("value")) pin->set_value(item["value"].get<std::string>());
     100           6 :         if (item.contains("rev")) pin->set_rev(item["rev"].get<std::string>());
     101           6 :         if (item.contains("path")) pin->set_path(item["path"].get<std::string>());
     102           9 :         pins->add(pin);
     103           3 :     }
     104             : 
     105           3 :     return ESYSREPO_RESULT(ResultCode::OK);
     106          87 : }
     107             : 
     108           2 : Result PinsFile::write(const std::string &path)
     109             : {
     110           2 :     if (!path.empty()) set_path(path);
     111           2 :     if (get_path().empty()) return ESYSREPO_RESULT(ResultCode::EMPTY_PATH);
     112           4 :     if (get_pins() == nullptr) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR);
     113             : 
     114           2 :     json root;
     115           2 :     json arr = json::array();
     116           6 :     for (auto &pin : get_pins()->get_pins())
     117             :     {
     118           2 :         if (pin == nullptr) continue;
     119           2 :         json item;
     120           4 :         item["selector"] = to_string(pin->get_selector());
     121           4 :         item["value"] = pin->get_value();
     122           4 :         item["rev"] = pin->get_rev();
     123           4 :         item["path"] = pin->get_path();
     124           2 :         arr.push_back(item);
     125           2 :     }
     126           2 :     root["pins"] = arr;
     127             : 
     128           2 :     std::ofstream ofs;
     129           2 :     if (boost::filesystem::exists(get_path()))
     130           0 :         ofs.open(get_path().c_str(), std::ios_base::trunc);
     131             :     else
     132           2 :         ofs.open(get_path().c_str(), std::ios_base::out);
     133             : 
     134           2 :     if (!ofs.is_open()) return ESYSREPO_RESULT(ResultCode::ERROR_OPENING_FILE, get_path());
     135             : 
     136           2 :     ofs << std::setw(4);
     137           2 :     ofs << root;
     138           2 :     ofs.close();
     139           2 :     return ESYSREPO_RESULT(ResultCode::OK);
     140           4 : }
     141             : 
     142             : } // namespace esys::repo

Generated by: LCOV version 1.14