id3lib
3.8.3
|
00001 // $Id: field_binary.cpp,v 1.27 2003/03/02 14:23:59 t1mpy 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 #include <stdio.h> 00028 //#include <string.h> 00029 #include <memory.h> 00030 00031 #include "field_impl.h" 00032 #include "reader.h" 00033 #include "writer.h" 00034 #include "io_helpers.h" 00035 #include "id3/utils.h" // has <config.h> "id3/id3lib_streams.h" "id3/globals.h" "id3/id3lib_strings.h" 00036 00037 using namespace dami; 00038 00039 size_t ID3_FieldImpl::Set(const uchar* data, size_t len) 00040 { 00041 size_t size = 0; 00042 if ((this->GetType() == ID3FTY_BINARY) && data && len) 00043 { 00044 BString str(data, len); 00045 size = dami::min(len, this->SetBinary(str)); 00046 } 00047 return size; 00048 } 00049 00055 size_t ID3_FieldImpl::SetBinary(BString data) //< data to assign to this field. 00056 { 00057 size_t size = 0; 00058 if (this->GetType() == ID3FTY_BINARY) 00059 { 00060 this->Clear(); 00061 size_t fixed = _fixed_size; 00062 size = data.size(); 00063 if (fixed == 0) 00064 { 00065 _binary = data; 00066 } 00067 else 00068 { 00069 _binary.assign(data, 0, dami::min(size, fixed)); 00070 if (size < fixed) 00071 { 00072 _binary.append(fixed - size, '\0'); 00073 } 00074 } 00075 size = _binary.size(); 00076 _changed = true; 00077 } 00078 return size; 00079 } 00080 00081 BString ID3_FieldImpl::GetBinary() const 00082 { 00083 BString data; 00084 if (this->GetType() == ID3FTY_BINARY) 00085 { 00086 data = _binary; 00087 } 00088 return data; 00089 } 00090 00091 00092 const uchar* ID3_FieldImpl::GetRawBinary() const 00093 { 00094 const uchar* data = NULL; 00095 if (this->GetType() == ID3FTY_BINARY) 00096 { 00097 data = _binary.data(); 00098 } 00099 return data; 00100 } 00101 00102 00113 size_t ID3_FieldImpl::Get(uchar *buffer, //< Destination of retrieved string 00114 size_t max_bytes //< Max number of bytes to copy 00115 ) const 00116 { 00117 size_t bytes = 0; 00118 if (this->GetType() == ID3FTY_BINARY) 00119 { 00120 bytes = dami::min(max_bytes, this->Size()); 00121 if (NULL != buffer && bytes > 0) 00122 { 00123 ::memcpy(buffer, _binary.data(), bytes); 00124 } 00125 } 00126 return bytes; 00127 } 00128 00129 00136 void ID3_FieldImpl::FromFile(const char *info //< Source filename 00137 ) 00138 { 00139 if (this->GetType() != ID3FTY_BINARY || NULL == info) 00140 { 00141 return; 00142 } 00143 00144 FILE* temp_file = ::fopen(info, "rb"); 00145 if (temp_file != NULL) 00146 { 00147 ::fseek(temp_file, 0, SEEK_END); 00148 size_t fileSize = ::ftell(temp_file); 00149 ::fseek(temp_file, 0, SEEK_SET); 00150 00151 uchar* buffer = new uchar[fileSize]; 00152 if (buffer != NULL) 00153 { 00154 ::fread(buffer, 1, fileSize, temp_file); 00155 00156 this->Set(buffer, fileSize); 00157 00158 delete [] buffer; 00159 } 00160 00161 ::fclose(temp_file); 00162 } 00163 } 00164 00165 00172 void ID3_FieldImpl::ToFile(const char *info //< Destination filename 00173 ) const 00174 { 00175 if (this->GetType() != ID3FTY_BINARY || NULL == info) 00176 { 00177 return; 00178 } 00179 00180 size_t size = this->Size(); 00181 if (size > 0) 00182 { 00183 FILE* temp_file = ::fopen(info, "wb"); 00184 if (temp_file != NULL) 00185 { 00186 ::fwrite(_binary.data(), 1, size, temp_file); 00187 ::fclose(temp_file); 00188 } 00189 } 00190 00191 return ; 00192 } 00193 00194 00195 bool ID3_FieldImpl::ParseBinary(ID3_Reader& reader) 00196 { 00197 // copy the remaining bytes, unless we're fixed length, in which case copy 00198 // the minimum of the remaining bytes vs. the fixed length 00199 _binary = io::readAllBinary(reader); 00200 return true; 00201 } 00202 00203 void ID3_FieldImpl::RenderBinary(ID3_Writer& writer) const 00204 { 00205 writer.writeChars(this->GetRawBinary(), this->Size()); 00206 } 00207