Chris@0: /* Chris@0: * libid3tag - ID3 tag manipulation library Chris@0: * Copyright (C) 2000-2004 Underbit Technologies, Inc. Chris@0: * Chris@0: * This program is free software; you can redistribute it and/or modify Chris@0: * it under the terms of the GNU General Public License as published by Chris@0: * the Free Software Foundation; either version 2 of the License, or Chris@0: * (at your option) any later version. Chris@0: * Chris@0: * This program is distributed in the hope that it will be useful, Chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: * GNU General Public License for more details. Chris@0: * Chris@0: * You should have received a copy of the GNU General Public License Chris@0: * along with this program; if not, write to the Free Software Chris@0: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@0: * Chris@0: * $Id: genre.c,v 1.8 2004/01/23 09:41:32 rob Exp $ Chris@0: */ Chris@0: Chris@0: # ifdef HAVE_CONFIG_H Chris@0: # include "config.h" Chris@0: # endif Chris@0: Chris@0: # include "global.h" Chris@0: Chris@0: # include "id3tag.h" Chris@0: # include "ucs4.h" Chris@0: Chris@0: /* genres are stored in ucs4 format */ Chris@0: # include "genre.dat" Chris@0: Chris@0: # define NGENRES (sizeof(genre_table) / sizeof(genre_table[0])) Chris@0: Chris@0: /* Chris@0: * NAME: genre->index() Chris@0: * DESCRIPTION: return an ID3v1 genre string indexed by number Chris@0: */ Chris@0: id3_ucs4_t const *id3_genre_index(unsigned int index) Chris@0: { Chris@0: return (index < NGENRES) ? genre_table[index] : 0; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: genre->name() Chris@0: * DESCRIPTION: translate an ID3v2 genre number/keyword to its full name Chris@0: */ Chris@0: id3_ucs4_t const *id3_genre_name(id3_ucs4_t const *string) Chris@0: { Chris@0: id3_ucs4_t const *ptr; Chris@0: static id3_ucs4_t const genre_remix[] = { 'R', 'e', 'm', 'i', 'x', 0 }; Chris@0: static id3_ucs4_t const genre_cover[] = { 'C', 'o', 'v', 'e', 'r', 0 }; Chris@0: unsigned long number; Chris@0: Chris@0: if (string == 0 || *string == 0) Chris@0: return id3_ucs4_empty; Chris@0: Chris@0: if (string[0] == 'R' && string[1] == 'X' && string[2] == 0) Chris@0: return genre_remix; Chris@0: if (string[0] == 'C' && string[1] == 'R' && string[2] == 0) Chris@0: return genre_cover; Chris@0: Chris@0: for (ptr = string; *ptr; ++ptr) { Chris@0: if (*ptr < '0' || *ptr > '9') Chris@0: return string; Chris@0: } Chris@0: Chris@0: number = id3_ucs4_getnumber(string); Chris@0: Chris@0: return (number < NGENRES) ? genre_table[number] : string; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: translate() Chris@0: * DESCRIPTION: return a canonicalized character for testing genre equivalence Chris@0: */ Chris@0: static Chris@0: id3_ucs4_t translate(id3_ucs4_t ch) Chris@0: { Chris@0: if (ch) { Chris@0: if (ch >= 'A' && ch <= 'Z') Chris@0: ch += 'a' - 'A'; Chris@0: Chris@0: if (ch < 'a' || ch > 'z') Chris@0: ch = ID3_UCS4_REPLACEMENTCHAR; Chris@0: } Chris@0: Chris@0: return ch; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: compare() Chris@0: * DESCRIPTION: test two ucs4 genre strings for equivalence Chris@0: */ Chris@0: static Chris@0: int compare(id3_ucs4_t const *str1, id3_ucs4_t const *str2) Chris@0: { Chris@0: id3_ucs4_t c1, c2; Chris@0: Chris@0: if (str1 == str2) Chris@0: return 1; Chris@0: Chris@0: do { Chris@0: do Chris@0: c1 = translate(*str1++); Chris@0: while (c1 == ID3_UCS4_REPLACEMENTCHAR); Chris@0: Chris@0: do Chris@0: c2 = translate(*str2++); Chris@0: while (c2 == ID3_UCS4_REPLACEMENTCHAR); Chris@0: } Chris@0: while (c1 && c1 == c2); Chris@0: Chris@0: return c1 == c2; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: genre->number() Chris@0: * DESCRIPTION: translate an ID3v2 genre name/number to its ID3v1 index number Chris@0: */ Chris@0: int id3_genre_number(id3_ucs4_t const *string) Chris@0: { Chris@0: id3_ucs4_t const *ptr; Chris@0: int i; Chris@0: Chris@0: if (string == 0 || *string == 0) Chris@0: return -1; Chris@0: Chris@0: for (ptr = string; *ptr; ++ptr) { Chris@0: if (*ptr < '0' || *ptr > '9') Chris@0: break; Chris@0: } Chris@0: Chris@0: if (*ptr == 0) { Chris@0: unsigned long number; Chris@0: Chris@0: number = id3_ucs4_getnumber(string); Chris@0: Chris@0: return (number <= 0xff) ? number : -1; Chris@0: } Chris@0: Chris@0: for (i = 0; i < NGENRES; ++i) { Chris@0: if (compare(string, genre_table[i])) Chris@0: return i; Chris@0: } Chris@0: Chris@0: /* no equivalent */ Chris@0: Chris@0: return -1; Chris@0: }