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

functional_write -> local ns

parent 053d89a3
No related branches found
No related tags found
1 merge request!1629IO further cleanup
...@@ -76,6 +76,40 @@ std::stringstream file2stream(const std::string& file_name) ...@@ -76,6 +76,40 @@ 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)
{
using namespace DataUtil::Format;
std::ofstream fout;
std::ios_base::openmode openmode = std::ios::out;
if (isTiffFile(file_name) || isCompressed(file_name))
openmode = std::ios::out | std::ios_base::binary;
#ifdef _WIN32
fout.open(BaseUtil::Filesystem::convert_utf8_to_utf16(file_name), openmode);
#else
fout.open(file_name, openmode);
#endif
if (!fout.is_open())
throw std::runtime_error("Cannot open file for writing: " + file_name);
if (!fout.good())
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;
if (DataUtil::Format::isGZipped(file_name))
input_filtered.push(boost::iostreams::gzip_compressor());
else if (DataUtil::Format::isBZipped(file_name))
input_filtered.push(boost::iostreams::bzip2_compressor());
input_filtered.push(ss);
boost::iostreams::copy(input_filtered, fout);
fout.close();
}
} // namespace } // namespace
Datafield* IOFactory::readDatafield(const std::string& file_name, LoaderSelector selector) Datafield* IOFactory::readDatafield(const std::string& file_name, LoaderSelector selector)
...@@ -128,41 +162,6 @@ void IOFactory::writeDatafield(const Datafield& data, const std::string& file_na ...@@ -128,41 +162,6 @@ void IOFactory::writeDatafield(const Datafield& data, const std::string& file_na
} }
} }
void IOFactory::functional_write(const std::string& file_name,
std::function<void(std::ostream&)> writeData)
{
using namespace DataUtil::Format;
std::ofstream fout;
std::ios_base::openmode openmode = std::ios::out;
if (isTiffFile(file_name) || isCompressed(file_name))
openmode = std::ios::out | std::ios_base::binary;
#ifdef _WIN32
fout.open(BaseUtil::Filesystem::convert_utf8_to_utf16(file_name), openmode);
#else
fout.open(file_name, openmode);
#endif
if (!fout.is_open())
throw std::runtime_error("Cannot open file for writing: " + file_name);
if (!fout.good())
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;
if (DataUtil::Format::isGZipped(file_name))
input_filtered.push(boost::iostreams::gzip_compressor());
else if (DataUtil::Format::isBZipped(file_name))
input_filtered.push(boost::iostreams::bzip2_compressor());
input_filtered.push(ss);
boost::iostreams::copy(input_filtered, fout);
fout.close();
}
bool IOFactory::fileTypeMatchesLoaderSelector(const std::string& fileName, LoaderSelector selector) bool IOFactory::fileTypeMatchesLoaderSelector(const std::string& fileName, LoaderSelector selector)
{ {
switch (selector) { switch (selector) {
...@@ -179,43 +178,6 @@ bool IOFactory::fileTypeMatchesLoaderSelector(const std::string& fileName, Loade ...@@ -179,43 +178,6 @@ bool IOFactory::fileTypeMatchesLoaderSelector(const std::string& fileName, Loade
return false; return false;
} }
Datafield* IOFactory::functional_read(const std::string& file_name,
std::function<Datafield*(std::istream&)> readData)
{
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 readData(str);
}
bool IOUtil::filesAgree(const std::string& datFileName, const std::string& refFileName, double tol) bool IOUtil::filesAgree(const std::string& datFileName, const std::string& refFileName, double tol)
{ {
std::unique_ptr<Datafield> datDat; std::unique_ptr<Datafield> datDat;
......
...@@ -60,12 +60,6 @@ public: ...@@ -60,12 +60,6 @@ public:
static void writeDatafield(const Datafield& data, const std::string& file_name); static void writeDatafield(const Datafield& data, const std::string& file_name);
private: private:
static Datafield* functional_read(const std::string& file_name,
std::function<Datafield*(std::istream&)> readData);
static void functional_write(const std::string& file_name,
std::function<void(std::ostream&)> writeData);
static bool fileTypeMatchesLoaderSelector(const std::string& fileName, LoaderSelector selector); static bool fileTypeMatchesLoaderSelector(const std::string& fileName, LoaderSelector selector);
}; };
......
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