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