Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

src/tag_parse_v1.cpp

Go to the documentation of this file.
00001 // $Id: tag_parse_v1.cpp,v 1.19 2001/12/16 11:44:24 shadrack 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 #if defined HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030 
00031 
00032 #include "tag_impl.h"
00033 #include "helpers.h"
00034 #include "utils.h"
00035 #include "io_decorators.h"
00036 #include "io_helpers.h"
00037 #include "io_strings.h"
00038 
00039 using namespace dami;
00040 
00041 bool id3::v1::parse(ID3_TagImpl& tag, ID3_Reader& reader)
00042 {
00043   io::ExitTrigger et(reader);
00044   
00045   ID3_Reader::pos_type end = reader.getCur();
00046   // posn ourselves at 128 bytes from the current position
00047   if (end < reader.getBeg() + ID3_V1_LEN)
00048   {
00049     ID3D_NOTICE( "id3::v1::parse: not enough bytes to parse, pos = " << end );
00050     return false;
00051   }
00052   reader.setCur(end - ID3_V1_LEN);
00053   ID3_Reader::pos_type beg = reader.getCur();
00054   //file.seekg(-static_cast<long>(ID3_V1_LEN), ios::cur);
00055   if (end != beg + ID3_V1_LEN)
00056   {
00057     ID3D_WARNING( "id3::v1::parse: failed to reposition " << ID3_V1_LEN << 
00058                   " bytes" );
00059     return false;
00060   }
00061   
00062   // read the next 128 bytes in;
00063   String id = io::readText(reader, ID3_V1_LEN_ID);
00064   
00065   // check to see if it was a tag
00066   if (id != "TAG")
00067   {
00068     return false;
00069   }
00070   et.setExitPos(beg);
00071   
00072   // guess so, let's start checking the v2 tag for frames which are the
00073   // equivalent of the v1 fields.  When we come across a v1 field that has
00074   // no current equivalent v2 frame, we create the frame, copy the data
00075   // from the v1 frame and attach it to the tag
00076 
00077   // (Scott Wheeler) The above comment was nice in theory, but it wasn't
00078   // first checking (before my hacks) to see if there already was v2 data.
00079 
00080   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00081   String title = io::readTrailingSpaces(reader, ID3_V1_LEN_TITLE);
00082   if (title.size() > 0 && !id3::v2::hasTitle(tag))
00083   {
00084     id3::v2::setTitle(tag, title);
00085   }
00086   ID3D_NOTICE( "id3::v1::parse: title = \"" << title << "\"" );
00087   
00088   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00089   String artist = io::readTrailingSpaces(reader, ID3_V1_LEN_ARTIST);
00090   if (artist.size() > 0 && !id3::v2::hasArtist(tag))
00091   {
00092     id3::v2::setArtist(tag, artist);
00093   }
00094   ID3D_NOTICE( "id3::v1::parse: artist = \"" << artist << "\"" );
00095   
00096   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00097   String album = io::readTrailingSpaces(reader, ID3_V1_LEN_ALBUM);
00098   if (album.size() > 0 && !id3::v2::hasAlbum(tag)) 
00099   {
00100     id3::v2::setAlbum(tag, album);
00101   }
00102   ID3D_NOTICE( "id3::v1::parse: album = \"" << title << "\"" );
00103   
00104   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00105   String year = io::readTrailingSpaces(reader, ID3_V1_LEN_YEAR);
00106   if (year.size() > 0 && !id3::v2::hasYear(tag))
00107   {
00108     id3::v2::setYear(tag, year);
00109   }
00110   ID3D_NOTICE( "id3::v1::parse: year = \"" << year << "\"" );
00111   
00112   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00113   String comment = io::readTrailingSpaces(reader, ID3_V1_LEN_COMMENT);
00114   if (comment.length() == ID3_V1_LEN_COMMENT  &&
00115       '\0' == comment[ID3_V1_LEN_COMMENT - 2] ||
00116       '\0' != comment[ID3_V1_LEN_COMMENT - 1])
00117   {
00118     // This is an id3v1.1 tag.  The last byte of the comment is the track
00119     // number.  
00120     size_t track = comment[ID3_V1_LEN_COMMENT - 1];
00121     if (track > 0 && !id3::v2::hasTrack(tag))
00122     {
00123       id3::v2::setTrack(tag, track, 0);
00124     }
00125     ID3D_NOTICE( "id3::v1::parse: track = \"" << track << "\"" );
00126 
00127     ID3D_NOTICE( "id3::v1::parse: comment length = \"" << comment.length() << "\"" );
00128     io::StringReader sr(comment);
00129     comment = io::readTrailingSpaces(sr, ID3_V1_LEN_COMMENT - 2);
00130   }
00131   ID3D_NOTICE( "id3::v1::parse: comment = \"" << comment << "\"" );
00132   if (comment.size() > 0)
00133   {
00134     id3::v2::setComment(tag, comment, STR_V1_COMMENT_DESC, "XXX");
00135   }
00136   
00137   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00138   // the GENRE field/frame
00139   uchar genre = reader.readChar();
00140   if (genre != 0xFF && !id3::v2::hasGenre(tag)) 
00141   {
00142     id3::v2::setGenre(tag, genre);
00143   }
00144   ID3D_NOTICE( "id3::v1::parse: genre = \"" << (int) genre << "\"" );
00145 
00146   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00147   return true;
00148 }

Generated on Thu Jan 3 07:35:56 2002 for id3lib by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001