git repos / blockattack-game

blame: source/code/Libs/include/cereal/external/rapidjson/filewritestream.h

normal view · raw

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