filewritestream.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_FILEWRITESTREAM_H_
20 #define RAPIDJSON_FILEWRITESTREAM_H_
21 
22 #include <cstdio>
23 #include "stream.h"
24 
25 #ifdef __clang__
26 RAPIDJSON_DIAG_PUSH
27 RAPIDJSON_DIAG_OFF(unreachable - code)
28 #endif
29 
31 
33 
37  public:
38  typedef char Ch;
39 
40  FileWriteStream(std::FILE *fp, char *buffer, size_t bufferSize)
41  : fp_(fp),
42  buffer_(buffer),
43  bufferEnd_(buffer + bufferSize),
44  current_(buffer_) {
45  RAPIDJSON_ASSERT(fp_ != 0);
46  }
47 
48  void Put(char c) {
49  if (current_ >= bufferEnd_) Flush();
50 
51  *current_++ = c;
52  }
53 
54  void PutN(char c, size_t n) {
55  size_t avail = static_cast<size_t>(bufferEnd_ - current_);
56  while (n > avail) {
57  std::memset(current_, c, avail);
58  current_ += avail;
59  Flush();
60  n -= avail;
61  avail = static_cast<size_t>(bufferEnd_ - current_);
62  }
63 
64  if (n > 0) {
65  std::memset(current_, c, n);
66  current_ += n;
67  }
68  }
69 
70  void Flush() {
71  if (current_ != buffer_) {
72  size_t result =
73  std::fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
74  if (result < static_cast<size_t>(current_ - buffer_)) {
75  // failure deliberately ignored at this time
76  // added to avoid warn_unused_result build errors
77  }
78  current_ = buffer_;
79  }
80  }
81 
82  // Not implemented
83  char Peek() const {
84  RAPIDJSON_ASSERT(false);
85  return 0;
86  }
87  char Take() {
88  RAPIDJSON_ASSERT(false);
89  return 0;
90  }
91  size_t Tell() const {
92  RAPIDJSON_ASSERT(false);
93  return 0;
94  }
95  char *PutBegin() {
96  RAPIDJSON_ASSERT(false);
97  return 0;
98  }
99  size_t PutEnd(char *) {
100  RAPIDJSON_ASSERT(false);
101  return 0;
102  }
103 
104  private:
105  // Prohibit copy constructor & assignment operator.
108 
109  std::FILE *fp_;
110  char *buffer_;
111  char *bufferEnd_;
112  char *current_;
113 };
114 
117 template <>
118 inline void PutN(FileWriteStream &stream, char c, size_t n) {
119  stream.PutN(c, n);
120 }
121 
123 
124 #ifdef __clang__
125 RAPIDJSON_DIAG_POP
126 #endif
127 
128 #endif // RAPIDJSON_FILESTREAM_H_
size_t PutEnd(char *)
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:433
void PutN(char c, size_t n)
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:131
Wrapper of C file stream for output using fwrite().
char Ch
Character type. Only support char.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:128
FileWriteStream & operator=(const FileWriteStream &)
void Put(char c)
FileWriteStream(std::FILE *fp, char *buffer, size_t bufferSize)
char Peek() const
size_t Tell() const


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