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. All rights reserved.
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 #if __clang__
22 RAPIDJSON_DIAG_PUSH
23 RAPIDJSON_DIAG_OFF(padded)
24 RAPIDJSON_DIAG_OFF(unreachable - code)
25 RAPIDJSON_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
61  {
62  return *current_;
63  }
64  Ch Take()
65  {
66  Ch c = *current_;
67  Read();
68  return c;
69  }
70  size_t Tell() const
71  {
72  return count_ + static_cast<size_t>(current_ - buffer_);
73  }
74 
75  // Not implemented
76  void Put(Ch)
77  {
78  RAPIDJSON_ASSERT(false);
79  }
80  void Flush()
81  {
82  RAPIDJSON_ASSERT(false);
83  }
84  Ch* PutBegin()
85  {
86  RAPIDJSON_ASSERT(false);
87  return 0;
88  }
89  size_t PutEnd(Ch*)
90  {
91  RAPIDJSON_ASSERT(false);
92  return 0;
93  }
94 
95  // For encoding detection only.
96  const Ch* Peek4() const
97  {
98  return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
99  }
100 
101 private:
102  void Read()
103  {
104  if (current_ < bufferLast_)
105  ++current_;
106  else if (!eof_)
107  {
108  count_ += readCount_;
109  readCount_ = std::fread(buffer_, 1, bufferSize_, fp_);
111  current_ = buffer_;
112 
113  if (readCount_ < bufferSize_)
114  {
115  buffer_[readCount_] = '\0';
116  ++bufferLast_;
117  eof_ = true;
118  }
119  }
120  }
121 
122  std::FILE* fp_;
123  Ch* buffer_;
124  size_t bufferSize_;
126  Ch* current_;
127  size_t readCount_;
128  size_t count_;
129  bool eof_;
130 };
131 
133 
134 #if __clang__
135 RAPIDJSON_DIAG_POP
136 #endif
137 
138 #endif // RAPIDJSON_FILESTREAM_H_
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:416
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
size_t count_
Number of characters read.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
FileReadStream(std::FILE *fp, char *buffer, size_t bufferSize)
Constructor.
Ch Peek() const
char Ch
Character type (byte).
std::FILE * fp_
size_t PutEnd(Ch *)
File byte stream for input using fread().
size_t Tell() const
const Ch * Peek4() const


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53