BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataReader.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/InputOutput/OutputDataReader.cpp
6 //! @brief Implements class OutputDataReader.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
17 #include "Device/Data/OutputData.h"
19 
20 #ifdef _WIN32
21 #pragma warning(push)
22 #pragma warning(disable : 4244 4275)
24 #pragma warning(pop)
25 #else
27 #endif
28 
29 #include <fstream>
30 
31 namespace
32 {
33 
34 std::stringstream getFromFilteredStream(std::istream& input_stream, const std::string& fname)
35 {
36  boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered;
37  if (DataFormatUtils::isGZipped(fname))
38  input_filtered.push(boost::iostreams::gzip_decompressor());
39  else if (DataFormatUtils::isBZipped(fname))
40  input_filtered.push(boost::iostreams::bzip2_decompressor());
41  input_filtered.push(input_stream);
42  // we use stringstream since it provides random access which is important for tiff files
43  std::stringstream ret;
44  boost::iostreams::copy(input_filtered, ret);
45  return ret;
46 }
47 
48 } // namespace
49 
50 OutputDataReader::OutputDataReader(const std::string& file_name) : m_file_name(file_name) {}
51 
53 {
54  using namespace DataFormatUtils;
55  if (!m_read_strategy)
57  "OutputDataReader::getOutputData() -> Error! No read strategy defined");
58 
59  std::ifstream fin;
60  std::ios_base::openmode openmode = std::ios::in;
62  openmode = std::ios::in | std::ios_base::binary;
63 
64 #ifdef _WIN32
66 #else
67  fin.open(m_file_name, openmode);
68 #endif
69 
70  if (!fin.is_open())
72  "OutputDataReader::getOutputData() -> Error. Can't open file '" + m_file_name
73  + "' for reading.");
74  if (!fin.good())
75  throw Exceptions::FileIsBadException("OutputDataReader::getOutputData() -> Error! "
76  "File is not good, probably it is a directory.");
77 
78  std::stringstream strstream = getFromFilteredStream(fin, m_file_name);
79 
80  OutputData<double>* result = m_read_strategy->readOutputData(strstream);
81 
82  return result;
83 }
84 
86 {
87  m_read_strategy.reset(read_strategy);
88 }
Defines class OutputDataIOFactory.
Defines namespace FileSystemUtils.
Defines class OutputDataReader.
Defines and implements template class OutputData.
Contains boost streams related headers.
Interface for reading strategy of OutputData from file.
OutputData< double > * getOutputData()
read output data from file (file name was set already from OutputDataIOFactory)
void setStrategy(IOutputDataReadStrategy *read_strategy)
Sets concrete reading strategy.
std::unique_ptr< IOutputDataReadStrategy > m_read_strategy
OutputDataReader(const std::string &file_name)
std::string m_file_name
Utility functions for data input and output.
bool isBZipped(const std::string &name)
Returns true if name contains *.bz2 extension.
bool isCompressed(const std::string &name)
Returns true if name contains *.gz extension.
bool isTiffFile(const std::string &file_name)
returns true if file name corresponds to tiff file (can be also compressed)
bool isGZipped(const std::string &name)
Returns true if name contains *.gz extension.
std::wstring convert_utf8_to_utf16(const std::string &str)
Converts utf8 string represented by std::string to utf16 string represented by std::wstring.
std::stringstream getFromFilteredStream(std::istream &input_stream, const std::string &fname)