123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #ifndef RAPIDJSON_ISTREAMWRAPPER_H_
- #define RAPIDJSON_ISTREAMWRAPPER_H_
- #include "stream.h"
- #include <iosfwd>
- #include <ios>
- #ifdef __clang__
- RAPIDJSON_DIAG_PUSH
- RAPIDJSON_DIAG_OFF(padded)
- #elif defined(_MSC_VER)
- RAPIDJSON_DIAG_PUSH
- RAPIDJSON_DIAG_OFF(4351)
- #endif
- RAPIDJSON_NAMESPACE_BEGIN
-
- template <typename StreamType>
- class BasicIStreamWrapper {
- public:
- typedef typename StreamType::char_type Ch;
-
-
- BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
- Read();
- }
-
-
- BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
- RAPIDJSON_ASSERT(bufferSize >= 4);
- Read();
- }
- Ch Peek() const { return *current_; }
- Ch Take() { Ch c = *current_; Read(); return c; }
- size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
-
- void Put(Ch) { RAPIDJSON_ASSERT(false); }
- void Flush() { RAPIDJSON_ASSERT(false); }
- Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
- size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
-
- const Ch* Peek4() const {
- return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
- }
- private:
- BasicIStreamWrapper();
- BasicIStreamWrapper(const BasicIStreamWrapper&);
- BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
- void Read() {
- if (current_ < bufferLast_)
- ++current_;
- else if (!eof_) {
- count_ += readCount_;
- readCount_ = bufferSize_;
- bufferLast_ = buffer_ + readCount_ - 1;
- current_ = buffer_;
- if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_))) {
- readCount_ = static_cast<size_t>(stream_.gcount());
- *(bufferLast_ = buffer_ + readCount_) = '\0';
- eof_ = true;
- }
- }
- }
- StreamType &stream_;
- Ch peekBuffer_[4], *buffer_;
- size_t bufferSize_;
- Ch *bufferLast_;
- Ch *current_;
- size_t readCount_;
- size_t count_;
- bool eof_;
- };
- typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
- typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
- #if defined(__clang__) || defined(_MSC_VER)
- RAPIDJSON_DIAG_POP
- #endif
- RAPIDJSON_NAMESPACE_END
- #endif
|