1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// The authors have released ID3Lib as Public Domain (PD) and claim no copyright,
// patent or other intellectual property protection in this work. This means that
// it may be modified, redistributed and used in commercial and non-commercial
// software and hardware without restrictions. ID3Lib is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
//
// The ID3Lib authors encourage improvements and optimisations to be sent to the
// ID3Lib coordinator, currently Dirk Mahoney (dirk@id3.org). Approved
// submissions may be altered, and will be included and released under these terms.
//
// Mon Nov 23 18:34:01 1998
#include <string.h>
#include "id3_tag.h"
ID3_Frame::ID3_Frame ( ID3_FrameID id )
{
luint lwordsForFields = 0;
version = ID3_TAGVERSION;
revision = ID3_TAGREVISION;
numFields = 0;
fields = NULL;
groupingID[ 0 ] = 0;
encryptionID[ 0 ] = 0;
compression = true;
lwordsForFields = ( ( (luint) ID3FN_LASTFIELDID ) - 1 ) / ( sizeof ( luint ) * 8 );
if ( ( ( (luint) ID3FN_LASTFIELDID ) - 1 ) % ( sizeof ( luint ) * 8 ) != 0 )
lwordsForFields++;
if ( fieldBits = new luint[ lwordsForFields ] )
{
for ( luint i = 0; i < lwordsForFields; i++ )
fieldBits[ i ] = 0;
}
else
ID3_THROW ( ID3E_NoMemory );
SetID ( id );
}
ID3_Frame::~ID3_Frame ( void )
{
Clear();
if ( fieldBits )
delete[] fieldBits;
}
void ID3_Frame::Clear ( void )
{
if ( numFields && fields )
{
for ( luint i = 0; i < numFields; i++ )
delete fields[ i ];
delete[] fields;
fields = NULL;
numFields = 0;
hasChanged = true;
}
return;
}
void ID3_Frame::SetID ( ID3_FrameID id )
{
ID3_FrameDef *info;
Clear();
if ( id != ID3FID_NOFRAME )
{
if ( info = ID3_FindFrameDef ( id ) )
{
frameID = id;
numFields = 0;
while ( info->fieldDefs[ numFields ].id != ID3FN_NOFIELD )
numFields++;
if ( ( fields = new ID3_Field *[ numFields ] ) == NULL )
ID3_THROW ( ID3E_NoMemory );
else
{
for ( luint i = 0; i < numFields; i++ )
{
if ( ( fields[ i ] = new ID3_Field ) == NULL )
ID3_THROW ( ID3E_NoMemory );
else
{
fields[ i ]->name = info->fieldDefs[ i ].id;
fields[ i ]->type = info->fieldDefs[ i ].type;
fields[ i ]->fixedLength = info->fieldDefs[ i ].fixedLength;
fields[ i ]->ioVersion = info->fieldDefs[ i ].version;
fields[ i ]->ioRevision = info->fieldDefs[ i ].revision;
fields[ i ]->control = info->fieldDefs[ i ].control;
fields[ i ]->flags = info->fieldDefs[ i ].flags;
// tell the frame that this field is present
BS_SET ( fieldBits, fields[ i ]->name );
}
}
hasChanged = true;
}
}
else
ID3_THROW ( ID3E_InvalidFrameID );
}
return;
}
ID3_FrameID ID3_Frame::GetID ( void )
{
return frameID;
}
void ID3_Frame::SetVersion ( uchar ver, uchar rev )
{
if ( version != ver || rev != rev )
hasChanged = true;
version = ver;
revision = rev;
return;
}
lsint ID3_Frame::FindField ( ID3_FieldID fieldName )
{
lsint num = 0;
bool done = false;
if ( BS_ISSET ( fieldBits, fieldName ) )
{
while ( ! done && num < (lsint) numFields )
{
if ( fields[ num ]->name == fieldName )
done = true;
else
num++;
}
}
if ( ! done )
num = -1;
return num;
}
ID3_Field& ID3_Frame::Field ( ID3_FieldID fieldName )
{
luint fieldNum = FindField ( fieldName );
if ( fieldNum == -1 )
ID3_THROW ( ID3E_FieldNotFound );
return *fields[ fieldNum ];
}
void ID3_Frame::UpdateFieldDeps ( void )
{
for ( luint i = 0; i < numFields; i++ )
{
if ( fields[ i ]->flags & ID3FF_ADJUSTEDBY )
{
switch ( fields[ i ]->type )
{
case ID3FTY_BITFIELD:
{
luint value = 0;
// now find the field on which this
// field is dependent and get a copy
// of the value of that field.
// then adjust the fixedLength of this
// field to that value / 8.
}
break;
}
}
}
return;
}
void ID3_Frame::UpdateStringTypes ( void )
{
for ( luint i = 0; i < numFields; i++ )
{
if ( fields[ i ]->flags & ID3FF_ADJUSTENC )
{
ID3_TextEnc enc;
ID3_FieldType newType;
enc = (ID3_TextEnc) Field ( ID3FN_TEXTENC ).Get();
switch ( enc )
{
case ID3TE_ASCII:
newType = ID3FTY_ASCIISTRING;
break;
case ID3TE_UNICODE:
newType = ID3FTY_UNICODESTRING;
break;
default:
newType = ID3FTY_ASCIISTRING;
break;
}
fields[ i ]->type = newType;
}
}
return;
}
luint ID3_Frame::Size ( void )
{
luint bytesUsed = 0;
float factor = 1.0;
ID3_FrameHeader header;
header.SetVersion ( version, revision );
bytesUsed += header.Size();
if ( strlen ( encryptionID ) )
bytesUsed++;
if ( strlen ( groupingID ) )
bytesUsed++;
// this call is to tell the string fields
// what they should be rendered/parsed as
// (ASCII or Unicode)
UpdateStringTypes();
for ( luint i = 0; i < numFields; i++ )
{
fields[ i ]->SetVersion ( version, revision );
bytesUsed += fields[ i ]->BinSize();
}
return bytesUsed;
}
bool ID3_Frame::HasChanged ( void )
{
bool changed = hasChanged;
if ( ! changed )
{
for ( luint i = 0; i < numFields; i++ )
{
changed = fields[ i ]->HasChanged();
if ( changed )
break;
}
}
return changed;
}
|