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

src/field_binary.cpp

Go to the documentation of this file.
00001 // $Id: field_binary.cpp,v 1.25 2001/08/26 23:33:02 dmazzoni 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 
00028 #if defined HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <memory.h>
00035 
00036 #include "field_impl.h"
00037 #include "reader.h"
00038 #include "writer.h"
00039 #include "io_helpers.h"
00040 #include "utils.h"
00041 
00042 using namespace dami;
00043 
00044 size_t ID3_FieldImpl::Set(const uchar* data, size_t len)
00045 {
00046   size_t size = 0;
00047   if (this->GetType() == ID3FTY_BINARY)
00048   {
00049     BString str(data, len);
00050     size = dami::min(len, this->SetBinary(str));
00051   }
00052   return size;
00053 }
00054 
00060 size_t ID3_FieldImpl::SetBinary(BString data) //< data to assign to this field.
00061 {
00062   size_t size = 0;
00063   if (this->GetType() == ID3FTY_BINARY)
00064   {
00065     this->Clear();
00066     size_t fixed = _fixed_size;
00067     size = data.size();
00068     if (fixed == 0)
00069     {
00070       _binary = data;
00071     }
00072     else
00073     {
00074       _binary.assign(data, 0, dami::min(size, fixed));
00075       if (size < fixed)
00076       {
00077         _binary.append(fixed - size, '\0');
00078       }
00079     }
00080     size = _binary.size();
00081     _changed = true;
00082   }
00083   return size;
00084 }
00085 
00086 BString ID3_FieldImpl::GetBinary() const
00087 {
00088   BString data;
00089   if (this->GetType() == ID3FTY_BINARY)
00090   {
00091     data = _binary;
00092   }
00093   return data;
00094 }
00095 
00096 
00097 const uchar* ID3_FieldImpl::GetRawBinary() const
00098 {
00099   const uchar* data = NULL;
00100   if (this->GetType() == ID3FTY_BINARY)
00101   {
00102     data = _binary.data();
00103   }
00104   return data;
00105 }
00106 
00107 
00118 size_t ID3_FieldImpl::Get(uchar *buffer,    //< Destination of retrieved string
00119                           size_t max_bytes //< Max number of bytes to copy
00120                           ) const
00121 {
00122   size_t bytes = 0;
00123   if (this->GetType() == ID3FTY_BINARY)
00124   {
00125     bytes = dami::min(max_bytes, this->Size());
00126     if (NULL != buffer && bytes > 0)
00127     {
00128       ::memcpy(buffer, _binary.data(), bytes);
00129     }
00130   }
00131   return bytes;
00132 }
00133 
00134 
00141 void ID3_FieldImpl::FromFile(const char *info //< Source filename
00142                              )
00143 {
00144   if (this->GetType() != ID3FTY_BINARY || NULL == info)
00145   {
00146     return;
00147   }
00148     
00149   FILE* temp_file = ::fopen(info, "rb");
00150   if (temp_file != NULL)
00151   {
00152     ::fseek(temp_file, 0, SEEK_END);
00153     size_t fileSize = ::ftell(temp_file);
00154     ::fseek(temp_file, 0, SEEK_SET);
00155     
00156     uchar* buffer = new uchar[fileSize];
00157     if (buffer != NULL)
00158     {
00159       ::fread(buffer, 1, fileSize, temp_file);
00160       
00161       this->Set(buffer, fileSize);
00162       
00163       delete [] buffer;
00164     }
00165     
00166     ::fclose(temp_file);
00167   }
00168 }
00169 
00170 
00177 void ID3_FieldImpl::ToFile(const char *info //< Destination filename
00178                            ) const
00179 {
00180   if (this->GetType() != ID3FTY_BINARY || NULL == info)
00181   {
00182     return;
00183   }
00184     
00185   size_t size = this->Size();
00186   if (size > 0)
00187   {
00188     FILE* temp_file = ::fopen(info, "wb");
00189     if (temp_file != NULL)
00190     {
00191       ::fwrite(_binary.data(), 1, size, temp_file);
00192       ::fclose(temp_file);
00193     }
00194   }
00195   
00196   return ;
00197 }
00198 
00199 
00200 bool ID3_FieldImpl::ParseBinary(ID3_Reader& reader)
00201 {
00202   // copy the remaining bytes, unless we're fixed length, in which case copy
00203   // the minimum of the remaining bytes vs. the fixed length
00204   _binary = io::readAllBinary(reader);
00205   return true;
00206 }
00207 
00208 void ID3_FieldImpl::RenderBinary(ID3_Writer& writer) const
00209 {
00210   writer.writeChars(this->GetRawBinary(), this->Size());
00211 }
00212 

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