home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / id3 / readers.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-04-10  |  4.4 KB  |  162 lines

  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.  
  34. class ID3_CPP_EXPORT ID3_IStreamReader : public ID3_Reader
  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.     
  46.   /** Read up to \c len chars into buf and advance the internal position
  47.    ** accordingly.  Returns the number of characters read into buf.
  48.    **/
  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 _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.     
  70.   /** Set the value of the internal position for reading.
  71.    **/
  72.   virtual pos_type setCur(pos_type pos) { _stream.seekg(pos); return pos; }
  73. };
  74.   
  75. class ID3_CPP_EXPORT ID3_IFStreamReader : public ID3_IStreamReader
  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.   
  88. class ID3_CPP_EXPORT ID3_MemoryReader : public ID3_Reader
  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:
  101.   ID3_MemoryReader()
  102.   {
  103.     this->setBuffer(NULL, 0);
  104.   }
  105.   ID3_MemoryReader(const char_type* buf, size_type size)
  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.     
  125.   /** Read up to \c len chars into buf and advance the internal position
  126.    ** accordingly.  Returns the number of characters read into buf.
  127.    **/
  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.     
  149.   /** Set the value of the internal position for reading.
  150.    **/
  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.  
  162.