filewritestream.h
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 
26 RAPIDJSON_NAMESPACE_BEGIN
27 
28 //! Wrapper of C file stream for input using fread().
29 /*!
30  \note implements Stream concept
31 */
33 public:
34  typedef char Ch; //!< Character type. Only support char.
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.
83  FileWriteStream(const FileWriteStream&);
84  FileWriteStream& operator=(const FileWriteStream&);
85 
86  std::FILE* fp_;
87  char *buffer_;
88  char *bufferEnd_;
89  char *current_;
90 };
91 
92 //! Implement specialized version of PutN() with memset() for better performance.
93 template<>
94 inline void PutN(FileWriteStream& stream, char c, size_t n) {
95  stream.PutN(c, n);
96 }
97 
98 RAPIDJSON_NAMESPACE_END
99 
100 #ifdef __clang__
101 RAPIDJSON_DIAG_POP
102 #endif
103 
104 #endif // RAPIDJSON_FILESTREAM_H_
rapidjson::FileWriteStream
Wrapper of C file stream for input using fread().
Definition: filewritestream.h:32
RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402
rapidjson::FileWriteStream::Ch
char Ch
Character type. Only support char.
Definition: filewritestream.h:34
rapidjson::PutN
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:94