11 #ifndef _RD_STREAMOPS_H 12 #define _RD_STREAMOPS_H 18 #include <boost/cstdint.hpp> 19 #include <boost/detail/endian.hpp> 27 #if defined(BOOST_LITTLE_ENDIAN) 29 #elif defined(BOOST_BIG_ENDIAN) 32 #error "Failed to determine the system endian value" 38 template <
class T,
unsigned int size>
47 for (
unsigned int i = 0; i < size / 2; ++i) {
48 out.bytes[i] = in.bytes[size - 1 - i];
49 out.bytes[size - 1 - i] = in.bytes[i];
63 template <EEndian from, EEndian to,
class T>
66 BOOST_STATIC_ASSERT(
sizeof(T) == 1 ||
sizeof(T) == 2 ||
sizeof(T) == 4 ||
68 if (
sizeof(T) == 1)
return value;
74 if (from == to)
return value;
76 return SwapBytes<T, sizeof(T)>(value);
78 template <EEndian from, EEndian to>
82 template <EEndian from, EEndian to>
86 template <EEndian from, EEndian to>
94 boost::uint32_t num) {
96 unsigned int val, res;
101 if (res < (1 << 7)) {
107 if (res < (1 << 14)) {
108 val = ((res << 2) | 1);
113 if (res < (1 << 21)) {
114 val = ((res << 3) | 3);
119 if (res < (1 << 29)) {
120 val = ((res << 3) | 7);
129 for (bix = 0; bix < nbytes; bix++) {
130 tc = (char)(val & 255);
138 boost::uint32_t val, num;
141 ss.read(&tmp,
sizeof(tmp));
144 if ((val & 1) == 0) {
146 }
else if ((val & 3) == 1) {
147 ss.read((
char *)&tmp,
sizeof(tmp));
148 val |= (
UCHAR(tmp) << 8);
151 }
else if ((val & 7) == 3) {
152 ss.read((
char *)&tmp,
sizeof(tmp));
153 val |= (
UCHAR(tmp) << 8);
154 ss.read((
char *)&tmp,
sizeof(tmp));
155 val |= (
UCHAR(tmp) << 16);
157 offset = (1 << 7) + (1 << 14);
159 ss.read((
char *)&tmp,
sizeof(tmp));
160 val |= (
UCHAR(tmp) << 8);
161 ss.read((
char *)&tmp,
sizeof(tmp));
162 val |= (
UCHAR(tmp) << 16);
163 ss.read((
char *)&tmp,
sizeof(tmp));
164 val |= (
UCHAR(tmp) << 24);
166 offset = (1 << 7) + (1 << 14) + (1 << 21);
168 num = (val >> shift) + offset;
176 boost::uint32_t val, num;
183 if ((val & 1) == 0) {
185 }
else if ((val & 3) == 1) {
188 val |= (
UCHAR(tmp) << 8);
191 }
else if ((val & 7) == 3) {
194 val |= (
UCHAR(tmp) << 8);
197 val |= (
UCHAR(tmp) << 16);
199 offset = (1 << 7) + (1 << 14);
203 val |= (
UCHAR(tmp) << 8);
206 val |= (
UCHAR(tmp) << 16);
209 val |= (
UCHAR(tmp) << 24);
211 offset = (1 << 7) + (1 << 14) + (1 << 21);
213 num = (val >> shift) + offset;
219 template <
typename T>
221 T tval = EndianSwapBytes<HOST_ENDIAN_ORDER, LITTLE_ENDIAN_ORDER>(val);
222 ss.write((
const char *)&tval,
sizeof(T));
225 template <
typename T>
228 ss.read((
char *)&tloc,
sizeof(T));
229 loc = EndianSwapBytes<LITTLE_ENDIAN_ORDER, HOST_ENDIAN_ORDER>(tloc);
233 inline std::string
getLine(std::istream *inStream) {
235 std::getline(*inStream, res);
236 if ((res.length() > 0) && (res[res.length() - 1] ==
'\r')) {
237 res.erase(res.length() - 1);
242 inline std::string
getLine(std::istream &inStream) {
T EndianSwapBytes(T value)
#define CHECK_INVARIANT(expr, mess)
void streamRead(std::istream &ss, T &loc)
does a binary read of an object from a stream
boost::uint32_t readPackedIntFromStream(std::stringstream &ss)
Reads an integer from a stream in packed format and returns the result.
boost::uint32_t pullPackedIntFromString(const char *&text)
Includes a bunch of functionality for handling Atom and Bond queries.
void streamWrite(std::ostream &ss, const T &val)
does a binary write of an object to a stream
std::string getLine(std::istream *inStream)
grabs the next line from an instream and returns it.
void appendPackedIntToStream(std::stringstream &ss, boost::uint32_t num)
Packs an integer and outputs it to a stream.