Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xdr.cpp
Go to the documentation of this file.
1 #include <xstream/xdr.h>
2 
3 
4 /*
5  * Minimal xdr implementation
6  * should be endian agnostic
7  *
8  * XXX need to check for EOF, and have some kind of failbit
9  * XXX throw exceptions
10  */
11 
12 namespace xstream {
13  namespace xdr{
14 
15 // OUTPUT
16 
17 ostream& ostream::operator<<(const string &s) {
18  static const char* cpad = "\0\0\0";
19  size_t len = s.size();
20  // mudar isto XXX
21  (*this) << (static_cast<uint32_t>(len));
22  _sb->sputn(s.c_str(), len);
23  //mod 4 via bitmask (only works for powers of 2)
24  size_t pad = len & 3;
25  if (pad > 0){
26  _sb->sputn(cpad, 4 - pad);
27  }
28  return *this;
29 }
30 
32  uint32_t v = _v;
33  unsigned char c[4];
34  for (int i=0; i < 4; i++, v >>= 8) {
35  c[i] = static_cast<unsigned char>(v & 0xff);
36  }
37  for (int i=0; i < 4; i++) { // RFC1832 mandates msb...lsb order
38  _sb->sputc(c[3 - i]);
39  }
40  return *this;
41 }
42 
43 /* ostream& ostream::operator<<(unsigned int v) {
44  uint32_t n = *(reinterpret_cast<const uint32_t*>(&v));
45  return (*this << n);
46 }
47 */
48 
50  uint32_t n = *(reinterpret_cast<const uint32_t*>(&v));
51  return (*this << n);
52 }
53 
54 /*
55 ostream& ostream::operator<<(int v) {
56  uint32_t n = *(reinterpret_cast<const uint32_t*>(&v));
57  return (*this << n);
58 }
59 */
60 
62  uint64_t n = *(reinterpret_cast<const uint64_t*>(&v));
63  return (*this << n);
64 }
65 
66 /*
67 ostream& ostream::operator<<(const long int v) {
68  uint64_t n = *(reinterpret_cast<const uint64_t*>(&v));
69  return (*this << n);
70 }
71 */
72 
74  uint64_t v=_v;
75  unsigned char c[8];
76  for (int i=0; i < 8; i++, v >>= 8) {
77  c[i] = static_cast<unsigned char>(v & 0xff);
78  }
79  for (int i=0; i < 8; i++) { // RFC1832 mandates msb...lsb order
80  _sb->sputc(c[7 - i]);
81  }
82  return *this;
83 }
84 
85 /*
86 ostream& ostream::operator<<(unsigned long int v) {
87  uint64_t n = *(reinterpret_cast<const uint64_t*>(&v));
88  return (*this << n);
89 }
90 */
91 
92 /*
93 ostream& ostream::operator<<(float v) {
94  uint32_t n = *(reinterpret_cast<const uint32_t*>(&v));
95  return (*this << n);
96 }
97 */
98 
99 // assume floats on this platform are IEEE-754 binary32
101  union jack {
102  float binary32;
103  uint32_t ui32;
104  } _v;
105  _v.binary32 = v;
106  return (*this << _v.ui32);
107 }
108 
109 /*
110 ostream& ostream::operator<<(double v) {
111  uint64_t n = *(reinterpret_cast<const uint64_t*>(&v));
112  return (*this << n);
113 }
114 */
115 
116 // assume doubles on this platform are IEEE-754 binary32
118  union jack {
119  float binary64;
120  uint32_t ui64;
121  } _v;
122  _v.binary64 = v;
123  return (*this << _v.ui64);
124 }
125 
126 // INPUT
127 
129  uint32_t len;
130  (*this) >> len;
131 
132  char line[len];
133  _sb->sgetn(line, len);
134  s = string(line, line + len);
135 
136  //mod 4 via bitmask (only works for powers of 2)
137  size_t pad = (4 - len) & 3;
138  //ignore padding zeros
139  char dummy[pad];
140  _sb->sgetn(dummy, pad);
141  return *this;
142 }
143 
145  v=0;
146  for (int i=0; i < 32; i += 8) {
147  uint32_t c = static_cast<uint32_t>(_sb->sbumpc());
148  v <<= 8;
149  v |= c;
150  }
151  return *this;
152 }
153 
154 /*
155 istream& ostream::operator>>(unsigned int &v) {
156  uint32_t _v;
157  (*this) >> _v;
158  v=reinterpret_cast<unsigned int>(_v);
159  return (*this);
160 }
161 */
162 
164  uint32_t _v;
165  (*this) >> _v;
166  v = static_cast<int32_t>(_v);
167  return (*this);
168 }
169 
170 /*
171 istream& istream::operator>>(int &v) {
172  uint32_t _v;
173  (*this) >> _v;
174  v=reinterpret_cast<int>(_v);
175  return (*this);
176 }
177 */
178 
180  v = 0;
181  for (int i=0; i < 64; i += 8) {
182  uint64_t c = static_cast<uint64_t>(_sb->sbumpc());
183  v <<= 8;
184  v |= c;
185  }
186  return *this;
187 }
188 
189 /*
190 istream& istream::operator>>(unsigned long int &v) {
191  uint32_t _v;
192  (*this) >> _v;
193  v = reinterpret_cast<unsigned long int>(_v);
194  return (*this);
195 }
196 */
197 
199  uint64_t _v;
200  (*this) >> _v;
201  v = static_cast<int64_t>(_v);
202  return (*this);
203 }
204 
205 /*
206 istream& istream::operator>>(long int &v) {
207  uint32_t _v;
208  (*this) >> _v;
209  v=reinterpret_cast<long int>(_v);
210  return (*this);
211 }
212 */
213 
215  uint32_t n;
216  (*this) >> n;
217  float* vp = reinterpret_cast<float*>(&n);
218  v = *vp;
219  return *this;
220 }
221 
223  uint64_t n;
224  (*this) >> n;
225  double* vp = reinterpret_cast<double*>(&n);
226  v = *vp;
227  return *this;
228 }
229 
230 }//namespace xdr
231 }//namespace xstream
C++ iostream like interface to read and write xdr streams.
TPad * pad
Definition: psc_mon.C:73
char string[256]
#define c
Output xdr stream class.
Definition: xdr.h:62
streambuf * _sb
Definition: xdr.h:130
streambuf * _sb
Definition: xdr.h:65
istream & operator>>(int32_t &v)
Definition: xdr.cpp:163
Input xdr stream class.
Definition: xdr.h:127
ostream & operator<<(int32_t v)
Definition: xdr.cpp:49