Maxim Integrated / Mbed OS MAXREFDES155#

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers filereadstream.h Source File

filereadstream.h

00001 // Tencent is pleased to support the open source community by making RapidJSON available.
00002 // 
00003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
00004 //
00005 // Licensed under the MIT License (the "License"); you may not use this file except
00006 // in compliance with the License. You may obtain a copy of the License at
00007 //
00008 // http://opensource.org/licenses/MIT
00009 //
00010 // Unless required by applicable law or agreed to in writing, software distributed 
00011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
00012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
00013 // specific language governing permissions and limitations under the License.
00014 
00015 #ifndef RAPIDJSON_FILEREADSTREAM_H_
00016 #define RAPIDJSON_FILEREADSTREAM_H_
00017 
00018 #include "stream.h"
00019 #include <cstdio>
00020 
00021 #ifdef __clang__
00022 RAPIDJSON_DIAG_PUSH
00023 RAPIDJSON_DIAG_OFF(padded)
00024 RAPIDJSON_DIAG_OFF(unreachable-code)
00025 RAPIDJSON_DIAG_OFF(missing-noreturn)
00026 #endif
00027 
00028 RAPIDJSON_NAMESPACE_BEGIN
00029 
00030 //! File byte stream for input using fread().
00031 /*!
00032     \note implements Stream concept
00033 */
00034 class FileReadStream {
00035 public:
00036     typedef char Ch;    //!< Character type (byte).
00037 
00038     //! Constructor.
00039     /*!
00040         \param fp File pointer opened for read.
00041         \param buffer user-supplied buffer.
00042         \param bufferSize size of buffer in bytes. Must >=4 bytes.
00043     */
00044     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) { 
00045         RAPIDJSON_ASSERT(fp_ != 0);
00046         RAPIDJSON_ASSERT(bufferSize >= 4);
00047         Read();
00048     }
00049 
00050     Ch Peek() const { return *current_; }
00051     Ch Take() { Ch c = *current_; Read(); return c; }
00052     size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
00053 
00054     // Not implemented
00055     void Put(Ch) { RAPIDJSON_ASSERT(false); }
00056     void Flush() { RAPIDJSON_ASSERT(false); } 
00057     Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
00058     size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
00059 
00060     // For encoding detection only.
00061     const Ch* Peek4() const {
00062         return (current_ + 4 <= bufferLast_) ? current_ : 0;
00063     }
00064 
00065 private:
00066     void Read() {
00067         if (current_ < bufferLast_)
00068             ++current_;
00069         else if (!eof_) {
00070             count_ += readCount_;
00071             readCount_ = fread(buffer_, 1, bufferSize_, fp_);
00072             bufferLast_ = buffer_ + readCount_ - 1;
00073             current_ = buffer_;
00074 
00075             if (readCount_ < bufferSize_) {
00076                 buffer_[readCount_] = '\0';
00077                 ++bufferLast_;
00078                 eof_ = true;
00079             }
00080         }
00081     }
00082 
00083     std::FILE* fp_;
00084     Ch *buffer_;
00085     size_t bufferSize_;
00086     Ch *bufferLast_;
00087     Ch *current_;
00088     size_t readCount_;
00089     size_t count_;  //!< Number of characters read
00090     bool eof_;
00091 };
00092 
00093 RAPIDJSON_NAMESPACE_END
00094 
00095 #ifdef __clang__
00096 RAPIDJSON_DIAG_POP
00097 #endif
00098 
00099 #endif // RAPIDJSON_FILESTREAM_H_