Main Page Data Structures File List Data Fields Globals Related Pages

# id3v1.cpp

Go to the documentation of this file.

    00001 /*  id3.cpp
    00002     Functions to read a ID3 tag from a file
    00003
    00004     10.08.2001: tk, initial implementation.
    00005
    00006     Copyright (c)2000 by Thomas Kindler, thomas.kindler@gmx.de
    00007
    00008     This program is free software; you can redistribute it and/or
    00009     modify it under the terms of the GNU General Public License as
    00010     published by the Free Software Foundation; either version 2 of
    00011     the License, or (at your option) any later version. Read the
    00012     full License at http://www.gnu.org/copyleft for more details.
    00013 */
    00014
    00015 // include files -----
    00016 //
    00017 #include <stdio.h>
    00018 #include <string.h>
    00019 #include <assert.h>
    00020 #include "id3v1.h"
    00021 #include "misc.h"
    00022
    00023 static const char* id3genres[] = {
    00024   "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
    00025   "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
    00026   "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
    00027   "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop",
    00028   "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental",
    00029   "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "Alternative Rock",
    00030   "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop",
    00031   "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial",
    00032   "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
    00033   "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
    00034   "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave", "Showtunes",
    00035   "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro",
    00036   "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk-Rock", "National Folk",
    00037   "Swing", "Fast Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass",
    00038   "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock",
    00039   "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening",
    00040   "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music",
    00041   "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove", "Satire",
    00042   "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad",
    00043   "Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "Acapella",
    00044   "Euro-House", "Dance Hall", "Goa", "Drum & Bass", "Club-House", "Hardcore",
    00045   "Terror", "Indie", "BritPop", "Negerpunk", "Polsk Punk", "Beat",
    00046   "Christian Gangsta Rap", "Heavy Metal", "Black Metal", "Crossover",
    00047   "Contemporary Christian", "Christian Rock", "Merengue", "Salsa", "Thrash Metal",
    00048   "Anime", "Jpop", "Synthpop"
    00049 };
    00050
    00051 #define NUMGENRES sizeof(id3genres)/sizeof(char*)
    00052
    00053
    00062 static void trim(char *str, int length)
    00063 {
    00064   for (int i = length-1; i>=0; i--) {
    00065     if (str[i] == ' ' || str[i] == '\0')
    00066       str[i] = '\0';
    00067     else
    00068       break;
    00069   }
    00070 }
    00071
    00072
    00079 const char* ID3v1_GetGenreString(int genre)
    00080 {
    00081   static char unknown[] = "genre #xxxxx";
    00082
    00083   if ((genre < 0) || (genre >= NUMGENRES)) {
    00084     assert(
    00085       sprintf(unknown, "genre #%u", genre)
    00086       < sizeof(unknown)
    00087     );
    00088     return unknown;
    00089   }
    00090
    00091   return id3genres[genre];
    00092 }
    00093
    00104 int ID3v1_ReadTag(FILE *f, ID3v1Tag *tag)
    00105 {
    00106   bool tagfound = false;
    00107   long oldpos;
    00108   char magic[3];
    00109
    00110   memset(tag, 0, sizeof(ID3v1Tag));
    00111
    00112   oldpos = ftell(f);
    00113   fseek(f, -128, SEEK_END);
    00114
    00115   fread(magic, sizeof(magic), 1, f);
    00116   if (magic[0]=='T' && magic[1]=='A' && magic[2]=='G') {
    00117     fread( tag->title,   30, 1, f);
    00118     fread( tag->artist,  30, 1, f);
    00119     fread( tag->album,   30, 1, f);
    00120     fread( tag->year,     4, 1, f);
    00121     fread( tag->comment, 30, 1, f);
    00122     fread(&tag->genre,    1, 1, f);
    00123
    00124     // ID3v1.1 extension for track number
    00125     //
    00126     if (!tag->comment[28] && tag->comment[29]) {
    00127       tag->track = tag->comment[29];
    00128       tag->comment[29] = '\0';
    00129     }
    00130     trim(tag->title,   30);
    00131     trim(tag->artist,  30);
    00132     trim(tag->album,   30);
    00133     trim(tag->year,     4);
    00134     trim(tag->comment, 30);
    00135
    00136     tagfound = true;
    00137   }
    00138
    00139   fseek(f, oldpos, SEEK_SET);
    00140   return tagfound;
    00141 }
    00142
    00143