1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
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
8 // http://opensource.org/licenses/MIT
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.
15 #ifndef RAPIDJSON_FILEWRITESTREAM_H_
16 #define RAPIDJSON_FILEWRITESTREAM_H_
23 RAPIDJSON_DIAG_OFF(unreachable-code)
26 RAPIDJSON_NAMESPACE_BEGIN
28 //! Wrapper of C file stream for input using fread().
30 \note implements Stream concept
32 class FileWriteStream {
34 typedef char Ch; //!< Character type. Only support char.
36 FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
37 RAPIDJSON_ASSERT(fp_ != 0);
41 if (current_ >= bufferEnd_)
47 void PutN(char c, size_t n) {
48 size_t avail = static_cast<size_t>(bufferEnd_ - current_);
50 std::memset(current_, c, avail);
54 avail = static_cast<size_t>(bufferEnd_ - current_);
58 std::memset(current_, c, n);
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
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; }
82 // Prohibit copy constructor & assignment operator.
83 FileWriteStream(const FileWriteStream&);
84 FileWriteStream& operator=(const FileWriteStream&);
92 //! Implement specialized version of PutN() with memset() for better performance.
94 inline void PutN(FileWriteStream& stream, char c, size_t n) {
98 RAPIDJSON_NAMESPACE_END
104 #endif // RAPIDJSON_FILESTREAM_H_