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.17 2000/10/29 01:37:29 eldamitri 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   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00077   String title = io::readTrailingSpaces(reader, ID3_V1_LEN_TITLE);
00078   if (title.size() > 0)
00079   {
00080     id3::v2::setTitle(tag, title);
00081   }
00082   ID3D_NOTICE( "id3::v1::parse: title = \"" << title << "\"" );
00083   
00084   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00085   String artist = io::readTrailingSpaces(reader, ID3_V1_LEN_ARTIST);
00086   if (artist.size() > 0)
00087   {
00088     id3::v2::setArtist(tag, artist);
00089   }
00090   ID3D_NOTICE( "id3::v1::parse: artist = \"" << artist << "\"" );
00091   
00092   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00093   String album = io::readTrailingSpaces(reader, ID3_V1_LEN_ALBUM);
00094   if (album.size() > 0) 
00095   {
00096     id3::v2::setAlbum(tag, album);
00097   }
00098   ID3D_NOTICE( "id3::v1::parse: album = \"" << title << "\"" );
00099   
00100   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00101   String year = io::readTrailingSpaces(reader, ID3_V1_LEN_YEAR);
00102   if (year.size() > 0)
00103   {
00104     id3::v2::setYear(tag, year);
00105   }
00106   ID3D_NOTICE( "id3::v1::parse: year = \"" << year << "\"" );
00107   
00108   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00109   String comment = io::readTrailingSpaces(reader, ID3_V1_LEN_COMMENT);
00110   if (comment.length() == ID3_V1_LEN_COMMENT  &&
00111       '\0' == comment[ID3_V1_LEN_COMMENT - 2] ||
00112       '\0' != comment[ID3_V1_LEN_COMMENT - 1])
00113   {
00114     // This is an id3v1.1 tag.  The last byte of the comment is the track
00115     // number.  
00116     size_t track = comment[ID3_V1_LEN_COMMENT - 1];
00117     id3::v2::setTrack(tag, track, 0);
00118     ID3D_NOTICE( "id3::v1::parse: track = \"" << track << "\"" );
00119 
00120     ID3D_NOTICE( "id3::v1::parse: comment length = \"" << comment.length() << "\"" );
00121     io::StringReader sr(comment);
00122     comment = io::readTrailingSpaces(sr, ID3_V1_LEN_COMMENT - 2);
00123   }
00124   ID3D_NOTICE( "id3::v1::parse: comment = \"" << comment << "\"" );
00125   if (comment.size() > 0)
00126   {
00127     id3::v2::setComment(tag, comment, STR_V1_COMMENT_DESC, "XXX");
00128   }
00129   
00130   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00131   // the GENRE field/frame
00132   uchar genre = reader.readChar();
00133   if (genre != 0xFF) 
00134   {
00135     id3::v2::setGenre(tag, genre);
00136   }
00137   ID3D_NOTICE( "id3::v1::parse: genre = \"" << (int) genre << "\"" );
00138 
00139   ID3D_NOTICE("id3::v1::parse: read bytes: " << reader.getCur() - beg);
00140   return true;
00141 }

Generated at Sat Sep 8 15:51:10 2001 for id3lib by doxygen1.2.8 written by Dimitri van Heesch, © 1997-2001