Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

src/header_frame.cpp

Go to the documentation of this file.
00001 // $Id: header_frame.cpp,v 1.20 2000/10/29 01:37:29 eldamitri Exp $
00002 
00003 // id3lib: a C++ library for creating and manipulating id3v1/v2 tags
00004 // Copyright 1999, 2000  Scott Thomas Haug
00005 
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Library General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or (at your
00009 // option) any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful, but WITHOUT
00012 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014 // License for more details.
00015 //
00016 // You should have received a copy of the GNU Library General Public License
00017 // along with this library; if not, write to the Free Software Foundation,
00018 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019 
00020 // The id3lib authors encourage improvements and optimisations to be sent to
00021 // the id3lib coordinator.  Please see the README file for details on where to
00022 // send such submissions.  See the AUTHORS file for a list of people who have
00023 // contributed to id3lib.  See the ChangeLog file for a list of changes to
00024 // id3lib.  These files are distributed with id3lib at
00025 // http://download.sourceforge.net/id3lib/
00026 
00027 #if defined HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030 
00031 
00032 
00033 #include <string.h>
00034 #include <memory.h>
00035 #include "header_frame.h"
00036 #include "utils.h"
00037 #include "frame_def.h"
00038 #include "field_def.h"
00039 #include "field_impl.h"
00040 #include "io_helpers.h"
00041 
00042 using namespace dami;
00043 
00044 void ID3_FrameHeader::SetUnknownFrame(const char* id)
00045 {
00046   Clear();
00047   _frame_def = new ID3_FrameDef;
00048   if (NULL == _frame_def)
00049   {
00050     // log this;
00051     return;
00052   }
00053   _frame_def->eID = ID3FID_NOFRAME;
00054   _frame_def->bTagDiscard = false;
00055   _frame_def->bFileDiscard = false;
00056   _frame_def->aeFieldDefs = ID3_FieldDef::DEFAULT;
00057   _frame_def->sDescription = NULL;
00058   if (strlen(id) <= 3)
00059   {
00060     strcpy(_frame_def->sShortTextID, id);
00061     strcpy(_frame_def->sLongTextID, "");
00062   }
00063   else
00064   {
00065     strcpy(_frame_def->sLongTextID, id);
00066     strcpy(_frame_def->sShortTextID, "");
00067   }
00068   _dyn_frame_def = true;
00069 }
00070 
00071 bool ID3_FrameHeader::SetFrameID(ID3_FrameID id)
00072 {
00073   if (id == ID3FID_NOFRAME || id == this->GetFrameID())
00074   {
00075     return false;
00076   }
00077   _frame_def = ID3_FindFrameDef(id);
00078   _flags.set(TAGALTER, _frame_def->bTagDiscard);
00079   _flags.set(FILEALTER, _frame_def->bFileDiscard);
00080 
00081   _changed = true;
00082   return true;
00083 }
00084 
00085 size_t ID3_FrameHeader::Size() const
00086 {
00087   if (!_info)
00088   {
00089     return 0;
00090   }
00091   return 
00092     _info->frame_bytes_id   + 
00093     _info->frame_bytes_size + 
00094     _info->frame_bytes_flags;
00095 }
00096 
00097 bool ID3_FrameHeader::Parse(ID3_Reader& reader)
00098 {
00099   ID3D_NOTICE( "ID3_FrameHeader::Parse(): getCur() = " << reader.getCur() );
00100   io::ExitTrigger et(reader);
00101   if (!_info)
00102   {
00103     return false;
00104   }
00105   if (reader.getEnd() < reader.getCur() + 10)
00106   {
00107     return false;
00108   }
00109 
00110   String textID = io::readText(reader, _info->frame_bytes_id);
00111 
00112   ID3D_NOTICE( "ID3_FrameHeader::Parse: textID = " << textID );
00113   ID3D_NOTICE( "ID3_FrameHeader::Parse: getCur() = " << reader.getCur() );
00114 
00115   ID3_FrameID fid = ID3_FindFrameID(textID.c_str());
00116   if (ID3FID_NOFRAME == fid)
00117   {
00118     this->SetUnknownFrame(textID.c_str());
00119     ID3D_NOTICE( "ID3_FrameHeader::Parse: unknown frame id" );
00120   }
00121   else
00122   {
00123     this->SetFrameID(fid);
00124   }
00125 
00126   uint32 dataSize = io::readBENumber(reader, _info->frame_bytes_size);
00127   ID3D_NOTICE( "ID3_FrameHeader::Parse: dataSize = " << dataSize );
00128   ID3D_NOTICE( "ID3_FrameHeader::Parse: getCur() = " << reader.getCur() );
00129   this->SetDataSize(dataSize);
00130 
00131   uint32 flags = io::readBENumber(reader, _info->frame_bytes_flags);
00132   _flags.add(flags);
00133 
00134   ID3D_NOTICE( "ID3_FrameHeader::Parse: flags = " << flags );
00135   ID3D_NOTICE( "ID3_FrameHeader::Parse: getCur() = " << reader.getCur() );
00136   et.setExitPos(reader.getCur());
00137 
00138   return true;
00139 }
00140 
00141 void ID3_FrameHeader::Render(ID3_Writer& writer) const
00142 {
00143   size_t size = 0;
00144 
00145   if (NULL == _frame_def)
00146   {
00147     // TODO: log this
00148     ID3D_WARNING( "ID3_FrameHeader::Render(): _frame_def is NULL!" );
00149     return;
00150     //ID3_THROW(ID3E_InvalidFrameID);
00151   }
00152   char *textID;
00153   if (_info->frame_bytes_id == strlen(_frame_def->sShortTextID))
00154   {
00155     textID = _frame_def->sShortTextID;
00156   }
00157   else
00158   {
00159     textID = _frame_def->sLongTextID;
00160   }
00161 
00162   ID3D_NOTICE( "ID3_FrameHeader::Render(): writing " << textID << ", " << (int) _info->frame_bytes_size << " bytes");
00163   writer.writeChars((uchar *) textID, _info->frame_bytes_id);
00164 
00165   io::writeBENumber(writer, _data_size, _info->frame_bytes_size);
00166   io::writeBENumber(writer, _flags.get(), _info->frame_bytes_flags);
00167 }
00168 
00169 const char* ID3_FrameHeader::GetTextID() const
00170 {
00171   char *textID = "";
00172   if (_info && _frame_def)
00173   {
00174     if (_info->frame_bytes_id == strlen(_frame_def->sShortTextID))
00175     {
00176       textID = _frame_def->sShortTextID;
00177     }
00178     else
00179     {
00180       textID = _frame_def->sLongTextID;
00181     }
00182   }
00183   return textID;
00184 }
00185 
00186 ID3_FrameHeader& ID3_FrameHeader::operator=(const ID3_FrameHeader& hdr)
00187 {
00188   if (this != &hdr)
00189   {
00190     this->Clear();
00191     this->ID3_Header::operator=(hdr);
00192     if (!hdr._dyn_frame_def)
00193     {
00194       _frame_def = hdr._frame_def;
00195     }
00196     else
00197     {
00198       _frame_def = new ID3_FrameDef;
00199       if (NULL == _frame_def)
00200       {
00201         // TODO: throw something here...
00202       }
00203       _frame_def->eID = hdr._frame_def->eID;
00204       _frame_def->bTagDiscard = hdr._frame_def->bTagDiscard;
00205       _frame_def->bFileDiscard = hdr._frame_def->bFileDiscard;
00206       _frame_def->aeFieldDefs = hdr._frame_def->aeFieldDefs;
00207       strcpy(_frame_def->sShortTextID, hdr._frame_def->sShortTextID);
00208       strcpy(_frame_def->sLongTextID, hdr._frame_def->sLongTextID);
00209       _dyn_frame_def = true;
00210     }
00211   }
00212   return *this;
00213 }
00214 
00215 ID3_FrameID ID3_FrameHeader::GetFrameID() const
00216 {
00217   ID3_FrameID eID = ID3FID_NOFRAME;
00218   if (NULL != _frame_def)
00219   {
00220     eID = _frame_def->eID;
00221   }
00222 
00223   return eID;
00224 }
00225 
00226 const ID3_FrameDef *ID3_FrameHeader::GetFrameDef() const
00227 {
00228   return _frame_def;
00229 }
00230 
00231 bool ID3_FrameHeader::Clear()
00232 {
00233   bool changed = this->ID3_Header::Clear();
00234   if (_dyn_frame_def)
00235   {
00236     delete _frame_def;
00237     _dyn_frame_def = false;
00238     changed = true;
00239   }
00240   if (_frame_def)
00241   {
00242     _frame_def = NULL;
00243     changed = true;
00244   }
00245   return changed;
00246 }

Generated at Sat Sep 8 15:51:09 2001 for id3lib by doxygen1.2.8 written by Dimitri van Heesch, © 1997-2001