filereadstream.h Source File

filereadstream.h Source File#

Composable Kernel: filereadstream.h Source File
filereadstream.h
Go to the documentation of this file.
1// Tencent is pleased to support the open source community by making RapidJSON available.
2//
3// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
4//
5// Licensed under the MIT License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// http://opensource.org/licenses/MIT
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#ifndef RAPIDJSON_FILEREADSTREAM_H_
16#define RAPIDJSON_FILEREADSTREAM_H_
17
18#include "stream.h"
19#include <cstdio>
20
21#ifdef __clang__
22RAPIDJSON_DIAG_PUSH
23RAPIDJSON_DIAG_OFF(padded)
24RAPIDJSON_DIAG_OFF(unreachable - code)
25RAPIDJSON_DIAG_OFF(missing - noreturn)
26#endif
27
29
31
35{
36 public:
37 typedef char Ch;
38
40
45 FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize)
46 : fp_(fp),
47 buffer_(buffer),
48 bufferSize_(bufferSize),
49 bufferLast_(0),
50 current_(buffer_),
51 readCount_(0),
52 count_(0),
53 eof_(false)
54 {
55 RAPIDJSON_ASSERT(fp_ != 0);
56 RAPIDJSON_ASSERT(bufferSize >= 4);
57 Read();
58 }
59
60 Ch Peek() const { return *current_; }
62 {
63 Ch c = *current_;
64 Read();
65 return c;
66 }
67 size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
68
69 // Not implemented
70 void Put(Ch) { RAPIDJSON_ASSERT(false); }
71 void Flush() { RAPIDJSON_ASSERT(false); }
73 {
74 RAPIDJSON_ASSERT(false);
75 return 0;
76 }
77 size_t PutEnd(Ch*)
78 {
79 RAPIDJSON_ASSERT(false);
80 return 0;
81 }
82
83 // For encoding detection only.
84 const Ch* Peek4() const { return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0; }
85
86 private:
87 void Read()
88 {
89 if(current_ < bufferLast_)
90 ++current_;
91 else if(!eof_)
92 {
93 count_ += readCount_;
94 readCount_ = std::fread(buffer_, 1, bufferSize_, fp_);
95 bufferLast_ = buffer_ + readCount_ - 1;
96 current_ = buffer_;
97
98 if(readCount_ < bufferSize_)
99 {
100 buffer_[readCount_] = '\0';
101 ++bufferLast_;
102 eof_ = true;
103 }
104 }
105 }
106
107 std::FILE* fp_;
108 Ch* buffer_;
109 size_t bufferSize_;
110 Ch* bufferLast_;
111 Ch* current_;
112 size_t readCount_;
113 size_t count_;
114 bool eof_;
115};
116
118
119#ifdef __clang__
120RAPIDJSON_DIAG_POP
121#endif
122
123#endif // RAPIDJSON_FILESTREAM_H_
const Ch * Peek4() const
Definition filereadstream.h:84
Ch * PutBegin()
Definition filereadstream.h:72
void Put(Ch)
Definition filereadstream.h:70
size_t PutEnd(Ch *)
Definition filereadstream.h:77
Ch Peek() const
Definition filereadstream.h:60
void Flush()
Definition filereadstream.h:71
Ch Take()
Definition filereadstream.h:61
FileReadStream(std::FILE *fp, char *buffer, size_t bufferSize)
Constructor.
Definition filereadstream.h:45
char Ch
Character type (byte).
Definition filereadstream.h:37
size_t Tell() const
Definition filereadstream.h:67
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:451
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:124