git repos / blockattack-game

blame: source/code/Libs/include/cereal/external/rapidjson/filereadstream.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_FILEREADSTREAM_H_
6c5f2c01 sago007 2017-03-15 17:56 16
#define CEREAL_RAPIDJSON_FILEREADSTREAM_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(padded)
6c5f2c01 sago007 2017-03-15 17:56 24
CEREAL_RAPIDJSON_DIAG_OFF(unreachable-code)
6c5f2c01 sago007 2017-03-15 17:56 25
CEREAL_RAPIDJSON_DIAG_OFF(missing-noreturn)
6c5f2c01 sago007 2017-03-15 17:56 26
#endif
6c5f2c01 sago007 2017-03-15 17:56 27
6c5f2c01 sago007 2017-03-15 17:56 28
CEREAL_RAPIDJSON_NAMESPACE_BEGIN
6c5f2c01 sago007 2017-03-15 17:56 29
6c5f2c01 sago007 2017-03-15 17:56 30
//! File byte stream for input using fread().
6c5f2c01 sago007 2017-03-15 17:56 31
/*!
6c5f2c01 sago007 2017-03-15 17:56 32
    \note implements Stream concept
6c5f2c01 sago007 2017-03-15 17:56 33
*/
6c5f2c01 sago007 2017-03-15 17:56 34
class FileReadStream {
6c5f2c01 sago007 2017-03-15 17:56 35
public:
6c5f2c01 sago007 2017-03-15 17:56 36
    typedef char Ch;    //!< Character type (byte).
6c5f2c01 sago007 2017-03-15 17:56 37
6c5f2c01 sago007 2017-03-15 17:56 38
    //! Constructor.
6c5f2c01 sago007 2017-03-15 17:56 39
    /*!
6c5f2c01 sago007 2017-03-15 17:56 40
        \param fp File pointer opened for read.
6c5f2c01 sago007 2017-03-15 17:56 41
        \param buffer user-supplied buffer.
6c5f2c01 sago007 2017-03-15 17:56 42
        \param bufferSize size of buffer in bytes. Must >=4 bytes.
6c5f2c01 sago007 2017-03-15 17:56 43
    */
6c5f2c01 sago007 2017-03-15 17:56 44
    FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { 
6c5f2c01 sago007 2017-03-15 17:56 45
        CEREAL_RAPIDJSON_ASSERT(fp_ != 0);
6c5f2c01 sago007 2017-03-15 17:56 46
        CEREAL_RAPIDJSON_ASSERT(bufferSize >= 4);
6c5f2c01 sago007 2017-03-15 17:56 47
        Read();
6c5f2c01 sago007 2017-03-15 17:56 48
    }
6c5f2c01 sago007 2017-03-15 17:56 49
6c5f2c01 sago007 2017-03-15 17:56 50
    Ch Peek() const { return *current_; }
6c5f2c01 sago007 2017-03-15 17:56 51
    Ch Take() { Ch c = *current_; Read(); return c; }
6c5f2c01 sago007 2017-03-15 17:56 52
    size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
6c5f2c01 sago007 2017-03-15 17:56 53
6c5f2c01 sago007 2017-03-15 17:56 54
    // Not implemented
6c5f2c01 sago007 2017-03-15 17:56 55
    void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); }
6c5f2c01 sago007 2017-03-15 17:56 56
    void Flush() { CEREAL_RAPIDJSON_ASSERT(false); } 
6c5f2c01 sago007 2017-03-15 17:56 57
    Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 58
    size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
6c5f2c01 sago007 2017-03-15 17:56 59
6c5f2c01 sago007 2017-03-15 17:56 60
    // For encoding detection only.
6c5f2c01 sago007 2017-03-15 17:56 61
    const Ch* Peek4() const {
6c5f2c01 sago007 2017-03-15 17:56 62
        return (current_ + 4 <= bufferLast_) ? current_ : 0;
6c5f2c01 sago007 2017-03-15 17:56 63
    }
6c5f2c01 sago007 2017-03-15 17:56 64
6c5f2c01 sago007 2017-03-15 17:56 65
private:
6c5f2c01 sago007 2017-03-15 17:56 66
    void Read() {
6c5f2c01 sago007 2017-03-15 17:56 67
        if (current_ < bufferLast_)
6c5f2c01 sago007 2017-03-15 17:56 68
            ++current_;
6c5f2c01 sago007 2017-03-15 17:56 69
        else if (!eof_) {
6c5f2c01 sago007 2017-03-15 17:56 70
            count_ += readCount_;
6c5f2c01 sago007 2017-03-15 17:56 71
            readCount_ = fread(buffer_, 1, bufferSize_, fp_);
6c5f2c01 sago007 2017-03-15 17:56 72
            bufferLast_ = buffer_ + readCount_ - 1;
6c5f2c01 sago007 2017-03-15 17:56 73
            current_ = buffer_;
6c5f2c01 sago007 2017-03-15 17:56 74
6c5f2c01 sago007 2017-03-15 17:56 75
            if (readCount_ < bufferSize_) {
6c5f2c01 sago007 2017-03-15 17:56 76
                buffer_[readCount_] = '\0';
6c5f2c01 sago007 2017-03-15 17:56 77
                ++bufferLast_;
6c5f2c01 sago007 2017-03-15 17:56 78
                eof_ = true;
6c5f2c01 sago007 2017-03-15 17:56 79
            }
6c5f2c01 sago007 2017-03-15 17:56 80
        }
6c5f2c01 sago007 2017-03-15 17:56 81
    }
6c5f2c01 sago007 2017-03-15 17:56 82
6c5f2c01 sago007 2017-03-15 17:56 83
    std::FILE* fp_;
6c5f2c01 sago007 2017-03-15 17:56 84
    Ch *buffer_;
6c5f2c01 sago007 2017-03-15 17:56 85
    size_t bufferSize_;
6c5f2c01 sago007 2017-03-15 17:56 86
    Ch *bufferLast_;
6c5f2c01 sago007 2017-03-15 17:56 87
    Ch *current_;
6c5f2c01 sago007 2017-03-15 17:56 88
    size_t readCount_;
6c5f2c01 sago007 2017-03-15 17:56 89
    size_t count_;  //!< Number of characters read
6c5f2c01 sago007 2017-03-15 17:56 90
    bool eof_;
6c5f2c01 sago007 2017-03-15 17:56 91
};
6c5f2c01 sago007 2017-03-15 17:56 92
6c5f2c01 sago007 2017-03-15 17:56 93
CEREAL_RAPIDJSON_NAMESPACE_END
6c5f2c01 sago007 2017-03-15 17:56 94
6c5f2c01 sago007 2017-03-15 17:56 95
#ifdef __clang__
6c5f2c01 sago007 2017-03-15 17:56 96
CEREAL_RAPIDJSON_DIAG_POP
6c5f2c01 sago007 2017-03-15 17:56 97
#endif
6c5f2c01 sago007 2017-03-15 17:56 98
6c5f2c01 sago007 2017-03-15 17:56 99
#endif // CEREAL_RAPIDJSON_FILESTREAM_H_
1970-01-01 00:00 100