id3lib  3.8.3
field_string_unicode.cpp
Go to the documentation of this file.
1 // $Id: field_string_unicode.cpp,v 1.33 2003/03/02 14:23:58 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 "field_impl.h"
28 #include "id3/utils.h" // has <config.h> "id3/id3lib_streams.h" "id3/globals.h" "id3/id3lib_strings.h"
29 #include "io_helpers.h"
30 
31 using namespace dami;
32 
49 size_t ID3_FieldImpl::Set(const unicode_t* data)
50 {
51  size_t size = 0;
52  if (this->GetType() == ID3FTY_TEXTSTRING &&
53  this->GetEncoding() == ID3TE_UNICODE && data)
54  {
55  String text((const char*) data, ucslen(data) * 2);
56  size = this->SetText_i(text);
57  }
58  return size;
59 }
60 
61 size_t ID3_FieldImpl::Add(const unicode_t* data)
62 {
63  size_t size = 0;
64  if (this->GetType() == ID3FTY_TEXTSTRING &&
65  this->GetEncoding() == ID3TE_UNICODE)
66  {
67  String text((const char*) data, ucslen(data) * 2);
68  size = this->AddText_i(text);
69  }
70  return size;
71 }
72 
93 size_t ID3_FieldImpl::Get(unicode_t *buffer, size_t maxLength) const
94 {
95  size_t length = 0;
96  if (this->GetType() == ID3FTY_TEXTSTRING &&
97  this->GetEncoding() == ID3TE_UNICODE &&
98  buffer != NULL && maxLength > 0)
99  {
100  size_t size = this->Size();
101  length = dami::min(maxLength, size);
102  ::memcpy((void *)buffer, (void *)_text.data(), length * 2);
103  if (length < maxLength)
104  {
105  buffer[length] = NULL_UNICODE;
106  }
107  }
108  return length;
109 }
110 
112 {
113  const unicode_t* text = NULL;
114  if (this->GetType() == ID3FTY_TEXTSTRING &&
115  this->GetEncoding() == ID3TE_UNICODE)
116  {
117  text = (unicode_t *)_text.data();
118  }
119  return text;
120 }
121 
123 {
124  const unicode_t* text = NULL;
125  if (this->GetType() == ID3FTY_TEXTSTRING &&
126  this->GetEncoding() == ID3TE_UNICODE &&
127  index < this->GetNumTextItems())
128  {
129  String unicode = _text + '\0' + '\0';
130  text = (unicode_t *) unicode.data();
131  for (size_t i = 0; i < index; ++i)
132  {
133  text += ucslen(text) + 1;
134  }
135  }
136  return text;
137 }
138 
139 size_t ID3_FieldImpl::Get(unicode_t *buffer, size_t maxLength, size_t itemNum) const
140 {
141  size_t length = 0;
142  size_t total_items = this->GetNumTextItems();
143  if (this->GetType() == ID3FTY_TEXTSTRING &&
144  this->GetEncoding() == ID3TE_UNICODE &&
145  buffer != NULL && maxLength > 0 && itemNum < total_items)
146  {
147  const unicode_t* text = this->GetRawUnicodeTextItem(itemNum);
148  if (NULL != text)
149  {
150  size_t length = dami::min(maxLength, ucslen(text));
151  ::memcpy(buffer, text, length * 2);
152  if (length < maxLength)
153  {
154  buffer[length] = NULL_UNICODE;
155  }
156  }
157  }
158 
159  return length;
160 }
161 
162 
size_t Add(const char *data)
const unicode_t * GetRawUnicodeTextItem(size_t) const
const unicode_t * GetRawUnicodeText() const
void Set(uint32)
Sets the value of the field to the specified integer.
uint32 Get() const
Returns the value of the integer field.
#define NULL
Definition: globals.h:743
@ ID3TE_UNICODE
Definition: globals.h:146
uint16 unicode_t
Definition: globals.h:117
@ ID3FTY_TEXTSTRING
Definition: globals.h:356
#define NULL_UNICODE
Definition: globals.h:120
Definition: tag_impl.h:42
const T & min(const T &a, const T &b)
Definition: utils.h:51
size_t ID3_C_EXPORT ucslen(const unicode_t *unicode)
Definition: utils.cpp:216