id3lib  3.8.3
readers.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id: readers.h,v 1.12 2002/06/29 17:43:42 t1mpy Exp $
3 
4 // id3lib: a software library for creating and manipulating id3v1/v2 tags
5 // Copyright 1999, 2000 Scott Thomas Haug
6 
7 // This library is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU Library General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or (at your
10 // option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public License
18 // along with this library; if not, write to the Free Software Foundation,
19 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 
21 // The id3lib authors encourage improvements and optimisations to be sent to
22 // the id3lib coordinator. Please see the README file for details on where to
23 // send such submissions. See the AUTHORS file for a list of people who have
24 // contributed to id3lib. See the ChangeLog file for a list of changes to
25 // id3lib. These files are distributed with id3lib at
26 // http://download.sourceforge.net/id3lib/
27 
28 #ifndef _ID3LIB_READERS_H_
29 #define _ID3LIB_READERS_H_
30 
31 #include "id3/id3lib_streams.h"
32 #include "id3/reader.h"
33 
35 {
36  istream& _stream;
37  protected:
38  istream& getReader() const { return _stream; }
39  public:
40  ID3_IStreamReader(istream& reader) : _stream(reader) { ; }
41  virtual ~ID3_IStreamReader() { ; }
42  virtual void close() { ; }
43 
44  virtual int_type peekChar() { return _stream.peek(); }
45 
49  virtual size_type readChars(char buf[], size_type len)
50  {
51  return this->readChars(reinterpret_cast<uchar *>(buf), len);
52  }
53  virtual size_type readChars(char_type buf[], size_type len)
54  {
55  _stream.read((char *)buf, len);
56  return _stream.gcount();
57  }
58 
59  virtual pos_type getBeg() { return 0; }
60  virtual pos_type getCur() { return streamoff(_stream.tellg()); }
61  virtual pos_type getEnd()
62  {
63  pos_type cur = this->getCur();
64  _stream.seekg(0, ios::end);
65  pos_type end = this->getCur();
66  this->setCur(cur);
67  return end;
68  }
69 
72  virtual pos_type setCur(pos_type pos) { _stream.seekg(pos); return pos; }
73 };
74 
76 {
77  ifstream& _file;
78  public:
79  ID3_IFStreamReader(ifstream& reader)
80  : ID3_IStreamReader(reader), _file(reader) { ; }
81 
82  virtual void close()
83  {
84  _file.close();
85  }
86 };
87 
89 {
90  const char_type* _beg;
91  const char_type* _cur;
92  const char_type* _end;
93  protected:
94  void setBuffer(const char_type* buf, size_type size)
95  {
96  _beg = buf;
97  _cur = buf;
98  _end = buf + size;
99  };
100  public:
102  {
103  this->setBuffer(NULL, 0);
104  }
106  {
107  this->setBuffer(buf, size);
108  };
109  ID3_MemoryReader(const char* buf, size_type size)
110  {
111  this->setBuffer(reinterpret_cast<const char_type*>(buf), size);
112  };
113  virtual ~ID3_MemoryReader() { ; }
114  virtual void close() { ; }
115 
116  virtual int_type peekChar()
117  {
118  if (!this->atEnd())
119  {
120  return *_cur;
121  }
122  return END_OF_READER;
123  }
124 
128  virtual size_type readChars(char buf[], size_type len)
129  {
130  return this->readChars(reinterpret_cast<char_type *>(buf), len);
131  }
132  virtual size_type readChars(char_type buf[], size_type len);
133 
134  virtual pos_type getCur()
135  {
136  return _cur - _beg;
137  }
138 
139  virtual pos_type getBeg()
140  {
141  return _beg - _beg;
142  }
143 
144  virtual pos_type getEnd()
145  {
146  return _end - _beg;
147  }
148 
151  virtual pos_type setCur(pos_type pos)
152  {
153  pos_type end = this->getEnd();
154  size_type size = (pos < end) ? pos : end;
155  _cur = _beg + size;
156  return this->getCur();
157  }
158 };
159 
160 #endif /* _ID3LIB_READERS_H_ */
161 
virtual void close()
Close the reader.
Definition: readers.h:82
ID3_IFStreamReader(ifstream &reader)
Definition: readers.h:79
virtual ~ID3_IStreamReader()
Definition: readers.h:41
virtual pos_type getBeg()
Return the beginning position in the reader.
Definition: readers.h:59
virtual size_type readChars(char buf[], size_type len)
Read up to len chars into buf and advance the internal position accordingly.
Definition: readers.h:49
virtual int_type peekChar()
Return the next character to be read without advancing the internal position.
Definition: readers.h:44
istream & getReader() const
Definition: readers.h:38
virtual pos_type getEnd()
Return the ending position in the reader.
Definition: readers.h:61
ID3_IStreamReader(istream &reader)
Definition: readers.h:40
virtual void close()
Close the reader.
Definition: readers.h:42
virtual size_type readChars(char_type buf[], size_type len)
Read up to len characters into buf and advance the internal position accordingly.
Definition: readers.h:53
virtual pos_type getCur()
Return the current position in the reader.
Definition: readers.h:60
virtual pos_type setCur(pos_type pos)
Set the value of the internal position for reading.
Definition: readers.h:72
virtual int_type peekChar()
Return the next character to be read without advancing the internal position.
Definition: readers.h:116
virtual pos_type getCur()
Return the current position in the reader.
Definition: readers.h:134
ID3_MemoryReader(const char_type *buf, size_type size)
Definition: readers.h:105
virtual pos_type getEnd()
Return the ending position in the reader.
Definition: readers.h:144
virtual size_type readChars(char buf[], size_type len)
Read up to len chars into buf and advance the internal position accordingly.
Definition: readers.h:128
virtual ~ID3_MemoryReader()
Definition: readers.h:113
virtual pos_type getBeg()
Return the beginning position in the reader.
Definition: readers.h:139
void setBuffer(const char_type *buf, size_type size)
Definition: readers.h:94
virtual pos_type setCur(pos_type pos)
Set the value of the internal position for reading.
Definition: readers.h:151
virtual void close()
Close the reader.
Definition: readers.h:114
ID3_MemoryReader(const char *buf, size_type size)
Definition: readers.h:109
virtual pos_type setCur(pos_type pos)=0
Set the value of the current position for reading.
static const int_type END_OF_READER
Definition: reader.h:41
virtual pos_type getCur()=0
Return the current position in the reader.
virtual size_type readChars(char_type buf[], size_type len)=0
Read up to len characters into buf and advance the internal position accordingly.
virtual bool atEnd()
Definition: reader.h:125
virtual pos_type getEnd()
Return the ending position in the reader.
Definition: reader.h:51
uint32 pos_type
Definition: reader.h:38
int16 int_type
Definition: reader.h:40
uint32 size_type
Definition: reader.h:36
uint8 char_type
Definition: reader.h:37
#define NULL
Definition: globals.h:743
unsigned char uchar
Definition: globals.h:114
#define ID3_CPP_EXPORT
Definition: globals.h:79