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

src/frame_render.cpp

Go to the documentation of this file.
00001 // $Id: frame_render.cpp,v 1.22 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 <zlib.h>
00036 
00037 #include "tag.h"
00038 #include "utils.h"
00039 #include "frame_impl.h"
00040 #include "io_decorators.h"
00041 #include "io_strings.h"
00042 #include "io_helpers.h"
00043 
00044 using namespace dami;
00045 
00046 namespace 
00047 {
00048   void renderFields(ID3_Writer& writer, const ID3_FrameImpl& frame)
00049   {
00050     ID3_TextEnc enc = ID3TE_ASCII;
00051     for (ID3_FrameImpl::const_iterator fi = frame.begin(); fi != frame.end(); ++fi)
00052     {
00053       ID3_Field* fld = *fi;
00054       if (fld != NULL && fld->InScope(frame.GetSpec()))
00055       {
00056         if (fld->GetID() == ID3FN_TEXTENC)  
00057         {
00058           enc = static_cast<ID3_TextEnc>(fld->Get());  
00059           ID3D_NOTICE( "id3::v2::renderFields(): found encoding = " << enc );
00060         }
00061         else
00062         {
00063           fld->SetEncoding(enc);
00064         }
00065         fld->Render(writer);
00066       }
00067     }
00068   }
00069 }
00070   
00071 void ID3_FrameImpl::Render(ID3_Writer& writer) const
00072 {
00073   // Return immediately if we have no fields, which (usually) means we're
00074   // trying to render a frame which has been Cleared or hasn't been initialized
00075   if (!this->NumFields())
00076   {
00077     return;
00078   }
00079 
00080   ID3_FrameHeader hdr;
00081   
00082   const size_t hdr_size = hdr.Size();
00083 
00084   // 1.  Write out the field data to the buffer, with the assumption that
00085   //     we won't be decompressing, since this is the usual behavior
00086   String flds;
00087   io::StringWriter fldWriter(flds);
00088   size_t origSize = 0;
00089   if (!this->GetCompression())
00090   {
00091     renderFields(fldWriter, *this);
00092     origSize = flds.size();
00093     ID3D_NOTICE ( "ID3_FrameImpl::Render(): uncompressed fields" );
00094   }
00095   else
00096   {
00097     io::CompressedWriter cr(fldWriter);
00098     renderFields(cr, *this);
00099     cr.flush();
00100     origSize = cr.getOrigSize();
00101     ID3D_NOTICE ( "ID3_FrameImpl::Render(): compressed fields, orig size = " <<
00102                   origSize );
00103   }
00104 
00105   size_t fldSize = flds.size();
00106   ID3D_NOTICE ( "ID3_FrameImpl::Render(): field size = " << fldSize );
00107   if (fldSize == 0)
00108   {
00109     ID3D_WARNING ( "ID3_FrameImpl::Render(): no field data" );
00110     return;
00111   }
00112   
00113   // determine which flags need to be set
00114   uchar eID = this->GetEncryptionID(), gID = this->GetGroupingID();
00115   hdr.SetFrameID(this->GetID());
00116   hdr.SetEncryption(eID > 0);
00117   hdr.SetGrouping(gID > 0);
00118   hdr.SetCompression(origSize > fldSize);
00119   hdr.SetDataSize(fldSize + ((hdr.GetCompression() ? 4 : 0) + 
00120                              (hdr.GetEncryption()  ? 1 : 0) + 
00121                              (hdr.GetGrouping()    ? 1 : 0)));
00122 
00123   // write out the header
00124   hdr.Render(writer);
00125 
00126   // No-man's land!  Not part of the header, not part of the data
00127   if (hdr.GetCompression())
00128   {
00129     io::writeBENumber(writer, origSize, sizeof(uint32));
00130     ID3D_NOTICE( "ID3_FrameImpl::Render(): frame is compressed, wrote origSize = " << origSize );
00131   }
00132   if (hdr.GetEncryption())
00133   {
00134     writer.writeChar(eID);
00135     ID3D_NOTICE( "ID3_FrameImpl::Render(): frame is compressed, encryption id = " << eID );
00136   }
00137   if (hdr.GetGrouping())
00138   {
00139     writer.writeChar(gID);
00140     ID3D_NOTICE( "ID3_FrameImpl::Render(): frame is compressed, grouping id = " << gID );
00141   }
00142 
00143   // Write the field data
00144   writer.writeChars(flds.data(), fldSize);
00145   _changed = false;
00146 }

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