rapidjson
A fast JSON parser/generator for C++ with both SAX/DOM style API
 All Classes Functions Variables Typedefs Pages
filestream.h
1 #ifndef RAPIDJSON_FILESTREAM_H_
2 #define RAPIDJSON_FILESTREAM_H_
3 
4 #include <cstdio>
5 
6 namespace rapidjson {
7 
8 //! Wrapper of C file stream for input or output.
9 /*!
10  This simple wrapper does not check the validity of the stream.
11  \implements Stream
12 */
13 class FileStream {
14 public:
15  typedef char Ch; //!< Character type. Only support char.
16 
17  FileStream(FILE* fp) : fp_(fp), count_(0) { Read(); }
18  char Peek() const { return current_; }
19  char Take() { char c = current_; Read(); return c; }
20  size_t Tell() const { return count_; }
21  void Put(char c) { fputc(c, fp_); }
22 
23  // Not implemented
24  char* PutBegin() { return 0; }
25  size_t PutEnd(char*) { return 0; }
26 
27 private:
28  void Read() {
29  RAPIDJSON_ASSERT(fp_ != 0);
30  int c = fgetc(fp_);
31  if (c != EOF) {
32  current_ = (char)c;
33  count_++;
34  }
35  else
36  current_ = '\0';
37  }
38 
39  FILE* fp_;
40  char current_;
41  size_t count_;
42 };
43 
44 } // namespace rapidjson
45 
46 #endif // RAPIDJSON_FILESTREAM_H_