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 #ifdef __clang__
22 RAPIDJSON_DIAG_PUSH
23 RAPIDJSON_DIAG_OFF(unreachable-code)
24 #endif
25 
27 
29 
33 public:
34  typedef char Ch;
35 
36  FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
37  RAPIDJSON_ASSERT(fp_ != 0);
38  }
39 
40  void Put(char c) {
41  if (current_ >= bufferEnd_)
42  Flush();
43 
44  *current_++ = c;
45  }
46 
47  void PutN(char c, size_t n) {
48  size_t avail = static_cast<size_t>(bufferEnd_ - current_);
49  while (n > avail) {
50  std::memset(current_, c, avail);
51  current_ += avail;
52  Flush();
53  n -= avail;
54  avail = static_cast<size_t>(bufferEnd_ - current_);
55  }
56 
57  if (n > 0) {
58  std::memset(current_, c, n);
59  current_ += n;
60  }
61  }
62 
63  void Flush() {
64  if (current_ != buffer_) {
65  size_t result = fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
66  if (result < static_cast<size_t>(current_ - buffer_)) {
67  // failure deliberately ignored at this time
68  // added to avoid warn_unused_result build errors
69  }
70  current_ = buffer_;
71  }
72  }
73 
74  // Not implemented
75  char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
76  char Take() { RAPIDJSON_ASSERT(false); return 0; }
77  size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
78  char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
79  size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
80 
81 private:
82  // Prohibit copy constructor & assignment operator.
85 
86  std::FILE* fp_;
87  char *buffer_;
88  char *bufferEnd_;
89  char *current_;
90 };
91 
93 template<>
94 inline void PutN(FileWriteStream& stream, char c, size_t n) {
95  stream.PutN(c, n);
96 }
97 
99 
100 #ifdef __clang__
101 RAPIDJSON_DIAG_POP
102 #endif
103 
104 #endif // RAPIDJSON_FILESTREAM_H_
size_t PutEnd(char *)
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:411
void PutN(char c, size_t n)
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
Wrapper of C file stream for input using fread().
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
std::FILE * fp_


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:09