id3lib
3.8.3
|
00001 // $Id: field_string_unicode.cpp,v 1.33 2003/03/02 14:23:58 t1mpy Exp $ 00002 00003 // id3lib: a C++ library for creating and manipulating id3v1/v2 tags 00004 // Copyright 1999, 2000 Scott Thomas Haug 00005 00006 // This library is free software; you can redistribute it and/or modify it 00007 // under the terms of the GNU Library General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or (at your 00009 // option) any later version. 00010 // 00011 // This library is distributed in the hope that it will be useful, but WITHOUT 00012 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 // License for more details. 00015 // 00016 // You should have received a copy of the GNU Library General Public License 00017 // along with this library; if not, write to the Free Software Foundation, 00018 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 00020 // The id3lib authors encourage improvements and optimisations to be sent to 00021 // the id3lib coordinator. Please see the README file for details on where to 00022 // send such submissions. See the AUTHORS file for a list of people who have 00023 // contributed to id3lib. See the ChangeLog file for a list of changes to 00024 // id3lib. These files are distributed with id3lib at 00025 // http://download.sourceforge.net/id3lib/ 00026 00027 #include "field_impl.h" 00028 #include "id3/utils.h" // has <config.h> "id3/id3lib_streams.h" "id3/globals.h" "id3/id3lib_strings.h" 00029 #include "io_helpers.h" 00030 00031 using namespace dami; 00032 00049 size_t ID3_FieldImpl::Set(const unicode_t* data) 00050 { 00051 size_t size = 0; 00052 if (this->GetType() == ID3FTY_TEXTSTRING && 00053 this->GetEncoding() == ID3TE_UNICODE && data) 00054 { 00055 String text((const char*) data, ucslen(data) * 2); 00056 size = this->SetText_i(text); 00057 } 00058 return size; 00059 } 00060 00061 size_t ID3_FieldImpl::Add(const unicode_t* data) 00062 { 00063 size_t size = 0; 00064 if (this->GetType() == ID3FTY_TEXTSTRING && 00065 this->GetEncoding() == ID3TE_UNICODE) 00066 { 00067 String text((const char*) data, ucslen(data) * 2); 00068 size = this->AddText_i(text); 00069 } 00070 return size; 00071 } 00072 00093 size_t ID3_FieldImpl::Get(unicode_t *buffer, size_t maxLength) const 00094 { 00095 size_t length = 0; 00096 if (this->GetType() == ID3FTY_TEXTSTRING && 00097 this->GetEncoding() == ID3TE_UNICODE && 00098 buffer != NULL && maxLength > 0) 00099 { 00100 size_t size = this->Size(); 00101 length = dami::min(maxLength, size); 00102 ::memcpy((void *)buffer, (void *)_text.data(), length * 2); 00103 if (length < maxLength) 00104 { 00105 buffer[length] = NULL_UNICODE; 00106 } 00107 } 00108 return length; 00109 } 00110 00111 const unicode_t* ID3_FieldImpl::GetRawUnicodeText() const 00112 { 00113 const unicode_t* text = NULL; 00114 if (this->GetType() == ID3FTY_TEXTSTRING && 00115 this->GetEncoding() == ID3TE_UNICODE) 00116 { 00117 text = (unicode_t *)_text.data(); 00118 } 00119 return text; 00120 } 00121 00122 const unicode_t* ID3_FieldImpl::GetRawUnicodeTextItem(size_t index) const 00123 { 00124 const unicode_t* text = NULL; 00125 if (this->GetType() == ID3FTY_TEXTSTRING && 00126 this->GetEncoding() == ID3TE_UNICODE && 00127 index < this->GetNumTextItems()) 00128 { 00129 String unicode = _text + '\0' + '\0'; 00130 text = (unicode_t *) unicode.data(); 00131 for (size_t i = 0; i < index; ++i) 00132 { 00133 text += ucslen(text) + 1; 00134 } 00135 } 00136 return text; 00137 } 00138 00139 size_t ID3_FieldImpl::Get(unicode_t *buffer, size_t maxLength, size_t itemNum) const 00140 { 00141 size_t length = 0; 00142 size_t total_items = this->GetNumTextItems(); 00143 if (this->GetType() == ID3FTY_TEXTSTRING && 00144 this->GetEncoding() == ID3TE_UNICODE && 00145 buffer != NULL && maxLength > 0 && itemNum < total_items) 00146 { 00147 const unicode_t* text = this->GetRawUnicodeTextItem(itemNum); 00148 if (NULL != text) 00149 { 00150 size_t length = dami::min(maxLength, ucslen(text)); 00151 ::memcpy(buffer, text, length * 2); 00152 if (length < maxLength) 00153 { 00154 buffer[length] = NULL_UNICODE; 00155 } 00156 } 00157 } 00158 00159 return length; 00160 } 00161 00162