nidas  v1.2-1520
IOStream.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2005, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_CORE_IOSTREAM_H
28 #define NIDAS_CORE_IOSTREAM_H
29 
30 #include "IOChannel.h"
31 
32 #include <iostream>
33 
34 namespace nidas { namespace core {
35 
36 class IOStream;
37 
41 class IOStream {
42 
43 public:
49  IOStream(IOChannel& input,size_t buflen = 8192);
50 
51  virtual ~IOStream();
52 
57  size_t available() const
58  {
59  return _head - _tail;
60  }
61 
66  bool isNewInput() const { return _newInput; }
67 
74  size_t read() throw(nidas::util::IOException);
75 
82  size_t readBuf(void* buf, size_t len) throw()
83  {
84  size_t l = available();
85  if (len > l) len = l;
86  if (len == 0) return len;
87  memcpy(buf,_tail,len);
88  _tail += len;
90  return len;
91  }
92 
100  size_t read(void* buf, size_t len) throw(nidas::util::IOException);
101 
107  size_t skip(size_t len) throw(nidas::util::IOException);
108 
118  size_t backup(size_t len) throw();
119 
123  size_t backup() throw();
124 
140  size_t readUntil(void* buf, size_t len,char term) throw(nidas::util::IOException);
141 
157  size_t write(const struct iovec* iov, int nbufs,bool flush)
158  throw(nidas::util::IOException);
159 
160  size_t write(const void*buf,size_t len,bool flush) throw (nidas::util::IOException);
161 
167  void flush() throw(nidas::util::IOException);
168 
177  dsm_time_t createFile(dsm_time_t t,bool exact) throw(nidas::util::IOException)
178  {
179  // std::cerr << "IOStream::createFile, doing flush" << std::endl;
180  flush();
181  return _iochannel.createFile(t,exact);
182  }
183 
184  const std::string& getName() const { return _iochannel.getName(); }
185 
189  long long getNumInputBytes() const { return _nbytesIn; }
190 
191  void addNumInputBytes(int val) { _nbytesIn += val; }
192 
196  long long getNumOutputBytes() const { return _nbytesOut; }
197 
198  void addNumOutputBytes(int val) { _nbytesOut += val; }
199 
200 protected:
201 
203 
204  void reallocateBuffer(size_t len);
205 
206 private:
208  char *_buffer;
209 
211  char* _head;
212 
214  char* _tail;
215 
219  size_t _buflen;
220 
221  size_t _halflen;
222 
226  char* _eob;
227 
231  bool _newInput;
232 
233  long long _nbytesIn;
234 
235  long long _nbytesOut;
236 
237  size_t _nEAGAIN;
238 
240  IOStream(const IOStream&);
241 
243  IOStream& operator=(const IOStream&);
244 
245 };
246 
247 }} // namespace nidas namespace core
248 
249 #endif
char * _buffer
data buffer
Definition: IOStream.h:208
size_t _nEAGAIN
Definition: IOStream.h:237
A base class for buffering data.
Definition: IOStream.h:41
size_t backup()
Move the read buffer pointer backwards to the beginning of the buffer.
Definition: IOStream.cc:195
void addNumOutputBytes(int val)
Definition: IOStream.h:198
bool isNewInput() const
Did last read(), or read(buf,len) call result in a new file being opened?
Definition: IOStream.h:66
long long _nbytesOut
Definition: IOStream.h:235
const std::string & getName() const
Definition: IOStream.h:184
char * _head
where we insert bytes into the buffer
Definition: IOStream.h:211
IOStream(IOChannel &input, size_t buflen=8192)
Create IOStream.
Definition: IOStream.cc:39
char * _eob
One past end of buffer.
Definition: IOStream.h:226
long long _nbytesIn
Definition: IOStream.h:233
long long dsm_time_t
Posix time in microseconds, the number of non-leap microseconds since 1970 Jan 1 00:00 UTC...
Definition: Sample.h:61
char * _tail
where we remove bytes from the buffer
Definition: IOStream.h:214
size_t _buflen
The actual buffer size.
Definition: IOStream.h:219
virtual dsm_time_t createFile(dsm_time_t t, bool exact)
Request that an IOChannel open a new file, with a name based on a time.
Definition: IOChannel.h:207
IOChannel & _iochannel
Definition: IOStream.h:202
size_t write(const struct iovec *iov, int nbufs, bool flush)
Write data.
Definition: IOStream.cc:215
long long getNumOutputBytes() const
Total number of bytes written with this IOStream.
Definition: IOStream.h:196
virtual ~IOStream()
Definition: IOStream.cc:48
A channel for Input or Output of data.
Definition: IOChannel.h:64
size_t _halflen
Definition: IOStream.h:221
size_t available() const
Number of bytes available to be copied from the buffer of IOStream.
Definition: IOStream.h:57
dsm_time_t createFile(dsm_time_t t, bool exact)
Request that IOChannel object open a new file, with a name based on a time.
Definition: IOStream.h:177
void reallocateBuffer(size_t len)
Definition: IOStream.cc:53
size_t readUntil(void *buf, size_t len, char term)
Read into the user buffer until a terminating character is found or len-1 bytes have been read...
Definition: IOStream.cc:149
size_t readBuf(void *buf, size_t len)
Copy available bytes from the internal buffer to buf, returning the number of bytes copied...
Definition: IOStream.h:82
int len
Definition: sing.cc:934
void flush()
Flush buffer to physical device.
Definition: IOStream.cc:305
bool _newInput
Was the previous read performed on a newly opened file?
Definition: IOStream.h:231
Definition: IOException.h:37
size_t read()
Do an IOChannel::read into the internal buffer of IOStream.
Definition: IOStream.cc:83
IOStream & operator=(const IOStream &)
No assignment.
void addNumInputBytes(int val)
Definition: IOStream.h:191
size_t skip(size_t len)
If the internal buffer is empty, do an IOChannel::read into the buffer.
Definition: IOStream.cc:133
long long getNumInputBytes() const
Number of bytes read with this IOStream.
Definition: IOStream.h:189
virtual const std::string & getName() const =0