OpenMS
Loading...
Searching...
No Matches
SVOutStream.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Hendrik Weisser $
6// $Authors: Hendrik Weisser $
7// --------------------------------------------------------------------------
8
9#pragma once
10
12
13#include <ostream>
14#include <fstream> // std::ofstream
15#include <sstream>
16#include <boost/math/special_functions/fpclassify.hpp> // because isfinite not supported on Mac
17
18namespace OpenMS
19{
21 enum Newline {nl};
22
30 class OPENMS_DLLAPI SVOutStream :
31 public std::ostream
32 {
33public:
34
43 SVOutStream(const std::string& file_out,
44 const std::string& sep = "\t",
45 const std::string& replacement = "_",
47
56 SVOutStream(std::ostream& out,
57 const std::string& sep = "\t",
58 const std::string& replacement = "_",
60
67 ~SVOutStream() override;
68
74 SVOutStream& operator<<(std::string str); // use call-by-value here
75
76
77
78
84 SVOutStream& operator<<(const char* c_str);
85
86
92 SVOutStream& operator<<(const char c);
93
95 SVOutStream& operator<<(std::ostream& (*fp)(std::ostream&));
96
103
106 template<typename T>
107 typename std::enable_if<std::is_arithmetic<typename std::remove_reference<T>::type>::value, SVOutStream&>::type operator<<(const T& value)
108 {
109 if (!newline_) static_cast<std::ostream&>(*this) << sep_;
110 else newline_ = false;
111 static_cast<std::ostream&>(*this) << StringUtils::toStr(value);
112 return *this;
113 };
114
116 template<typename T>
117 typename std::enable_if<!std::is_arithmetic<typename std::remove_reference<T>::type>::value, SVOutStream&>::type operator<<(const T& value)
118 {
119 if (!newline_)
120 static_cast<std::ostream &>(*this) << sep_;
121 else
122 newline_ = false;
123 static_cast<std::ostream &>(*this) << value;
124 return *this;
125 };
126
128 SVOutStream& write(const std::string& str); // write unmodified string
129
130
136 bool modifyStrings(bool modify);
137
138
140 template <typename NumericT>
142 {
143 if ((boost::math::isfinite)(thing)) return operator<<(thing);
144
145 bool old = modifyStrings(false);
146 if ((boost::math::isnan)(thing))
147 {
148 operator<<(nan_);
149 }
150 else if (thing < 0)
151 {
152 operator<<("-" + inf_);
153 }
154 else
155 {
156 operator<<(inf_);
157 }
158 modifyStrings(old);
159 return *this;
160 }
161
162protected:
164 std::ofstream* ofs_;
165
167 std::string sep_;
168
170 std::string replacement_;
171
173 std::string nan_;
174
176 std::string inf_;
177
180
183
186
188 std::stringstream ss_;
189 };
190
191}
192
Stream class for writing to comma/tab/...-separated values files.
Definition SVOutStream.h:32
bool newline_
Are we at the beginning of a line? (Otherwise, insert separator before next item.)
Definition SVOutStream.h:185
SVOutStream & operator<<(const char c)
Stream output operator for char.
SVOutStream(std::ostream &out, const std::string &sep="\t", const std::string &replacement="_", OpenMS::QuotingMethod quoting=OpenMS::QuotingMethod::DOUBLE)
Constructor.
SVOutStream(const std::string &file_out, const std::string &sep="\t", const std::string &replacement="_", OpenMS::QuotingMethod quoting=OpenMS::QuotingMethod::DOUBLE)
Constructor.
std::stringstream ss_
Stream for testing if a manipulator is "std::endl".
Definition SVOutStream.h:188
SVOutStream & operator<<(const char *c_str)
Stream output operator for char*.
std::string nan_
std::string to use for NaN values
Definition SVOutStream.h:173
std::enable_if< std::is_arithmetic< typenamestd::remove_reference< T >::type >::value, SVOutStream & >::type operator<<(const T &value)
Definition SVOutStream.h:107
OpenMS::QuotingMethod quoting_
std::string quoting method
Definition SVOutStream.h:179
SVOutStream & write(const std::string &str)
Unformatted output (no quoting: useful for comments, but use only on a line of its own!...
bool modifyStrings(bool modify)
Switch modification of strings (quoting/replacing of separators) on/off.
std::string inf_
std::string to use for Inf values
Definition SVOutStream.h:176
SVOutStream & operator<<(std::string str)
Stream output operator for String.
~SVOutStream() override
Destructor.
std::enable_if<!std::is_arithmetic< typenamestd::remove_reference< T >::type >::value, SVOutStream & >::type operator<<(const T &value)
Generic stream output operator (for non-character-based types)
Definition SVOutStream.h:117
SVOutStream & operator<<(enum Newline)
Stream output operator for custom newline (nl) without flushing.
std::string sep_
Separator string.
Definition SVOutStream.h:167
SVOutStream & operator<<(std::ostream &(*fp)(std::ostream &))
Stream output operator for manipulators (used to catch std::endl)
std::ofstream * ofs_
internal file stream when C'tor is called with a filename
Definition SVOutStream.h:164
bool modify_strings_
On/off switch for modification of strings.
Definition SVOutStream.h:182
SVOutStream & writeValueOrNan(NumericT thing)
Write a numeric value or "nan"/"inf"/"-inf", if applicable (would not be needed for Linux)
Definition SVOutStream.h:141
std::string replacement_
Replacement for separator.
Definition SVOutStream.h:170
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition StringUtils.h:43
Newline
custom newline indicator
Definition SVOutStream.h:21
@ nl
Definition SVOutStream.h:21