id3lib  3.8.3
frame_impl.cpp
Go to the documentation of this file.
1 // $Id: frame_impl.cpp,v 1.9 2002/07/02 22:12:38 t1mpy Exp $
2 
3 // id3lib: a C++ library for creating and manipulating id3v1/v2 tags
4 // Copyright 1999, 2000 Scott Thomas Haug
5 
6 // This library is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU Library General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or (at your
9 // option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU Library General Public License
17 // along with this library; if not, write to the Free Software Foundation,
18 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 // The id3lib authors encourage improvements and optimisations to be sent to
21 // the id3lib coordinator. Please see the README file for details on where to
22 // send such submissions. See the AUTHORS file for a list of people who have
23 // contributed to id3lib. See the ChangeLog file for a list of changes to
24 // id3lib. These files are distributed with id3lib at
25 // http://download.sourceforge.net/id3lib/
26 
27 #if defined HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 
31 //#include <string.h>
32 #include "tag.h"
33 #include "frame_impl.h"
34 #include "field_impl.h"
35 #include "frame_def.h"
36 #include "field_def.h"
37 
39  : _changed(false),
40  _bitset(),
41  _fields(),
42  _encryption_id('\0'),
43  _grouping_id('\0')
44 {
45  this->SetSpec(ID3V2_LATEST);
46  this->SetID(id);
47 }
48 
50  : _changed(false),
51  _bitset(),
52  _fields(),
53  _hdr(hdr),
54  _encryption_id('\0'),
55  _grouping_id('\0')
56 {
57  this->_InitFields();
58 }
59 
61  : _changed(false),
62  _bitset(),
63  _fields(),
64  _encryption_id('\0'),
65  _grouping_id('\0')
66 {
67  *this = frame;
68 }
69 
71 {
72  Clear();
73 }
74 
76 {
77  for (iterator fi = _fields.begin(); fi != _fields.end(); ++fi)
78  {
79  delete (ID3_FieldImpl*) *fi;
80  }
81 
82  _fields.clear();
83  _bitset.reset();
84 
85  _changed = true;
86  return true;
87 }
88 
90 {
91  this->_ClearFields();
92  _hdr.Clear();
93  _encryption_id = '\0';
94  _grouping_id = '\0';
95 }
96 
98 {
99  const ID3_FrameDef* info = _hdr.GetFrameDef();
100  if (NULL == info)
101  {
102  // log this
104  _fields.push_back(fld);
105  _bitset.set(fld->GetID());
106  }
107  else
108  {
109 
110  for (size_t i = 0; info->aeFieldDefs[i]._id != ID3FN_NOFIELD; ++i)
111  {
112  ID3_Field* fld = new ID3_FieldImpl(info->aeFieldDefs[i]);
113  _fields.push_back(fld);
114  _bitset.set(fld->GetID());
115  }
116 
117  _changed = true;
118  }
119 }
120 
122 {
123  bool changed = (this->GetID() != id);
124  if (changed)
125  {
126  this->_SetID(id);
127  _changed = true;
128  }
129  return changed;
130 }
131 
133 {
134  bool changed = this->_ClearFields();
135  changed = _hdr.SetFrameID(id) || changed;
136  this->_InitFields();
137  return changed;
138 }
139 
141 {
142  return _hdr.SetSpec(spec);
143 }
144 
146 {
147  return _hdr.GetSpec();
148 }
149 
151 {
152  ID3_Field* field = NULL;
153  if (this->Contains(fieldName))
154  {
155  for (const_iterator fi = _fields.begin(); fi != _fields.end(); ++fi)
156  {
157  if ((*fi)->GetID() == fieldName)
158  {
159  field = *fi;
160  break;
161  }
162  }
163  }
164  return field;
165 }
166 
168 {
169  return _fields.size();
170 }
171 
173 {
174  size_t bytesUsed = _hdr.Size();
175 
176  if (this->GetEncryptionID())
177  {
178  bytesUsed++;
179  }
180 
181  if (this->GetGroupingID())
182  {
183  bytesUsed++;
184  }
185 
186  ID3_TextEnc enc = ID3TE_ASCII;
187  for (iterator fi = _fields.begin(); fi != _fields.end(); ++fi)
188  {
189  if (*fi && (*fi)->InScope(this->GetSpec()))
190  {
191  if ((*fi)->GetID() == ID3FN_TEXTENC)
192  {
193  enc = (ID3_TextEnc) (*fi)->Get();
194  }
195  else
196  {
197  (*fi)->SetEncoding(enc);
198  }
199  bytesUsed += (*fi)->BinSize();
200  }
201  }
202 
203  return bytesUsed;
204 }
205 
206 
208 {
209  bool changed = _changed;
210 
211  for (const_iterator fi = _fields.begin(); fi != _fields.end(); ++fi)
212  {
213  if (*fi && (*fi)->InScope(this->GetSpec()))
214  {
215  changed = (*fi)->HasChanged();
216  }
217  }
218 
219  return changed;
220 }
221 
224 {
225  ID3_FrameID eID = rFrame.GetID();
226  this->SetID(eID);
227  ID3_Frame::ConstIterator* ri = rFrame.CreateIterator();
228  iterator li = this->begin();
229  while (li != this->end())
230  {
231  ID3_Field* thisFld = *li++;
232  const ID3_Field* thatFld = ri->GetNext();
233  if (thisFld != NULL && thatFld != NULL)
234  {
235  *thisFld = *thatFld;
236  }
237  }
238  delete ri;
239  this->SetEncryptionID(rFrame.GetEncryptionID());
240  this->SetGroupingID(rFrame.GetGroupingID());
241  this->SetCompression(rFrame.GetCompression());
242  this->SetSpec(rFrame.GetSpec());
243  _changed = false;
244 
245  return *this;
246 }
247 
249 {
250  ID3_FrameDef* myFrameDef = ID3_FindFrameDef(id);
251  if (myFrameDef != NULL)
252  {
253  return myFrameDef->sDescription;
254  }
255  return NULL;
256 }
257 
258 const char* ID3_FrameImpl::GetDescription() const
259 {
260  const ID3_FrameDef* def = _hdr.GetFrameDef();
261  if (def)
262  {
263  return def->sDescription;
264  }
265  return NULL;
266 }
267 
The representative class of an ID3v2 field.
Definition: field.h:37
virtual ID3_FieldID GetID() const =0
const ID3_FrameDef * GetFrameDef() const
size_t Size() const
bool SetFrameID(ID3_FrameID id)
The representative class of an id3v2 frame.
ID3_FrameID GetID() const
Definition: frame_impl.h:59
bool SetGroupingID(uchar id)
Definition: frame_impl.h:106
bool HasChanged() const
Definition: frame_impl.cpp:207
void _InitFields()
Definition: frame_impl.cpp:97
size_t Size()
Definition: frame_impl.cpp:172
uchar GetGroupingID() const
Definition: frame_impl.h:114
uchar GetEncryptionID() const
Definition: frame_impl.h:105
ID3_FrameImpl(ID3_FrameID id=ID3FID_NOFRAME)
Definition: frame_impl.cpp:38
bool SetSpec(ID3_V2Spec)
Definition: frame_impl.cpp:140
size_t NumFields() const
Definition: frame_impl.cpp:167
iterator end()
Definition: frame_impl.h:117
ID3_V2Spec GetSpec() const
Definition: frame_impl.cpp:145
bool SetCompression(bool b)
Sets the compression flag within the frame.
Definition: frame_impl.h:85
Fields::iterator iterator
Definition: frame_impl.h:46
bool SetEncryptionID(uchar id)
Definition: frame_impl.h:97
bool _SetID(ID3_FrameID)
Definition: frame_impl.cpp:132
ID3_Field * GetField(ID3_FieldID name) const
Definition: frame_impl.cpp:150
const char * GetDescription() const
Definition: frame_impl.cpp:258
bool _ClearFields()
Definition: frame_impl.cpp:75
virtual ~ID3_FrameImpl()
Destructor.
Definition: frame_impl.cpp:70
Fields::const_iterator const_iterator
Definition: frame_impl.h:47
ID3_FrameImpl & operator=(const ID3_Frame &)
Definition: frame_impl.cpp:223
bool Contains(ID3_FieldID fld) const
Definition: frame_impl.h:75
iterator begin()
Definition: frame_impl.h:116
bool SetID(ID3_FrameID id)
Definition: frame_impl.cpp:121
ID3_V2Spec GetSpec() const
Definition: header.h:62
virtual bool SetSpec(ID3_V2Spec)
Definition: header.cpp:34
ID3_FrameDef * ID3_FindFrameDef(ID3_FrameID id)
Definition: field.cpp:1075
#define NULL
Definition: globals.h:743
ID3_FieldID
Enumeration of the different types of fields in a frame.
Definition: globals.h:198
@ ID3FN_NOFIELD
No field.
Definition: globals.h:199
@ ID3FN_TEXTENC
Text encoding (unicode or ASCII)
Definition: globals.h:200
ID3_TextEnc
Enumeration of the types of text encodings: ascii or unicode.
Definition: globals.h:138
@ ID3TE_ASCII
Definition: globals.h:145
ID3_V2Spec
Definition: globals.h:162
@ ID3V2_LATEST
Definition: globals.h:169
ID3_FrameID
Enumeration of the different types of frames recognized by id3lib.
Definition: globals.h:230
ID3_FieldID _id
Definition: field_def.h:35
static const ID3_FieldDef * DEFAULT
Definition: field_def.h:42
const ID3_FieldDef * aeFieldDefs
Definition: frame_def.h:43
const char * sDescription
Definition: frame_def.h:44