Skip to content
Snippets Groups Projects
Commit 053d89a3 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

rm functional_read

parent f01d5d34
No related branches found
No related tags found
1 merge request!1629IO further cleanup
...@@ -39,6 +39,45 @@ ...@@ -39,6 +39,45 @@
#include "Device/IO/boost_streams.h" #include "Device/IO/boost_streams.h"
#endif #endif
namespace {
std::stringstream file2stream(const std::string& file_name)
{
if (!BaseUtil::Filesystem::IsFileExists(file_name))
throw std::runtime_error("File does not exist: " + file_name);
using namespace DataUtil::Format;
std::ifstream input_stream;
std::ios_base::openmode openmode = std::ios::in;
if (isTiffFile(file_name) || isCompressed(file_name))
openmode = std::ios::in | std::ios_base::binary;
#ifdef _WIN32
input_stream.open(BaseUtil::Filesystem::convert_utf8_to_utf16(file_name), openmode);
#else
input_stream.open(file_name, openmode);
#endif
if (!input_stream.is_open())
throw std::runtime_error("Cannot open file for reading: " + file_name);
if (!input_stream.good())
throw std::runtime_error("File is not good, probably it is a directory:" + file_name);
boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered;
if (DataUtil::Format::isGZipped(file_name))
input_filtered.push(boost::iostreams::gzip_decompressor());
else if (DataUtil::Format::isBZipped(file_name))
input_filtered.push(boost::iostreams::bzip2_decompressor());
input_filtered.push(input_stream);
// we use stringstream since it provides random access which is important for tiff files
std::stringstream str;
boost::iostreams::copy(input_filtered, str);
return str;
}
} // namespace
Datafield* IOFactory::readDatafield(const std::string& file_name, LoaderSelector selector) Datafield* IOFactory::readDatafield(const std::string& file_name, LoaderSelector selector)
{ {
const auto readAs = [=](LoaderSelector testForSelector) { const auto readAs = [=](LoaderSelector testForSelector) {
...@@ -47,33 +86,30 @@ Datafield* IOFactory::readDatafield(const std::string& file_name, LoaderSelector ...@@ -47,33 +86,30 @@ Datafield* IOFactory::readDatafield(const std::string& file_name, LoaderSelector
&& fileTypeMatchesLoaderSelector(file_name, testForSelector)); && fileTypeMatchesLoaderSelector(file_name, testForSelector));
}; };
Datafield* result = nullptr; auto s = ::file2stream(file_name);
if (readAs(bornagain)) if (readAs(bornagain))
result = functional_read(file_name, [](std::istream& s) { return IO::readBAInt(s); }); return IO::readBAInt(s);
else if (readAs(nicos)) else if (readAs(nicos))
result = functional_read(file_name, [](std::istream& s) { return IO::readNicos(s); }); return IO::readNicos(s);
#ifdef BA_TIFF_SUPPORT #ifdef BA_TIFF_SUPPORT
else if (readAs(tiff)) else if (readAs(tiff))
result = functional_read(file_name, [](std::istream& s) { return IO::readTiff(s); }); return IO::readTiff(s);
#endif #endif
else else
// Try to read ASCII by default. Binary maps to ASCII. // Try to read ASCII by default. Binary maps to ASCII.
// If the file is not actually a matrix of numbers, // If the file is not actually a matrix of numbers,
// the error will be thrown during the reading. // the error will be thrown during the reading.
result = functional_read(file_name, [](std::istream& s) { return IO::readNumpyTxt(s); }); return IO::readNumpyTxt(s);
ASSERT(result);
return result;
} }
Datafield* IOFactory::readReflectometryData(const std::string& file_name) Datafield* IOFactory::readReflectometryData(const std::string& file_name)
{ {
return functional_read(file_name, auto s = file2stream(file_name);
[](std::istream& s) { return IO::readReflectometryTable(s); }); return IO::readReflectometryTable(s);
} }
void IOFactory::writeDatafield(const Datafield& data, const std::string& file_name) void IOFactory::writeDatafield(const Datafield& data, const std::string& file_name)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment