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
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)
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,
00119 size_t max_bytes
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
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
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
00203
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