source: trunk/src2/core/RpSerialBuffer.cc @ 605

Last change on this file since 605 was 413, checked in by mmc, 18 years ago
  • Added <description> capability to output objects, including axes.
  • Fixed the ResultSet? so that it is more compact and supports the simulation number as a parameter. This is useful when there are datasets with wildly varying parameters.
File size: 4.2 KB
Line 
1/*
2 * ======================================================================
3 *  Rappture::SerialBuffer
4 *
5 *  AUTHOR:  Michael McLennan, Purdue University
6 *           Carol X Song, Purdue University
7 *
8 *  Copyright (c) 2004-2006  Purdue Research Foundation
9 * ----------------------------------------------------------------------
10 *  See the file "license.terms" for information on usage and
11 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 * ======================================================================
13 */
14#include "RpSerialBuffer.h"
15
16#ifdef BIGENDIAN
17#  define ENDIAN_FOR_LOOP(var,size)  \
18     for (int var=size-1; var >= 0; var--)
19#else
20#  define ENDIAN_FOR_LOOP(var,size)  \
21     for (int var=0; var < size; var++)
22#endif
23
24using namespace Rappture;
25
26/**
27 * Construct an empty SerialBuffer.
28 */
29SerialBuffer::SerialBuffer()
30  : _buffer(),
31    _pos(0)
32{
33}
34
35/**
36 * Construct a SerialBuffer loaded with bytes produced by another
37 * SerialBuffer.  This is used to decode information from the buffer.
38 *
39 * @param bytes pointer to bytes being decoded.
40 * @param nbytes number of bytes being decoded.
41 */
42SerialBuffer::SerialBuffer(const char* bytes, int nbytes)
43  : _buffer(),
44    _pos(0)
45{
46    _buffer.reserve(nbytes);
47    while (nbytes-- > 0) {
48        _buffer.push_back( *bytes++ );
49    }
50}
51
52/**
53 * Copy constructor
54 */
55SerialBuffer::SerialBuffer(const SerialBuffer& sb)
56  : _buffer(sb._buffer),
57    _pos(0)  // auto-rewind
58{
59}
60
61/**
62 * Assignment operator
63 */
64SerialBuffer&
65SerialBuffer::operator=(const SerialBuffer& sb)
66{
67    _buffer = sb._buffer;
68    _pos = 0;  // auto-rewind
69}
70
71SerialBuffer::~SerialBuffer()
72{
73}
74
75/**
76 * Get the bytes currently stored in the buffer.  These bytes can
77 * be stored, and used later to construct another SerialBuffer to
78 * decode the information.
79 *
80 * @return Pointer to the bytes in the buffer.
81 */
82const char*
83SerialBuffer::bytes() const
84{
85    return &_buffer[0];
86}
87
88/**
89 * Get the number of bytes currently stored in the buffer.
90 * @return Number of the bytes in the buffer.
91 */
92int
93SerialBuffer::size() const
94{
95    return _buffer.size();
96}
97
98/**
99 * Clear the buffer, making it empty.
100 */
101SerialBuffer&
102SerialBuffer::clear()
103{
104    _buffer.clear();
105    return *this;
106}
107
108SerialBuffer&
109SerialBuffer::writeChar(char cval)
110{
111    _buffer.push_back(cval);
112    return *this;
113}
114
115SerialBuffer&
116SerialBuffer::writeInt(int ival)
117{
118    char *ptr = (char*)(&ival);
119
120    ENDIAN_FOR_LOOP(i, sizeof(int)) {
121        _buffer.push_back(ptr[i]);
122    }
123    return *this;
124}
125
126SerialBuffer&
127SerialBuffer::writeDouble(double dval)
128{
129    char *ptr = (char*)(&dval);
130
131    ENDIAN_FOR_LOOP(i, sizeof(double)) {
132        _buffer.push_back(ptr[i]);
133    }
134    return *this;
135}
136
137SerialBuffer&
138SerialBuffer::writeString(const char* sval)
139{
140    while (*sval != '\0') {
141        _buffer.push_back(*sval++);
142    }
143    _buffer.push_back('\0');
144    return *this;
145}
146
147SerialBuffer&
148SerialBuffer::writeBytes(const char* bval, int nbytes)
149{
150    writeInt(nbytes);
151    while (nbytes-- > 0) {
152        _buffer.push_back(*bval++);
153    }
154    return *this;
155}
156
157void
158SerialBuffer::rewind()
159{
160    _pos = 0;
161}
162
163int
164SerialBuffer::atEnd() const
165{
166    return (_pos >= _buffer.size());
167}
168
169char
170SerialBuffer::readChar()
171{
172    char c = '\0';
173    if (_pos < _buffer.size()) {
174        c = _buffer[_pos++];
175    }
176    return c;
177}
178
179int
180SerialBuffer::readInt()
181{
182    int ival = 0;
183    char *ptr = (char*)(&ival);
184
185    ENDIAN_FOR_LOOP(i, sizeof(int)) {
186        if (_pos < _buffer.size()) {
187            ptr[i] = _buffer[_pos++];
188        }
189    }
190    return ival;
191}
192
193double
194SerialBuffer::readDouble()
195{
196    double dval = 0;
197    char *ptr = (char*)(&dval);
198
199    ENDIAN_FOR_LOOP(i, sizeof(double)) {
200        if (_pos < _buffer.size()) {
201            ptr[i] = _buffer[_pos++];
202        }
203    }
204    return dval;
205}
206
207std::string
208SerialBuffer::readString()
209{
210    std::string sval;
211    char c;
212    while (_pos < _buffer.size()) {
213        c = _buffer[_pos++];
214        if (c == '\0') {
215            break;
216        } else {
217            sval.push_back(c);
218        }
219    }
220    return sval;
221}
222
223std::vector<char>
224SerialBuffer::readBytes()
225{
226    int nbytes;
227    std::vector<char> bval;
228
229    nbytes = readInt();
230    while (_pos < _buffer.size() && nbytes-- > 0) {
231        bval.push_back( _buffer[_pos++] );
232    }
233    return bval;
234}
Note: See TracBrowser for help on using the repository browser.