Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 2c9d30f3cf217a6cae41860ebc0d6704 > files > 187

libid3lib3.8_0-devel-3.8.0-0.pre2.1.1mdk.i586.rpm

<!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/tag_file.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> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="namespacemembers.html">Namespace Members</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center>
<hr><h1>src/tag_file.cpp</h1><a href="tag__file_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">// $Id: tag_file.cpp,v 1.34 2001/12/16 09:40:56 shadrack 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 &lt;config.h&gt;</font>
00029 <font class="preprocessor">#endif</font>
00030 <font class="preprocessor"></font>
00031 
00032 
00033 <font class="preprocessor">#include &lt;string.h&gt;</font>
00034 <font class="preprocessor">#include &lt;fstream.h&gt;</font>
00035 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00036 <font class="preprocessor">#include "utils.h"</font>
00037 <font class="preprocessor">#include "<a class="code" href="writers_8h.html">writers.h</a>"</font>
00038 <font class="preprocessor">#include "io_strings.h"</font>
00039 <font class="preprocessor">#include "<a class="code" href="tag__impl_8h.html">tag_impl.h</a>"</font>
00040 <font class="preprocessor">#include "utils.h"</font>
00041 
00042 <font class="keyword">using</font> <font class="keyword">namespace </font>dami;
00043 
00044 <font class="preprocessor">#if !defined HAVE_MKSTEMP</font>
00045 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;stdio.h&gt;</font>
00046 <font class="preprocessor">#endif</font>
00047 <font class="preprocessor"></font>
00048 <font class="preprocessor">#if defined HAVE_UNISTD_H</font>
00049 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;unistd.h&gt;</font>
00050 <font class="preprocessor">#endif</font>
00051 <font class="preprocessor"></font>
00052 <font class="preprocessor">#if defined WIN32 &amp;&amp; (!defined(WINCE))</font>
00053 <font class="preprocessor"></font><font class="preprocessor">#  include &lt;windows.h&gt;</font>
00054 <font class="keyword">static</font> <font class="keywordtype">int</font> truncate(<font class="keyword">const</font> <font class="keywordtype">char</font> *path, size_t length)
00055 {
00056   <font class="keywordtype">int</font> result = -1;
00057   HANDLE fh;
00058   
00059   fh = ::CreateFile(path,
00060                     GENERIC_WRITE | GENERIC_READ,
00061                     0,
00062                     <a class="code" href="globals_8h.html#a24">NULL</a>,
00063                     OPEN_EXISTING,
00064                     FILE_ATTRIBUTE_NORMAL,
00065                     <a class="code" href="globals_8h.html#a24">NULL</a>);
00066   
00067   <font class="keywordflow">if</font>(INVALID_HANDLE_VALUE != fh)
00068   {
00069     SetFilePointer(fh, length, <a class="code" href="globals_8h.html#a24">NULL</a>, FILE_BEGIN);
00070     SetEndOfFile(fh);
00071     CloseHandle(fh);
00072     result = 0;
00073   }
00074   
00075   <font class="keywordflow">return</font> result;
00076 }
00077 
00078 <font class="comment">// prevents a weird error I was getting compiling this under windows</font>
00079 <font class="preprocessor">#  if defined CreateFile</font>
00080 <font class="preprocessor"></font><font class="preprocessor">#    undef CreateFile</font>
00081 <font class="preprocessor"></font><font class="preprocessor">#  endif</font>
00082 <font class="preprocessor"></font>
00083 <font class="preprocessor">#elif defined(WINCE)</font>
00084 <font class="preprocessor"></font><font class="comment">// Createfile is apparently to defined to CreateFileW. (Bad Bad Bad), so we </font>
00085 <font class="comment">// work around it by converting path to Unicode</font>
00086 <font class="preprocessor">#  include &lt;windows.h&gt;</font>
00087 <font class="keyword">static</font> <font class="keywordtype">int</font> truncate(<font class="keyword">const</font> <font class="keywordtype">char</font> *path, size_t length)
00088 {
00089   <font class="keywordtype">int</font> result = -1;
00090   <font class="keywordtype">wchar_t</font> wcTempPath[256];
00091   mbstowcs(wcTempPath,path,255);
00092   HANDLE fh;
00093   fh = ::CreateFile(wcTempPath,
00094                     GENERIC_WRITE | GENERIC_READ,
00095                     0,
00096                     <a class="code" href="globals_8h.html#a24">NULL</a>,
00097                     OPEN_EXISTING,
00098                     FILE_ATTRIBUTE_NORMAL,
00099                     <a class="code" href="globals_8h.html#a24">NULL</a>);
00100 
00101   <font class="keywordflow">if</font> (INVALID_HANDLE_VALUE != fh)
00102   {
00103     SetFilePointer(fh, length, <a class="code" href="globals_8h.html#a24">NULL</a>, FILE_BEGIN);
00104     SetEndOfFile(fh);
00105     CloseHandle(fh);
00106     result = 0;
00107   }
00108   
00109   <font class="keywordflow">return</font> result;
00110 }
00111 
00112 <font class="preprocessor">#elif defined(macintosh)</font>
00113 <font class="preprocessor"></font>        
00114 <font class="keyword">static</font> <font class="keywordtype">int</font> truncate(<font class="keyword">const</font> <font class="keywordtype">char</font> *path, size_t length)
00115 {
00116    <font class="comment">/* not implemented on the Mac */</font>
00117    <font class="keywordflow">return</font> -1;
00118 }
00119         
00120 <font class="preprocessor">#endif</font>
00121 <font class="preprocessor"></font>
<a name="l00122"></a><a class="code" href="class_i_d3___tag_impl.html#a39">00122</a> size_t <a class="code" href="class_i_d3___tag_impl.html#a18">ID3_TagImpl::Link</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *fileInfo, <font class="keywordtype">bool</font> parseID3v1, <font class="keywordtype">bool</font> parseLyrics3)
00123 {
00124   <a class="code" href="globals_8h.html#a40">flags_t</a> tt = <a class="code" href="globals_8h.html#a217a59">ID3TT_NONE</a>;
00125   <font class="keywordflow">if</font> (parseID3v1)
00126   {
00127     tt |= <a class="code" href="globals_8h.html#a217a60">ID3TT_ID3V1</a>;
00128   }
00129   <font class="keywordflow">if</font> (parseLyrics3)
00130   {
00131     tt |= <a class="code" href="globals_8h.html#a217a65">ID3TT_LYRICS</a>;
00132   }
00133   <font class="keywordflow">return</font> this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a18">Link</a>(fileInfo, tt);
00134 }
00135 
<a name="l00136"></a><a class="code" href="class_i_d3___tag_impl.html#a18">00136</a> size_t <a class="code" href="class_i_d3___tag_impl.html#a18">ID3_TagImpl::Link</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *fileInfo, <a class="code" href="globals_8h.html#a40">flags_t</a> tag_types)
00137 {
00138   _tags_to_parse.<a class="code" href="class_i_d3___flags.html#a4">set</a>(tag_types);
00139   
00140   <font class="keywordflow">if</font> (<a class="code" href="globals_8h.html#a24">NULL</a> == fileInfo)
00141   {
00142     <font class="keywordflow">return</font> 0;
00143   }
00144 
00145   _file_name = fileInfo;
00146   _changed = <font class="keyword">true</font>;
00147 
00148   this-&gt;<a class="code" href="class_i_d3___tag_impl.html#b3">ParseFile</a>();
00149   
00150   <font class="keywordflow">return</font> this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a21">GetPrependedBytes</a>();
00151 }
00152 
<a name="l00153"></a><a class="code" href="tag__file_8cpp.html#a0">00153</a> size_t <a class="code" href="tag__file_8cpp.html#a0">RenderV1ToFile</a>(<a class="code" href="class_i_d3___tag_impl.html">ID3_TagImpl</a>&amp; tag, fstream&amp; file)
00154 {
00155   <font class="keywordflow">if</font> (!file)
00156   {
00157     <font class="keywordflow">return</font> 0;
00158   }
00159 
00160   <font class="comment">// Heck no, this is stupid.  If we do not read in an initial V1(.1)</font>
00161   <font class="comment">// header then we are constantly appending new V1(.1) headers. Files</font>
00162   <font class="comment">// can get very big that way if we never overwrite the old ones.</font>
00163   <font class="comment">//  if (ID3_V1_LEN &gt; tag.GetAppendedBytes())   - Daniel Hazelbaker</font>
00164   <font class="keywordflow">if</font> (<a class="code" href="globals_8h.html#a220a173">ID3_V1_LEN</a> &gt; tag.<a class="code" href="class_i_d3___tag_impl.html#a23">GetFileSize</a>())
00165   {
00166     file.seekp(0, ios::end);
00167   }
00168   <font class="keywordflow">else</font>
00169   {
00170     <font class="comment">// We want to check if there is already an id3v1 tag, so we can write over</font>
00171     <font class="comment">// it.  First, seek to the beginning of any possible id3v1 tag</font>
00172     file.seekg(0-<a class="code" href="globals_8h.html#a220a173">ID3_V1_LEN</a>, ios::end);
00173     <font class="keywordtype">char</font> sID[<a class="code" href="globals_8h.html#a220a174">ID3_V1_LEN_ID</a>];
00174 
00175     <font class="comment">// Read in the TAG characters</font>
00176     file.read(sID, <a class="code" href="globals_8h.html#a220a174">ID3_V1_LEN_ID</a>);
00177 
00178     <font class="comment">// If those three characters are TAG, then there's a preexisting id3v1 tag,</font>
00179     <font class="comment">// so we should set the file cursor so we can overwrite it with a new tag.</font>
00180     <font class="keywordflow">if</font> (memcmp(sID, <font class="stringliteral">"TAG"</font>, <a class="code" href="globals_8h.html#a220a174">ID3_V1_LEN_ID</a>) == 0)
00181     {
00182       file.seekp(0-<a class="code" href="globals_8h.html#a220a173">ID3_V1_LEN</a>, ios::end);
00183     }
00184     <font class="comment">// Otherwise, set the cursor to the end of the file so we can append on </font>
00185     <font class="comment">// the new tag.</font>
00186     <font class="keywordflow">else</font>
00187     {
00188       file.seekp(0, ios::end);
00189     }
00190   }
00191   
00192   <a class="code" href="class_i_d3___i_o_stream_writer.html">ID3_IOStreamWriter</a> out(file);
00193   
00194   id3::v1::render(out, tag);
00195 
00196   <font class="keywordflow">return</font> <a class="code" href="globals_8h.html#a220a173">ID3_V1_LEN</a>;
00197 }
00198 
<a name="l00199"></a><a class="code" href="tag__file_8cpp.html#a1">00199</a> size_t <a class="code" href="tag__file_8cpp.html#a1">RenderV2ToFile</a>(<font class="keyword">const</font> <a class="code" href="class_i_d3___tag_impl.html">ID3_TagImpl</a>&amp; tag, fstream&amp; file)
00200 {
00201   ID3D_NOTICE( <font class="stringliteral">"RenderV2ToFile: starting"</font> );
00202   <font class="keywordflow">if</font> (!file)
00203   {
00204     ID3D_WARNING( <font class="stringliteral">"RenderV2ToFile: error in file"</font> );
00205     <font class="keywordflow">return</font> 0;
00206   }
00207 
00208   String tagString; 
00209   io::StringWriter writer(tagString);
00210   id3::v2::render(writer, tag);
00211   ID3D_NOTICE( <font class="stringliteral">"RenderV2ToFile: rendered v2"</font> );
00212 
00213   <font class="keyword">const</font> <font class="keywordtype">char</font>* tagData = tagString.data();
00214   size_t tagSize = tagString.size();
00215 
00216   <font class="comment">// if the new tag fits perfectly within the old and the old one</font>
00217   <font class="comment">// actually existed (ie this isn't the first tag this file has had)</font>
00218   <font class="keywordflow">if</font> ((!tag.<a class="code" href="class_i_d3___tag_impl.html#a21">GetPrependedBytes</a>() &amp;&amp; !<a class="code" href="tag__impl_8h.html#a0">ID3_GetDataSize</a>(tag)) ||
00219       (tagSize == tag.<a class="code" href="class_i_d3___tag_impl.html#a21">GetPrependedBytes</a>()))
00220   {
00221     file.seekp(0, ios::beg);
00222     file.write(tagData, tagSize);
00223   }
00224   <font class="keywordflow">else</font>
00225   {
00226     String filename = tag.<a class="code" href="class_i_d3___tag_impl.html#a24">GetFileName</a>();
00227 <font class="preprocessor">#if !defined HAVE_MKSTEMP</font>
00228 <font class="preprocessor"></font>    <font class="comment">// This section is for Windows folk</font>
00229 
00230     FILE *tempOut = tmpfile();
00231     <font class="keywordflow">if</font> (<a class="code" href="globals_8h.html#a24">NULL</a> == tempOut)
00232     {
00233       <font class="comment">// log this</font>
00234       <font class="keywordflow">return</font> 0;
00235       <font class="comment">//ID3_THROW(ID3E_ReadOnly);</font>
00236     }
00237     
00238     fwrite(tagData, 1, tagSize, tempOut);
00239     
00240     file.seekg(tag.<a class="code" href="class_i_d3___tag_impl.html#a21">GetPrependedBytes</a>(), ios::beg);
00241     
00242     <a class="code" href="globals_8h.html#a33">uchar</a> tmpBuffer[BUFSIZ];
00243     <font class="keywordflow">while</font> (!file.eof())
00244     {
00245       file.read((<font class="keywordtype">char</font> *)tmpBuffer, BUFSIZ);
00246       size_t nBytes = file.gcount();
00247       fwrite(tmpBuffer, 1, nBytes, tempOut);
00248     }
00249     
00250     rewind(tempOut);
00251     openWritableFile(filename, file);
00252     
00253     <font class="keywordflow">while</font> (!feof(tempOut))
00254     {
00255       size_t nBytes = fread((<font class="keywordtype">char</font> *)tmpBuffer, 1, BUFSIZ, tempOut);
00256       file.write((<font class="keywordtype">char</font> *)tmpBuffer, nBytes);
00257     }
00258     
00259     fclose(tempOut);
00260     
00261 <font class="preprocessor">#else</font>
00262 <font class="preprocessor"></font>
00263     <font class="comment">// else we gotta make a temp file, copy the tag into it, copy the</font>
00264     <font class="comment">// rest of the old file after the tag, delete the old file, rename</font>
00265     <font class="comment">// this new file to the old file's name and update the handle</font>
00266     String sTmpSuffix = <font class="stringliteral">".XXXXXX"</font>;
00267     <font class="keywordflow">if</font> (filename.size() + sTmpSuffix.size() &gt; ID3_PATH_LENGTH)
00268     {
00269       <font class="comment">// log this</font>
00270       <font class="keywordflow">return</font> 0;
00271       <font class="comment">//ID3_THROW_DESC(ID3E_NoFile, "filename too long");</font>
00272     }
00273     <font class="keywordtype">char</font> sTempFile[ID3_PATH_LENGTH];
00274     strcpy(sTempFile, filename.c_str());
00275     strcat(sTempFile, sTmpSuffix.c_str());
00276     
00277     <font class="keywordtype">int</font> fd = mkstemp(sTempFile);
00278     <font class="keywordflow">if</font> (fd &lt; 0)
00279     {
00280       remove(sTempFile);
00281       <font class="comment">//ID3_THROW_DESC(ID3E_NoFile, "couldn't open temp file");</font>
00282     }
00283 
00284     ofstream tmpOut(fd);
00285     <font class="keywordflow">if</font> (!tmpOut)
00286     {
00287       tmpOut.close();
00288       remove(sTempFile);
00289       <font class="keywordflow">return</font> 0;
00290       <font class="comment">// log this</font>
00291       <font class="comment">//ID3_THROW(ID3E_ReadOnly);</font>
00292     }
00293 
00294     tmpOut.write(tagData, tagSize);
00295     file.seekg(tag.<a class="code" href="class_i_d3___tag_impl.html#a21">GetPrependedBytes</a>(), ios::beg);
00296     <a class="code" href="globals_8h.html#a33">uchar</a> tmpBuffer[BUFSIZ];
00297     <font class="keywordflow">while</font> (file)
00298     {
00299       file.read(tmpBuffer, BUFSIZ);
00300       size_t nBytes = file.gcount();
00301       tmpOut.write(tmpBuffer, nBytes);
00302     }
00303       
00304     tmpOut.close();
00305 
00306     file.close();
00307 
00308     remove(filename.c_str());
00309     rename(sTempFile, filename.c_str());
00310 
00311     openWritableFile(filename, file);
00312 <font class="preprocessor">#endif</font>
00313 <font class="preprocessor"></font>  }
00314 
00315   <font class="keywordflow">return</font> tagSize;
00316 }
00317 
00318 
<a name="l00319"></a><a class="code" href="class_i_d3___tag_impl.html#a19">00319</a> <a class="code" href="globals_8h.html#a40">flags_t</a> <a class="code" href="class_i_d3___tag_impl.html#a19">ID3_TagImpl::Update</a>(<a class="code" href="globals_8h.html#a40">flags_t</a> ulTagFlag)
00320 {
00321   <a class="code" href="globals_8h.html#a40">flags_t</a> tags = <a class="code" href="globals_8h.html#a217a59">ID3TT_NONE</a>;
00322   
00323   fstream file;
00324   String filename = this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a24">GetFileName</a>();
00325   <a class="code" href="globals_8h.html#a223">ID3_Err</a> err = openWritableFile(filename, file);
00326   _file_size = getFileSize(file);
00327   
00328   <font class="keywordflow">if</font> (err == <a class="code" href="globals_8h.html#a223a202">ID3E_NoFile</a>)
00329   {
00330     err = createFile(filename, file);
00331   }
00332   <font class="keywordflow">if</font> (err == <a class="code" href="globals_8h.html#a223a203">ID3E_ReadOnly</a>)
00333   {
00334     <font class="keywordflow">return</font> tags;
00335   }
00336 
00337   <font class="keywordflow">if</font> ((ulTagFlag &amp; <a class="code" href="globals_8h.html#a217a61">ID3TT_ID3V2</a>) &amp;&amp; this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a4">HasChanged</a>())
00338   {
00339     _prepended_bytes = <a class="code" href="tag__file_8cpp.html#a1">RenderV2ToFile</a>(*<font class="keyword">this</font>, file);
00340     <font class="keywordflow">if</font> (_prepended_bytes)
00341     {
00342       tags |= <a class="code" href="globals_8h.html#a217a61">ID3TT_ID3V2</a>;
00343     }
00344   }
00345   
00346   <font class="keywordflow">if</font> ((ulTagFlag &amp; <a class="code" href="globals_8h.html#a217a60">ID3TT_ID3V1</a>) &amp;&amp; 
00347       (!this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a31">HasTagType</a>(<a class="code" href="globals_8h.html#a217a60">ID3TT_ID3V1</a>) || this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a4">HasChanged</a>()))
00348   {
00349     size_t tag_bytes = <a class="code" href="tag__file_8cpp.html#a0">RenderV1ToFile</a>(*<font class="keyword">this</font>, file);
00350     <font class="keywordflow">if</font> (tag_bytes)
00351     {
00352       <font class="comment">// only add the tag_bytes if there wasn't an id3v1 tag before</font>
00353       <font class="keywordflow">if</font> (! _file_tags.<a class="code" href="class_i_d3___flags.html#a3">test</a>(<a class="code" href="globals_8h.html#a217a60">ID3TT_ID3V1</a>))
00354       {
00355         _appended_bytes += tag_bytes;
00356       }
00357       tags |= <a class="code" href="globals_8h.html#a217a60">ID3TT_ID3V1</a>;
00358     }
00359   }
00360   _changed = <font class="keyword">false</font>;
00361   _file_tags.<a class="code" href="class_i_d3___flags.html#a5">add</a>(tags);
00362   _file_size = getFileSize(file);
00363   file.close();
00364   <font class="keywordflow">return</font> tags;
00365 }
00366 
<a name="l00367"></a><a class="code" href="class_i_d3___tag_impl.html#a20">00367</a> <a class="code" href="globals_8h.html#a40">flags_t</a> <a class="code" href="class_i_d3___tag_impl.html#a20">ID3_TagImpl::Strip</a>(<a class="code" href="globals_8h.html#a40">flags_t</a> ulTagFlag)
00368 {
00369   <a class="code" href="globals_8h.html#a40">flags_t</a> ulTags = <a class="code" href="globals_8h.html#a217a59">ID3TT_NONE</a>;
00370   <font class="keyword">const</font> size_t data_size = <a class="code" href="tag__impl_8h.html#a0">ID3_GetDataSize</a>(*<font class="keyword">this</font>);
00371   
00372   <font class="comment">// First remove the v2 tag, if requested</font>
00373   <font class="keywordflow">if</font> (ulTagFlag &amp; <a class="code" href="globals_8h.html#a217a68">ID3TT_PREPENDED</a> &amp; _file_tags.<a class="code" href="class_i_d3___flags.html#a2">get</a>())
00374   {
00375     fstream file;
00376     <font class="keywordflow">if</font> (<a class="code" href="globals_8h.html#a223a191">ID3E_NoError</a> != openWritableFile(this-&gt;GetFileName(), file))
00377     {
00378       <font class="keywordflow">return</font> ulTags;
00379     }
00380     _file_size = getFileSize(file);
00381 
00382     <font class="comment">// We will remove the id3v2 tag in place: since it comes at the beginning</font>
00383     <font class="comment">// of the file, we'll effectively move all the data that comes after the</font>
00384     <font class="comment">// tag back n bytes, where n is the size of the id3v2 tag.  Once we've</font>
00385     <font class="comment">// copied the data, we'll truncate the file.</font>
00386     file.seekg(this-&gt;GetPrependedBytes(), ios::beg);
00387     
00388     <a class="code" href="globals_8h.html#a33">uchar</a> aucBuffer[BUFSIZ];
00389     
00390     <font class="comment">// The nBytesRemaining variable indicates how many bytes are to be copied</font>
00391     size_t nBytesToCopy = data_size;
00392 
00393     <font class="comment">// Here we increase the nBytesToCopy by the size of any tags that appear</font>
00394     <font class="comment">// at the end of the file if we don't want to strip them</font>
00395     <font class="keywordflow">if</font> (!(ulTagFlag &amp; <a class="code" href="globals_8h.html#a217a69">ID3TT_APPENDED</a>))
00396     {
00397       nBytesToCopy += this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a22">GetAppendedBytes</a>();
00398     }
00399     
00400     <font class="comment">// The nBytesRemaining variable indicates how many bytes are left to be </font>
00401     <font class="comment">// moved in the actual file.</font>
00402     <font class="comment">// The nBytesCopied variable keeps track of how many actual bytes were</font>
00403     <font class="comment">// copied (or moved) so far.</font>
00404     size_t 
00405       nBytesRemaining = nBytesToCopy,
00406       nBytesCopied = 0;
00407     <font class="keywordflow">while</font> (!file.eof())
00408     {
00409       size_t nBytesToRead = dami::min&lt;size_t&gt;(nBytesRemaining - nBytesCopied, BUFSIZ);
00410       file.read((<font class="keywordtype">char</font> *)aucBuffer, nBytesToRead);
00411       size_t nBytesRead = file.gcount();
00412 
00413       <font class="keywordflow">if</font> (nBytesRead != nBytesToRead)
00414       {
00415         <font class="comment">// TODO: log this</font>
00416         <font class="comment">//cerr &lt;&lt; "--- attempted to write " &lt;&lt; nBytesRead &lt;&lt; " bytes, "</font>
00417         <font class="comment">//     &lt;&lt; "only wrote " &lt;&lt; nBytesWritten &lt;&lt; endl;</font>
00418       }
00419       <font class="keywordflow">if</font> (nBytesRead &gt; 0)
00420       {
00421         <font class="keywordtype">long</font> offset = nBytesRead + this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a21">GetPrependedBytes</a>();
00422         file.seekp(-offset, ios::cur);
00423         file.write((<font class="keywordtype">char</font> *)aucBuffer, nBytesRead);
00424         file.seekg(this-&gt;GetPrependedBytes(), ios::cur);
00425         nBytesCopied += nBytesRead;
00426       }
00427       
00428       <font class="keywordflow">if</font> (nBytesCopied == nBytesToCopy || nBytesToRead &lt; BUFSIZ)
00429       {
00430         <font class="keywordflow">break</font>;
00431       }
00432     }
00433     file.close();
00434   }
00435   
00436   size_t nNewFileSize = data_size;
00437 
00438   <font class="keywordflow">if</font> ((_file_tags.<a class="code" href="class_i_d3___flags.html#a2">get</a>() &amp; <a class="code" href="globals_8h.html#a217a69">ID3TT_APPENDED</a>) &amp;&amp; (ulTagFlag &amp; <a class="code" href="globals_8h.html#a217a69">ID3TT_APPENDED</a>))
00439   {
00440     ulTags |= _file_tags.<a class="code" href="class_i_d3___flags.html#a2">get</a>() &amp; <a class="code" href="globals_8h.html#a217a69">ID3TT_APPENDED</a>;
00441   }
00442   <font class="keywordflow">else</font>
00443   {
00444     <font class="comment">// if we're not stripping the appended tags, be sure to increase the file</font>
00445     <font class="comment">// size by those bytes</font>
00446     nNewFileSize += this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a22">GetAppendedBytes</a>();
00447   }
00448   
00449   <font class="keywordflow">if</font> ((ulTagFlag &amp; <a class="code" href="globals_8h.html#a217a68">ID3TT_PREPENDED</a>) &amp;&amp; (_file_tags.<a class="code" href="class_i_d3___flags.html#a2">get</a>() &amp; <a class="code" href="globals_8h.html#a217a68">ID3TT_PREPENDED</a>))
00450   {
00451     <font class="comment">// If we're stripping the ID3v2 tag, there's no need to adjust the new</font>
00452     <font class="comment">// file size, since it doesn't account for the ID3v2 tag size</font>
00453     ulTags |= _file_tags.<a class="code" href="class_i_d3___flags.html#a2">get</a>() &amp; <a class="code" href="globals_8h.html#a217a68">ID3TT_PREPENDED</a>;
00454   }
00455   <font class="keywordflow">else</font>
00456   {
00457     <font class="comment">// add the original prepended tag size since we don't want to delete it,</font>
00458     <font class="comment">// and the new file size represents the file size _not_ counting the ID3v2</font>
00459     <font class="comment">// tag</font>
00460     nNewFileSize += this-&gt;<a class="code" href="class_i_d3___tag_impl.html#a21">GetPrependedBytes</a>();
00461   }
00462 
00463   <font class="keywordflow">if</font> (ulTags &amp;&amp; (truncate(_file_name.c_str(), nNewFileSize) == -1))
00464   {
00465     <font class="comment">// log this</font>
00466     <font class="keywordflow">return</font> 0;
00467     <font class="comment">//ID3_THROW(ID3E_NoFile);</font>
00468   }
00469 
00470   _prepended_bytes = (ulTags &amp; <a class="code" href="globals_8h.html#a217a68">ID3TT_PREPENDED</a>) ? 0 : _prepended_bytes;
00471   _appended_bytes  = (ulTags &amp; <a class="code" href="globals_8h.html#a217a69">ID3TT_APPENDED</a>)  ? 0 : _appended_bytes;
00472   _file_size = data_size + _prepended_bytes + _appended_bytes;
00473   
00474   _changed = _file_tags.<a class="code" href="class_i_d3___flags.html#a6">remove</a>(ulTags) || _changed;
00475   
00476   <font class="keywordflow">return</font> ulTags;
00477 }
</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>,
 &copy;&nbsp;1997-2001</small></address>
</body>
</html>