lsst.astshim  master-gd1207e8857
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Stream.h
1 /*
2  * LSST Data Management System
3  * Copyright 2016 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 FitsChan; // forward declaration for friendship
37 
41 class Stream {
42 public:
51  explicit Stream(std::istream * istreamPtr, std::ostream * ostreamPtr)
52  :
53  _istreamPtr(),
54  _ostreamPtr(),
55  _sourceStr(),
56  _isFits(false)
57  {
58  if (istreamPtr) {
59  _istreamPtr = std::make_shared<std::istream>(istreamPtr->rdbuf());
60  }
61  if (ostreamPtr) {
62  _ostreamPtr = std::make_shared<std::ostream>(ostreamPtr->rdbuf());
63  }
64  }
65 
66 
67  explicit Stream() : Stream(nullptr, nullptr) {}
68 
69  virtual ~Stream() {}
70 
71  Stream(Stream const &) = default;
72  Stream(Stream &&) = default;
73  Stream & operator=(Stream const &) = default;
74  Stream & operator=(Stream &&) = default;
75 
79  bool hasStdStream() {
80  return _istreamPtr || _ostreamPtr;
81  }
82 
92  char const * source() {
93  if ((_istreamPtr) && (*_istreamPtr)) {
94  if (_isFits) {
95  // http://codereview.stackexchange.com/a/28759
96  _sourceStr.reserve(detail::FITSLEN);
97  _istreamPtr->read(&_sourceStr[0], detail::FITSLEN);
98  } else {
99  std::getline(*_istreamPtr, _sourceStr);
100  }
101  if (*_istreamPtr) {
102  return _sourceStr.c_str();
103  }
104  }
105  return nullptr;
106  }
107 
118  bool sink(char const * cstr) {
119  if (_ostreamPtr) {
120  (*_ostreamPtr) << cstr;
121  if (!_isFits) {
122  (*_ostreamPtr) << std::endl;
123  }
124  return static_cast<bool>(*_ostreamPtr);
125  } else {
126  return true;
127  }
128  }
129 
130  friend class FitsChan;
131 
133  bool getIsFits() const { return _isFits; }
134 
135 protected:
137  void setIsFits(bool isFits) { _isFits = isFits; }
138 
139  std::shared_ptr<std::istream> _istreamPtr;
140  std::shared_ptr<std::ostream> _ostreamPtr;
141  std::string _sourceStr;
144  bool _isFits;
145 };
146 
147 
151 class FileStream : public Stream {
152 public:
159  explicit FileStream(std::string const & path, bool doWrite=false)
160  :
161  Stream(),
162  _path(path)
163  {
164  auto mode = doWrite ? std::ios_base::out : std::ios_base::in;
165  auto fstreamPtr = std::make_shared<std::fstream>(path, mode);
166  if (!*fstreamPtr) {
167  std::ostringstream os;
168  os << "Failed to open file \"" << path << "\" for " << (doWrite ? "writing" : "reading");
169  throw std::runtime_error(os.str());
170  }
171  if (doWrite) {
172  _ostreamPtr = fstreamPtr;
173  } else {
174  _istreamPtr = fstreamPtr;
175  }
176  }
177 
178  virtual ~FileStream() {}
179 
180  std::string getPath() const { return _path; }
181 
182 private:
183  std::string _path;
184 };
185 
186 
193 class StringStream : public Stream {
194 public:
200  explicit StringStream(std::string const & data="")
201  :
202  Stream(),
203  _istringstreamPtr(),
204  _ostringstreamPtr()
205  {
206  _istringstreamPtr = std::make_shared<std::istringstream>(data);
207  _ostringstreamPtr = std::make_shared<std::ostringstream>();
208  _istreamPtr = _istringstreamPtr;
209  _ostreamPtr = _ostringstreamPtr;
210  }
211 
212  virtual ~StringStream() {}
213 
215  std::string getSourceData() const { return _istringstreamPtr->str(); }
216 
218  std::string getSinkData() const { return _ostringstreamPtr->str(); }
219 
221  void sinkToSource() {
222  _istringstreamPtr->clear();
223  _istringstreamPtr->str(getSinkData());
224  _ostringstreamPtr->str("");
225  }
226 
227 private:
229  std::shared_ptr<std::istringstream> _istringstreamPtr;
231  std::shared_ptr<std::ostringstream> _ostringstreamPtr;
232 };
233 
234 namespace detail {
235 
242 inline const char * source() {
243  auto ssptr = reinterpret_cast<Stream *>(astChannelData);
244  if (ssptr) {
245  return ssptr->source();
246  } else {
247  return nullptr;
248  }
249 }
250 
257 inline void sink(const char * cstr) {
258  auto ssptr = reinterpret_cast<Stream *>(astChannelData);
259  if (ssptr) {
260  auto isok = ssptr->sink(cstr);
261  if (!isok) {
262  astSetStatus(AST__ATGER);
263  }
264  }
265 }
266 
267 } // namespace ast::detail
268 
269 } // namespace ast
270 
271 #endif
std::string _sourceStr
Definition: Stream.h:143
Definition: FitsChan.h:199
Stream(std::istream *istreamPtr, std::ostream *ostreamPtr)
Definition: Stream.h:51
bool sink(char const *cstr)
Definition: Stream.h:118
void setIsFits(bool isFits)
set isFits
Definition: Stream.h:137
std::string getSourceData() const
Get a copy of the text from the sink/output stream, without changing the stream.
Definition: Stream.h:215
StringStream(std::string const &data="")
Definition: Stream.h:200
bool _isFits
is this a FITS stream?
Definition: Stream.h:144
std::string getSinkData() const
Get a copy of the text from the sink/output stream, without changing the stream.
Definition: Stream.h:218
Definition: Stream.h:193
bool hasStdStream()
Definition: Stream.h:79
Definition: Stream.h:151
Definition: Stream.h:41
FileStream(std::string const &path, bool doWrite=false)
Definition: Stream.h:159
std::shared_ptr< std::istream > _istreamPtr
input stream
Definition: Stream.h:139
std::shared_ptr< std::ostream > _ostreamPtr
Definition: Stream.h:140
char const * source()
Definition: Stream.h:92
bool getIsFits() const
get isfits
Definition: Stream.h:133
void sinkToSource()
Move output/sink data to input/source.
Definition: Stream.h:221