Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

include/id3/writers.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: writers.h,v 1.8 2001/08/26 23:33:35 dmazzoni Exp $
00003 
00004 // id3lib: a software library for creating and manipulating id3v1/v2 tags
00005 // Copyright 1999, 2000  Scott Thomas Haug
00006 
00007 // This library is free software; you can redistribute it and/or modify it
00008 // under the terms of the GNU Library General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or (at your
00010 // option) any later version.
00011 //
00012 // This library is distributed in the hope that it will be useful, but WITHOUT
00013 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015 // License for more details.
00016 //
00017 // You should have received a copy of the GNU Library General Public License
00018 // along with this library; if not, write to the Free Software Foundation,
00019 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 
00021 // The id3lib authors encourage improvements and optimisations to be sent to
00022 // the id3lib coordinator.  Please see the README file for details on where to
00023 // send such submissions.  See the AUTHORS file for a list of people who have
00024 // contributed to id3lib.  See the ChangeLog file for a list of changes to
00025 // id3lib.  These files are distributed with id3lib at
00026 // http://download.sourceforge.net/id3lib/
00027 
00028 #ifndef _ID3LIB_WRITERS_H_
00029 #define _ID3LIB_WRITERS_H_
00030 
00031 #include <id3/writer.h>
00032 #include <fstream.h>
00033 #include <iostream.h>
00034 #include <string.h>
00035 
00036 class ID3_CPP_EXPORT ID3_OStreamWriter : public ID3_Writer
00037 {
00038   ostream& _stream;
00039   pos_type _beg;
00040  protected:
00041   ostream& getWriter() const { return _stream; }
00042  public:
00043   ID3_OStreamWriter(ostream& writer) : _stream(writer), _beg(_stream.tellp()) { ; }
00044   virtual ~ID3_OStreamWriter() { ; }
00045 
00046   virtual void close() { ; }
00047   virtual void flush() { _stream.flush(); }
00048   
00049   virtual int_type writeChar(char_type ch)
00050   {
00051     _stream.put(ch);
00052     return ch;
00053   }
00054 
00058   virtual size_type writeChars(const char buf[], size_type len)
00059   { 
00060     _stream.write(buf, len);
00061     return len;
00062   }
00063   virtual size_type writeChars(const char_type buf[], size_type len)
00064   {
00065     _stream.write(reinterpret_cast<const char*>(buf), len);
00066     return len;
00067   }
00068 
00069   virtual pos_type getBeg() { return _beg; }
00070   virtual pos_type getCur() { return _stream.tellp(); }
00071 };
00072 
00073 class ID3_CPP_EXPORT ID3_OFStreamWriter : public ID3_OStreamWriter
00074 {
00075   ofstream& _file;
00076  public:
00077   ID3_OFStreamWriter(ofstream& writer) 
00078     : ID3_OStreamWriter(writer), _file(writer) { ; }
00079     
00080   virtual void close() 
00081   { 
00082     _file.close();
00083   }
00084 };
00085   
00086 class ID3_CPP_EXPORT ID3_IOStreamWriter : public ID3_Writer
00087 {
00088   iostream& _stream;
00089   pos_type  _beg;
00090  protected:
00091   iostream& getWriter() const { return _stream; }
00092  public:
00093   ID3_IOStreamWriter(iostream& writer) : _stream(writer), _beg(_stream.tellp()) { ; }
00094   virtual ~ID3_IOStreamWriter() { ; }
00095 
00096   virtual void close() { ; }
00097   virtual void flush() { _stream.flush(); }
00098   
00099   virtual int_type writeChar(char_type ch)
00100   {
00101     _stream.put(ch);
00102     return ch;
00103   }
00104 
00108   virtual size_type writeChars(const char buf[], size_type len)
00109   { 
00110     _stream.write(buf, len);
00111     return len;
00112   }
00113   virtual size_type writeChars(const char_type buf[], size_type len)
00114   {
00115     _stream.write(reinterpret_cast<const char*>(buf), len);
00116     return len;
00117   }
00118 
00119   virtual pos_type getBeg() { return _beg; }
00120   virtual pos_type getCur() { return _stream.tellp(); }
00121 };
00122 
00123 class ID3_CPP_EXPORT ID3_FStreamWriter : public ID3_IOStreamWriter
00124 {
00125   fstream& _file;
00126  public:
00127   ID3_FStreamWriter(fstream& writer) 
00128     : ID3_IOStreamWriter(writer), _file(writer) { ; }
00129     
00130   virtual void close() 
00131   { 
00132     _file.close();
00133   }
00134 };
00135   
00136 class ID3_CPP_EXPORT ID3_MemoryWriter : public ID3_Writer
00137 {
00138   const char_type* _beg;
00139   /* */ char_type* _cur;
00140   const char_type* _end;
00141  protected:
00142   void setBuffer(char_type* buf, size_t size)
00143   {
00144     _beg = buf;
00145     _cur = buf;
00146     _end = buf + size;
00147   };
00148  public:
00149   ID3_MemoryWriter()
00150   {
00151     this->setBuffer(NULL, 0);
00152   }
00153   ID3_MemoryWriter(char_type buf[], size_t size)
00154   {
00155     this->setBuffer(buf, size);
00156   }
00157   virtual ~ID3_MemoryWriter() { ; }
00158   virtual void close() { ; }
00159   virtual void flush() { ; }
00160     
00164   virtual size_type writeChars(const char buf[], size_type len)
00165   { 
00166     return this->writeChars(reinterpret_cast<const char_type *>(buf), len);
00167   }
00168   virtual size_type writeChars(const char_type buf[], size_type len)
00169   {
00170     size_type remaining = _end - _cur;
00171     size_type size = (remaining > len) ? len : remaining;
00172     ::memcpy(_cur, buf, size);
00173     _cur += size;
00174     return size;
00175   }
00176     
00177   virtual pos_type getCur() 
00178   { 
00179     return _cur - _beg; 
00180   }
00181     
00182   virtual pos_type getBeg()
00183   {
00184     return _beg - _beg;
00185   }
00186     
00187   virtual pos_type getEnd()
00188   {
00189     return _end - _beg;
00190   }
00191 };
00192 
00193 #endif /* _ID3LIB_WRITERS_H_ */

Generated at Sat Sep 8 15:51:08 2001 for id3lib by doxygen1.2.8 written by Dimitri van Heesch, © 1997-2001