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