<!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/utils.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/utils.cpp</h1><a href="utils_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">// $Id: utils.cpp,v 1.19 2001/08/26 23:33:32 dmazzoni 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 <font class="preprocessor">#if defined HAVE_ICONV_H</font> 00032 <font class="preprocessor"></font><font class="preprocessor"># include <iconv.h></font> 00033 <font class="preprocessor"># include <errno.h></font> 00034 <font class="preprocessor">#endif</font> 00035 <font class="preprocessor"></font> 00036 <font class="preprocessor">#include <ctype.h></font> 00037 <font class="preprocessor">#include <iostream.h></font> 00038 00039 <font class="preprocessor">#ifdef macintosh</font> 00040 <font class="preprocessor"></font><font class="preprocessor">#define NOCREATE ((std::ios_base::openmode)0)</font> 00041 <font class="preprocessor"></font><font class="preprocessor">#define toascii(X) (X)</font> 00042 <font class="preprocessor"></font><font class="preprocessor">#else</font> <a name="l00043"></a><a class="code" href="utils_8cpp.html#a0">00043</a> <font class="preprocessor"></font><font class="preprocessor">#define NOCREATE ios::nocreate</font> 00044 <font class="preprocessor"></font><font class="preprocessor">#endif</font> 00045 <font class="preprocessor"></font> 00046 <font class="preprocessor">#include "utils.h"</font> 00047 00048 <font class="keyword">using</font> <font class="keyword">namespace </font>dami; 00049 00050 size_t dami::renderNumber(<a class="code" href="globals_8h.html#a33">uchar</a> *buffer, <a class="code" href="sized__types_8h.html#a4">uint32</a> val, size_t size) 00051 { 00052 <a class="code" href="sized__types_8h.html#a4">uint32</a> num = val; 00053 <font class="keywordflow">for</font> (size_t i = 0; i < size; i++) 00054 { 00055 buffer[size - i - 1] = (uchar)(num & <a class="code" href="globals_8h.html#a19">MASK8</a>); 00056 num >>= 8; 00057 } 00058 <font class="keywordflow">return</font> size; 00059 } 00060 00061 String dami::renderNumber(<a class="code" href="sized__types_8h.html#a4">uint32</a> val, size_t size) 00062 { 00063 String str(size, <font class="charliteral">'\0'</font>); 00064 <a class="code" href="sized__types_8h.html#a4">uint32</a> num = val; 00065 <font class="keywordflow">for</font> (size_t i = 0; i < size; i++) 00066 { 00067 str[size - i - 1] = (uchar)(num & <a class="code" href="globals_8h.html#a19">MASK8</a>); 00068 num >>= 8; 00069 } 00070 <font class="keywordflow">return</font> str; 00071 } 00072 00073 00074 <font class="keyword">namespace </font> 00075 <font class="keyword"></font>{ 00076 <font class="preprocessor">#if !defined(HAVE_ICONV_H)</font> 00077 <font class="preprocessor"></font> <font class="comment">// converts an ASCII string into a Unicode one</font> 00078 String mbstoucs(String data) 00079 { 00080 size_t size = data.size(); 00081 String unicode(size * 2, <font class="charliteral">'\0'</font>); 00082 <font class="keywordflow">for</font> (<a class="code" href="globals_8h.html#a7">index_t</a> i = 0; i < size; i++) 00083 { 00084 unicode[i*2+1] = toascii(data[i]); 00085 } 00086 <font class="keywordflow">return</font> unicode; 00087 } 00088 00089 <font class="comment">// converts a Unicode string into ASCII</font> 00090 00091 String ucstombs(String data) 00092 { 00093 size_t size = data.size() / 2; 00094 String ascii(size, <font class="charliteral">'\0'</font>); 00095 <font class="keywordflow">for</font> (<a class="code" href="globals_8h.html#a7">index_t</a> i = 0; i < size; i++) 00096 { 00097 ascii[i] = toascii(data[i*2+1]); 00098 } 00099 <font class="keywordflow">return</font> ascii; 00100 } 00101 <font class="preprocessor">#else</font> 00102 <font class="preprocessor"></font> 00103 String convert_i(iconv_t cd, String source) 00104 { 00105 String target; 00106 size_t source_size = source.size(); 00107 <font class="comment">// const char* source_str = source.data();</font> 00108 <font class="keywordtype">char</font> * source_str = <font class="keyword">new</font> <font class="keywordtype">char</font>[source.length()+1]; 00109 source.copy(source_str, string::npos); 00110 source_str[source.length()] = 0; 00111 00112 <font class="preprocessor">#define BUFSIZ 1024</font> 00113 <font class="preprocessor"></font> <font class="keywordtype">char</font> buf[BUFSIZ]; 00114 <font class="keywordtype">char</font>* target_str = buf; 00115 size_t target_size = BUFSIZ; 00116 00117 <font class="keywordflow">do</font> 00118 { 00119 size_t nconv = iconv(cd, 00120 &source_str, &source_size, 00121 &target_str, &target_size); 00122 <font class="keywordflow">if</font> (nconv == (size_t) -1 && errno != EINVAL && errno != E2BIG) 00123 { 00124 <font class="keywordflow">return</font> target; 00125 } 00126 target.append(buf, BUFSIZ - target_size); 00127 target_str = buf; 00128 target_size = BUFSIZ; 00129 } 00130 <font class="keywordflow">while</font> (source_size > 0); 00131 <font class="keywordflow">return</font> target; 00132 } 00133 00134 <font class="keyword">const</font> <font class="keywordtype">char</font>* getFormat(<a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> enc) 00135 { 00136 <font class="keyword">const</font> <font class="keywordtype">char</font>* format = <a class="code" href="globals_8h.html#a24">NULL</a>; 00137 <font class="keywordflow">switch</font> (enc) 00138 { 00139 <font class="keywordflow">case</font> <a class="code" href="globals_8h.html#a214a47">ID3TE_ASCII</a>: 00140 format = ID3_ICONV_FORMAT_ASCII; 00141 <font class="keywordflow">break</font>; 00142 00143 <font class="keywordflow">case</font> <a class="code" href="globals_8h.html#a214a43">ID3TE_UTF16</a>: 00144 format = ID3_ICONV_FORMAT_UTF16; 00145 <font class="keywordflow">break</font>; 00146 00147 <font class="keywordflow">case</font> <a class="code" href="globals_8h.html#a214a44">ID3TE_UTF16BE</a>: 00148 format = ID3_ICONV_FORMAT_UTF16BE; 00149 <font class="keywordflow">break</font>; 00150 00151 <font class="keywordflow">case</font> <a class="code" href="globals_8h.html#a214a45">ID3TE_UTF8</a>: 00152 format = ID3_ICONV_FORMAT_UTF8; 00153 <font class="keywordflow">break</font>; 00154 00155 <font class="keywordflow">default</font>: 00156 <font class="keywordflow">break</font>; 00157 } 00158 <font class="keywordflow">return</font> format; 00159 } 00160 <font class="preprocessor">#endif</font> 00161 <font class="preprocessor"></font>} 00162 00163 String dami::convert(String data, <a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> sourceEnc, <a class="code" href="globals_8h.html#a214">ID3_TextEnc</a> targetEnc) 00164 { 00165 String target; 00166 <font class="preprocessor">#if !defined HAVE_ICONV_H</font> 00167 <font class="preprocessor"></font><font class="preprocessor">#define ID3_IS_ASCII(enc) \</font> 00168 <font class="preprocessor"> ((enc) == ID3TE_ASCII || \</font> 00169 <font class="preprocessor"> (enc) == ID3TE_ISO8859_1 || \</font> 00170 <font class="preprocessor"> (enc) == ID3TE_UTF8)</font> 00171 <font class="preprocessor"></font><font class="preprocessor">#define ID3_IS_UNICODE(enc) \</font> 00172 <font class="preprocessor"> ((enc) == ID3TE_UNICODE || \</font> 00173 <font class="preprocessor"> (enc) == ID3TE_UTF16 || \</font> 00174 <font class="preprocessor"> (enc) == ID3TE_UTF16BE)</font> 00175 <font class="preprocessor"></font> <font class="keywordflow">if</font> (<a class="code" href="utils_8cpp.html#a1">ID3_IS_ASCII</a>(sourceEnc) && <a class="code" href="utils_8cpp.html#a2">ID3_IS_UNICODE</a>(targetEnc)) 00176 { 00177 target = mbstoucs(data); 00178 } 00179 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (<a class="code" href="utils_8cpp.html#a2">ID3_IS_UNICODE</a>(sourceEnc) && <a class="code" href="utils_8cpp.html#a1">ID3_IS_ASCII</a>(targetEnc)) 00180 { 00181 target = ucstombs(data); 00182 } 00183 <font class="preprocessor">#else</font> 00184 <font class="preprocessor"></font> <font class="keywordflow">if</font> (sourceEnc != targetEnc) 00185 { 00186 <font class="keyword">const</font> <font class="keywordtype">char</font>* targetFormat = getFormat(targetEnc); 00187 <font class="keyword">const</font> <font class="keywordtype">char</font>* sourceFormat = getFormat(sourceEnc); 00188 00189 iconv_t cd = iconv_open (targetFormat, sourceFormat); 00190 <font class="keywordflow">if</font> (cd != (iconv_t) -1) 00191 { 00192 target = convert_i(cd, data); 00193 } 00194 iconv_close (cd); 00195 } 00196 <font class="preprocessor">#endif</font> 00197 <font class="preprocessor"></font> <font class="keywordflow">return</font> target; 00198 } 00199 00200 size_t dami::ucslen(<font class="keyword">const</font> <a class="code" href="globals_8h.html#a39">unicode_t</a> *unicode) 00201 { 00202 <font class="keywordflow">if</font> (<a class="code" href="globals_8h.html#a24">NULL</a> != unicode) 00203 { 00204 <font class="keywordflow">for</font> (size_t size = 0; <font class="keyword">true</font>; size++) 00205 { 00206 <font class="keywordflow">if</font> (<a class="code" href="globals_8h.html#a8">NULL_UNICODE</a> == unicode[size]) 00207 { 00208 <font class="keywordflow">return</font> size; 00209 } 00210 } 00211 } 00212 <font class="keywordflow">return</font> 0; 00213 } 00214 00215 <font class="keyword">namespace</font> 00216 <font class="keyword"></font>{ 00217 <font class="keywordtype">bool</font> exists(String name) 00218 { 00219 ifstream file(name.c_str(), <a class="code" href="utils_8cpp.html#a0">NOCREATE</a>); 00220 <font class="keywordflow">return</font> file.is_open() != 0; 00221 } 00222 }; 00223 00224 <a class="code" href="globals_8h.html#a223">ID3_Err</a> dami::createFile(String name, fstream& file) 00225 { 00226 <font class="keywordflow">if</font> (file.is_open()) 00227 { 00228 file.close(); 00229 } 00230 00231 file.open(name.c_str(), ios::out | ios::binary | ios::trunc); 00232 <font class="keywordflow">if</font> (!file) 00233 { 00234 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a203">ID3E_ReadOnly</a>; 00235 } 00236 00237 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a191">ID3E_NoError</a>; 00238 } 00239 00240 size_t dami::getFileSize(fstream& file) 00241 { 00242 size_t size = 0; 00243 <font class="keywordflow">if</font> (file.is_open()) 00244 { 00245 streamoff curpos = file.tellg(); 00246 file.seekg(0, ios::end); 00247 size = file.tellg(); 00248 file.seekg(curpos); 00249 } 00250 <font class="keywordflow">return</font> size; 00251 } 00252 00253 size_t dami::getFileSize(ifstream& file) 00254 { 00255 size_t size = 0; 00256 <font class="keywordflow">if</font> (file.is_open()) 00257 { 00258 streamoff curpos = file.tellg(); 00259 file.seekg(0, ios::end); 00260 size = file.tellg(); 00261 file.seekg(curpos); 00262 } 00263 <font class="keywordflow">return</font> size; 00264 } 00265 00266 size_t dami::getFileSize(ofstream& file) 00267 { 00268 size_t size = 0; 00269 <font class="keywordflow">if</font> (file.is_open()) 00270 { 00271 streamoff curpos = file.tellp(); 00272 file.seekp(0, ios::end); 00273 size = file.tellp(); 00274 file.seekp(curpos); 00275 } 00276 <font class="keywordflow">return</font> size; 00277 } 00278 00279 <a class="code" href="globals_8h.html#a223">ID3_Err</a> dami::openWritableFile(String name, fstream& file) 00280 { 00281 <font class="keywordflow">if</font> (!exists(name)) 00282 { 00283 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a202">ID3E_NoFile</a>; 00284 } 00285 00286 <font class="keywordflow">if</font> (file.is_open()) 00287 { 00288 file.close(); 00289 } 00290 file.open(name.c_str(), ios::in | ios::out | ios::binary | <a class="code" href="utils_8cpp.html#a0">NOCREATE</a>); 00291 <font class="keywordflow">if</font> (!file) 00292 { 00293 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a203">ID3E_ReadOnly</a>; 00294 } 00295 00296 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a191">ID3E_NoError</a>; 00297 } 00298 00299 <a class="code" href="globals_8h.html#a223">ID3_Err</a> dami::openWritableFile(String name, ofstream& file) 00300 { 00301 <font class="keywordflow">if</font> (!exists(name)) 00302 { 00303 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a202">ID3E_NoFile</a>; 00304 } 00305 00306 <font class="keywordflow">if</font> (file.is_open()) 00307 { 00308 file.close(); 00309 } 00310 file.open(name.c_str(), ios::in | ios::out | ios::binary | <a class="code" href="utils_8cpp.html#a0">NOCREATE</a>); 00311 <font class="keywordflow">if</font> (!file) 00312 { 00313 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a203">ID3E_ReadOnly</a>; 00314 } 00315 00316 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a191">ID3E_NoError</a>; 00317 } 00318 00319 <a class="code" href="globals_8h.html#a223">ID3_Err</a> dami::openReadableFile(String name, fstream& file) 00320 { 00321 <font class="keywordflow">if</font> (file.is_open()) 00322 { 00323 file.close(); 00324 } 00325 file.open(name.c_str(), ios::in | ios::binary | <a class="code" href="utils_8cpp.html#a0">NOCREATE</a>); 00326 <font class="keywordflow">if</font> (!file) 00327 { 00328 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a202">ID3E_NoFile</a>; 00329 } 00330 00331 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a191">ID3E_NoError</a>; 00332 } 00333 00334 <a class="code" href="globals_8h.html#a223">ID3_Err</a> dami::openReadableFile(String name, ifstream& file) 00335 { 00336 <font class="keywordflow">if</font> (file.is_open()) 00337 { 00338 file.close(); 00339 } 00340 file.open(name.c_str(), ios::in | ios::binary | <a class="code" href="utils_8cpp.html#a0">NOCREATE</a>); 00341 <font class="keywordflow">if</font> (!file) 00342 { 00343 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a202">ID3E_NoFile</a>; 00344 } 00345 00346 <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a223a191">ID3E_NoError</a>; 00347 } 00348 00349 String dami::toString(<a class="code" href="sized__types_8h.html#a4">uint32</a> val) 00350 { 00351 <font class="keywordflow">if</font> (val == 0) 00352 { 00353 <font class="keywordflow">return</font> <font class="stringliteral">"0"</font>; 00354 } 00355 String text; 00356 <font class="keywordflow">while</font> (val > 0) 00357 { 00358 String tmp; 00359 <font class="keywordtype">char</font> ch = (val % 10) + <font class="charliteral">'0'</font>; 00360 tmp += ch; 00361 text = tmp + text; 00362 val /= 10; 00363 } 00364 <font class="keywordflow">return</font> text; 00365 } 00366 00367 WString dami::toWString(<font class="keyword">const</font> <a class="code" href="globals_8h.html#a39">unicode_t</a> buf[], size_t len) 00368 { 00369 WString str; 00370 str.reserve(len); 00371 00372 <font class="keywordflow">for</font> (size_t i = 0; i < len; ++i) 00373 { 00374 str += static_cast<WString::value_type>(buf[i]); 00375 } 00376 <font class="keywordflow">return</font> str; 00377 } </pre></div><hr><address><small>Generated on Thu Jan 3 07:35:56 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>