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_FILEREADSTREAM_H_
0016 #define RAPIDJSON_FILEREADSTREAM_H_
0017 
0018 #include "stream.h"
0019 #include <cstdio>
0020 
0021 #ifdef __clang__
0022 RAPIDJSON_DIAG_PUSH
0023 RAPIDJSON_DIAG_OFF(padded)
0024 RAPIDJSON_DIAG_OFF(unreachable-code)
0025 RAPIDJSON_DIAG_OFF(missing-noreturn)
0026 #endif
0027 
0028 RAPIDJSON_NAMESPACE_BEGIN
0029 
0030 //! File byte stream for input using fread().
0031 /*!
0032     \note implements Stream concept
0033 */
0034 class FileReadStream {
0035 public:
0036     typedef char Ch;    //!< Character type (byte).
0037 
0038     //! Constructor.
0039     /*!
0040         \param fp File pointer opened for read.
0041         \param buffer user-supplied buffer.
0042         \param bufferSize size of buffer in bytes. Must >=4 bytes.
0043     */
0044     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) { 
0045         RAPIDJSON_ASSERT(fp_ != 0);
0046         RAPIDJSON_ASSERT(bufferSize >= 4);
0047         Read();
0048     }
0049 
0050     Ch Peek() const { return *current_; }
0051     Ch Take() { Ch c = *current_; Read(); return c; }
0052     size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
0053 
0054     // Not implemented
0055     void Put(Ch) { RAPIDJSON_ASSERT(false); }
0056     void Flush() { RAPIDJSON_ASSERT(false); } 
0057     Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
0058     size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
0059 
0060     // For encoding detection only.
0061     const Ch* Peek4() const {
0062         return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
0063     }
0064 
0065 private:
0066     void Read() {
0067         if (current_ < bufferLast_)
0068             ++current_;
0069         else if (!eof_) {
0070             count_ += readCount_;
0071             readCount_ = std::fread(buffer_, 1, bufferSize_, fp_);
0072             bufferLast_ = buffer_ + readCount_ - 1;
0073             current_ = buffer_;
0074 
0075             if (readCount_ < bufferSize_) {
0076                 buffer_[readCount_] = '\0';
0077                 ++bufferLast_;
0078                 eof_ = true;
0079             }
0080         }
0081     }
0082 
0083     std::FILE* fp_;
0084     Ch *buffer_;
0085     size_t bufferSize_;
0086     Ch *bufferLast_;
0087     Ch *current_;
0088     size_t readCount_;
0089     size_t count_;  //!< Number of characters read
0090     bool eof_;
0091 };
0092 
0093 RAPIDJSON_NAMESPACE_END
0094 
0095 #ifdef __clang__
0096 RAPIDJSON_DIAG_POP
0097 #endif
0098 
0099 #endif // RAPIDJSON_FILESTREAM_H_