Gorgon Game Engine
MemoryStream.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <streambuf>
4 
5 namespace Gorgon { namespace IO {
6 
9  class MemoryInputBuffer : public std::streambuf {
10  public:
11  MemoryInputBuffer(const char *begin, const char *end) : begin(begin), end(end), cur(begin) { }
12 
14 
16 
17  private:
18  int_type underflow() {
19  if(cur == end)
20  return traits_type::eof();
21 
22  return traits_type::to_int_type(*cur);
23  }
24 
25  int_type uflow() {
26  if(cur == end)
27  return traits_type::eof();
28 
29  return traits_type::to_int_type(*cur++);
30  }
31 
32  int_type pbackfail(int_type ch) {
33  if(cur == begin || (ch != traits_type::eof() && ch != cur[-1]))
34  return traits_type::eof();
35 
36  return traits_type::to_int_type(*--cur);
37  }
38 
39  std::streamsize showmanyc() {
40  return end - cur;
41  }
42 
43  pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which) {
44  if(way == std::ios_base::cur) {
45  cur += off;
46  if(cur > end) cur = end;
47  return cur-begin;
48  }
49  else if(way == std::ios_base::end) {
50  cur = end - off;
51  if(cur < begin) cur = begin;
52  return cur-begin;
53  }
54  else {
55  cur = begin + off;
56  if(cur > end) cur = end;
57 
58  return cur-begin;
59  }
60 
61  return pos_type(off_type(-1));
62  }
63 
64  pos_type seekpos(pos_type sp, std::ios_base::openmode which) {
65  cur = begin + (int)sp;
66  if(cur > end) cur = end;
67  return cur-begin;
68  }
69  private:
70  const char *begin;
71  const char *end;
72  const char *cur;
73  };
74 
75  class MemoryInputStream : public std::istream {
76  public:
77  MemoryInputStream(const char *begin, const char *end) :
78  std::istream(new MemoryInputBuffer(begin, end)) {}
79 
80  virtual ~MemoryInputStream() {
81  delete rdbuf();
82  }
83  };
84 
85 }}
Gorgon::begin
std::vector< T_ >::const_iterator begin(enum_type_id< T_ >)
Definition: Enum.h:283
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::IO::MemoryInputStream
Definition: MemoryStream.h:75
Gorgon::IO::MemoryInputBuffer::MemoryInputBuffer
MemoryInputBuffer(const MemoryInputBuffer &)=delete
Gorgon::IO::MemoryInputBuffer
This class is an input only memory stream buffer.
Definition: MemoryStream.h:9
Gorgon::IO::MemoryInputStream::MemoryInputStream
MemoryInputStream(const char *begin, const char *end)
Definition: MemoryStream.h:77
Gorgon::IO::MemoryInputStream::~MemoryInputStream
virtual ~MemoryInputStream()
Definition: MemoryStream.h:80
Gorgon::IO::MemoryInputBuffer::MemoryInputBuffer
MemoryInputBuffer(const char *begin, const char *end)
Definition: MemoryStream.h:11
Gorgon::end
std::vector< T_ >::const_iterator end(enum_type_id< T_ >)
Definition: Enum.h:288
Gorgon::IO::MemoryInputBuffer::operator=
MemoryInputBuffer & operator=(const MemoryInputBuffer &)=delete