Torque3D Documentation / _generateds / filereadstream.h

filereadstream.h

Engine/source/persistence/rapidjson/filereadstream.h

More...

Classes:

class

File byte stream for input using fread().

Namespaces:

namespace

Detailed Description

 1
 2#ifndef RAPIDJSON_FILEREADSTREAM_H_
 3#define RAPIDJSON_FILEREADSTREAM_H_
 4
 5#include "rapidjson.h"
 6#include <cstdio>
 7
 8namespace rapidjson {
 9
10//! File byte stream for input using fread().
11/*!
12   \implements Stream
13*/
14class FileReadStream {
15public:
16   typedef char Ch;  //!< Character type (byte).
17
18   //! Constructor.
19   /*!
20      \param fp File pointer opened for read.
21      \param buffer user-supplied buffer.
22      \param bufferSize size of buffer in bytes. Must >=4 bytes.
23   */
24   FileReadStream(FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { 
25      RAPIDJSON_ASSERT(fp_ != 0);
26      RAPIDJSON_ASSERT(bufferSize >= 4);
27      Read();
28   }
29
30   Ch Peek() const { return *current_; }
31   Ch Take() { Ch c = *current_; Read(); return c; }
32   size_t Tell() const { return count_ + (current_ - buffer_); }
33
34   // Not implemented
35   void Put(Ch) { RAPIDJSON_ASSERT(false); }
36   void Flush() { RAPIDJSON_ASSERT(false); } 
37   Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
38   size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
39
40   // For encoding detection only.
41   const Ch* Peek4() const {
42      return (current_ + 4 <= bufferLast_) ? current_ : 0;
43   }
44
45private:
46   void Read() {
47      if (current_ < bufferLast_)
48         ++current_;
49      else if (!eof_) {
50         count_ += readCount_;
51         readCount_ = fread(buffer_, 1, bufferSize_, fp_);
52         bufferLast_ = buffer_ + readCount_ - 1;
53         current_ = buffer_;
54
55         if (readCount_ < bufferSize_) {
56            buffer_[readCount_] = '\0';
57            ++bufferLast_;
58            eof_ = true;
59         }
60      }
61   }
62
63   FILE* fp_;
64   Ch *buffer_;
65   size_t bufferSize_;
66   Ch *bufferLast_;
67   Ch *current_;
68   size_t readCount_;
69   size_t count_; //!< Number of characters read
70   bool eof_;
71};
72
73} // namespace rapidjson
74
75#endif // RAPIDJSON_FILESTREAM_H_
76