LCOV - code coverage report
Current view: top level - elsa/io - IO.cpp (source / functions) Hit Total Coverage
Test: coverage-all.lcov Lines: 32 32 100.0 %
Date: 2024-12-21 07:37:52 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "IO.h"
       2             : 
       3             : #include "EDFHandler.h"
       4             : #include "Error.h"
       5             : #include "MHDHandler.h"
       6             : #include "PGMHandler.h"
       7             : #include <iostream>
       8             : 
       9             : namespace elsa::io
      10             : {
      11             :     [[nodiscard]] std::optional<std::string_view> get_extension(std::string_view str)
      12          16 :     {
      13          16 :         const auto delimiter = '.';
      14          16 :         const auto last_part = str.find_last_of(delimiter);
      15             : 
      16             :         // String doesn't contain delimiter
      17          16 :         if (last_part == std::string_view::npos) {
      18           4 :             return std::nullopt;
      19           4 :         }
      20             : 
      21          12 :         return str.substr(last_part);
      22          12 :     }
      23             : 
      24             :     template <typename data_t>
      25             :     [[nodiscard]] DataContainer<data_t> read(std::string_view filename)
      26           8 :     {
      27           8 :         const auto opt = get_extension(filename);
      28             : 
      29             :         // No dot present in filename, so throw right away
      30           8 :         if (!opt.has_value()) {
      31           2 :             throw Error("No extension found in filename (\"{}\")", filename);
      32           2 :         }
      33             : 
      34           6 :         const auto extension = *opt;
      35             : 
      36           6 :         if (extension == ".edf") {
      37           4 :             return EDF::read<data_t>(std::string{filename});
      38           4 :         }
      39             : 
      40           2 :         throw Error("Can not read with unsupported file extension \"{}\"", extension);
      41           2 :     }
      42             : 
      43             :     template <typename data_t>
      44             :     void write(DataContainer<data_t> x, std::string_view filename)
      45           8 :     {
      46           8 :         const auto opt = get_extension(filename);
      47             : 
      48             :         // No dot present in filename, so throw right away
      49           8 :         if (!opt.has_value()) {
      50           2 :             throw Error("No extension found in filename (\"{}\")", filename);
      51           2 :         }
      52             : 
      53           6 :         const auto extension = *opt;
      54             : 
      55           6 :         if (extension == ".edf") {
      56           2 :             return EDF::write<data_t>(x, std::string{filename});
      57           4 :         } else if (extension == ".pgm") {
      58           2 :             return PGM::write<data_t>(x, std::string{filename});
      59           2 :         }
      60             : 
      61           2 :         throw Error("Can not write with unsupported file extension \"{}\"", extension);
      62           2 :     }
      63             : 
      64             :     template DataContainer<float> read<float>(std::string_view);
      65             :     template DataContainer<double> read<double>(std::string_view);
      66             : 
      67             :     template void write<float>(DataContainer<float>, std::string_view);
      68             :     template void write<double>(DataContainer<double>, std::string_view);
      69             : } // namespace elsa::io

Generated by: LCOV version 1.14