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

simplify determination of file type

parent ad538ed2
No related branches found
No related tags found
1 merge request!1629IO further cleanup
...@@ -40,15 +40,6 @@ std::istringstream getAxisStringRepresentation(std::istream& input_stream) ...@@ -40,15 +40,6 @@ std::istringstream getAxisStringRepresentation(std::istream& input_stream)
return std::istringstream(line); return std::istringstream(line);
} }
std::string uncompressedFilename(const std::string& name)
{
if (DataUtil::Format::isGZipped(name))
return name.substr(0, name.size() - GzipExtension.size());
if (DataUtil::Format::isBZipped(name))
return name.substr(0, name.size() - BzipExtension.size());
return name;
}
void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss) void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss)
{ {
iss.imbue(std::locale::classic()); iss.imbue(std::locale::classic());
...@@ -58,6 +49,14 @@ void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss) ...@@ -58,6 +49,14 @@ void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss)
} // namespace } // namespace
std::string DataUtil::Format::uncompressedFilename(const std::string& name)
{
if (DataUtil::Format::isGZipped(name))
return name.substr(0, name.size() - GzipExtension.size());
if (DataUtil::Format::isBZipped(name))
return name.substr(0, name.size() - BzipExtension.size());
return name;
}
bool DataUtil::Format::isCompressed(const std::string& name) bool DataUtil::Format::isCompressed(const std::string& name)
{ {
...@@ -78,18 +77,18 @@ bool DataUtil::Format::isBZipped(const std::string& name) ...@@ -78,18 +77,18 @@ bool DataUtil::Format::isBZipped(const std::string& name)
bool DataUtil::Format::isIntFile(const std::string& file_name) bool DataUtil::Format::isIntFile(const std::string& file_name)
{ {
return BaseUtil::Filesystem::hasExtension(::uncompressedFilename(file_name), IntExtension); return BaseUtil::Filesystem::hasExtension(uncompressedFilename(file_name), IntExtension);
} }
bool DataUtil::Format::isNicosFile(const std::string& file_name) bool DataUtil::Format::isNicosFile(const std::string& file_name)
{ {
return BaseUtil::Filesystem::hasExtension(::uncompressedFilename(file_name), NicosExtension); return BaseUtil::Filesystem::hasExtension(uncompressedFilename(file_name), NicosExtension);
} }
bool DataUtil::Format::isTiffFile(const std::string& file_name) bool DataUtil::Format::isTiffFile(const std::string& file_name)
{ {
return BaseUtil::Filesystem::hasExtension(::uncompressedFilename(file_name), TiffExtension) return BaseUtil::Filesystem::hasExtension(uncompressedFilename(file_name), TiffExtension)
|| BaseUtil::Filesystem::hasExtension(::uncompressedFilename(file_name), TiffExtension2); || BaseUtil::Filesystem::hasExtension(uncompressedFilename(file_name), TiffExtension2);
} }
//! Creates axis of certain type from input stream //! Creates axis of certain type from input stream
......
...@@ -28,6 +28,8 @@ class Datafield; ...@@ -28,6 +28,8 @@ class Datafield;
namespace DataUtil::Format { namespace DataUtil::Format {
std::string uncompressedFilename(const std::string& name);
//! Returns true if name contains *.gz extension //! Returns true if name contains *.gz extension
bool isCompressed(const std::string& name); bool isCompressed(const std::string& name);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "Device/IO/IOFactory.h" #include "Device/IO/IOFactory.h"
#include "Base/Util/Assert.h" #include "Base/Util/Assert.h"
#include "Base/Util/FileSystemUtil.h" #include "Base/Util/FileSystemUtil.h"
#include "Base/Util/StringUtil.h"
#include "Device/Data/Datafield.h" #include "Device/Data/Datafield.h"
#include "Device/Histo/DiffUtil.h" #include "Device/Histo/DiffUtil.h"
#include "Device/Histo/SimulationResult.h" #include "Device/Histo/SimulationResult.h"
...@@ -24,6 +25,8 @@ ...@@ -24,6 +25,8 @@
#include "Device/IO/ReadWriteNicos.h" #include "Device/IO/ReadWriteNicos.h"
#include "Device/IO/ReadWriteNumpyTXT.h" #include "Device/IO/ReadWriteNumpyTXT.h"
#include "Device/IO/ReadWriteTiff.h" #include "Device/IO/ReadWriteTiff.h"
#include <algorithm>
#include <cctype>
#include <exception> #include <exception>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
...@@ -108,50 +111,48 @@ void stream2file(const std::string& file_name, std::stringstream& s) ...@@ -108,50 +111,48 @@ void stream2file(const std::string& file_name, std::stringstream& s)
fout.close(); fout.close();
} }
bool fileTypeMatchesFiletype(const std::string& fileName, IO::Filetype selector) IO::Filetype filename2type(const std::string& filename)
{ {
switch (selector) { std::string s = DataUtil::Format::uncompressedFilename(filename);
case IO::bornagain: s = BaseUtil::Filesystem::extension(s);
return DataUtil::Format::isIntFile(fileName); s = BaseUtil::String::to_lower(s);
case IO::nicos:
return DataUtil::Format::isNicosFile(fileName); if (s == ".int")
case IO::tiff: return IO::bornagain;
return DataUtil::Format::isTiffFile(fileName); if (s == ".001")
default: return IO::nicos;
return false; if (s == ".tif" || s == ".tiff")
} return IO::tiff;
if (s == ".mft")
return false; return IO::mft;
return IO::nil;
} }
} // namespace } // namespace
Datafield* IO::readData2D(const std::string& file_name, Filetype selector) Datafield* IO::readData2D(const std::string& file_name, Filetype ftype)
{ {
const auto readAs = [=](Filetype testForSelector) { if (ftype == nil)
return (selector == testForSelector) ftype = ::filename2type(file_name);
|| (selector == nil && ::fileTypeMatchesFiletype(file_name, testForSelector));
};
auto s = ::file2stream(file_name); auto s = ::file2stream(file_name);
if (readAs(bornagain)) if (ftype == bornagain)
return Util::RW::readBAInt(s); return Util::RW::readBAInt(s);
else if (readAs(nicos)) if (ftype == nicos)
return Util::RW::readNicos(s); return Util::RW::readNicos(s);
#ifdef BA_TIFF_SUPPORT #ifdef BA_TIFF_SUPPORT
else if (readAs(tiff)) if (ftype == tiff)
return Util::RW::readTiff(s); return Util::RW::readTiff(s);
#endif #endif
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. return Util::RW::readNumpyTxt(s);
return Util::RW::readNumpyTxt(s);
} }
Datafield* IO::readData1D(const std::string& file_name, Filetype /*selector*/) Datafield* IO::readData1D(const std::string& file_name, Filetype /*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