id3lib  3.8.3
writers.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id: writers.h,v 1.11 2002/07/02 22:11:16 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_WRITERS_H_
29 #define _ID3LIB_WRITERS_H_
30 
31 #include <string.h>
32 
33 #include "id3/writer.h"
34 #include "id3/id3lib_streams.h"
35 //#include <string.h>
36 
38 {
39  ostream& _stream;
40  pos_type _beg;
41  protected:
42  ostream& getWriter() const { return _stream; }
43  public:
44  ID3_OStreamWriter(ostream& writer) : _stream(writer), _beg(_stream.tellp()) { ; }
45  virtual ~ID3_OStreamWriter() { ; }
46 
47  virtual void close() { ; }
48  virtual void flush() { _stream.flush(); }
49 
51  {
52  _stream.put(ch);
53  return ch;
54  }
55 
59  virtual size_type writeChars(const char buf[], size_type len)
60  {
61  _stream.write(buf, len);
62  return len;
63  }
64  virtual size_type writeChars(const char_type buf[], size_type len)
65  {
66  _stream.write(reinterpret_cast<const char*>(buf), len);
67  return len;
68  }
69 
70  virtual pos_type getBeg() { return _beg; }
71  virtual pos_type getCur() { return _stream.tellp(); }
72 };
73 
75 {
76  ofstream& _file;
77  public:
78  ID3_OFStreamWriter(ofstream& writer)
79  : ID3_OStreamWriter(writer), _file(writer) { ; }
80 
81  virtual void close()
82  {
83  _file.close();
84  }
85 };
86 
88 {
89  iostream& _stream;
90  pos_type _beg;
91  protected:
92  iostream& getWriter() const { return _stream; }
93  public:
94  ID3_IOStreamWriter(iostream& writer) : _stream(writer), _beg(_stream.tellp()) { ; }
95  virtual ~ID3_IOStreamWriter() { ; }
96 
97  virtual void close() { ; }
98  virtual void flush() { _stream.flush(); }
99 
101  {
102  _stream.put(ch);
103  return ch;
104  }
105 
109  virtual size_type writeChars(const char buf[], size_type len)
110  {
111  _stream.write(buf, len);
112  return len;
113  }
114  virtual size_type writeChars(const char_type buf[], size_type len)
115  {
116  _stream.write(reinterpret_cast<const char*>(buf), len);
117  return len;
118  }
119 
120  virtual pos_type getBeg() { return _beg; }
121  virtual pos_type getCur() { return _stream.tellp(); }
122 };
123 
125 {
126  fstream& _file;
127  public:
128  ID3_FStreamWriter(fstream& writer)
129  : ID3_IOStreamWriter(writer), _file(writer) { ; }
130 
131  virtual void close()
132  {
133  _file.close();
134  }
135 };
136 
138 {
139  const char_type* _beg;
140  /* */ char_type* _cur;
141  const char_type* _end;
142  protected:
143  void setBuffer(char_type* buf, size_t size)
144  {
145  _beg = buf;
146  _cur = buf;
147  _end = buf + size;
148  };
149  public:
151  {
152  this->setBuffer(NULL, 0);
153  }
154  ID3_MemoryWriter(char_type buf[], size_t size)
155  {
156  this->setBuffer(buf, size);
157  }
158  virtual ~ID3_MemoryWriter() { ; }
159  virtual void close() { ; }
160  virtual void flush() { ; }
161 
165  virtual size_type writeChars(const char buf[], size_type len)
166  {
167  return this->writeChars(reinterpret_cast<const char_type *>(buf), len);
168  }
169  virtual size_type writeChars(const char_type buf[], size_type len)
170  {
171  size_type remaining = _end - _cur;
172  size_type size = (remaining > len) ? len : remaining;
173  ::memcpy(_cur, buf, size);
174  _cur += size;
175  return size;
176  }
177 
178  virtual pos_type getCur()
179  {
180  return _cur - _beg;
181  }
182 
183  virtual pos_type getBeg()
184  {
185  return _beg - _beg;
186  }
187 
188  virtual pos_type getEnd()
189  {
190  return _end - _beg;
191  }
192 };
193 
194 #endif /* _ID3LIB_WRITERS_H_ */
195 
virtual void flush()
Flush the writer.
Definition: writers.h:98
virtual pos_type getCur()
Return the next position that will be written to.
Definition: writers.h:178
virtual ~ID3_MemoryWriter()
Definition: writers.h:158
ID3_OFStreamWriter(ofstream &writer)
Definition: writers.h:78
virtual size_type writeChars(const char_type buf[], size_type len)=0
Write up to len characters into buf and advance the internal position accordingly.
virtual void flush()
Flush the writer.
Definition: writers.h:160
virtual int_type writeChar(char_type ch)
Write a single character and advance the internal position.
Definition: writers.h:100
#define ID3_CPP_EXPORT
Definition: globals.h:79
uint32 size_type
Definition: writer.h:36
virtual ~ID3_IOStreamWriter()
Definition: writers.h:95
virtual void close()
Close the writer.
Definition: writers.h:97
virtual size_type writeChars(const char_type buf[], size_type len)
Write up to len characters into buf and advance the internal position accordingly.
Definition: writers.h:114
virtual pos_type getBeg()
Return the beginning position in the writer.
Definition: writers.h:70
int16 int_type
Definition: writer.h:40
virtual void close()
Close the writer.
Definition: writers.h:159
virtual ~ID3_OStreamWriter()
Definition: writers.h:45
void setBuffer(char_type *buf, size_t size)
Definition: writers.h:143
virtual pos_type getCur()
Return the next position that will be written to.
Definition: writers.h:121
virtual size_type writeChars(const char_type buf[], size_type len)
Write up to len characters into buf and advance the internal position accordingly.
Definition: writers.h:64
uint8 char_type
Definition: writer.h:37
virtual void close()
Close the writer.
Definition: writers.h:47
virtual size_type writeChars(const char buf[], size_type len)
Write up to len chars into buf and advance the internal position accordingly.
Definition: writers.h:109
virtual size_type writeChars(const char buf[], size_type len)
Write up to len chars into buf and advance the internal position accordingly.
Definition: writers.h:59
iostream & getWriter() const
Definition: writers.h:92
virtual void close()
Close the writer.
Definition: writers.h:81
ID3_MemoryWriter(char_type buf[], size_t size)
Definition: writers.h:154
uint32 pos_type
Definition: writer.h:38
virtual size_type writeChars(const char buf[], size_type len)
Write up to len chars from buf and advance the internal position accordingly.
Definition: writers.h:165
virtual int_type writeChar(char_type ch)
Write a single character and advance the internal position.
Definition: writers.h:50
ID3_IOStreamWriter(iostream &writer)
Definition: writers.h:94
virtual pos_type getEnd()
Return the first position that can&#39;t be written to.
Definition: writers.h:188
#define NULL
Definition: globals.h:743
virtual pos_type getBeg()
Return the beginning position in the writer.
Definition: writers.h:183
ID3_FStreamWriter(fstream &writer)
Definition: writers.h:128
ID3_OStreamWriter(ostream &writer)
Definition: writers.h:44
virtual pos_type getCur()
Return the next position that will be written to.
Definition: writers.h:71
virtual size_type writeChars(const char_type buf[], size_type len)
Write up to len characters into buf and advance the internal position accordingly.
Definition: writers.h:169
virtual void close()
Close the writer.
Definition: writers.h:131
ostream & getWriter() const
Definition: writers.h:42
virtual pos_type getBeg()
Return the beginning position in the writer.
Definition: writers.h:120
virtual void flush()
Flush the writer.
Definition: writers.h:48