| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef CEREAL_RAPIDJSON_ISTREAMWRAPPER_H_ |
| 16 |
#define CEREAL_RAPIDJSON_ISTREAMWRAPPER_H_ |
| 17 |
|
| 18 |
#include "stream.h" |
| 19 |
#include <iosfwd> |
| 20 |
#include <ios> |
| 21 |
|
| 22 |
#ifdef __clang__ |
| 23 |
CEREAL_RAPIDJSON_DIAG_PUSH |
| 24 |
CEREAL_RAPIDJSON_DIAG_OFF(padded) |
| 25 |
#elif defined(_MSC_VER) |
| 26 |
CEREAL_RAPIDJSON_DIAG_PUSH |
| 27 |
CEREAL_RAPIDJSON_DIAG_OFF(4351) |
| 28 |
#endif |
| 29 |
|
| 30 |
CEREAL_RAPIDJSON_NAMESPACE_BEGIN |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
template <typename StreamType> |
| 49 |
class BasicIStreamWrapper { |
| 50 |
public: |
| 51 |
typedef typename StreamType::char_type Ch; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { |
| 58 |
Read(); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { |
| 68 |
CEREAL_RAPIDJSON_ASSERT(bufferSize >= 4); |
| 69 |
Read(); |
| 70 |
} |
| 71 |
|
| 72 |
Ch Peek() const { return *current_; } |
| 73 |
Ch Take() { Ch c = *current_; Read(); return c; } |
| 74 |
size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); } |
| 75 |
|
| 76 |
|
| 77 |
void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); } |
| 78 |
void Flush() { CEREAL_RAPIDJSON_ASSERT(false); } |
| 79 |
Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; } |
| 80 |
size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; } |
| 81 |
|
| 82 |
|
| 83 |
const Ch* Peek4() const { |
| 84 |
return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0; |
| 85 |
} |
| 86 |
|
| 87 |
private: |
| 88 |
BasicIStreamWrapper(); |
| 89 |
BasicIStreamWrapper(const BasicIStreamWrapper&); |
| 90 |
BasicIStreamWrapper& operator=(const BasicIStreamWrapper&); |
| 91 |
|
| 92 |
void Read() { |
| 93 |
if (current_ < bufferLast_) |
| 94 |
++current_; |
| 95 |
else if (!eof_) { |
| 96 |
count_ += readCount_; |
| 97 |
readCount_ = bufferSize_; |
| 98 |
bufferLast_ = buffer_ + readCount_ - 1; |
| 99 |
current_ = buffer_; |
| 100 |
|
| 101 |
if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_))) { |
| 102 |
readCount_ = static_cast<size_t>(stream_.gcount()); |
| 103 |
*(bufferLast_ = buffer_ + readCount_) = '\0'; |
| 104 |
eof_ = true; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
StreamType &stream_; |
| 110 |
Ch peekBuffer_[4], *buffer_; |
| 111 |
size_t bufferSize_; |
| 112 |
Ch *bufferLast_; |
| 113 |
Ch *current_; |
| 114 |
size_t readCount_; |
| 115 |
size_t count_; |
| 116 |
bool eof_; |
| 117 |
}; |
| 118 |
|
| 119 |
typedef BasicIStreamWrapper<std::istream> IStreamWrapper; |
| 120 |
typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper; |
| 121 |
|
| 122 |
#if defined(__clang__) || defined(_MSC_VER) |
| 123 |
CEREAL_RAPIDJSON_DIAG_POP |
| 124 |
#endif |
| 125 |
|
| 126 |
CEREAL_RAPIDJSON_NAMESPACE_END |
| 127 |
|
| 128 |
#endif |