00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
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
00063 String id = io::readText(reader, ID3_V1_LEN_ID);
00064
00065
00066 if (id != "TAG")
00067 {
00068 return false;
00069 }
00070 et.setExitPos(beg);
00071
00072
00073
00074
00075
00076
00077
00078
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
00119
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
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 }