filestream.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef RAPIDJSON_FILESTREAM_H_
  2. #define RAPIDJSON_FILESTREAM_H_
  3. #include "rapidjson.h"
  4. #include <cstdio>
  5. namespace rapidjson {
  6. //! (Depreciated) Wrapper of C file stream for input or output.
  7. /*!
  8. This simple wrapper does not check the validity of the stream.
  9. \implements Stream
  10. \deprecated { This was only for basic testing in version 0.1, it is found that the performance is very low by using fgetc(). Use FileReadStream instead. }
  11. */
  12. class FileStream {
  13. public:
  14. typedef char Ch; //!< Character type. Only support char.
  15. FileStream(FILE* fp) : fp_(fp), count_(0) { Read(); }
  16. char Peek() const { return current_; }
  17. char Take() { char c = current_; Read(); return c; }
  18. size_t Tell() const { return count_; }
  19. void Put(char c) { fputc(c, fp_); }
  20. void Flush() { fflush(fp_); }
  21. // Not implemented
  22. char* PutBegin() { return 0; }
  23. size_t PutEnd(char*) { return 0; }
  24. private:
  25. void Read() {
  26. RAPIDJSON_ASSERT(fp_ != 0);
  27. int c = fgetc(fp_);
  28. if (c != EOF) {
  29. current_ = (char)c;
  30. count_++;
  31. }
  32. else if (current_ != '\0')
  33. current_ = '\0';
  34. }
  35. FILE* fp_;
  36. char current_;
  37. size_t count_;
  38. };
  39. } // namespace rapidjson
  40. #endif // RAPIDJSON_FILESTREAM_H_