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


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46