id3lib  3.8.3
field_binary.cpp
Go to the documentation of this file.
1 // $Id: field_binary.cpp,v 1.27 2003/03/02 14:23:59 t1mpy Exp $
2 
3 // id3lib: a C++ library for creating and manipulating id3v1/v2 tags
4 // Copyright 1999, 2000 Scott Thomas Haug
5 
6 // This library is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU Library General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or (at your
9 // option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU Library General Public License
17 // along with this library; if not, write to the Free Software Foundation,
18 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 // The id3lib authors encourage improvements and optimisations to be sent to
21 // the id3lib coordinator. Please see the README file for details on where to
22 // send such submissions. See the AUTHORS file for a list of people who have
23 // contributed to id3lib. See the ChangeLog file for a list of changes to
24 // id3lib. These files are distributed with id3lib at
25 // http://download.sourceforge.net/id3lib/
26 
27 #include <stdio.h>
28 //#include <string.h>
29 #include <memory.h>
30 
31 #include "field_impl.h"
32 #include "reader.h"
33 #include "writer.h"
34 #include "io_helpers.h"
35 #include "id3/utils.h" // has <config.h> "id3/id3lib_streams.h" "id3/globals.h" "id3/id3lib_strings.h"
36 
37 using namespace dami;
38 
39 size_t ID3_FieldImpl::Set(const uchar* data, size_t len)
40 {
41  size_t size = 0;
42  if ((this->GetType() == ID3FTY_BINARY) && data && len)
43  {
44  BString str(data, len);
45  size = dami::min(len, this->SetBinary(str));
46  }
47  return size;
48 }
49 
55 size_t ID3_FieldImpl::SetBinary(BString data) //< data to assign to this field.
56 {
57  size_t size = 0;
58  if (this->GetType() == ID3FTY_BINARY)
59  {
60  this->Clear();
61  size_t fixed = _fixed_size;
62  size = data.size();
63  if (fixed == 0)
64  {
65  _binary = data;
66  }
67  else
68  {
69  _binary.assign(data, 0, dami::min(size, fixed));
70  if (size < fixed)
71  {
72  _binary.append(fixed - size, '\0');
73  }
74  }
75  size = _binary.size();
76  _changed = true;
77  }
78  return size;
79 }
80 
81 BString ID3_FieldImpl::GetBinary() const
82 {
83  BString data;
84  if (this->GetType() == ID3FTY_BINARY)
85  {
86  data = _binary;
87  }
88  return data;
89 }
90 
91 
93 {
94  const uchar* data = NULL;
95  if (this->GetType() == ID3FTY_BINARY)
96  {
97  data = _binary.data();
98  }
99  return data;
100 }
101 
102 
113 size_t ID3_FieldImpl::Get(uchar *buffer, //< Destination of retrieved string
114  size_t max_bytes //< Max number of bytes to copy
115  ) const
116 {
117  size_t bytes = 0;
118  if (this->GetType() == ID3FTY_BINARY)
119  {
120  bytes = dami::min(max_bytes, this->Size());
121  if (NULL != buffer && bytes > 0)
122  {
123  ::memcpy(buffer, _binary.data(), bytes);
124  }
125  }
126  return bytes;
127 }
128 
129 
136 void ID3_FieldImpl::FromFile(const char *info //< Source filename
137  )
138 {
139  if (this->GetType() != ID3FTY_BINARY || NULL == info)
140  {
141  return;
142  }
143 
144  FILE* temp_file = ::fopen(info, "rb");
145  if (temp_file != NULL)
146  {
147  ::fseek(temp_file, 0, SEEK_END);
148  size_t fileSize = ::ftell(temp_file);
149  ::fseek(temp_file, 0, SEEK_SET);
150 
151  uchar* buffer = new uchar[fileSize];
152  if (buffer != NULL)
153  {
154  ::fread(buffer, 1, fileSize, temp_file);
155 
156  this->Set(buffer, fileSize);
157 
158  delete [] buffer;
159  }
160 
161  ::fclose(temp_file);
162  }
163 }
164 
165 
172 void ID3_FieldImpl::ToFile(const char *info //< Destination filename
173  ) const
174 {
175  if (this->GetType() != ID3FTY_BINARY || NULL == info)
176  {
177  return;
178  }
179 
180  size_t size = this->Size();
181  if (size > 0)
182  {
183  FILE* temp_file = ::fopen(info, "wb");
184  if (temp_file != NULL)
185  {
186  ::fwrite(_binary.data(), 1, size, temp_file);
187  ::fclose(temp_file);
188  }
189  }
190 
191  return ;
192 }
193 
194 
196 {
197  // copy the remaining bytes, unless we're fixed length, in which case copy
198  // the minimum of the remaining bytes vs. the fixed length
199  _binary = io::readAllBinary(reader);
200  return true;
201 }
202 
204 {
205  writer.writeChars(this->GetRawBinary(), this->Size());
206 }
207 
void ToFile(const char *sInfo) const
Copies binary data from the field to the specified file.
bool ParseBinary(ID3_Reader &)
void Set(uint32)
Sets the value of the field to the specified integer.
uint32 Get() const
Returns the value of the integer field.
const uchar * GetRawBinary() const
void RenderBinary(ID3_Writer &) const
size_t SetBinary(dami::BString)
Copies the supplied unicode string to the field.
dami::BString GetBinary() const
void FromFile(const char *)
Copies binary data from the file specified to the field.
virtual size_type writeChars(const char_type buf[], size_type len)=0
Write up to len characters into buf and advance the internal position accordingly.
#define NULL
Definition: globals.h:743
unsigned char uchar
Definition: globals.h:114
@ ID3FTY_BINARY
Definition: globals.h:355
Definition: tag_impl.h:42
const T & min(const T &a, const T &b)
Definition: utils.h:51