OpenMS  2.6.0
SVOutStream.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2020.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Hendrik Weisser $
32 // $Authors: Hendrik Weisser $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
38 #include <ostream>
39 #include <fstream> // std::ofstream
40 #include <boost/math/special_functions/fpclassify.hpp> // for "isnan"
41 
42 namespace OpenMS
43 {
45  enum Newline {nl};
46 
54  class OPENMS_DLLAPI SVOutStream :
55  public std::ostream
56  {
57 public:
58 
67  SVOutStream(const String& file_out,
68  const String& sep = "\t",
69  const String& replacement = "_",
71 
80  SVOutStream(std::ostream& out,
81  const String& sep = "\t",
82  const String& replacement = "_",
84 
91  ~SVOutStream() override;
92 
98  SVOutStream& operator<<(String str); // use call-by-value here
99 
100 
106  SVOutStream& operator<<(const std::string& str);
107 
108 
114  SVOutStream& operator<<(const char* c_str);
115 
116 
122  SVOutStream& operator<<(const char c);
123 
125  SVOutStream& operator<<(std::ostream& (*fp)(std::ostream&));
126 
133 
136  template<typename T>
137  typename std::enable_if<std::is_arithmetic<typename std::remove_reference<T>::type>::value, SVOutStream&>::type operator<<(const T& value)
138  {
139  if (!newline_) static_cast<std::ostream&>(*this) << sep_;
140  else newline_ = false;
141  static_cast<std::ostream&>(*this) << String(value);
142  return *this;
143  };
144 
146  template<typename T>
147  typename std::enable_if<!std::is_arithmetic<typename std::remove_reference<T>::type>::value, SVOutStream&>::type operator<<(const T& value)
148  {
149  if (!newline_)
150  static_cast<std::ostream &>(*this) << sep_;
151  else
152  newline_ = false;
153  static_cast<std::ostream &>(*this) << value;
154  return *this;
155  };
156 
158  SVOutStream& write(const String& str); // write unmodified string
159 
160 
166  bool modifyStrings(bool modify);
167 
168 
170  template <typename NumericT>
171  SVOutStream& writeValueOrNan(NumericT thing)
172  {
173  if ((boost::math::isfinite)(thing)) return operator<<(thing);
174 
175  bool old = modifyStrings(false);
176  if ((boost::math::isnan)(thing)) operator<<(nan_);
177  else if (thing < 0) operator<<("-" + inf_);
178  else operator<<(inf_);
179  modifyStrings(old);
180  return *this;
181  }
182 
183 protected:
185  std::ofstream* ofs_;
186 
189 
192 
195 
198 
201 
204 
206  bool newline_;
207 
209  std::stringstream ss_;
210  };
211 
212 }
213 
OpenMS::String::QuotingMethod
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:82
OpenMS::SVOutStream::operator<<
std::enable_if<!std::is_arithmetic< typename std::remove_reference< T >::type >::value, SVOutStream & >::type operator<<(const T &value)
Generic stream output operator (for non-character-based types)
Definition: SVOutStream.h:147
OpenMS::SVOutStream::operator<<
std::enable_if< std::is_arithmetic< typename std::remove_reference< T >::type >::value, SVOutStream & >::type operator<<(const T &value)
Definition: SVOutStream.h:137
OpenMS::SVOutStream::modify_strings_
bool modify_strings_
On/off switch for modification of strings.
Definition: SVOutStream.h:203
OpenMS::SVOutStream::sep_
String sep_
Separator string.
Definition: SVOutStream.h:188
OpenMS::String
A more convenient string class.
Definition: String.h:59
OpenMS::Constants::c
const double c
OpenMS::Newline
Newline
custom newline indicator
Definition: SVOutStream.h:45
OpenMS::String::DOUBLE
Definition: String.h:82
OpenMS::SVOutStream::ofs_
std::ofstream * ofs_
internal file stream when C'tor is called with a filename
Definition: SVOutStream.h:185
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::SVOutStream
Stream class for writing to comma/tab/...-separated values files.
Definition: SVOutStream.h:54
OpenMS::SVOutStream::newline_
bool newline_
Are we at the beginning of a line? (Otherwise, insert separator before next item.)
Definition: SVOutStream.h:206
OpenMS::nl
Definition: SVOutStream.h:45
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS::SVOutStream::ss_
std::stringstream ss_
Stream for testing if a manipulator is "std::endl".
Definition: SVOutStream.h:209
OpenMS::SVOutStream::writeValueOrNan
SVOutStream & writeValueOrNan(NumericT thing)
Write a numeric value or "nan"/"inf"/"-inf", if applicable (would not be needed for Linux)
Definition: SVOutStream.h:171
OpenMS::SVOutStream::inf_
String inf_
String to use for Inf values.
Definition: SVOutStream.h:197
String.h
OpenMS::SVOutStream::nan_
String nan_
String to use for NaN values.
Definition: SVOutStream.h:194
OpenMS::SVOutStream::quoting_
String::QuotingMethod quoting_
String quoting method.
Definition: SVOutStream.h:200
OpenMS::SVOutStream::replacement_
String replacement_
Replacement for separator.
Definition: SVOutStream.h:191