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

functional_write -> stream2file

parent 636bab2c
No related branches found
No related tags found
1 merge request!1629IO further cleanup
...@@ -76,7 +76,7 @@ std::stringstream file2stream(const std::string& file_name) ...@@ -76,7 +76,7 @@ std::stringstream file2stream(const std::string& file_name)
return str; return str;
} }
void functional_write(const std::string& file_name, std::function<void(std::ostream&)> writeData) void stream2file(const std::string& file_name, std::stringstream& s)
{ {
using namespace DataUtil::Format; using namespace DataUtil::Format;
...@@ -95,15 +95,13 @@ void functional_write(const std::string& file_name, std::function<void(std::ostr ...@@ -95,15 +95,13 @@ void functional_write(const std::string& file_name, std::function<void(std::ostr
throw std::runtime_error("Cannot open file for writing: " + file_name); throw std::runtime_error("Cannot open file for writing: " + file_name);
if (!fout.good()) if (!fout.good())
throw std::runtime_error("File is not good, probably it is a directory: " + file_name); throw std::runtime_error("File is not good, probably it is a directory: " + file_name);
std::stringstream ss;
writeData(ss);
boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered; boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered;
if (DataUtil::Format::isGZipped(file_name)) if (DataUtil::Format::isGZipped(file_name))
input_filtered.push(boost::iostreams::gzip_compressor()); input_filtered.push(boost::iostreams::gzip_compressor());
else if (DataUtil::Format::isBZipped(file_name)) else if (DataUtil::Format::isBZipped(file_name))
input_filtered.push(boost::iostreams::bzip2_compressor()); input_filtered.push(boost::iostreams::bzip2_compressor());
input_filtered.push(ss); input_filtered.push(s);
boost::iostreams::copy(input_filtered, fout); boost::iostreams::copy(input_filtered, fout);
...@@ -149,14 +147,19 @@ Datafield* IOFactory::readReflectometryData(const std::string& file_name) ...@@ -149,14 +147,19 @@ Datafield* IOFactory::readReflectometryData(const std::string& file_name)
void IOFactory::writeDatafield(const Datafield& data, const std::string& file_name) void IOFactory::writeDatafield(const Datafield& data, const std::string& file_name)
{ {
try { try {
std::stringstream s;
if (DataUtil::Format::isIntFile(file_name)) if (DataUtil::Format::isIntFile(file_name))
functional_write(file_name, [&](std::ostream& s) { IO::writeBAInt(data, s); }); IO::writeBAInt(data, s);
#ifdef BA_TIFF_SUPPORT #ifdef BA_TIFF_SUPPORT
else if (DataUtil::Format::isTiffFile(file_name)) else if (DataUtil::Format::isTiffFile(file_name))
functional_write(file_name, [&](std::ostream& s) { IO::writeTiff(data, s); }); IO::writeTiff(data, s);
#endif #endif
else else
functional_write(file_name, [&](std::ostream& s) { IO::writeNumpyTxt(data, s); }); IO::writeNumpyTxt(data, s);
stream2file(file_name, s);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
throw std::runtime_error("Failed writing to " + file_name + ": " + ex.what()); throw std::runtime_error("Failed writing to " + file_name + ": " + ex.what());
} }
......
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