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

more uses of filename2type; extract common fct for binary openmode

parent 0b01d06e
No related branches found
No related tags found
1 merge request!1629IO further cleanup
...@@ -44,6 +44,12 @@ ...@@ -44,6 +44,12 @@
namespace { namespace {
bool is_binary(const std::string& file_name)
{
const IO::Filetype ftype = IO::filename2type(file_name);
return ftype == IO::tiff || DataUtil::Format::isCompressed(file_name);
}
std::stringstream file2stream(const std::string& file_name) std::stringstream file2stream(const std::string& file_name)
{ {
if (!BaseUtil::Filesystem::IsFileExists(file_name)) if (!BaseUtil::Filesystem::IsFileExists(file_name))
...@@ -52,8 +58,8 @@ std::stringstream file2stream(const std::string& file_name) ...@@ -52,8 +58,8 @@ std::stringstream file2stream(const std::string& file_name)
using namespace DataUtil::Format; using namespace DataUtil::Format;
std::ifstream input_stream; std::ifstream input_stream;
std::ios_base::openmode openmode = std::ios::in; std::ios_base::openmode openmode = std::ios::in;
if (isTiffFile(file_name) || isCompressed(file_name)) if (is_binary(file_name))
openmode = std::ios::in | std::ios_base::binary; openmode |= std::ios_base::binary;
#ifdef _WIN32 #ifdef _WIN32
input_stream.open(BaseUtil::Filesystem::convert_utf8_to_utf16(file_name), openmode); input_stream.open(BaseUtil::Filesystem::convert_utf8_to_utf16(file_name), openmode);
...@@ -85,8 +91,8 @@ void stream2file(const std::string& file_name, std::stringstream& s) ...@@ -85,8 +91,8 @@ void stream2file(const std::string& file_name, std::stringstream& s)
std::ofstream fout; std::ofstream fout;
std::ios_base::openmode openmode = std::ios::out; std::ios_base::openmode openmode = std::ios::out;
if (isTiffFile(file_name) || isCompressed(file_name)) if (is_binary(file_name))
openmode = std::ios::out | std::ios_base::binary; openmode |= std::ios_base::binary;
#ifdef _WIN32 #ifdef _WIN32
fout.open(BaseUtil::Filesystem::convert_utf8_to_utf16(file_name), openmode); fout.open(BaseUtil::Filesystem::convert_utf8_to_utf16(file_name), openmode);
...@@ -162,15 +168,18 @@ Datafield* IO::readData1D(const std::string& file_name, Filetype /*selector*/) ...@@ -162,15 +168,18 @@ Datafield* IO::readData1D(const std::string& file_name, Filetype /*selector*/)
void IO::writeDatafield(const Datafield& data, const std::string& file_name) void IO::writeDatafield(const Datafield& data, const std::string& file_name)
{ {
const Filetype ftype = filename2type(file_name);
try { try {
std::stringstream s; std::stringstream s;
if (DataUtil::Format::isIntFile(file_name)) if (ftype == bornagain)
Util::RW::writeBAInt(data, s); Util::RW::writeBAInt(data, s);
#ifdef BA_TIFF_SUPPORT #ifdef BA_TIFF_SUPPORT
else if (DataUtil::Format::isTiffFile(file_name)) else if (ftype == tiff)
Util::RW::writeTiff(data, s); Util::RW::writeTiff(data, s);
#endif #endif
else else
Util::RW::writeNumpyTxt(data, s); Util::RW::writeNumpyTxt(data, s);
......
...@@ -73,13 +73,8 @@ QString GUI::View::ImportDataUtil::Import1dData(RealItem* realItem, ...@@ -73,13 +73,8 @@ QString GUI::View::ImportDataUtil::Import1dData(RealItem* realItem,
return QString(); return QString();
} }
} catch (std::exception& ex) { } catch (std::exception& ex) {
// If it is not tiff but e.g. dat.gz, it could be tried with CSV import // Try with CSV import ? TODO check the logic, simplify
const bool tryWithLoaders = if (filetype != IO::bornagain || DataUtil::Format::isCompressed(fileNameStdString))
(DataUtil::Format::isIntFile(fileNameStdString)
&& !DataUtil::Format::isCompressed(
fileNameStdString)); // #baimport support compressed
if (!tryWithLoaders)
// import is not possible // import is not possible
return QString::fromLatin1(ex.what()); return QString::fromLatin1(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