1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
|
//
// Copyright (C) 2002-2008 Greg Landrum and Rational Discovery LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
//
#include <RDGeneral/export.h>
#ifndef _RD_STREAMOPS_H
#define _RD_STREAMOPS_H
#include "types.h"
#include "Invariant.h"
#include "RDProps.h"
#include <string>
#include <sstream>
#include <iostream>
#include <boost/cstdint.hpp>
#include <boost/detail/endian.hpp>
namespace RDKit {
// this code block for handling endian problems is from :
// http://stackoverflow.com/questions/105252/how-do-i-convert-between-big-endian-and-little-endian-values-in-c
enum EEndian {
LITTLE_ENDIAN_ORDER,
BIG_ENDIAN_ORDER,
#if defined(BOOST_LITTLE_ENDIAN)
HOST_ENDIAN_ORDER = LITTLE_ENDIAN_ORDER
#elif defined(BOOST_BIG_ENDIAN)
HOST_ENDIAN_ORDER = BIG_ENDIAN_ORDER
#else
#error "Failed to determine the system endian value"
#endif
};
// this function swap the bytes of values given it's size as a template
// parameter (could sizeof be used?).
template <class T, unsigned int size>
inline T SwapBytes(T value) {
if (size < 2)
return value;
union {
T value;
char bytes[size];
} in, out;
in.value = value;
for (unsigned int i = 0; i < size; ++i) {
out.bytes[i] = in.bytes[size - 1 - i];
}
return out.value;
}
// Here is the function you will use. Again there is two compile-time assertion
// that use the boost librarie. You could probably comment them out, but if you
// do be cautious not to use this function for anything else than integers
// types. This function need to be calles like this :
//
// int x = someValue;
// int i = EndianSwapBytes<HOST_ENDIAN_ORDER, BIG_ENDIAN_ORDER>(x);
//
template <EEndian from, EEndian to, class T>
inline T EndianSwapBytes(T value) {
// A : La donnée à swapper à une taille de 2, 4 ou 8 octets
BOOST_STATIC_ASSERT(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 ||
sizeof(T) == 8);
if (sizeof(T) == 1) return value;
// A : La donnée à swapper est d'un type arithmetic
// BOOST_STATIC_ASSERT(boost::is_arithmetic<T>::value);
// Si from et to sont du même type on ne swap pas.
if (from == to) return value;
return SwapBytes<T, sizeof(T)>(value);
}
template <EEndian from, EEndian to>
inline char EndianSwapBytes(char value) {
return value;
}
template <EEndian from, EEndian to>
inline unsigned char EndianSwapBytes(unsigned char value) {
return value;
}
template <EEndian from, EEndian to>
inline signed char EndianSwapBytes(signed char value) {
return value;
}
// --------------------------------------
//! Packs an integer and outputs it to a stream
inline void appendPackedIntToStream(std::stringstream &ss,
boost::uint32_t num) {
int nbytes, bix;
unsigned int val, res;
char tc;
res = num;
while (1) {
if (res < (1 << 7)) {
val = (res << 1);
nbytes = 1;
break;
}
res -= (1 << 7);
if (res < (1 << 14)) {
val = ((res << 2) | 1);
nbytes = 2;
break;
}
res -= (1 << 14);
if (res < (1 << 21)) {
val = ((res << 3) | 3);
nbytes = 3;
break;
}
res -= (1 << 21);
if (res < (1 << 29)) {
val = ((res << 3) | 7);
nbytes = 4;
break;
} else {
CHECK_INVARIANT(0, "ERROR: Integer too big to pack\n");
}
}
// val = EndianSwapBytes<HOST_ENDIAN_ORDER,LITTLE_ENDIAN_ORDER>(val);
for (bix = 0; bix < nbytes; bix++) {
tc = (char)(val & 255);
ss.write(&tc, 1);
val >>= 8;
}
}
//! Reads an integer from a stream in packed format and returns the result.
inline boost::uint32_t readPackedIntFromStream(std::stringstream &ss) {
boost::uint32_t val, num;
int shift, offset;
char tmp;
ss.read(&tmp, sizeof(tmp));
val = UCHAR(tmp);
offset = 0;
if ((val & 1) == 0) {
shift = 1;
} else if ((val & 3) == 1) {
ss.read((char *)&tmp, sizeof(tmp));
val |= (UCHAR(tmp) << 8);
shift = 2;
offset = (1 << 7);
} else if ((val & 7) == 3) {
ss.read((char *)&tmp, sizeof(tmp));
val |= (UCHAR(tmp) << 8);
ss.read((char *)&tmp, sizeof(tmp));
val |= (UCHAR(tmp) << 16);
shift = 3;
offset = (1 << 7) + (1 << 14);
} else {
ss.read((char *)&tmp, sizeof(tmp));
val |= (UCHAR(tmp) << 8);
ss.read((char *)&tmp, sizeof(tmp));
val |= (UCHAR(tmp) << 16);
ss.read((char *)&tmp, sizeof(tmp));
val |= (UCHAR(tmp) << 24);
shift = 3;
offset = (1 << 7) + (1 << 14) + (1 << 21);
}
num = (val >> shift) + offset;
// num = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(num);
return num;
}
//! Reads an integer from a char * in packed format and returns the result.
//! The argument is advanced
inline boost::uint32_t pullPackedIntFromString(const char *&text) {
boost::uint32_t val, num;
int shift, offset;
char tmp;
tmp = *text;
text++;
val = UCHAR(tmp);
offset = 0;
if ((val & 1) == 0) {
shift = 1;
} else if ((val & 3) == 1) {
tmp = *text;
text++;
val |= (UCHAR(tmp) << 8);
shift = 2;
offset = (1 << 7);
} else if ((val & 7) == 3) {
tmp = *text;
text++;
val |= (UCHAR(tmp) << 8);
tmp = *text;
text++;
val |= (UCHAR(tmp) << 16);
shift = 3;
offset = (1 << 7) + (1 << 14);
} else {
tmp = *text;
text++;
val |= (UCHAR(tmp) << 8);
tmp = *text;
text++;
val |= (UCHAR(tmp) << 16);
tmp = *text;
text++;
val |= (UCHAR(tmp) << 24);
shift = 3;
offset = (1 << 7) + (1 << 14) + (1 << 21);
}
num = (val >> shift) + offset;
// num = EndianSwapBytes<LITTLE_ENDIAN_ORDER,HOST_ENDIAN_ORDER>(num);
return num;
}
//! does a binary write of an object to a stream
template <typename T>
void streamWrite(std::ostream &ss, const T &val) {
T tval = EndianSwapBytes<HOST_ENDIAN_ORDER, LITTLE_ENDIAN_ORDER>(val);
ss.write((const char *)&tval, sizeof(T));
}
//! special case for string
inline void streamWrite(std::ostream &ss, const std::string &what) {
unsigned int l = rdcast<unsigned int>(what.length());
ss.write((const char *)&l, sizeof(l));
ss.write(what.c_str(), sizeof(char) * l);
};
template <typename T>
void streamWriteVec(std::ostream &ss, const T &val) {
streamWrite(ss, static_cast<boost::uint64_t>(val.size()));
for (size_t i = 0; i < val.size(); ++i) streamWrite(ss, val[i]);
}
//! does a binary read of an object from a stream
template <typename T>
void streamRead(std::istream &ss, T &loc) {
T tloc;
ss.read((char *)&tloc, sizeof(T));
loc = EndianSwapBytes<LITTLE_ENDIAN_ORDER, HOST_ENDIAN_ORDER>(tloc);
}
//! special case for string
template <class T>
void streamRead(std::istream &ss, T &obj, int version) {
RDUNUSED_PARAM(version);
streamRead(ss, obj);
}
inline void streamRead(std::istream &ss, std::string &what, int version) {
RDUNUSED_PARAM(version);
unsigned int l;
ss.read((char *)&l, sizeof(l));
char *buff = new char[l + 1];
ss.read(buff, sizeof(char) * l);
buff[l] = 0;
what = buff;
delete[] buff;
};
template <class T>
void streamReadVec(std::istream &ss, T &val) {
boost::uint64_t size;
streamRead(ss, size);
val.resize(size);
for (size_t i = 0; i < size; ++i) streamRead(ss, val[i]);
}
inline void streamReadStringVec(std::istream &ss, std::vector<std::string> &val,
int version) {
boost::uint64_t size;
streamRead(ss, size);
val.resize(size);
for (size_t i = 0; i < size; ++i) streamRead(ss, val[i], version);
}
//! grabs the next line from an instream and returns it.
inline std::string getLine(std::istream *inStream) {
std::string res;
std::getline(*inStream, res);
if ((res.length() > 0) && (res[res.length() - 1] == '\r')) {
res.erase(res.length() - 1);
}
return res;
}
//! grabs the next line from an instream and returns it.
inline std::string getLine(std::istream &inStream) {
return getLine(&inStream);
}
// n.b. We can't use RDTypeTag directly, they are implementation
// specific
namespace DTags {
const unsigned char StringTag = 0;
const unsigned char IntTag = 1;
const unsigned char UnsignedIntTag = 2;
const unsigned char BoolTag = 3;
const unsigned char FloatTag = 4;
const unsigned char DoubleTag = 5;
const unsigned char VecStringTag = 6;
const unsigned char VecIntTag = 7;
const unsigned char VecUIntTag = 8;
const unsigned char VecBoolTag = 9;
const unsigned char VecFloatTag = 10;
const unsigned char VecDoubleTag = 11;
const unsigned char EndTag = 0xFF;
}
inline bool isSerializable(const Dict::Pair &pair) {
switch (pair.val.getTag()) {
case RDTypeTag::StringTag:
case RDTypeTag::IntTag:
case RDTypeTag::UnsignedIntTag:
case RDTypeTag::BoolTag:
case RDTypeTag::FloatTag:
case RDTypeTag::DoubleTag:
case RDTypeTag::VecStringTag:
case RDTypeTag::VecIntTag:
case RDTypeTag::VecUnsignedIntTag:
case RDTypeTag::VecFloatTag:
case RDTypeTag::VecDoubleTag:
return true;
default:
return false;
}
}
inline bool streamWriteProp(std::ostream &ss, const Dict::Pair &pair) {
if (!isSerializable(pair)) return false;
streamWrite(ss, pair.key);
switch (pair.val.getTag()) {
case RDTypeTag::StringTag:
streamWrite(ss, DTags::StringTag);
streamWrite(ss, rdvalue_cast<std::string>(pair.val));
break;
case RDTypeTag::IntTag:
streamWrite(ss, DTags::IntTag);
streamWrite(ss, rdvalue_cast<int>(pair.val));
break;
case RDTypeTag::UnsignedIntTag:
streamWrite(ss, DTags::UnsignedIntTag);
streamWrite(ss, rdvalue_cast<unsigned int>(pair.val));
break;
case RDTypeTag::BoolTag:
streamWrite(ss, DTags::BoolTag);
streamWrite(ss, rdvalue_cast<bool>(pair.val));
break;
case RDTypeTag::FloatTag:
streamWrite(ss, DTags::FloatTag);
streamWrite(ss, rdvalue_cast<float>(pair.val));
break;
case RDTypeTag::DoubleTag:
streamWrite(ss, DTags::DoubleTag);
streamWrite(ss, rdvalue_cast<double>(pair.val));
break;
case RDTypeTag::VecStringTag:
streamWrite(ss, DTags::VecStringTag);
streamWriteVec(ss, rdvalue_cast<std::vector<std::string>>(pair.val));
break;
case RDTypeTag::VecDoubleTag:
streamWrite(ss, DTags::VecDoubleTag);
streamWriteVec(ss, rdvalue_cast<std::vector<double>>(pair.val));
break;
case RDTypeTag::VecFloatTag:
streamWrite(ss, DTags::VecFloatTag);
streamWriteVec(ss, rdvalue_cast<std::vector<float>>(pair.val));
break;
case RDTypeTag::VecIntTag:
streamWrite(ss, DTags::VecIntTag);
streamWriteVec(ss, rdvalue_cast<std::vector<int>>(pair.val));
break;
case RDTypeTag::VecUnsignedIntTag:
streamWrite(ss, DTags::VecUIntTag);
streamWriteVec(ss, rdvalue_cast<std::vector<unsigned int>>(pair.val));
break;
default:
std::cerr << "Failed to write " << pair.key << std::endl;
return false;
}
return true;
}
inline bool streamWriteProps(std::ostream &ss, const RDProps &props,
bool savePrivate=false, bool saveComputed=false) {
STR_VECT propsToSave = props.getPropList(savePrivate, saveComputed);
std::set<std::string> propnames(propsToSave.begin(), propsToSave.end());
const Dict &dict = props.getDict();
unsigned int count = 0;
for(Dict::DataType::const_iterator it = dict.getData().begin();
it != dict.getData().end();
++it) {
if(isSerializable(*it) && propnames.find(it->key) != propnames.end()) {
count ++;
}
}
streamWrite(ss, count); // packed int?
unsigned int writtenCount = 0;
for(Dict::DataType::const_iterator it = dict.getData().begin();
it != dict.getData().end();
++it) {
if (propnames.find(it->key) != propnames.end()) {
if (isSerializable(*it)) {
// note - not all properties are serializable, this may be
// a null op
if (streamWriteProp(ss, *it)) {
writtenCount++;
}
}
}
}
POSTCONDITION(count==writtenCount, "Estimated property count not equal to written");
return true;
}
template<class T>
void readRDValue(std::istream &ss, RDValue &value) {
T v;
streamRead(ss, v);
value = v;
}
template <class T>
void readRDVecValue(std::istream &ss, RDValue &value) {
std::vector<T> v;
streamReadVec(ss, v);
value = v;
}
inline void readRDValueString(std::istream &ss, RDValue &value) {
std::string v;
int version=0;
streamRead(ss, v, version);
value = v;
}
inline void readRDStringVecValue(std::istream &ss, RDValue &value) {
std::vector<std::string> v;
int version = 0;
streamReadStringVec(ss, v, version);
value = v;
}
inline bool streamReadProp(std::istream &ss, Dict::Pair &pair) {
int version=0;
streamRead(ss, pair.key, version);
unsigned char type;
streamRead(ss, type);
switch(type) {
case DTags::StringTag: readRDValueString(ss, pair.val); break;
case DTags::IntTag: readRDValue<int>(ss, pair.val); break;
case DTags::UnsignedIntTag: readRDValue<unsigned int>(ss, pair.val); break;
case DTags::BoolTag: readRDValue<bool>(ss, pair.val); break;
case DTags::FloatTag: readRDValue<float>(ss, pair.val); break;
case DTags::DoubleTag: readRDValue<double>(ss, pair.val); break;
case DTags::VecStringTag:
readRDStringVecValue(ss, pair.val);
break;
case DTags::VecIntTag:
readRDVecValue<int>(ss, pair.val);
break;
case DTags::VecUIntTag:
readRDVecValue<unsigned int>(ss, pair.val);
break;
case DTags::VecFloatTag:
readRDVecValue<float>(ss, pair.val);
break;
case DTags::VecDoubleTag:
readRDVecValue<double>(ss, pair.val);
break;
default:
return false;
}
return true;
}
inline unsigned int streamReadProps(std::istream &ss, RDProps &props) {
unsigned int count;
streamRead(ss, count);
Dict &dict = props.getDict();
dict.getData().resize(count);
for(unsigned index = 0; index<count; ++index) {
CHECK_INVARIANT(streamReadProp(ss, dict.getData()[index]),
"Corrupted property serialization detected");
}
return count;
}
}
#endif
|