nidas v1.2.3
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
34namespace nidas { namespace core {
35
36class IOStream;
37
41class IOStream {
42
43public:
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
76 size_t read();
77
84 size_t readBuf(void* buf, size_t len) throw()
85 {
86 size_t l = available();
87 if (len > l) len = l;
88 if (len == 0) return len;
89 memcpy(buf,_tail,len);
90 _tail += len;
92 return len;
93 }
94
104 size_t read(void* buf, size_t len);
105
113 size_t skip(size_t len);
114
124 size_t backup(size_t len) throw();
125
129 size_t backup() throw();
130
148 size_t readUntil(void* buf, size_t len, char term);
149
167 size_t write(const struct iovec* iov, int nbufs, bool flush);
168
172 size_t write(const void*buf,size_t len,bool flush);
173
181 void flush();
182
194 {
195 // std::cerr << "IOStream::createFile, doing flush" << std::endl;
196 flush();
197 return _iochannel.createFile(t,exact);
198 }
199
200 const std::string& getName() const {
201 return _iochannel.getName();
202 }
203
207 long long getNumInputBytes() const {
208 return _nbytesIn;
209 }
210
211 void addNumInputBytes(int val) {
212 _nbytesIn += val;
213 }
214
218 long long getNumOutputBytes() const {
219 return _nbytesOut;
220 }
221
222 void addNumOutputBytes(int val) {
223 _nbytesOut += val;
224 }
225
226protected:
227
229
230 void reallocateBuffer(size_t len);
231
232private:
234 char *_buffer;
235
237 char* _head;
238
240 char* _tail;
241
245 size_t _buflen;
246
247 size_t _halflen;
248
252 char* _eob;
253
258
259 long long _nbytesIn;
260
261 long long _nbytesOut;
262
263 size_t _nEAGAIN;
264
267
270
271};
272
273}} // namespace nidas namespace core
274
275#endif
A channel for Input or Output of data.
Definition IOChannel.h:65
virtual const std::string & getName() const =0
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:229
A base class for buffering data.
Definition IOStream.h:41
void addNumInputBytes(int val)
Definition IOStream.h:211
virtual ~IOStream()
Definition IOStream.cc:48
size_t skip(size_t len)
If the internal buffer is empty, do an IOChannel::read into the buffer.
Definition IOStream.cc:133
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:84
void flush()
Flush buffer to physical device.
Definition IOStream.cc:304
size_t _nEAGAIN
Definition IOStream.h:263
long long getNumOutputBytes() const
Total number of bytes written with this IOStream.
Definition IOStream.h:218
char * _buffer
data buffer
Definition IOStream.h:234
bool isNewInput() const
Did last read(), or read(buf,len) call result in a new file being opened?
Definition IOStream.h:66
size_t _halflen
Definition IOStream.h:247
size_t _buflen
The actual buffer size.
Definition IOStream.h:245
void reallocateBuffer(size_t len)
Definition IOStream.cc:53
bool _newInput
Was the previous read performed on a newly opened file?
Definition IOStream.h:257
const std::string & getName() const
Definition IOStream.h:200
void addNumOutputBytes(int val)
Definition IOStream.h:222
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
char * _eob
One past end of buffer.
Definition IOStream.h:252
size_t write(const struct iovec *iov, int nbufs, bool flush)
Write data.
Definition IOStream.cc:214
long long getNumInputBytes() const
Number of bytes read with this IOStream.
Definition IOStream.h:207
char * _tail
where we remove bytes from the buffer
Definition IOStream.h:240
size_t available() const
Number of bytes available to be copied from the buffer of IOStream.
Definition IOStream.h:57
long long _nbytesIn
Definition IOStream.h:259
IOChannel & _iochannel
Definition IOStream.h:228
size_t backup()
Move the read buffer pointer backwards to the beginning of the buffer.
Definition IOStream.cc:194
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:193
IOStream(const IOStream &)
No copying.
IOStream & operator=(const IOStream &)
No assignment.
IOStream(IOChannel &input, size_t buflen=8192)
Create IOStream.
Definition IOStream.cc:39
long long _nbytesOut
Definition IOStream.h:261
size_t read()
Do an IOChannel::read into the internal buffer of IOStream.
Definition IOStream.cc:83
char * _head
where we insert bytes into the buffer
Definition IOStream.h:237
Sample * getSample(sampleType type, unsigned int len)
A convienence method for getting a sample of an enumerated type from a pool.
Definition Sample.cc:70
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:62
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
int len
Definition sing.cc:948