Coverage Report

Created: 2025-11-11 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/src/butil/third_party/rapidjson/filewritestream.h
Line
Count
Source
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 "rapidjson.h"
19
#include <cstdio>
20
21
BUTIL_RAPIDJSON_NAMESPACE_BEGIN
22
23
//! Wrapper of C file stream for input using fread().
24
/*!
25
    \note implements Stream concept
26
*/
27
class FileWriteStream {
28
public:
29
    typedef char Ch;    //!< Character type. Only support char.
30
31
0
    FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { 
32
0
        RAPIDJSON_ASSERT(fp_ != 0);
33
0
    }
34
35
0
    void Put(char c) { 
36
0
        if (current_ >= bufferEnd_)
37
0
            Flush();
38
0
39
0
        *current_++ = c;
40
0
    }
41
42
0
    void PutN(char c, size_t n) {
43
0
        size_t avail = static_cast<size_t>(bufferEnd_ - current_);
44
0
        while (n > avail) {
45
0
            std::memset(current_, c, avail);
46
0
            current_ += avail;
47
0
            Flush();
48
0
            n -= avail;
49
0
            avail = static_cast<size_t>(bufferEnd_ - current_);
50
0
        }
51
0
52
0
        if (n > 0) {
53
0
            std::memset(current_, c, n);
54
0
            current_ += n;
55
0
        }
56
0
    }
57
58
0
    void Flush() {
59
0
        if (current_ != buffer_) {
60
0
            fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
61
0
            current_ = buffer_;
62
0
        }
63
0
    }
64
65
    // Not implemented
66
0
    char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
67
0
    char Take() { RAPIDJSON_ASSERT(false); return 0; }
68
0
    size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
69
0
    char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
70
0
    size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
71
72
private:
73
    // Prohibit copy constructor & assignment operator.
74
    FileWriteStream(const FileWriteStream&);
75
    FileWriteStream& operator=(const FileWriteStream&);
76
77
    std::FILE* fp_;
78
    char *buffer_;
79
    char *bufferEnd_;
80
    char *current_;
81
};
82
83
//! Implement specialized version of PutN() with memset() for better performance.
84
template<>
85
0
inline void PutN(FileWriteStream& stream, char c, size_t n) {
86
0
    stream.PutN(c, n);
87
0
}
88
89
BUTIL_RAPIDJSON_NAMESPACE_END
90
91
#endif // RAPIDJSON_FILESTREAM_H_