id3lib 3.8.3
|
00001 // -*- C++ -*- 00002 // $Id: writers.h,v 1.11 2002/07/02 22:11:16 t1mpy 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 <cstring> 00032 #include "id3/writer.h" 00033 #include "id3/id3lib_streams.h" 00034 00035 class ID3_CPP_EXPORT ID3_OStreamWriter : public ID3_Writer 00036 { 00037 ostream& _stream; 00038 pos_type _beg; 00039 protected: 00040 ostream& getWriter() const { return _stream; } 00041 public: 00042 ID3_OStreamWriter(ostream& writer) : _stream(writer), _beg(_stream.tellp()) { ; } 00043 virtual ~ID3_OStreamWriter() { ; } 00044 00045 virtual void close() { ; } 00046 virtual void flush() { _stream.flush(); } 00047 00048 virtual int_type writeChar(char_type ch) 00049 { 00050 _stream.put(ch); 00051 return ch; 00052 } 00053 00057 virtual size_type writeChars(const char buf[], size_type len) 00058 { 00059 _stream.write(buf, len); 00060 return len; 00061 } 00062 virtual size_type writeChars(const char_type buf[], size_type len) 00063 { 00064 _stream.write(reinterpret_cast<const char*>(buf), len); 00065 return len; 00066 } 00067 00068 virtual pos_type getBeg() { return _beg; } 00069 virtual pos_type getCur() { return _stream.tellp(); } 00070 }; 00071 00072 class ID3_CPP_EXPORT ID3_OFStreamWriter : public ID3_OStreamWriter 00073 { 00074 ofstream& _file; 00075 public: 00076 ID3_OFStreamWriter(ofstream& writer) 00077 : ID3_OStreamWriter(writer), _file(writer) { ; } 00078 00079 virtual void close() 00080 { 00081 _file.close(); 00082 } 00083 }; 00084 00085 class ID3_CPP_EXPORT ID3_IOStreamWriter : public ID3_Writer 00086 { 00087 iostream& _stream; 00088 pos_type _beg; 00089 protected: 00090 iostream& getWriter() const { return _stream; } 00091 public: 00092 ID3_IOStreamWriter(iostream& writer) : _stream(writer), _beg(_stream.tellp()) { ; } 00093 virtual ~ID3_IOStreamWriter() { ; } 00094 00095 virtual void close() { ; } 00096 virtual void flush() { _stream.flush(); } 00097 00098 virtual int_type writeChar(char_type ch) 00099 { 00100 _stream.put(ch); 00101 return ch; 00102 } 00103 00107 virtual size_type writeChars(const char buf[], size_type len) 00108 { 00109 _stream.write(buf, len); 00110 return len; 00111 } 00112 virtual size_type writeChars(const char_type buf[], size_type len) 00113 { 00114 _stream.write(reinterpret_cast<const char*>(buf), len); 00115 return len; 00116 } 00117 00118 virtual pos_type getBeg() { return _beg; } 00119 virtual pos_type getCur() { return _stream.tellp(); } 00120 }; 00121 00122 class ID3_CPP_EXPORT ID3_FStreamWriter : public ID3_IOStreamWriter 00123 { 00124 fstream& _file; 00125 public: 00126 ID3_FStreamWriter(fstream& writer) 00127 : ID3_IOStreamWriter(writer), _file(writer) { ; } 00128 00129 virtual void close() 00130 { 00131 _file.close(); 00132 } 00133 }; 00134 00135 class ID3_CPP_EXPORT ID3_MemoryWriter : public ID3_Writer 00136 { 00137 const char_type* _beg; 00138 /* */ char_type* _cur; 00139 const char_type* _end; 00140 protected: 00141 void setBuffer(char_type* buf, size_t size) 00142 { 00143 _beg = buf; 00144 _cur = buf; 00145 _end = buf + size; 00146 }; 00147 public: 00148 ID3_MemoryWriter() 00149 { 00150 this->setBuffer(NULL, 0); 00151 } 00152 ID3_MemoryWriter(char_type buf[], size_t size) 00153 { 00154 this->setBuffer(buf, size); 00155 } 00156 virtual ~ID3_MemoryWriter() { ; } 00157 virtual void close() { ; } 00158 virtual void flush() { ; } 00159 00163 virtual size_type writeChars(const char buf[], size_type len) 00164 { 00165 return this->writeChars(reinterpret_cast<const char_type *>(buf), len); 00166 } 00167 virtual size_type writeChars(const char_type buf[], size_type len) 00168 { 00169 size_type remaining = _end - _cur; 00170 size_type size = (remaining > len) ? len : remaining; 00171 ::memcpy(_cur, buf, size); 00172 _cur += size; 00173 return size; 00174 } 00175 00176 virtual pos_type getCur() 00177 { 00178 return _cur - _beg; 00179 } 00180 00181 virtual pos_type getBeg() 00182 { 00183 return _beg - _beg; 00184 } 00185 00186 virtual pos_type getEnd() 00187 { 00188 return _end - _beg; 00189 } 00190 }; 00191 00192 #endif /* _ID3LIB_WRITERS_H_ */ 00193