1 /*
2  * libid3tag - ID3 tag manipulation library
3  * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4  *
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.
9  *
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.
14  *
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
18  *
19  * $Id: genre.c,v 1.8 2004/01/23 09:41:32 rob Exp $
20  */
21 
22 # ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 # endif
25 
26 # include "global.h"
27 
28 # include "id3tag.h"
29 # include "ucs4.h"
30 
31 /* genres are stored in ucs4 format */
32 # include "genre.dat"
33 
34 # define NGENRES  (sizeof(genre_table) / sizeof(genre_table[0]))
35 
36 /*
37  * NAME:	genre->index()
38  * DESCRIPTION:	return an ID3v1 genre string indexed by number
39  */
id3_genre_index(unsigned int index)40 id3_ucs4_t const *id3_genre_index(unsigned int index)
41 {
42   return (index < NGENRES) ? genre_table[index] : 0;
43 }
44 
45 /*
46  * NAME:	genre->name()
47  * DESCRIPTION:	translate an ID3v2 genre number/keyword to its full name
48  */
id3_genre_name(id3_ucs4_t const * string)49 id3_ucs4_t const *id3_genre_name(id3_ucs4_t const *string)
50 {
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;
55 
56   if (string == 0 || *string == 0)
57     return id3_ucs4_empty;
58 
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;
63 
64   for (ptr = string; *ptr; ++ptr) {
65     if (*ptr < '0' || *ptr > '9')
66       return string;
67   }
68 
69   number = id3_ucs4_getnumber(string);
70 
71   return (number < NGENRES) ? genre_table[number] : string;
72 }
73 
74 /*
75  * NAME:	translate()
76  * DESCRIPTION:	return a canonicalized character for testing genre equivalence
77  */
78 static
translate(id3_ucs4_t ch)79 id3_ucs4_t translate(id3_ucs4_t ch)
80 {
81   if (ch) {
82     if (ch >= 'A' && ch <= 'Z')
83       ch += 'a' - 'A';
84 
85     if (ch < 'a' || ch > 'z')
86       ch = ID3_UCS4_REPLACEMENTCHAR;
87   }
88 
89   return ch;
90 }
91 
92 /*
93  * NAME:	compare()
94  * DESCRIPTION:	test two ucs4 genre strings for equivalence
95  */
96 static
compare(id3_ucs4_t const * str1,id3_ucs4_t const * str2)97 int compare(id3_ucs4_t const *str1, id3_ucs4_t const *str2)
98 {
99   id3_ucs4_t c1, c2;
100 
101   if (str1 == str2)
102     return 1;
103 
104   do {
105     do
106       c1 = translate(*str1++);
107     while (c1 == ID3_UCS4_REPLACEMENTCHAR);
108 
109     do
110       c2 = translate(*str2++);
111     while (c2 == ID3_UCS4_REPLACEMENTCHAR);
112   }
113   while (c1 && c1 == c2);
114 
115   return c1 == c2;
116 }
117 
118 /*
119  * NAME:	genre->number()
120  * DESCRIPTION:	translate an ID3v2 genre name/number to its ID3v1 index number
121  */
id3_genre_number(id3_ucs4_t const * string)122 int id3_genre_number(id3_ucs4_t const *string)
123 {
124   id3_ucs4_t const *ptr;
125   int i;
126 
127   if (string == 0 || *string == 0)
128     return -1;
129 
130   for (ptr = string; *ptr; ++ptr) {
131     if (*ptr < '0' || *ptr > '9')
132       break;
133   }
134 
135   if (*ptr == 0) {
136     unsigned long number;
137 
138     number = id3_ucs4_getnumber(string);
139 
140     return (number <= 0xff) ? number : -1;
141   }
142 
143   for (i = 0; i < NGENRES; ++i) {
144     if (compare(string, genre_table[i]))
145       return i;
146   }
147 
148   /* no equivalent */
149 
150   return -1;
151 }
152