<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>src/field_string_ascii.cpp Source File</title> <link href="id3lib.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Generated by Doxygen 1.2.12 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="namespacemembers.html">Namespace Members</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> </center> <hr><h1>src/field_string_ascii.cpp</h1><a href="field__string__ascii_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">// $Id: field_string_ascii.cpp,v 1.26 2000/10/29 01:37:28 eldamitri Exp $</font> 00002 00003 <font class="comment">// id3lib: a C++ library for creating and manipulating id3v1/v2 tags</font> 00004 <font class="comment">// Copyright 1999, 2000 Scott Thomas Haug</font> 00005 00006 <font class="comment">// This library is free software; you can redistribute it and/or modify it</font> 00007 <font class="comment">// under the terms of the GNU Library General Public License as published by</font> 00008 <font class="comment">// the Free Software Foundation; either version 2 of the License, or (at your</font> 00009 <font class="comment">// option) any later version.</font> 00010 <font class="comment">//</font> 00011 <font class="comment">// This library is distributed in the hope that it will be useful, but WITHOUT</font> 00012 <font class="comment">// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or</font> 00013 <font class="comment">// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public</font> 00014 <font class="comment">// License for more details.</font> 00015 <font class="comment">//</font> 00016 <font class="comment">// You should have received a copy of the GNU Library General Public License</font> 00017 <font class="comment">// along with this library; if not, write to the Free Software Foundation,</font> 00018 <font class="comment">// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</font> 00019 00020 <font class="comment">// The id3lib authors encourage improvements and optimisations to be sent to</font> 00021 <font class="comment">// the id3lib coordinator. Please see the README file for details on where to</font> 00022 <font class="comment">// send such submissions. See the AUTHORS file for a list of people who have</font> 00023 <font class="comment">// contributed to id3lib. See the ChangeLog file for a list of changes to</font> 00024 <font class="comment">// id3lib. These files are distributed with id3lib at</font> 00025 <font class="comment">// http://download.sourceforge.net/id3lib/</font> 00026 00027 <font class="preprocessor">#if defined HAVE_CONFIG_H</font> 00028 <font class="preprocessor"></font><font class="preprocessor">#include <config.h></font> 00029 <font class="preprocessor">#endif</font> 00030 <font class="preprocessor"></font> 00031 00032 00033 <font class="preprocessor">#include <string.h></font> 00034 <font class="preprocessor">#include <stdlib.h></font> 00035 00036 <font class="preprocessor">#include "<a class="code" href="field__impl_8h.html">field_impl.h</a>"</font> 00037 <font class="preprocessor">#include "utils.h"</font> 00038 <font class="preprocessor">#include "io_helpers.h"</font> 00039 00040 <font class="keyword">using</font> <font class="keyword">namespace </font>dami; 00041 <a name="l00054"></a><a class="code" href="class_i_d3___field_impl.html#a11">00054</a> size_t <a class="code" href="class_i_d3___field_impl.html#a6">ID3_FieldImpl::Set</a>(<font class="keyword">const</font> <font class="keywordtype">char</font>* data) 00055 { 00056 size_t len = 0; 00057 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a>) 00058 { 00059 String str(data); 00060 len = this->SetText_i(str); 00061 } 00062 <font class="keywordflow">return</font> len; 00063 } 00064 00065 <font class="comment">// the ::Get() function for ASCII</font> 00066 <a name="l00096"></a><a class="code" href="class_i_d3___field_impl.html#a12">00096</a> size_t <a class="code" href="class_i_d3___field_impl.html#a7">ID3_FieldImpl::Get</a>(<font class="keywordtype">char</font>* buffer, size_t maxLength)<font class="keyword"> const</font> 00097 <font class="keyword"></font>{ 00098 size_t size = 0; 00099 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a> && 00100 this-><a class="code" href="class_i_d3___field_impl.html#a41">GetEncoding</a>() == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a> && 00101 buffer != <a class="code" href="globals_8h.html#a24">NULL</a> && maxLength > 0) 00102 { 00103 String data = this-><a class="code" href="class_i_d3___field_impl.html#a17">GetText</a>(); 00104 size = dami::min(maxLength, data.size()); 00105 ::memcpy(buffer, data.data(), size); 00106 <font class="keywordflow">if</font> (size < maxLength) 00107 { 00108 buffer[size] = <font class="charliteral">'\0'</font>; 00109 } 00110 } 00111 00112 <font class="keywordflow">return</font> size; 00113 } 00114 <a name="l00115"></a><a class="code" href="class_i_d3___field_impl.html#a13">00115</a> size_t <a class="code" href="class_i_d3___field_impl.html#a7">ID3_FieldImpl::Get</a>(<font class="keywordtype">char</font>* buf, size_t maxLen, <a class="code" href="globals_8h.html#a7">index_t</a> index)<font class="keyword"> const</font> 00116 <font class="keyword"></font>{ 00117 size_t size = 0; 00118 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a> && 00119 this-><a class="code" href="class_i_d3___field_impl.html#a41">GetEncoding</a>() == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a> && 00120 buf != <a class="code" href="globals_8h.html#a24">NULL</a> && maxLen > 0) 00121 { 00122 String data = this-><a class="code" href="class_i_d3___field_impl.html#a18">GetTextItem</a>(index); 00123 size = dami::min(maxLen, data.size()); 00124 ::memcpy(buf, data.data(), size); 00125 <font class="keywordflow">if</font> (size < maxLen) 00126 { 00127 buf[size] = <font class="charliteral">'\0'</font>; 00128 } 00129 } 00130 <font class="keywordflow">return</font> size; 00131 } 00132 <a name="l00133"></a><a class="code" href="class_i_d3___field_impl.html#a17">00133</a> String <a class="code" href="class_i_d3___field_impl.html#a17">ID3_FieldImpl::GetText</a>()<font class="keyword"> const</font> 00134 <font class="keyword"></font>{ 00135 String data; 00136 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a>) 00137 { 00138 data = _text; 00139 } 00140 <font class="keywordflow">return</font> data; 00141 } 00142 <a name="l00143"></a><a class="code" href="class_i_d3___field_impl.html#a18">00143</a> String <a class="code" href="class_i_d3___field_impl.html#a18">ID3_FieldImpl::GetTextItem</a>(<a class="code" href="globals_8h.html#a7">index_t</a> index)<font class="keyword"> const</font> 00144 <font class="keyword"></font>{ 00145 String data; 00146 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a> && 00147 this-><a class="code" href="class_i_d3___field_impl.html#a41">GetEncoding</a>() == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a>) 00148 { 00149 <font class="keyword">const</font> <font class="keywordtype">char</font>* raw = this-><a class="code" href="class_i_d3___field_impl.html#a15">GetRawTextItem</a>(index); 00150 <font class="keywordflow">if</font> (raw != <a class="code" href="globals_8h.html#a24">NULL</a>) 00151 { 00152 data = raw; 00153 } 00154 } 00155 <font class="keywordflow">return</font> data; 00156 } 00157 00158 <font class="keyword">namespace</font> 00159 <font class="keyword"></font>{ 00160 String getFixed(String data, size_t size) 00161 { 00162 String text(data, 0, size); 00163 <font class="keywordflow">if</font> (text.size() < size) 00164 { 00165 text.append(size - text.size(), <font class="charliteral">'\0'</font>); 00166 } 00167 <font class="keywordflow">return</font> text; 00168 } 00169 } 00170 00171 00172 size_t ID3_FieldImpl::SetText_i(String data) 00173 { 00174 this-><a class="code" href="class_i_d3___field_impl.html#a1">Clear</a>(); 00175 <font class="keywordflow">if</font> (_fixed_size > 0) 00176 { 00177 _text = getFixed(data, _fixed_size); 00178 } 00179 <font class="keywordflow">else</font> 00180 { 00181 _text = data; 00182 } 00183 ID3D_NOTICE( <font class="stringliteral">"SetText_i: text = \""</font> << _text << <font class="stringliteral">"\""</font> ); 00184 _changed = <font class="keyword">true</font>; 00185 00186 <font class="keywordflow">if</font> (_text.size() == 0) 00187 { 00188 _num_items = 0; 00189 } 00190 <font class="keywordflow">else</font> 00191 { 00192 _num_items = 1; 00193 } 00194 00195 <font class="keywordflow">return</font> _text.size(); 00196 } 00197 <a name="l00198"></a><a class="code" href="class_i_d3___field_impl.html#a19">00198</a> size_t <a class="code" href="class_i_d3___field_impl.html#a19">ID3_FieldImpl::SetText</a>(String data) 00199 { 00200 size_t len = 0; 00201 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a>) 00202 { 00203 len = this->SetText_i(data); 00204 } 00205 <font class="keywordflow">return</font> len; 00206 } 00207 00208 00222 size_t ID3_FieldImpl::AddText_i(String data) 00223 { 00224 size_t len = 0; <font class="comment">// how much of str we copied into this field (max is strLen)</font> 00225 ID3D_NOTICE (<font class="stringliteral">"ID3_FieldImpl::AddText_i: Adding \""</font> << data << <font class="stringliteral">"\""</font> ); 00226 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a4">GetNumTextItems</a>() == 0) 00227 { 00228 <font class="comment">// there aren't any text items in the field so just assign the string to</font> 00229 <font class="comment">// the field</font> 00230 len = this->SetText_i(data); 00231 } 00232 <font class="keywordflow">else</font> 00233 { 00234 00235 <font class="comment">// ASSERT(_fixed_size == 0)</font> 00236 _text += <font class="charliteral">'\0'</font>; 00237 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a41">GetEncoding</a>() == <a class="code" href="globals_8h.html#a214a48">ID3TE_UNICODE</a>) 00238 { 00239 _text += <font class="charliteral">'\0'</font>; 00240 } 00241 _text.append(data); 00242 len = data.size(); 00243 _num_items++; 00244 } 00245 00246 <font class="keywordflow">return</font> len; 00247 } 00248 <a name="l00249"></a><a class="code" href="class_i_d3___field_impl.html#a20">00249</a> size_t <a class="code" href="class_i_d3___field_impl.html#a20">ID3_FieldImpl::AddText</a>(String data) 00250 { 00251 size_t len = 0; 00252 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a>) 00253 { 00254 len = this->AddText_i(data); 00255 } 00256 <font class="keywordflow">return</font> len; 00257 } 00258 <a name="l00259"></a><a class="code" href="class_i_d3___field_impl.html#a16">00259</a> size_t <a class="code" href="class_i_d3___field_impl.html#a16">ID3_FieldImpl::Add</a>(<font class="keyword">const</font> <font class="keywordtype">char</font>* data) 00260 { 00261 size_t len = 0; 00262 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a>) 00263 { 00264 String str(data); 00265 len = this->AddText_i(str); 00266 } 00267 <font class="keywordflow">return</font> len; 00268 } 00269 <a name="l00270"></a><a class="code" href="class_i_d3___field_impl.html#a14">00270</a> <font class="keyword">const</font> <font class="keywordtype">char</font>* <a class="code" href="class_i_d3___field_impl.html#a14">ID3_FieldImpl::GetRawText</a>()<font class="keyword"> const</font> 00271 <font class="keyword"></font>{ 00272 <font class="keyword">const</font> <font class="keywordtype">char</font>* text = <a class="code" href="globals_8h.html#a24">NULL</a>; 00273 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a> && 00274 this-><a class="code" href="class_i_d3___field_impl.html#a41">GetEncoding</a>() == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a>) 00275 { 00276 text = _text.c_str(); 00277 } 00278 <font class="keywordflow">return</font> text; 00279 } 00280 <a name="l00281"></a><a class="code" href="class_i_d3___field_impl.html#a15">00281</a> <font class="keyword">const</font> <font class="keywordtype">char</font>* <a class="code" href="class_i_d3___field_impl.html#a15">ID3_FieldImpl::GetRawTextItem</a>(<a class="code" href="globals_8h.html#a7">index_t</a> index)<font class="keyword"> const</font> 00282 <font class="keyword"></font>{ 00283 <font class="keyword">const</font> <font class="keywordtype">char</font>* text = <a class="code" href="globals_8h.html#a24">NULL</a>; 00284 <font class="keywordflow">if</font> (this-><a class="code" href="class_i_d3___field_impl.html#a39">GetType</a>() == <a class="code" href="globals_8h.html#a222a189">ID3FTY_TEXTSTRING</a> && 00285 this-><a class="code" href="class_i_d3___field_impl.html#a41">GetEncoding</a>() == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a> && 00286 index < this-><a class="code" href="class_i_d3___field_impl.html#a4">GetNumTextItems</a>()) 00287 { 00288 text = _text.c_str(); 00289 <font class="keywordflow">for</font> (size_t i = 0; i < index; ++i) 00290 { 00291 text += strlen(text) + 1; 00292 } 00293 } 00294 <font class="keywordflow">return</font> text; 00295 } 00296 00297 <font class="keyword">namespace</font> 00298 <font class="keyword"></font>{ 00299 String readEncodedText(<a class="code" href="class_i_d3___reader.html">ID3_Reader</a>& reader, size_t len, <a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> enc) 00300 { 00301 <font class="keywordflow">if</font> (enc == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a>) 00302 { 00303 <font class="keywordflow">return</font> io::readText(reader, len); 00304 } 00305 <font class="keywordflow">return</font> io::readUnicodeText(reader, len); 00306 } 00307 00308 String readEncodedString(<a class="code" href="class_i_d3___reader.html">ID3_Reader</a>& reader, <a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> enc) 00309 { 00310 <font class="keywordflow">if</font> (enc == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a>) 00311 { 00312 <font class="keywordflow">return</font> io::readString(reader); 00313 } 00314 <font class="keywordflow">return</font> io::readUnicodeString(reader); 00315 } 00316 00317 size_t writeEncodedText(<a class="code" href="class_i_d3___writer.html">ID3_Writer</a>& writer, String data, <a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> enc) 00318 { 00319 <font class="keywordflow">if</font> (enc == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a>) 00320 { 00321 <font class="keywordflow">return</font> io::writeText(writer, data); 00322 } 00323 <font class="keywordflow">return</font> io::writeUnicodeText(writer, data); 00324 } 00325 00326 size_t writeEncodedString(<a class="code" href="class_i_d3___writer.html">ID3_Writer</a>& writer, String data, <a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> enc) 00327 { 00328 <font class="keywordflow">if</font> (enc == <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a>) 00329 { 00330 <font class="keywordflow">return</font> io::writeString(writer, data); 00331 } 00332 <font class="keywordflow">return</font> io::writeUnicodeString(writer, data); 00333 } 00334 } 00335 <a name="l00336"></a><a class="code" href="class_i_d3___field_impl.html#b4">00336</a> <font class="keywordtype">bool</font> <a class="code" href="class_i_d3___field_impl.html#b4">ID3_FieldImpl::ParseText</a>(<a class="code" href="class_i_d3___reader.html">ID3_Reader</a>& reader) 00337 { 00338 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): reader.getBeg() = "</font> << reader.<a class="code" href="class_i_d3___reader.html#a1">getBeg</a>() ); 00339 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): reader.getCur() = "</font> << reader.<a class="code" href="class_i_d3___reader.html#a3">getCur</a>() ); 00340 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): reader.getEnd() = "</font> << reader.<a class="code" href="class_i_d3___reader.html#a2">getEnd</a>() ); 00341 this-><a class="code" href="class_i_d3___field_impl.html#a1">Clear</a>(); 00342 00343 <a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> enc = this-><a class="code" href="class_i_d3___field_impl.html#a41">GetEncoding</a>(); 00344 size_t fixed_size = this-><a class="code" href="class_i_d3___field_impl.html#a2">Size</a>(); 00345 <font class="keywordflow">if</font> (fixed_size) 00346 { 00347 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): fixed size string"</font> ); 00348 <font class="comment">// The string is of fixed length</font> 00349 String text = readEncodedText(reader, fixed_size, enc); 00350 this-><a class="code" href="class_i_d3___field_impl.html#a19">SetText</a>(text); 00351 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): fixed size string = "</font> << text ); 00352 } 00353 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (_flags & <a class="code" href="globals_8h.html#a221a183">ID3FF_LIST</a>) 00354 { 00355 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): text list"</font> ); 00356 <font class="comment">// lists are always the last field in a frame. parse all remaining </font> 00357 <font class="comment">// characters in the reader</font> 00358 <font class="keywordflow">while</font> (!reader.<a class="code" href="class_i_d3___reader.html#a11">atEnd</a>()) 00359 { 00360 String text = readEncodedString(reader, enc); 00361 this-><a class="code" href="class_i_d3___field_impl.html#a20">AddText</a>(text); 00362 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): adding string = "</font> << text ); 00363 } 00364 } 00365 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (_flags & <a class="code" href="globals_8h.html#a221a182">ID3FF_CSTR</a>) 00366 { 00367 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): null terminated string"</font> ); 00368 String text = readEncodedString(reader, enc); 00369 this-><a class="code" href="class_i_d3___field_impl.html#a19">SetText</a>(text); 00370 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): null terminated string = "</font> << text ); 00371 } 00372 <font class="keywordflow">else</font> 00373 { 00374 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): last field string"</font> ); 00375 String text = readEncodedText(reader, reader.<a class="code" href="class_i_d3___reader.html#a10">remainingBytes</a>(), enc); 00376 <font class="comment">// not null terminated. </font> 00377 this-><a class="code" href="class_i_d3___field_impl.html#a20">AddText</a>(text); 00378 ID3D_NOTICE( <font class="stringliteral">"ID3_Field::ParseText(): last field string = "</font> << text ); 00379 } 00380 00381 _changed = <font class="keyword">false</font>; 00382 <font class="keywordflow">return</font> <font class="keyword">true</font>; 00383 } 00384 <a name="l00385"></a><a class="code" href="class_i_d3___field_impl.html#b1">00385</a> <font class="keywordtype">void</font> <a class="code" href="class_i_d3___field_impl.html#b1">ID3_FieldImpl::RenderText</a>(<a class="code" href="class_i_d3___writer.html">ID3_Writer</a>& writer)<font class="keyword"> const</font> 00386 <font class="keyword"></font>{ 00387 <a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> enc = this-><a class="code" href="class_i_d3___field_impl.html#a41">GetEncoding</a>(); 00388 00389 <font class="keywordflow">if</font> (_flags & <a class="code" href="globals_8h.html#a221a182">ID3FF_CSTR</a>) 00390 { 00391 writeEncodedString(writer, _text, enc); 00392 } 00393 <font class="keywordflow">else</font> 00394 { 00395 writeEncodedText(writer, _text, enc); 00396 } 00397 _changed = <font class="keyword">false</font>; 00398 }; 00399 <a name="l00408"></a><a class="code" href="class_i_d3___field_impl.html#a4">00408</a> size_t <a class="code" href="class_i_d3___field_impl.html#a4">ID3_FieldImpl::GetNumTextItems</a>()<font class="keyword"> const</font> 00409 <font class="keyword"></font>{ 00410 <font class="keywordflow">return</font> _num_items; 00411 } 00412 </pre></div><hr><address><small>Generated on Thu Jan 3 07:35:55 2002 for id3lib by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.gif" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.12 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2001</small></address> </body> </html>