filestream.h
Go to the documentation of this file.
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_FILESTREAM_H_
22 #define RAPIDJSON_FILESTREAM_H_
23 
24 #include "rapidjson.h"
25 #include <cstdio>
26 
27 namespace rapidjson {
28 
29 //! (Deprecated) Wrapper of C file stream for input or output.
30 /*!
31  This simple wrapper does not check the validity of the stream.
32  \note implements Stream concept
33  \note 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.
34 */
35 class FileStream {
36 public:
37  typedef char Ch; //!< Character type. Only support char.
38 
39  FileStream(FILE* fp) : fp_(fp), current_('\0'), count_(0) { Read(); }
40  char Peek() const { return current_; }
41  char Take() { char c = current_; Read(); return c; }
42  size_t Tell() const { return count_; }
43  void Put(char c) { fputc(c, fp_); }
44  void Flush() { fflush(fp_); }
45 
46  // Not implemented
47  char* PutBegin() { return 0; }
48  size_t PutEnd(char*) { return 0; }
49 
50 private:
51  // Prohibit copy constructor & assignment operator.
52  FileStream(const FileStream&);
54 
55  void Read() {
56  RAPIDJSON_ASSERT(fp_ != 0);
57  int c = fgetc(fp_);
58  if (c != EOF) {
59  current_ = (char)c;
60  count_++;
61  }
62  else if (current_ != '\0')
63  current_ = '\0';
64  }
65 
66  FILE* fp_;
67  char current_;
68  size_t count_;
69 };
70 
71 } // namespace rapidjson
72 
73 #endif // RAPIDJSON_FILESTREAM_H_
size_t PutEnd(char *)
Definition: filestream.h:48
FileStream(FILE *fp)
Definition: filestream.h:39
char Peek() const
Definition: filestream.h:40
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
(Deprecated) Wrapper of C file stream for input or output.
Definition: filestream.h:35
char Ch
Character type. Only support char.
Definition: filestream.h:37
main RapidJSON namespace
Definition: filestream.h:27
size_t Tell() const
Definition: filestream.h:42
common definitions and configuration
void Put(char c)
Definition: filestream.h:43
FileStream & operator=(const FileStream &)