BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataWriter.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/InputOutput/OutputDataWriter.cpp
6 //! @brief Implements class OutputDataWriter.
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 
16 #include "Device/Data/OutputData.h"
18 #ifdef _WIN32
19 #pragma warning(push)
20 #pragma warning(disable : 4244 4275)
22 #pragma warning(pop)
23 #else
25 #endif
27 #include <fstream>
28 
29 OutputDataWriter::OutputDataWriter(const std::string& file_name) : m_file_name(file_name) {}
30 
32 {
33  using namespace DataFormatUtils;
34  if (!m_write_strategy)
35  throw Exceptions::NullPointerException("OutputDataWriter::getOutputData() ->"
36  " Error! No read strategy defined");
37 
38  std::ofstream fout;
39  std::ios_base::openmode openmode = std::ios::out;
41  openmode = std::ios::out | std::ios_base::binary;
42 
43 #ifdef _WIN32
45 #else
46  fout.open(m_file_name, openmode);
47 #endif
48 
49  if (!fout.is_open())
50  throw Exceptions::FileNotIsOpenException("OutputDataWriter::writeOutputData() -> Error. "
51  "Can't open file '"
52  + m_file_name + "' for writing.");
53  if (!fout.good())
54  throw Exceptions::FileIsBadException("OutputDataReader::writeOutputData() -> Error! "
55  "File is not good, probably it is a directory.");
56  std::stringstream ss;
57  m_write_strategy->writeOutputData(data, ss);
58 
59  boost::iostreams::filtering_streambuf<boost::iostreams::input> input_filtered;
61  input_filtered.push(boost::iostreams::gzip_compressor());
63  input_filtered.push(boost::iostreams::bzip2_compressor());
64  input_filtered.push(ss);
65 
66  boost::iostreams::copy(input_filtered, fout);
67 
68  fout.close();
69 }
70 
72 {
73  m_write_strategy.reset(write_strategy);
74 }
Defines class OutputDataIOFactory.
Defines namespace FileSystemUtils.
Defines class OutputDataWriter.
Defines and implements template class OutputData.
Contains boost streams related headers.
Strategy interface to write OututData in file.
OutputDataWriter(const std::string &file_name)
void setStrategy(IOutputDataWriteStrategy *write_strategy)
Sets concrete writing strategy.
void writeOutputData(const OutputData< double > &data)
Writes output data to file.
std::string m_file_name
std::unique_ptr< IOutputDataWriteStrategy > m_write_strategy
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.