LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
Stream.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2017 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 #ifndef ASTSHIM_SOURCESINK_H
23 #define ASTSHIM_SOURCESINK_H
24 
25 #include <string>
26 #include <fstream>
27 #include <sstream>
28 #include <iostream>
29 
30 #include "astshim/base.h"
31 #include "astshim/detail/utils.h"
32 #include "astshim/Object.h"
33 
34 namespace ast {
35 
36 class Channel; // forward declaration for friendship
37 
41 class Stream {
42 public:
51  explicit Stream(std::istream *istreamPtr, std::ostream *ostreamPtr)
52  : _istreamPtr(), _ostreamPtr(), _sourceStr(), _isFits(false) {
53  if (istreamPtr) {
54  _istreamPtr = std::make_shared<std::istream>(istreamPtr->rdbuf());
55  }
56  if (ostreamPtr) {
57  _ostreamPtr = std::make_shared<std::ostream>(ostreamPtr->rdbuf());
58  }
59  }
60 
61  explicit Stream() : Stream(nullptr, nullptr) {}
62 
63  virtual ~Stream() {}
64 
65  Stream(Stream const &) = default;
66  Stream(Stream &&) = default;
67  Stream &operator=(Stream const &) = default;
68  Stream &operator=(Stream &&) = default;
69 
73  bool hasStdStream() { return _istreamPtr || _ostreamPtr; }
74 
84  char const *source() {
85  if ((_istreamPtr) && (*_istreamPtr)) {
86  if (_isFits) {
87  // http://codereview.stackexchange.com/a/28759
88  _sourceStr.resize(detail::FITSLEN);
89  _istreamPtr->read(&_sourceStr[0], detail::FITSLEN);
90  } else {
92  }
93  if (*_istreamPtr) {
94  return _sourceStr.c_str();
95  }
96  }
97  return nullptr;
98  }
99 
110  bool sink(char const *cstr) {
111  if (_ostreamPtr) {
112  (*_ostreamPtr) << cstr;
113  if (!_isFits) {
114  (*_ostreamPtr) << std::endl;
115  }
116  return static_cast<bool>(*_ostreamPtr);
117  } else {
118  return true;
119  }
120  }
121 
122  friend class Channel;
123 
125  bool getIsFits() const { return _isFits; }
126 
127 protected:
129  void setIsFits(bool isFits) { _isFits = isFits; }
130 
136  bool _isFits;
137 };
138 
142 class FileStream : public Stream {
143 public:
150  explicit FileStream(std::string const &path, bool doWrite = false) : Stream(), _path(path) {
151  auto mode = doWrite ? std::ios_base::out : std::ios_base::in;
152  auto fstreamPtr = std::make_shared<std::fstream>(path, mode);
153  if (!*fstreamPtr) {
155  os << "Failed to open file \"" << path << "\" for " << (doWrite ? "writing" : "reading");
156  throw std::runtime_error(os.str());
157  }
158  if (doWrite) {
159  _ostreamPtr = fstreamPtr;
160  } else {
161  _istreamPtr = fstreamPtr;
162  }
163  }
164 
165  virtual ~FileStream() {}
166 
168  std::string getPath() const { return _path; }
169 
170 private:
171  std::string _path;
172 };
173 
180 class StringStream : public Stream {
181 public:
187  explicit StringStream(std::string const &data = "") : Stream(), _istringstreamPtr(), _ostringstreamPtr() {
188  _istringstreamPtr = std::make_shared<std::istringstream>(data);
189  _ostringstreamPtr = std::make_shared<std::ostringstream>();
190  _istreamPtr = _istringstreamPtr;
191  _ostreamPtr = _ostringstreamPtr;
192  }
193 
194  virtual ~StringStream() {}
195 
197  std::string getSourceData() const { return _istringstreamPtr->str(); }
198 
200  std::string getSinkData() const { return _ostringstreamPtr->str(); }
201 
203  void sinkToSource() {
204  _istringstreamPtr->clear();
205  _istringstreamPtr->str(getSinkData());
206  _ostringstreamPtr->str("");
207  }
208 
209 private:
211  std::shared_ptr<std::istringstream> _istringstreamPtr;
213  std::shared_ptr<std::ostringstream> _ostringstreamPtr;
214 };
215 
216 namespace detail {
217 
224 inline const char *source() {
225  auto ssptr = reinterpret_cast<Stream *>(astChannelData);
226  if (ssptr) {
227  return ssptr->source();
228  } else {
229  return nullptr;
230  }
231 }
232 
239 inline void sink(const char *cstr) {
240  auto ssptr = reinterpret_cast<Stream *>(astChannelData);
241  if (ssptr) {
242  auto isok = ssptr->sink(cstr);
243  if (!isok) {
244  astSetStatus(AST__ATGER);
245  }
246  }
247 }
248 
249 } // namespace detail
250 
251 } // namespace ast
252 
253 #endif
ast::FileStream
File-based source or sink (not both) for channels.
Definition: Stream.h:142
std::string::resize
T resize(T... args)
std::istream::rdbuf
T rdbuf(T... args)
std::string
STL class.
std::shared_ptr< std::istream >
ast::Stream::source
char const * source()
Source (read) from the stream.
Definition: Stream.h:84
ast::Stream::_sourceStr
std::string _sourceStr
string containing a local copy of sourced data, so source can return a char * that won't disappear ri...
Definition: Stream.h:135
ast::Stream::Stream
Stream(std::istream *istreamPtr, std::ostream *ostreamPtr)
Construct a Stream from input and output std::streams.
Definition: Stream.h:51
ast::StringStream::~StringStream
virtual ~StringStream()
Definition: Stream.h:194
ast::StringStream::getSinkData
std::string getSinkData() const
Get a copy of the text from the sink/output stream, without changing the stream.
Definition: Stream.h:200
std::istream::read
T read(T... args)
ast::Stream::setIsFits
void setIsFits(bool isFits)
set isFits
Definition: Stream.h:129
std::istringstream::clear
T clear(T... args)
ast::Stream::sink
bool sink(char const *cstr)
Sink (write) to the stream.
Definition: Stream.h:110
ast::Channel
Channel provides input/output of AST objects.
Definition: Channel.h:60
ast::Stream
A stream for ast::Channel.
Definition: Stream.h:41
ast::StringStream::getSourceData
std::string getSourceData() const
Get a copy of the text from the sink/output stream, without changing the stream.
Definition: Stream.h:197
ast::Stream::Stream
Stream()
Definition: Stream.h:61
data
char * data
Definition: BaseRecord.cc:62
ast::detail::source
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
std::ostream
STL class.
std::string::c_str
T c_str(T... args)
ast::Stream::operator=
Stream & operator=(Stream &&)=default
std::runtime_error
STL class.
ast::Stream::Stream
Stream(Stream const &)=default
ast::FileStream::~FileStream
virtual ~FileStream()
Definition: Stream.h:165
ast::Stream::operator=
Stream & operator=(Stream const &)=default
utils.h
base.h
ast::StringStream
String-based source and sink for channels.
Definition: Stream.h:180
ast::Stream::Stream
Stream(Stream &&)=default
std::ostringstream
STL class.
ast
AST wrapper classes and functions.
Definition: attributes_channel.dox:1
os
std::ostream * os
Definition: Schema.cc:746
std::endl
T endl(T... args)
ast::Stream::hasStdStream
bool hasStdStream()
Return true if this Stream has an input or output std::stream.
Definition: Stream.h:73
std::getline
T getline(T... args)
ast::StringStream::sinkToSource
void sinkToSource()
Move output/sink data to input/source.
Definition: Stream.h:203
ast::Stream::~Stream
virtual ~Stream()
Definition: Stream.h:63
ast::FileStream::getPath
std::string getPath() const
Get the path to the file, as a string.
Definition: Stream.h:168
std::istringstream::str
T str(T... args)
ast::Stream::_istreamPtr
std::shared_ptr< std::istream > _istreamPtr
input stream
Definition: Stream.h:131
ast::Stream::_isFits
bool _isFits
is this a FITS stream?
Definition: Stream.h:136
ast::FileStream::FileStream
FileStream(std::string const &path, bool doWrite=false)
Construct a FileStream for reading or writing, but not both.
Definition: Stream.h:150
std::istream
STL class.
Object.h
ast::Stream::_ostreamPtr
std::shared_ptr< std::ostream > _ostreamPtr
output stream
Definition: Stream.h:132
ast::detail::sink
void sink(const char *cstr)
Sink function that allows astChannel to sink to a Stream.
Definition: Stream.h:239
ast::StringStream::StringStream
StringStream(std::string const &data="")
Construct a StringStream.
Definition: Stream.h:187
ast::Stream::getIsFits
bool getIsFits() const
get isfits
Definition: Stream.h:125