1 // -*- C++ -*-
2 // $Id: reader.h,v 1.13 2002/07/02 22:10:57 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_READER_H_
29 #define _ID3LIB_READER_H_
30 
31 #include "id3/globals.h" //has <stdlib.h> "id3/sized_types.h"
32 
33 class ID3_CPP_EXPORT ID3_Reader
34 {
35  public:
36   typedef uint32 size_type;
37   typedef uint8  char_type;
38   typedef uint32 pos_type;
39   typedef  int32 off_type;
40   typedef  int16 int_type;
41   static const int_type END_OF_READER;
42 
43   /** Close the reader.  Any further actions on the reader should fail.
44    **/
45   virtual void close() = 0;
46 
47   /** Return the beginning position in the reader */
getBeg()48   virtual pos_type getBeg() { return static_cast<pos_type>(0); }
49 
50   /** Return the ending position in the reader */
getEnd()51   virtual pos_type getEnd() { return static_cast<pos_type>(-1); }
52 
53   /** Return the current position in the reader */
54   virtual pos_type getCur() = 0;
55 
56   /** Set the value of the current position for reading.
57    **/
58   virtual pos_type setCur(pos_type pos) = 0;
59 
60   /**
61    ** Read a single character and advance the internal position.  Note that the
62    ** interal position may advance more than one byte for a single character
63    ** read.  Returns END_OF_READER if there isn't a character to read.
64    **/
readChar()65   virtual int_type readChar()
66   {
67     if (this->atEnd())
68     {
69       return END_OF_READER;
70     }
71     char_type ch;
72     this->readChars(&ch, 1);
73     return ch;
74   }
75 
76   /**
77    ** Return the next character to be read without advancing the internal
78    ** position.  Returns END_OF_READER if there isn't a character to read.
79    **/
80   virtual int_type peekChar() = 0;
81 
82   /** Read up to \c len characters into buf and advance the internal position
83    ** accordingly.  Returns the number of characters read into buf.  Note that
84    ** the value returned may be less than the number of bytes that the internal
85    ** position advances, due to multi-byte characters.
86    **/
87   virtual size_type readChars(char_type buf[], size_type len) = 0;
readChars(char buf[],size_type len)88   virtual size_type readChars(char buf[], size_type len)
89   {
90     return this->readChars(reinterpret_cast<char_type *>(buf), len);
91   }
92 
93   /** Skip up to \c len chars in the stream and advance the internal position
94    ** accordingly.  Returns the number of characters actually skipped (may be
95    ** less than requested).
96    **/
skipChars(size_type len)97   virtual size_type skipChars(size_type len)
98   {
99     const size_type SIZE = 1024;
100     char_type bytes[SIZE];
101     size_type remaining = len;
102     while (!this->atEnd() && remaining > 0)
103     {
104       remaining -= this->readChars(bytes, (remaining < SIZE ? remaining : SIZE));
105     }
106     return len - remaining;
107   }
108 
remainingBytes()109   virtual size_type remainingBytes()
110   {
111     pos_type end = this->getEnd(), cur = this->getCur();
112     if (end == pos_type(-1))
113     {
114       return size_type(-1);
115     }
116 
117     if (end >= cur)
118     {
119       return end - cur;
120     }
121 
122     return 0;
123   }
124 
atEnd()125   virtual bool atEnd() { return this->getCur() >= this->getEnd(); }
126 };
127 
128 #endif /* _ID3LIB_READER_H_ */
129 
130