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)
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)
{
iss.imbue(std::locale::classic());
......@@ -58,6 +49,14 @@ void readLineOfDoubles(std::vector<double>& buffer, std::istringstream& iss)
} // 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)
{
......@@ -78,18 +77,18 @@ bool DataUtil::Format::isBZipped(const std::string& 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)
{
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)
{
return BaseUtil::Filesystem::hasExtension(::uncompressedFilename(file_name), TiffExtension)
|| BaseUtil::Filesystem::hasExtension(::uncompressedFilename(file_name), TiffExtension2);
return BaseUtil::Filesystem::hasExtension(uncompressedFilename(file_name), TiffExtension)
|| BaseUtil::Filesystem::hasExtension(uncompressedFilename(file_name), TiffExtension2);
}
//! Creates axis of certain type from input stream
......
......@@ -28,6 +28,8 @@ class Datafield;
namespace DataUtil::Format {
std::string uncompressedFilename(const std::string& name);
//! Returns true if name contains *.gz extension
bool isCompressed(const std::string& name);
......
......@@ -15,6 +15,7 @@
#include "Device/IO/IOFactory.h"
#include "Base/Util/Assert.h"
#include "Base/Util/FileSystemUtil.h"
#include "Base/Util/StringUtil.h"
#include "Device/Data/Datafield.h"
#include "Device/Histo/DiffUtil.h"
#include "Device/Histo/SimulationResult.h"
......@@ -24,6 +25,8 @@
#include "Device/IO/ReadWriteNicos.h"
#include "Device/IO/ReadWriteNumpyTXT.h"
#include "Device/IO/ReadWriteTiff.h"
#include <algorithm>
#include <cctype>
#include <exception>
#include <fstream>
#include <iostream>
......@@ -108,50 +111,48 @@ void stream2file(const std::string& file_name, std::stringstream& s)
fout.close();
}
bool fileTypeMatchesFiletype(const std::string& fileName, IO::Filetype selector)
IO::Filetype filename2type(const std::string& filename)
{
switch (selector) {
case IO::bornagain:
return DataUtil::Format::isIntFile(fileName);
case IO::nicos:
return DataUtil::Format::isNicosFile(fileName);
case IO::tiff:
return DataUtil::Format::isTiffFile(fileName);
default:
return false;
}
return false;
std::string s = DataUtil::Format::uncompressedFilename(filename);
s = BaseUtil::Filesystem::extension(s);
s = BaseUtil::String::to_lower(s);
if (s == ".int")
return IO::bornagain;
if (s == ".001")
return IO::nicos;
if (s == ".tif" || s == ".tiff")
return IO::tiff;
if (s == ".mft")
return IO::mft;
return IO::nil;
}
} // 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) {
return (selector == testForSelector)
|| (selector == nil && ::fileTypeMatchesFiletype(file_name, testForSelector));
};
if (ftype == nil)
ftype = ::filename2type(file_name);
auto s = ::file2stream(file_name);
if (readAs(bornagain))
if (ftype == bornagain)
return Util::RW::readBAInt(s);
else if (readAs(nicos))
if (ftype == nicos)
return Util::RW::readNicos(s);
#ifdef BA_TIFF_SUPPORT
else if (readAs(tiff))
if (ftype == tiff)
return Util::RW::readTiff(s);
#endif
else
// Try to read ASCII by default. Binary maps to ASCII.
// If the file is not actually a matrix of numbers,
// the error will be thrown during the reading.
return Util::RW::readNumpyTxt(s);
// Try to read ASCII by default. Binary maps to ASCII.
// If the file is not actually a matrix of numbers,
// the error will be thrown during the reading.
return Util::RW::readNumpyTxt(s);
}
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