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


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