Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:13:01

0001 // Tencent is pleased to support the open source community by making RapidJSON available.
0002 // 
0003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
0004 //
0005 // Licensed under the MIT License (the "License"); you may not use this file except
0006 // in compliance with the License. You may obtain a copy of the License at
0007 //
0008 // http://opensource.org/licenses/MIT
0009 //
0010 // Unless required by applicable law or agreed to in writing, software distributed 
0011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
0012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
0013 // specific language governing permissions and limitations under the License.
0014 
0015 #ifndef RAPIDJSON_FILEWRITESTREAM_H_
0016 #define RAPIDJSON_FILEWRITESTREAM_H_
0017 
0018 #include "stream.h"
0019 #include <cstdio>
0020 
0021 #ifdef __clang__
0022 RAPIDJSON_DIAG_PUSH
0023 RAPIDJSON_DIAG_OFF(unreachable-code)
0024 #endif
0025 
0026 RAPIDJSON_NAMESPACE_BEGIN
0027 
0028 //! Wrapper of C file stream for output using fwrite().
0029 /*!
0030     \note implements Stream concept
0031 */
0032 class FileWriteStream {
0033 public:
0034     typedef char Ch;    //!< Character type. Only support char.
0035 
0036     FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { 
0037         RAPIDJSON_ASSERT(fp_ != 0);
0038     }
0039 
0040     void Put(char c) { 
0041         if (current_ >= bufferEnd_)
0042             Flush();
0043 
0044         *current_++ = c;
0045     }
0046 
0047     void PutN(char c, size_t n) {
0048         size_t avail = static_cast<size_t>(bufferEnd_ - current_);
0049         while (n > avail) {
0050             std::memset(current_, c, avail);
0051             current_ += avail;
0052             Flush();
0053             n -= avail;
0054             avail = static_cast<size_t>(bufferEnd_ - current_);
0055         }
0056 
0057         if (n > 0) {
0058             std::memset(current_, c, n);
0059             current_ += n;
0060         }
0061     }
0062 
0063     void Flush() {
0064         if (current_ != buffer_) {
0065             size_t result = std::fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
0066             if (result < static_cast<size_t>(current_ - buffer_)) {
0067                 // failure deliberately ignored at this time
0068                 // added to avoid warn_unused_result build errors
0069             }
0070             current_ = buffer_;
0071         }
0072     }
0073 
0074     // Not implemented
0075     char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
0076     char Take() { RAPIDJSON_ASSERT(false); return 0; }
0077     size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
0078     char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
0079     size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
0080 
0081 private:
0082     // Prohibit copy constructor & assignment operator.
0083     FileWriteStream(const FileWriteStream&);
0084     FileWriteStream& operator=(const FileWriteStream&);
0085 
0086     std::FILE* fp_;
0087     char *buffer_;
0088     char *bufferEnd_;
0089     char *current_;
0090 };
0091 
0092 //! Implement specialized version of PutN() with memset() for better performance.
0093 template<>
0094 inline void PutN(FileWriteStream& stream, char c, size_t n) {
0095     stream.PutN(c, n);
0096 }
0097 
0098 RAPIDJSON_NAMESPACE_END
0099 
0100 #ifdef __clang__
0101 RAPIDJSON_DIAG_POP
0102 #endif
0103 
0104 #endif // RAPIDJSON_FILESTREAM_H_