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 0 : [[nodiscard]] std::optional<std::string_view> get_extension(std::string_view str) 12 : { 13 0 : const auto delimiter = '.'; 14 0 : const auto last_part = str.find_last_of(delimiter); 15 : 16 : // String doesn't contain delimiter 17 0 : if (last_part == std::string_view::npos) { 18 0 : return std::nullopt; 19 : } 20 : 21 0 : return str.substr(last_part); 22 : } 23 : 24 : template <typename data_t> 25 0 : [[nodiscard]] DataContainer<data_t> read(std::string_view filename) 26 : { 27 0 : const auto opt = get_extension(filename); 28 : 29 : // No dot present in filename, so throw right away 30 0 : if (!opt.has_value()) { 31 0 : throw Error("No extension found in filename (\"{}\")", filename); 32 : } 33 : 34 0 : const auto extension = *opt; 35 : 36 0 : if (extension == ".edf") { 37 0 : return EDF::read<data_t>(std::string{filename}); 38 : } 39 : 40 0 : throw Error("Can not read with unsupported file extension \"{}\"", extension); 41 : } 42 : 43 : template <typename data_t> 44 0 : void write(DataContainer<data_t> x, std::string_view filename) 45 : { 46 0 : const auto opt = get_extension(filename); 47 : 48 : // No dot present in filename, so throw right away 49 0 : if (!opt.has_value()) { 50 0 : throw Error("No extension found in filename (\"{}\")", filename); 51 : } 52 : 53 0 : const auto extension = *opt; 54 : 55 0 : if (extension == ".edf") { 56 0 : return EDF::write<data_t>(x, std::string{filename}); 57 0 : } else if (extension == ".pgm") { 58 0 : return PGM::write<data_t>(x, std::string{filename}); 59 : } 60 : 61 0 : throw Error("Can not write with unsupported file extension \"{}\"", extension); 62 : } 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