filewritestream.h
Engine/source/persistence/rapidjson/filewritestream.h
Classes:
class
Wrapper of C file stream for output using fwrite().
Public Functions
PutN(FileWriteStream & stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Detailed Description
Public Functions
PutN(FileWriteStream & stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
1 2// Tencent is pleased to support the open source community by making RapidJSON available. 3// 4// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 5// 6// Licensed under the MIT License (the "License"); you may not use this file except 7// in compliance with the License. You may obtain a copy of the License at 8// 9// http://opensource.org/licenses/MIT 10// 11// Unless required by applicable law or agreed to in writing, software distributed 12// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13// CONDITIONS OF ANY KIND, either express or implied. See the License for the 14// specific language governing permissions and limitations under the License. 15 16#ifndef RAPIDJSON_FILEWRITESTREAM_H_ 17#define RAPIDJSON_FILEWRITESTREAM_H_ 18 19#include "stream.h" 20#include <cstdio> 21 22#ifdef __clang__ 23RAPIDJSON_DIAG_PUSH 24RAPIDJSON_DIAG_OFF(unreachable-code) 25#endif 26 27RAPIDJSON_NAMESPACE_BEGIN 28 29//! Wrapper of C file stream for output using fwrite(). 30/*! 31 \note implements Stream concept 32*/ 33class FileWriteStream { 34public: 35 typedef char Ch; //!< Character type. Only support char. 36 37 FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { 38 RAPIDJSON_ASSERT(fp_ != 0); 39 } 40 41 void Put(char c) { 42 if (current_ >= bufferEnd_) 43 Flush(); 44 45 *current_++ = c; 46 } 47 48 void PutN(char c, size_t n) { 49 size_t avail = static_cast<size_t>(bufferEnd_ - current_); 50 while (n > avail) { 51 std::memset(current_, c, avail); 52 current_ += avail; 53 Flush(); 54 n -= avail; 55 avail = static_cast<size_t>(bufferEnd_ - current_); 56 } 57 58 if (n > 0) { 59 std::memset(current_, c, n); 60 current_ += n; 61 } 62 } 63 64 void Flush() { 65 if (current_ != buffer_) { 66 size_t result = std::fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_); 67 if (result < static_cast<size_t>(current_ - buffer_)) { 68 // failure deliberately ignored at this time 69 // added to avoid warn_unused_result build errors 70 } 71 current_ = buffer_; 72 } 73 } 74 75 // Not implemented 76 char Peek() const { RAPIDJSON_ASSERT(false); return 0; } 77 char Take() { RAPIDJSON_ASSERT(false); return 0; } 78 size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } 79 char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 80 size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } 81 82private: 83 // Prohibit copy constructor & assignment operator. 84 FileWriteStream(const FileWriteStream&); 85 FileWriteStream& operator=(const FileWriteStream&); 86 87 std::FILE* fp_; 88 char *buffer_; 89 char *bufferEnd_; 90 char *current_; 91}; 92 93//! Implement specialized version of PutN() with memset() for better performance. 94template<> 95inline void PutN(FileWriteStream& stream, char c, size_t n) { 96 stream.PutN(c, n); 97} 98 99RAPIDJSON_NAMESPACE_END 100 101#ifdef __clang__ 102RAPIDJSON_DIAG_POP 103#endif 104 105#endif // RAPIDJSON_FILESTREAM_H_ 106