File indexing completed on 2025-02-21 10:13:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef RAPIDJSON_FILEREADSTREAM_H_
0016 #define RAPIDJSON_FILEREADSTREAM_H_
0017
0018 #include "stream.h"
0019 #include <cstdio>
0020
0021 #ifdef __clang__
0022 RAPIDJSON_DIAG_PUSH
0023 RAPIDJSON_DIAG_OFF(padded)
0024 RAPIDJSON_DIAG_OFF(unreachable-code)
0025 RAPIDJSON_DIAG_OFF(missing-noreturn)
0026 #endif
0027
0028 RAPIDJSON_NAMESPACE_BEGIN
0029
0030
0031
0032
0033
0034 class FileReadStream {
0035 public:
0036 typedef char Ch;
0037
0038
0039
0040
0041
0042
0043
0044 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) {
0045 RAPIDJSON_ASSERT(fp_ != 0);
0046 RAPIDJSON_ASSERT(bufferSize >= 4);
0047 Read();
0048 }
0049
0050 Ch Peek() const { return *current_; }
0051 Ch Take() { Ch c = *current_; Read(); return c; }
0052 size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
0053
0054
0055 void Put(Ch) { RAPIDJSON_ASSERT(false); }
0056 void Flush() { RAPIDJSON_ASSERT(false); }
0057 Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
0058 size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
0059
0060
0061 const Ch* Peek4() const {
0062 return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
0063 }
0064
0065 private:
0066 void Read() {
0067 if (current_ < bufferLast_)
0068 ++current_;
0069 else if (!eof_) {
0070 count_ += readCount_;
0071 readCount_ = std::fread(buffer_, 1, bufferSize_, fp_);
0072 bufferLast_ = buffer_ + readCount_ - 1;
0073 current_ = buffer_;
0074
0075 if (readCount_ < bufferSize_) {
0076 buffer_[readCount_] = '\0';
0077 ++bufferLast_;
0078 eof_ = true;
0079 }
0080 }
0081 }
0082
0083 std::FILE* fp_;
0084 Ch *buffer_;
0085 size_t bufferSize_;
0086 Ch *bufferLast_;
0087 Ch *current_;
0088 size_t readCount_;
0089 size_t count_;
0090 bool eof_;
0091 };
0092
0093 RAPIDJSON_NAMESPACE_END
0094
0095 #ifdef __clang__
0096 RAPIDJSON_DIAG_POP
0097 #endif
0098
0099 #endif