OpenMS
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-2023.
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 
39 #include <ostream>
40 #include <fstream> // std::ofstream
41 #include <sstream>
42 #include <boost/math/special_functions/fpclassify.hpp> // because isfinite not supported on Mac
43 
44 namespace OpenMS
45 {
47  enum Newline {nl};
48 
56  class OPENMS_DLLAPI SVOutStream :
57  public std::ostream
58  {
59 public:
60 
69  SVOutStream(const String& file_out,
70  const String& sep = "\t",
71  const String& replacement = "_",
73 
82  SVOutStream(std::ostream& out,
83  const String& sep = "\t",
84  const String& replacement = "_",
86 
93  ~SVOutStream() override;
94 
100  SVOutStream& operator<<(String str); // use call-by-value here
101 
102 
108  SVOutStream& operator<<(const std::string& str);
109 
110 
116  SVOutStream& operator<<(const char* c_str);
117 
118 
124  SVOutStream& operator<<(const char c);
125 
127  SVOutStream& operator<<(std::ostream& (*fp)(std::ostream&));
128 
135 
138  template<typename T>
139  typename std::enable_if<std::is_arithmetic<typename std::remove_reference<T>::type>::value, SVOutStream&>::type operator<<(const T& value)
140  {
141  if (!newline_) static_cast<std::ostream&>(*this) << sep_;
142  else newline_ = false;
143  static_cast<std::ostream&>(*this) << String(value);
144  return *this;
145  };
146 
148  template<typename T>
149  typename std::enable_if<!std::is_arithmetic<typename std::remove_reference<T>::type>::value, SVOutStream&>::type operator<<(const T& value)
150  {
151  if (!newline_)
152  static_cast<std::ostream &>(*this) << sep_;
153  else
154  newline_ = false;
155  static_cast<std::ostream &>(*this) << value;
156  return *this;
157  };
158 
160  SVOutStream& write(const String& str); // write unmodified string
161 
162 
168  bool modifyStrings(bool modify);
169 
170 
172  template <typename NumericT>
173  SVOutStream& writeValueOrNan(NumericT thing)
174  {
175  if ((boost::math::isfinite)(thing)) return operator<<(thing);
176 
177  bool old = modifyStrings(false);
178  if ((boost::math::isnan)(thing))
179  {
180  operator<<(nan_);
181  }
182  else if (thing < 0)
183  {
184  operator<<("-" + inf_);
185  }
186  else
187  {
188  operator<<(inf_);
189  }
190  modifyStrings(old);
191  return *this;
192  }
193 
194 protected:
196  std::ofstream* ofs_;
197 
200 
203 
206 
209 
212 
215 
217  bool newline_;
218 
220  std::stringstream ss_;
221  };
222 
223 }
224 
Stream class for writing to comma/tab/...-separated values files.
Definition: SVOutStream.h:58
String::QuotingMethod quoting_
String quoting method.
Definition: SVOutStream.h:211
bool newline_
Are we at the beginning of a line? (Otherwise, insert separator before next item.)
Definition: SVOutStream.h:217
SVOutStream & operator<<(const char *c_str)
Stream output operator for char*.
SVOutStream & operator<<(std::ostream &(*fp)(std::ostream &))
Stream output operator for manipulators (used to catch std::endl)
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:149
std::stringstream ss_
Stream for testing if a manipulator is "std::endl".
Definition: SVOutStream.h:220
SVOutStream & operator<<(enum Newline)
Stream output operator for custom newline (nl) without flushing.
String sep_
Separator string.
Definition: SVOutStream.h:199
SVOutStream & operator<<(String str)
Stream output operator for String.
String inf_
String to use for Inf values.
Definition: SVOutStream.h:208
bool modifyStrings(bool modify)
Switch modification of strings (quoting/replacing of separators) on/off.
SVOutStream & operator<<(const char c)
Stream output operator for char.
SVOutStream(const String &file_out, const String &sep="\t", const String &replacement="_", String::QuotingMethod quoting=String::DOUBLE)
Constructor.
String replacement_
Replacement for separator.
Definition: SVOutStream.h:202
std::enable_if< std::is_arithmetic< typename std::remove_reference< T >::type >::value, SVOutStream & >::type operator<<(const T &value)
Definition: SVOutStream.h:139
SVOutStream & writeValueOrNan(NumericT thing)
Write a numeric value or "nan"/"inf"/"-inf", if applicable (would not be needed for Linux)
Definition: SVOutStream.h:173
SVOutStream & operator<<(const std::string &str)
Stream output operator for std::string.
~SVOutStream() override
Destructor.
String nan_
String to use for NaN values.
Definition: SVOutStream.h:205
SVOutStream(std::ostream &out, const String &sep="\t", const String &replacement="_", String::QuotingMethod quoting=String::DOUBLE)
Constructor.
std::ofstream * ofs_
internal file stream when C'tor is called with a filename
Definition: SVOutStream.h:196
bool modify_strings_
On/off switch for modification of strings.
Definition: SVOutStream.h:214
SVOutStream & write(const String &str)
Unformatted output (no quoting: useful for comments, but use only on a line of its own!...
A more convenient string class.
Definition: String.h:60
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:81
@ DOUBLE
Definition: String.h:81
const double c
Definition: Constants.h:214
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
Newline
custom newline indicator
Definition: SVOutStream.h:47
@ nl
Definition: SVOutStream.h:47