00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028
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"
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)
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,
00114 size_t max_bytes
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
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
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
00198
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