~ubuntu-branches/ubuntu/maverick/libcdio/maverick

« back to all changes in this revision

Viewing changes to lib/cdtext.c

  • Committer: Bazaar Package Importer
  • Author(s): Nicolas Boullis
  • Date: 2005-02-06 00:17:06 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050206001706-d54gpyhfixexuj2d
Tags: 0.71-2
* Comment out all extraneous declarations of
  'gl_default_cdio_log_handler'. (Closes: 289694)
* Fix the broken symbol-versionning script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    $Id: cdtext.c,v 1.10 2004/11/08 04:13:27 rocky Exp $
 
3
 
 
4
    Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
 
5
    toc reading routine adapted from cuetools
 
6
    Copyright (C) 2003 Svend Sanjay Sorensen <ssorensen@fastmail.fm>
 
7
 
 
8
    This program is free software; you can redistribute it and/or modify
 
9
    it under the terms of the GNU General Public License as published by
 
10
    the Free Software Foundation; either version 2 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    This program is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU General Public License
 
19
    along with this program; if not, write to the Free Software
 
20
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
*/
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
# include "config.h"
 
25
#endif
 
26
 
 
27
#include <cdio/cdtext.h>
 
28
#include <cdio/logging.h>
 
29
#include "cdtext_private.h"
 
30
 
 
31
#ifdef HAVE_STDLIB_H
 
32
#include <stdlib.h>
 
33
#endif
 
34
 
 
35
#ifdef HAVE_STRING_H
 
36
#include <string.h>
 
37
#endif
 
38
 
 
39
/*! Note: the order and number items (except CDTEXT_INVALID) should
 
40
  match the cdtext_field_t enumeration. */
 
41
const char *cdtext_keywords[] = 
 
42
  {
 
43
    "ARRANGER",
 
44
    "COMPOSER",
 
45
    "DISC_ID",
 
46
    "GENRE",
 
47
    "ISRC",
 
48
    "MESSAGE",
 
49
    "PERFORMER",
 
50
    "SIZE_INFO",
 
51
    "SONGWRITER",
 
52
    "TITLE",
 
53
    "TOC_INFO",
 
54
    "TOC_INFO2",
 
55
    "UPC_EAN",
 
56
  };
 
57
 
 
58
 
 
59
/*! Return string representation of the enum values above */
 
60
const char *
 
61
cdtext_field2str (cdtext_field_t i)
 
62
{
 
63
  if (i >= MAX_CDTEXT_FIELDS)
 
64
    return "Invalid CDTEXT field index";
 
65
  else 
 
66
    return cdtext_keywords[i];
 
67
}
 
68
 
 
69
 
 
70
/*! Free memory assocated with cdtext*/
 
71
void 
 
72
cdtext_destroy (cdtext_t *cdtext)
 
73
{
 
74
  cdtext_field_t i;
 
75
 
 
76
  for (i=0; i < MAX_CDTEXT_FIELDS; i++) {
 
77
    if (cdtext->field[i]) free(cdtext->field[i]);
 
78
  }
 
79
}
 
80
 
 
81
/*! 
 
82
  returns the CDTEXT value associated with key. NULL is returned
 
83
  if key is CDTEXT_INVALID or the field is not set.
 
84
 */
 
85
const char *
 
86
cdtext_get (cdtext_field_t key, const cdtext_t *cdtext)
 
87
{
 
88
  if (key == CDTEXT_INVALID) return NULL;
 
89
  return cdtext->field[key];
 
90
}
 
91
 
 
92
/*! Initialize a new cdtext structure.
 
93
  When the structure is no longer needed, release the 
 
94
  resources using cdtext_delete.
 
95
*/
 
96
void 
 
97
cdtext_init (cdtext_t *cdtext)
 
98
{
 
99
  cdtext_field_t i;
 
100
 
 
101
  for (i=0; i < MAX_CDTEXT_FIELDS; i++) {
 
102
    cdtext->field[i] = NULL;
 
103
  }
 
104
}
 
105
 
 
106
/*!
 
107
  returns 0 if field is a CD-TEXT keyword, returns non-zero otherwise 
 
108
*/
 
109
cdtext_field_t
 
110
cdtext_is_keyword (const char *key)
 
111
{
 
112
#if 0  
 
113
  char *item;
 
114
  
 
115
  item = bsearch(key, 
 
116
                 cdtext_keywords, 12,
 
117
                 sizeof (char *), 
 
118
                 (int (*)(const void *, const void *))
 
119
                 strcmp);
 
120
  return (NULL != item) ? 0 : 1;
 
121
#else 
 
122
  unsigned int i;
 
123
  
 
124
  for (i = 0; i < 13 ; i++)
 
125
    if (0 == strcmp (cdtext_keywords[i], key)) {
 
126
      return i;
 
127
    }
 
128
  return CDTEXT_INVALID;
 
129
#endif
 
130
}
 
131
 
 
132
/*! sets cdtext's keyword entry to field.
 
133
 */
 
134
void 
 
135
cdtext_set (cdtext_field_t key, const char *value, cdtext_t *cdtext)
 
136
{
 
137
  if (NULL == value || key == CDTEXT_INVALID) return;
 
138
  
 
139
  if (cdtext->field[key]) free (cdtext->field[key]);
 
140
  cdtext->field[key] = strdup (value);
 
141
  
 
142
}
 
143
 
 
144
#define SET_CDTEXT_FIELD(FIELD) \
 
145
  (*set_cdtext_field_fn)(user_data, i_track, i_first_track, FIELD, buffer);
 
146
 
 
147
/* 
 
148
  parse all CD-TEXT data retrieved.
 
149
*/       
 
150
bool
 
151
cdtext_data_init(void *user_data, track_t i_first_track, 
 
152
                 unsigned char *wdata, 
 
153
                 set_cdtext_field_fn_t set_cdtext_field_fn) 
 
154
{
 
155
  CDText_data_t *pdata;
 
156
  int           i;
 
157
  int           j;
 
158
  char          buffer[256];
 
159
  int           idx;
 
160
  int           i_track;
 
161
  bool          b_ret = false;
 
162
  
 
163
  memset( buffer, 0x00, sizeof(buffer) );
 
164
  idx = 0;
 
165
  
 
166
  pdata = (CDText_data_t *) (&wdata[4]);
 
167
  for( i=0; i < CDIO_CDTEXT_MAX_PACK_DATA; i++ ) {
 
168
 
 
169
#if TESTED
 
170
    if ( pdata->bDBC ) {
 
171
      cdio_warn("Double-byte characters not supported");
 
172
      return false;
 
173
    }
 
174
#endif
 
175
    
 
176
    if( pdata->seq != i )
 
177
      break;
 
178
    
 
179
    if( (pdata->type >= 0x80) 
 
180
        && (pdata->type <= 0x85) && (pdata->block == 0) ) {
 
181
      i_track = pdata->i_track;
 
182
      
 
183
      for( j=0; j < CDIO_CDTEXT_MAX_TEXT_DATA; j++ ) {
 
184
        if( pdata->text[j] == 0x00 ) {
 
185
          bool b_field_set=true;
 
186
          switch( pdata->type) {
 
187
          case CDIO_CDTEXT_TITLE: 
 
188
            SET_CDTEXT_FIELD(CDTEXT_TITLE);
 
189
            break;
 
190
          case CDIO_CDTEXT_PERFORMER:  
 
191
            SET_CDTEXT_FIELD(CDTEXT_PERFORMER);
 
192
            break;
 
193
          case CDIO_CDTEXT_SONGWRITER:
 
194
            SET_CDTEXT_FIELD(CDTEXT_SONGWRITER);
 
195
            break;
 
196
          case CDIO_CDTEXT_COMPOSER:
 
197
            SET_CDTEXT_FIELD(CDTEXT_COMPOSER);
 
198
            break;
 
199
          case CDIO_CDTEXT_ARRANGER:
 
200
            SET_CDTEXT_FIELD(CDTEXT_ARRANGER);
 
201
            break;
 
202
          case CDIO_CDTEXT_MESSAGE:
 
203
            SET_CDTEXT_FIELD(CDTEXT_MESSAGE);
 
204
            break;
 
205
          case CDIO_CDTEXT_DISCID: 
 
206
            SET_CDTEXT_FIELD(CDTEXT_DISCID);
 
207
            break;
 
208
          case CDIO_CDTEXT_GENRE: 
 
209
            SET_CDTEXT_FIELD(CDTEXT_GENRE);
 
210
            break;
 
211
          default : b_field_set = false;
 
212
          }
 
213
          if (b_field_set) {
 
214
            b_ret = true;
 
215
            i_track++;
 
216
            idx = 0;
 
217
          }
 
218
        } else {
 
219
          buffer[idx++] = pdata->text[j];
 
220
        }
 
221
        buffer[idx] = 0x00;
 
222
      }
 
223
    }
 
224
    pdata++;
 
225
  }
 
226
  return b_ret;
 
227
}
 
228