Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
BufferedStream.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef BufferedStream_INCLUDE_ONCE
33 #define BufferedStream_INCLUDE_ONCE
34 
35 #include <vlCore/String.hpp>
36 #include <vlCore/VirtualFile.hpp>
37 #include <string>
38 #include <vector>
39 
40 namespace vl
41 {
42 //-----------------------------------------------------------------------------
43 // BufferedStream
44 //-----------------------------------------------------------------------------
48  template<class Element_Type, int Chunk_Size>
49  class BufferedStream: public Object
50  {
52 
53  public:
55  {
56  mSize = 0;
57  mPtr = 0;
58  mBuffer.resize(Chunk_Size);
59  mIsEndOfFile = true;
60  }
61 
62  void seek(long long pos)
63  {
64  mSize = 0;
65  mPtr = 0;
66  if ( inputFile() )
67  inputFile()->seekSet(pos);
68  }
69 
70  bool readToken(Element_Type* token)
71  {
72  if ( mUngetBuffer.size() )
73  {
74  *token = mUngetBuffer.back();
75  mUngetBuffer.pop_back();
76  return true;
77  }
78 
79  if (bufferEmpty())
80  {
81  fillBuffer();
82  }
83 
84  if (bufferEmpty())
85  {
86  mIsEndOfFile = true;
87  return false;
88  }
89  else
90  {
91  *token = mBuffer[mPtr];
92  mPtr++;
93  return true;
94  }
95  }
96 
97  bool readTextChar(Element_Type& ch)
98  {
99  if (!readToken(&ch))
100  return false;
101 
102  Element_Type ch2 = 0;
103  switch(ch)
104  {
105  case 10:
106  ch = '\n';
107  if (readToken(&ch2) && ch2 != 13)
108  ungetToken(ch2);
109  break;
110 
111  case 13:
112  ch = '\n';
113  if (readToken(&ch2) && ch2 != 10)
114  ungetToken(ch2);
115  break;
116  }
117 
118  return true;
119  }
120 
121  void ungetToken(const Element_Type& token)
122  {
123  mUngetBuffer.push_back(token);
124  }
125 
126  bool bufferEmpty()
127  {
128  return mPtr == mSize;
129  }
130 
132  {
133  if ( inputFile() )
134  {
135  if ( !inputFile()->isOpen() )
136  if(!inputFile()->open(OM_ReadOnly))
137  return 0;
138 
139  mPtr = 0;
140  mSize = (int)inputFile()->read(&mBuffer[0], Chunk_Size);
141  return mSize;
142  }
143  else
144  {
145  return 0;
146  }
147  }
148 
149  bool isEndOfFile() const { return mIsEndOfFile; }
150 
152  {
153  mInputFile = file;
154  mIsEndOfFile = false;
155  }
156 
157  VirtualFile* inputFile() { return mInputFile.get(); }
158 
159  const VirtualFile* inputFile() const { return mInputFile.get(); }
160 
161  protected:
163  std::vector<Element_Type> mUngetBuffer;
164  std::vector<Element_Type> mBuffer;
165  int mPtr;
166  int mSize;
168  };
169 }
170 
171 #endif
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
Definition: VirtualFile.cpp:82
An abstract class representing a file.
Definition: VirtualFile.hpp:60
void ungetToken(const Element_Type &token)
bool readToken(Element_Type *token)
#define VL_GROUP(...)
Definition: TypeInfo.hpp:77
const VirtualFile * inputFile() const
bool readTextChar(Element_Type &ch)
The BufferedStream class is a template class that that performs a buffered read of Element_Type data ...
VirtualFile * inputFile()
std::vector< Element_Type > mBuffer
void setInputFile(VirtualFile *file)
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Visualization Library main namespace.
std::vector< Element_Type > mUngetBuffer
The base class for all the reference counted objects.
Definition: Object.hpp:158
void seek(long long pos)
bool isEndOfFile() const
bool seekSet(long long offset)
Changes the current read/write position of a file.
ref< VirtualFile > mInputFile
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55