11 #ifndef _RD_STREAMOPS_H 12 #define _RD_STREAMOPS_H 18 #include <boost/cstdint.hpp> 19 #include <boost/detail/endian.hpp> 28 #if defined(BOOST_LITTLE_ENDIAN) 30 #elif defined(BOOST_BIG_ENDIAN) 33 #error "Failed to determine the system endian value" 39 template <
class T,
unsigned int size>
50 for (
unsigned int i = 0; i < size / 2; ++i)
52 out.bytes[i] = in.bytes[size - 1 - i];
53 out.bytes[size - 1 - i] = in.bytes[i];
67 template<EEndian from, EEndian to,
class T>
71 BOOST_STATIC_ASSERT(
sizeof(T)==1 ||
sizeof(T) == 2 ||
sizeof(T) == 4 ||
sizeof(T) == 8);
72 if(
sizeof(T)==1)
return value;
81 return SwapBytes<T, sizeof(T)>(value);
83 template<EEndian from, EEndian to>
88 template<EEndian from, EEndian to>
93 template<EEndian from, EEndian to>
103 unsigned int val, res;
116 val = ((res<<2) | 1);
122 val = ((res<<3) | 3);
127 if ( res < (1<<29)) {
128 val = ((res<<3) | 7);
138 for (bix = 0; bix < nbytes; bix++) {
139 tc = (char) (val & 255);
147 boost::uint32_t val, num;
150 ss.read(&tmp,
sizeof(tmp));
156 else if ((val&3) == 1) {
157 ss.read((
char *)&tmp,
sizeof(tmp));
158 val |= (
UCHAR(tmp) << 8);
162 else if ((val&7) == 3) {
163 ss.read((
char *)&tmp,
sizeof(tmp));
164 val |= (
UCHAR(tmp) << 8);
165 ss.read((
char *)&tmp,
sizeof(tmp));
166 val |= (
UCHAR(tmp) << 16);
168 offset = (1<<7) + (1<<14);
171 ss.read((
char *)&tmp,
sizeof(tmp));
172 val |= (
UCHAR(tmp) << 8);
173 ss.read((
char *)&tmp,
sizeof(tmp));
174 val |= (
UCHAR(tmp) << 16);
175 ss.read((
char *)&tmp,
sizeof(tmp));
176 val |= (
UCHAR(tmp) << 24);
178 offset = (1<<7) + (1<<14) + (1<<21);
180 num = (val >> shift) + offset;
188 boost::uint32_t val, num;
198 else if ((val&3) == 1) {
201 val |= (
UCHAR(tmp) << 8);
205 else if ((val&7) == 3) {
208 val |= (
UCHAR(tmp) << 8);
211 val |= (
UCHAR(tmp) << 16);
213 offset = (1<<7) + (1<<14);
218 val |= (
UCHAR(tmp) << 8);
221 val |= (
UCHAR(tmp) << 16);
224 val |= (
UCHAR(tmp) << 24);
226 offset = (1<<7) + (1<<14) + (1<<21);
228 num = (val >> shift) + offset;
234 template <
typename T>
236 T tval=EndianSwapBytes<HOST_ENDIAN_ORDER,LITTLE_ENDIAN_ORDER>(val);
237 ss.write((
const char *)&tval,
sizeof(T));
240 template <
typename T>
243 ss.read((
char *)&tloc,
sizeof(T));
244 loc = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(tloc);
248 inline std::string
getLine(std::istream *inStream) {
250 std::getline(*inStream,res);
251 if ((res.length() > 0) && (res[res.length()-1]==
'\r')){
252 res.erase(res.length()-1);
257 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.