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 
33 /** \fn ID3_Field& ID3_Field::operator=(const unicode_t*)
34  ** \brief Shortcut for the Set operator.
35  ** Performs similarly as operator=(const char*), taking a unicode_t
36  ** string as a parameter rather than an ascii string.
37  ** \sa Set(const unicode_t*)
38  ** \param string The string to assign to the field
39  **/
40 
41 /** \brief Copies the supplied unicode string to the field.
42  **
43  ** Performs similarly as the ASCII Set() method, taking a unicode_t string
44  ** as a parameter rather than an ascii string.
45  **
46  ** \param string The unicode string to set this field to.
47  ** \sa Add(const unicode_t*)
48  **/
Set(const unicode_t * data)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 
Add(const unicode_t * data)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 
73 /** Copies the contents of the field into the supplied buffer, up to the
74  ** number of characters specified; for fields with multiple entries, the
75  ** optional third parameter indicates which of the fields to retrieve.
76  **
77  ** Performs similarly as the ASCII Get(char *, size_t, size_t) method, taking
78  ** a unicode_t string as a parameter rather than an ascii string.  The
79  ** maxChars parameter still represents the maximum number of characters, not
80  ** bytes.
81  **
82  ** \code
83  **   unicode_t myBuffer[1024];
84  **   size_t charsUsed = myFrame.GetField(ID3FN_UNICODE)->Get(buffer, 1024);
85  ** \endcode
86  **
87  ** \param buffer   Where the field's data is copied to
88  ** \param maxChars The maximum number of characters to copy to the buffer.
89  ** \param itemNum  For fields with multiple items (such as the involved
90  **                 people frame, the item number to retrieve.
91  ** \sa Get(char *, size_t, size_t)
92  **/
Get(unicode_t * buffer,size_t maxLength) const93 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 
GetRawUnicodeText() const111 const unicode_t* ID3_FieldImpl::GetRawUnicodeText() const
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 
GetRawUnicodeTextItem(size_t index) const122 const unicode_t* ID3_FieldImpl::GetRawUnicodeTextItem(size_t index) const
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 
Get(unicode_t * buffer,size_t maxLength,size_t itemNum) const139 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 
163