| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef CEREAL_RAPIDJSON_FILEREADSTREAM_H_ |
| 16 |
#define CEREAL_RAPIDJSON_FILEREADSTREAM_H_ |
| 17 |
|
| 18 |
#include "stream.h" |
| 19 |
#include <cstdio> |
| 20 |
|
| 21 |
#ifdef __clang__ |
| 22 |
CEREAL_RAPIDJSON_DIAG_PUSH |
| 23 |
CEREAL_RAPIDJSON_DIAG_OFF(padded) |
| 24 |
CEREAL_RAPIDJSON_DIAG_OFF(unreachable-code) |
| 25 |
CEREAL_RAPIDJSON_DIAG_OFF(missing-noreturn) |
| 26 |
#endif |
| 27 |
|
| 28 |
CEREAL_RAPIDJSON_NAMESPACE_BEGIN |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class FileReadStream { |
| 35 |
public: |
| 36 |
typedef char Ch; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
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) { |
| 45 |
CEREAL_RAPIDJSON_ASSERT(fp_ != 0); |
| 46 |
CEREAL_RAPIDJSON_ASSERT(bufferSize >= 4); |
| 47 |
Read(); |
| 48 |
} |
| 49 |
|
| 50 |
Ch Peek() const { return *current_; } |
| 51 |
Ch Take() { Ch c = *current_; Read(); return c; } |
| 52 |
size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); } |
| 53 |
|
| 54 |
|
| 55 |
void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); } |
| 56 |
void Flush() { CEREAL_RAPIDJSON_ASSERT(false); } |
| 57 |
Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; } |
| 58 |
size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; } |
| 59 |
|
| 60 |
|
| 61 |
const Ch* Peek4() const { |
| 62 |
return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0; |
| 63 |
} |
| 64 |
|
| 65 |
private: |
| 66 |
void Read() { |
| 67 |
if (current_ < bufferLast_) |
| 68 |
++current_; |
| 69 |
else if (!eof_) { |
| 70 |
count_ += readCount_; |
| 71 |
readCount_ = std::fread(buffer_, 1, bufferSize_, fp_); |
| 72 |
bufferLast_ = buffer_ + readCount_ - 1; |
| 73 |
current_ = buffer_; |
| 74 |
|
| 75 |
if (readCount_ < bufferSize_) { |
| 76 |
buffer_[readCount_] = '\0'; |
| 77 |
++bufferLast_; |
| 78 |
eof_ = true; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
std::FILE* fp_; |
| 84 |
Ch *buffer_; |
| 85 |
size_t bufferSize_; |
| 86 |
Ch *bufferLast_; |
| 87 |
Ch *current_; |
| 88 |
size_t readCount_; |
| 89 |
size_t count_; |
| 90 |
bool eof_; |
| 91 |
}; |
| 92 |
|
| 93 |
CEREAL_RAPIDJSON_NAMESPACE_END |
| 94 |
|
| 95 |
#ifdef __clang__ |
| 96 |
CEREAL_RAPIDJSON_DIAG_POP |
| 97 |
#endif |
| 98 |
|
| 99 |
#endif |