MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / libid3tag / genre.c
blob32fccb1977cb4dcdc16e12be7d8b831b4711f27e
1 /*
2 * libid3tag - ID3 tag manipulation library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * $Id: genre.c,v 1.8 2004/01/23 09:41:32 rob Exp $
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
28 # include "id3tag.h"
29 # include "ucs4.h"
31 /* genres are stored in ucs4 format */
32 # include "genre.dat"
34 # define NGENRES (sizeof(genre_table) / sizeof(genre_table[0]))
37 * NAME: genre->index()
38 * DESCRIPTION: return an ID3v1 genre string indexed by number
40 id3_ucs4_t const *id3_genre_index(unsigned int index)
42 return (index < NGENRES) ? genre_table[index] : 0;
46 * NAME: genre->name()
47 * DESCRIPTION: translate an ID3v2 genre number/keyword to its full name
49 id3_ucs4_t const *id3_genre_name(id3_ucs4_t const *string)
51 id3_ucs4_t const *ptr;
52 static id3_ucs4_t const genre_remix[] = { 'R', 'e', 'm', 'i', 'x', 0 };
53 static id3_ucs4_t const genre_cover[] = { 'C', 'o', 'v', 'e', 'r', 0 };
54 unsigned long number;
56 if (string == 0 || *string == 0)
57 return id3_ucs4_empty;
59 if (string[0] == 'R' && string[1] == 'X' && string[2] == 0)
60 return genre_remix;
61 if (string[0] == 'C' && string[1] == 'R' && string[2] == 0)
62 return genre_cover;
64 for (ptr = string; *ptr; ++ptr) {
65 if (*ptr < '0' || *ptr > '9')
66 return string;
69 number = id3_ucs4_getnumber(string);
71 return (number < NGENRES) ? genre_table[number] : string;
75 * NAME: translate()
76 * DESCRIPTION: return a canonicalized character for testing genre equivalence
78 static
79 id3_ucs4_t translate(id3_ucs4_t ch)
81 if (ch) {
82 if (ch >= 'A' && ch <= 'Z')
83 ch += 'a' - 'A';
85 if (ch < 'a' || ch > 'z')
86 ch = ID3_UCS4_REPLACEMENTCHAR;
89 return ch;
93 * NAME: compare()
94 * DESCRIPTION: test two ucs4 genre strings for equivalence
96 static
97 int compare(id3_ucs4_t const *str1, id3_ucs4_t const *str2)
99 id3_ucs4_t c1, c2;
101 if (str1 == str2)
102 return 1;
104 do {
106 c1 = translate(*str1++);
107 while (c1 == ID3_UCS4_REPLACEMENTCHAR);
110 c2 = translate(*str2++);
111 while (c2 == ID3_UCS4_REPLACEMENTCHAR);
113 while (c1 && c1 == c2);
115 return c1 == c2;
119 * NAME: genre->number()
120 * DESCRIPTION: translate an ID3v2 genre name/number to its ID3v1 index number
122 int id3_genre_number(id3_ucs4_t const *string)
124 id3_ucs4_t const *ptr;
125 int i;
127 if (string == 0 || *string == 0)
128 return -1;
130 for (ptr = string; *ptr; ++ptr) {
131 if (*ptr < '0' || *ptr > '9')
132 break;
135 if (*ptr == 0) {
136 unsigned long number;
138 number = id3_ucs4_getnumber(string);
140 return (number <= 0xff) ? number : -1;
143 for (i = 0; i < NGENRES; ++i) {
144 if (compare(string, genre_table[i]))
145 return i;
148 /* no equivalent */
150 return -1;