id3lib 3.8.3

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

tag_render.cpp
Go to the documentation of this file.
1 // $Id: tag_render.cpp,v 1.44 2002/07/31 13:20:49 t1mpy Exp $
2
3 // id3lib: a C++ library for creating and manipulating id3v1/v2 tags
4 // Copyright 1999, 2000 Scott Thomas Haug
5 // Copyright 2002 Thijmen Klok (thijmen@id3lib.org)
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 #include <memory.h>
29 #include "tag_impl.h" //has <stdio.h> "tag.h" "header_tag.h" "frame.h" "field.h" "spec.h" "id3lib_strings.h" "utils.h"
30 #include "helpers.h"
31 #include "writers.h"
32 #include "id3/io_decorators.h" //has "readers.h" "io_helpers.h" "utils.h"
33 #include "io_helpers.h"
34 #include "io_strings.h"
35
36 #if defined HAVE_SYS_PARAM_H
37 #include <sys/param.h>
38 #endif
39
40 using namespace dami;
41
42 void id3::v1::render(ID3_Writer& writer, const ID3_TagImpl& tag)
43 {
44 writer.writeChars("TAG", 3);
45
46 io::writeTrailingSpaces(writer, id3::v2::getTitle(tag), ID3_V1_LEN_TITLE);
47 io::writeTrailingSpaces(writer, id3::v2::getArtist(tag), ID3_V1_LEN_ARTIST);
48 io::writeTrailingSpaces(writer, id3::v2::getAlbum(tag), ID3_V1_LEN_ALBUM);
49 io::writeTrailingSpaces(writer, id3::v2::getYear(tag), ID3_V1_LEN_YEAR);
50
51 size_t track = id3::v2::getTrackNum(tag);
52 String comment = id3::v2::getV1Comment(tag);
53 if (track > 0)
54 {
55 io::writeTrailingSpaces(writer, comment, ID3_V1_LEN_COMMENT - 2);
56 writer.writeChar('\0');
57 writer.writeChar((char) track);
58 }
59 else
60 {
61 io::writeTrailingSpaces(writer, comment, ID3_V1_LEN_COMMENT);
62 }
63 writer.writeChar((char) id3::v2::getGenreNum(tag));
64 }
65
66 namespace
67 {
68 void renderFrames(ID3_Writer& writer, const ID3_TagImpl& tag)
69 {
70 for (ID3_TagImpl::const_iterator iter = tag.begin(); iter != tag.end(); ++iter)
71 {
72 const ID3_Frame* frame = *iter;
73 if (frame) frame->Render(writer);
74 }
75 }
76 }
77
78 void id3::v2::render(ID3_Writer& writer, const ID3_TagImpl& tag)
79 {
80 // There has to be at least one frame for there to be a tag...
81 if (tag.NumFrames() == 0)
82 {
83 ID3D_WARNING( "id3::v2::render(): no frames to render" );
84 return;
85 }
86
87 ID3D_NOTICE( "id3::v2::render(): rendering" );
88 ID3_TagHeader hdr;
89 hdr.SetSpec(tag.GetSpec());
90 hdr.SetExtended(tag.GetExtended());
91 hdr.SetExperimental(tag.GetExperimental());
92 hdr.SetFooter(tag.GetFooter());
93
94 // set up the encryption and grouping IDs
95
96 // ...
97 String frms;
98 io::StringWriter frmWriter(frms);
99 if (!tag.GetUnsync())
100 {
101 ID3D_NOTICE( "id3::v2::render(): rendering frames" );
102 renderFrames(frmWriter, tag);
103 hdr.SetUnsync(false);
104 }
105 else
106 {
107 ID3D_NOTICE( "id3::v2::render(): rendering unsynced frames" );
108 io::UnsyncedWriter uw(frmWriter);
109 renderFrames(uw, tag);
110 uw.flush();
111 ID3D_NOTICE( "id3::v2::render(): numsyncs = " << uw.getNumSyncs() );
112 hdr.SetUnsync(uw.getNumSyncs() > 0);
113 }
114 size_t frmSize = frms.size();
115 if (frmSize == 0)
116 {
117 ID3D_WARNING( "id3::v2::render(): rendered frame size is 0 bytes" );
118 return;
119 }
120
121 // zero the remainder of the buffer so that our padding bytes are zero
122 luint nPadding = tag.PaddingSize(frmSize);
123 ID3D_NOTICE( "id3::v2::render(): padding size = " << nPadding );
124
125 hdr.SetDataSize(frmSize + tag.GetExtendedBytes() + nPadding);
126
127 hdr.Render(writer);
128
129 writer.writeChars(frms.data(), frms.size());
130
131 for (size_t i = 0; i < nPadding; ++i)
132 {
133 if (writer.writeChar('\0') == ID3_Writer::END_OF_WRITER)
134 {
135 break;
136 }
137 }
138 }
139
140 size_t ID3_TagImpl::Size() const
141 {
142 if (this->NumFrames() == 0)
143 {
144 return 0;
145 }
146 ID3_TagHeader hdr;
147
148 hdr.SetSpec(this->GetSpec());
149 size_t bytesUsed = hdr.Size();
150
151 size_t frameBytes = 0;
152 for (const_iterator cur = _frames.begin(); cur != _frames.end(); ++cur)
153 {
154 if (*cur)
155 {
156 (*cur)->SetSpec(this->GetSpec());
157 frameBytes += (*cur)->Size();
158 }
159 }
160
161 if (!frameBytes)
162 {
163 return 0;
164 }
165
166 bytesUsed += frameBytes;
167 // add 30% for sync
168 if (this->GetUnsync())
169 {
170 bytesUsed += bytesUsed / 3;
171 }
172
173 bytesUsed += this->PaddingSize(bytesUsed);
174 return bytesUsed;
175 }
176
177
178 void ID3_TagImpl::RenderExtHeader(uchar *buffer)
179 {
180 if (this->GetSpec() == ID3V2_3_0)
181 {
182 }
183
184 return ;
185 }
186
187
188 #define ID3_PADMULTIPLE (2048)
189 #define ID3_PADMAX (4096)
190
191
192 size_t ID3_TagImpl::PaddingSize(size_t curSize) const
193 {
194 luint newSize = 0;
195
196 // if padding is switched off
197 if (! _is_padded)
198 {
199 return 0;
200 }
201
202 // if the old tag was large enough to hold the new tag, then we will simply
203 // pad out the difference - that way the new tag can be written without
204 // shuffling the rest of the song file around
205 if ((this->GetPrependedBytes()-ID3_TagHeader::SIZE > 0) &&
206 (this->GetPrependedBytes()-ID3_TagHeader::SIZE >= curSize) &&
207 (this->GetPrependedBytes()-ID3_TagHeader::SIZE - curSize) < ID3_PADMAX)
208 {
209 newSize = this->GetPrependedBytes()-ID3_TagHeader::SIZE;
210 }
211 else
212 {
213 luint tempSize = curSize + ID3_GetDataSize(*this) +
214 this->GetAppendedBytes() + ID3_TagHeader::SIZE;
215
216 // this method of automatic padding rounds the COMPLETE FILE up to the
217 // nearest 2K. If the file will already be an even multiple of 2K (with
218 // the tag included) then we just add another 2K of padding
219 tempSize = ((tempSize / ID3_PADMULTIPLE) + 1) * ID3_PADMULTIPLE;
220
221 // the size of the new tag is the new filesize minus the audio data
222 newSize = tempSize - ID3_GetDataSize(*this) - this->GetAppendedBytes () -
223 ID3_TagHeader::SIZE;
224 }
225
226 return newSize - curSize;
227 }
228
dami::id3::v2::getArtist
ID3_C_EXPORT String getArtist(const ID3_TagImpl &)
Definition: helpers.cpp:130
ID3_TagHeader::SetFooter
bool SetFooter(bool b)
Definition: header_tag.h:87
dami::id3::v2::getTitle
ID3_C_EXPORT String getTitle(const ID3_TagImpl &)
Definition: helpers.cpp:188
ID3_TagImpl::GetExtendedBytes
size_t GetExtendedBytes() const
Definition: tag_impl.cpp:278
ID3_TagImpl::GetExtended
bool GetExtended() const
Definition: tag_impl.cpp:263
ID3_TagImpl::end
iterator end()
Definition: tag_impl.h:133
ID3_TagImpl::begin
iterator begin()
Definition: tag_impl.h:132
ID3_Writer::writeChars
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.
dami::String
std::basic_string< char > String
Definition: id3lib_strings.h:198
ID3_TagImpl
Definition: tag_impl.h:73
ID3_TagImpl::GetFooter
bool GetFooter() const
Definition: tag_impl.cpp:273
dami::id3::v2::getYear
ID3_C_EXPORT String getYear(const ID3_TagImpl &)
Definition: helpers.cpp:211
dami::id3::v2::getGenreNum
ID3_C_EXPORT size_t getGenreNum(const ID3_TagImpl &)
Definition: helpers.cpp:388
ID3_V1_LEN_COMMENT
Definition: globals.h:337
ID3_Writer::END_OF_WRITER
static const int_type END_OF_WRITER
Definition: writer.h:41
ID3_TagHeader::SetUnsync
bool SetUnsync(bool b)
Definition: header_tag.h:66
ID3_V1_LEN_ALBUM
Definition: globals.h:335
ID3_GetDataSize
size_t ID3_GetDataSize(const ID3_TagImpl &tag)
Definition: tag_impl.cpp:323
dami::id3::v2::getAlbum
ID3_C_EXPORT String getAlbum(const ID3_TagImpl &)
Definition: helpers.cpp:165
ID3_V1_LEN_ARTIST
Definition: globals.h:334
ID3_PADMAX
#define ID3_PADMAX
Definition: tag_render.cpp:189
dami::id3::v2::render
void render(ID3_Writer &writer, const ID3_TagImpl &tag)
Definition: tag_render.cpp:78
ID3_TagImpl::NumFrames
size_t NumFrames() const
Definition: tag_impl.h:121
ID3_TagHeader::SIZE
Definition: header_tag.h:108
ID3_TagImpl::PaddingSize
size_t PaddingSize(size_t) const
Definition: tag_render.cpp:192
ID3_TagImpl::GetExperimental
bool GetExperimental() const
Definition: tag_impl.cpp:268
ID3_TagHeader
Definition: header_tag.h:34
uchar
unsigned char uchar
Definition: globals.h:114
dami::id3::v1::render
void render(ID3_Writer &, const ID3_TagImpl &)
Definition: tag_render.cpp:42
ID3_Header::SetDataSize
bool SetDataSize(size_t size)
Definition: header.h:64
dami::id3::v2::getTrackNum
ID3_C_EXPORT size_t getTrackNum(const ID3_TagImpl &)
Definition: helpers.cpp:350
ID3_V1_LEN_TITLE
Definition: globals.h:333
ID3_TagImpl::GetUnsync
bool GetUnsync() const
Definition: tag_impl.cpp:258
tag_impl.h
ID3V2_3_0
Definition: globals.h:166
ID3_TagImpl::RenderExtHeader
void RenderExtHeader(uchar *)
Definition: tag_render.cpp:178
ID3_TagImpl::GetSpec
ID3_V2Spec GetSpec() const
Definition: tag_impl.cpp:232
ID3_V1_LEN_YEAR
Definition: globals.h:336
ID3_Frame
The representative class of an id3v2 frame.
Definition: id3lib_frame.h:38
ID3_TagImpl::const_iterator
Frames::const_iterator const_iterator
Definition: tag_impl.h:78
writers.h
ID3_TagHeader::SetExperimental
bool SetExperimental(bool b)
Definition: header_tag.h:80
ID3_TagHeader::SetSpec
bool SetSpec(ID3_V2Spec)
Definition: header_tag.cpp:39
dami::id3::v2::getV1Comment
ID3_C_EXPORT String getV1Comment(const ID3_TagImpl &)
Definition: helpers.cpp:243
ID3_Writer
Definition: writer.h:33
helpers.h
ID3_TagImpl::Size
size_t Size() const
Definition: tag_render.cpp:140
ID3_TagHeader::SetExtended
bool SetExtended(bool b)
Definition: header_tag.h:73
ID3_TagHeader::Render
void Render(ID3_Writer &) const
Definition: header_tag.cpp:66
luint
long unsigned int luint
Definition: globals.h:115
ID3_PADMULTIPLE
#define ID3_PADMULTIPLE
Definition: tag_render.cpp:188
ID3_Writer::writeChar
virtual int_type writeChar(char_type ch)
Write a single character and advance the internal position.
Definition: writer.h:71
ID3_TagHeader::Size
size_t Size() const
Definition: header_tag.cpp:53
ID3_Frame::Render
void Render(ID3_Writer &) const
Definition: frame.cpp:204