filereadstream.h
Engine/source/persistence/rapidjson/filereadstream.h
Classes:
class
File byte stream for input using fread().
Detailed Description
1 2// Tencent is pleased to support the open source community by making RapidJSON available. 3// 4// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 5// 6// Licensed under the MIT License (the "License"); you may not use this file except 7// in compliance with the License. You may obtain a copy of the License at 8// 9// http://opensource.org/licenses/MIT 10// 11// Unless required by applicable law or agreed to in writing, software distributed 12// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13// CONDITIONS OF ANY KIND, either express or implied. See the License for the 14// specific language governing permissions and limitations under the License. 15 16#ifndef RAPIDJSON_FILEREADSTREAM_H_ 17#define RAPIDJSON_FILEREADSTREAM_H_ 18 19#include "stream.h" 20#include <cstdio> 21 22#ifdef __clang__ 23RAPIDJSON_DIAG_PUSH 24RAPIDJSON_DIAG_OFF(padded) 25RAPIDJSON_DIAG_OFF(unreachable-code) 26RAPIDJSON_DIAG_OFF(missing-noreturn) 27#endif 28 29RAPIDJSON_NAMESPACE_BEGIN 30 31//! File byte stream for input using fread(). 32/*! 33 \note implements Stream concept 34*/ 35class FileReadStream { 36public: 37 typedef char Ch; //!< Character type (byte). 38 39 //! Constructor. 40 /*! 41 \param fp File pointer opened for read. 42 \param buffer user-supplied buffer. 43 \param bufferSize size of buffer in bytes. Must >=4 bytes. 44 */ 45 FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { 46 RAPIDJSON_ASSERT(fp_ != 0); 47 RAPIDJSON_ASSERT(bufferSize >= 4); 48 Read(); 49 } 50 51 Ch Peek() const { return *current_; } 52 Ch Take() { Ch c = *current_; Read(); return c; } 53 size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); } 54 55 // Not implemented 56 void Put(Ch) { RAPIDJSON_ASSERT(false); } 57 void Flush() { RAPIDJSON_ASSERT(false); } 58 Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 59 size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 60 61 // For encoding detection only. 62 const Ch* Peek4() const { 63 return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0; 64 } 65 66private: 67 void Read() { 68 if (current_ < bufferLast_) 69 ++current_; 70 else if (!eof_) { 71 count_ += readCount_; 72 readCount_ = std::fread(buffer_, 1, bufferSize_, fp_); 73 bufferLast_ = buffer_ + readCount_ - 1; 74 current_ = buffer_; 75 76 if (readCount_ < bufferSize_) { 77 buffer_[readCount_] = '\0'; 78 ++bufferLast_; 79 eof_ = true; 80 } 81 } 82 } 83 84 std::FILE* fp_; 85 Ch *buffer_; 86 size_t bufferSize_; 87 Ch *bufferLast_; 88 Ch *current_; 89 size_t readCount_; 90 size_t count_; //!< Number of characters read 91 bool eof_; 92}; 93 94RAPIDJSON_NAMESPACE_END 95 96#ifdef __clang__ 97RAPIDJSON_DIAG_POP 98#endif 99 100#endif // RAPIDJSON_FILESTREAM_H_ 101