genre.c 3.39 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/*
 * libid3tag - ID3 tag manipulation library
 * Copyright (C) 2000-2003 Underbit Technologies, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: genre.c,v 1.1 2003/08/14 03:57:13 shank Exp $
 */

# ifdef HAVE_CONFIG_H
#  include "config.h"
# endif

# include "global.h"

# include "id3tag.h"
# include "ucs4.h"

/* genres are stored in ucs4 format */
# include "genre.dat"

# define NGENRES  (sizeof(genre_table) / sizeof(genre_table[0]))

/*
 * NAME:	genre->index()
 * DESCRIPTION:	return an ID3v1 genre string indexed by number
 */
id3_ucs4_t const *id3_genre_index(unsigned int index)
{
  return (index < NGENRES) ? genre_table[index] : 0;
}

/*
 * NAME:	genre->name()
 * DESCRIPTION:	translate an ID3v2 genre number/keyword to its full name
 */
id3_ucs4_t const *id3_genre_name(id3_ucs4_t const *string)
{
  id3_ucs4_t const *ptr;
  static id3_ucs4_t const genre_remix[] = { 'R', 'e', 'm', 'i', 'x', 0 };
  static id3_ucs4_t const genre_cover[] = { 'C', 'o', 'v', 'e', 'r', 0 };
  unsigned long number;

  if (string == 0 || *string == 0)
    return id3_ucs4_empty;

  if (string[0] == 'R' && string[1] == 'X' && string[2] == 0)
    return genre_remix;
  if (string[0] == 'C' && string[1] == 'R' && string[2] == 0)
    return genre_cover;

  for (ptr = string; *ptr; ++ptr) {
    if (*ptr < '0' || *ptr > '9')
      return string;
  }

  number = id3_ucs4_getnumber(string);

  return (number < NGENRES) ? genre_table[number] : string;
}

/*
 * NAME:	translate()
 * DESCRIPTION:	return a canonicalized character for testing genre equivalence
 */
static
id3_ucs4_t translate(id3_ucs4_t ch)
{
  if (ch) {
    if (ch >= 'A' && ch <= 'Z')
      ch += 'a' - 'A';

    if (ch < 'a' || ch > 'z')
      ch = ID3_UCS4_REPLACEMENTCHAR;
  }

  return ch;
}

/*
 * NAME:	compare()
 * DESCRIPTION:	test two ucs4 genre strings for equivalence
 */
static
int compare(id3_ucs4_t const *str1, id3_ucs4_t const *str2)
{
  id3_ucs4_t c1, c2;

  if (str1 == str2)
    return 1;

  do {
    do
      c1 = translate(*str1++);
    while (c1 == ID3_UCS4_REPLACEMENTCHAR);

    do
      c2 = translate(*str2++);
    while (c2 == ID3_UCS4_REPLACEMENTCHAR);
  }
  while (c1 && c1 == c2);

  return c1 == c2;
}

/*
 * NAME:	genre->number()
 * DESCRIPTION:	translate an ID3v2 genre name/number to its ID3v1 index number
 */
int id3_genre_number(id3_ucs4_t const *string)
{
  id3_ucs4_t const *ptr;
  int i;

  if (string == 0 || *string == 0)
    return -1;

  for (ptr = string; *ptr; ++ptr) {
    if (*ptr < '0' || *ptr > '9')
      break;
  }

  if (*ptr == 0) {
    unsigned long number;

    number = id3_ucs4_getnumber(string);

    return (number <= 0xff) ? number : -1;
  }

  for (i = 0; i < NGENRES; ++i) {
    if (compare(string, genre_table[i]))
      return i;
  }

  /* no equivalent */

  return -1;
}