diff --git a/src/cd-info.c b/src/cd-info.c index 09d870a2..990eb21f 100644 --- a/src/cd-info.c +++ b/src/cd-info.c @@ -1,5 +1,5 @@ /* - $Id: cd-info.c,v 1.133 2005/03/11 10:34:28 rocky Exp $ + $Id: cd-info.c,v 1.134 2005/03/12 06:02:36 rocky Exp $ Copyright (C) 2003, 2004, 2005 Rocky Bernstein Copyright (C) 1996, 1997, 1998 Gerd Knorr @@ -397,81 +397,39 @@ print_cdtext_info(CdIo_t *p_cdio, track_t i_tracks, track_t i_first_track) { } #ifdef HAVE_CDDB + static void -print_cddb_info(CdIo_t *p_cdio, track_t i_tracks, track_t i_first_track) { - int i, matches; +cddb_errmsg(const char *msg) +{ + report(stderr, "%s: %s\n", program_name, msg); +} + + +static void +print_cddb_info(CdIo_t *p_cdio, track_t i_tracks, track_t i_first_track) +{ + int i, i_cddb_matches = 0; - cddb_conn_t *p_conn = cddb_new(); - cddb_disc_t *disc = NULL; + cddb_conn_t *p_conn = NULL; + cddb_disc_t *p_cddb_disc = NULL; - if (!p_conn) { - report(stderr, "%s: unable to initialize libcddb\n", program_name); - goto cddb_destroy; - } + if ( init_cddb(p_cdio, &p_conn, &p_cddb_disc, cddb_errmsg, i_first_track, + i_tracks, &i_cddb_matches) ) { - if (NULL == cddb_opts.email) - cddb_set_email_address(p_conn, "me@home"); - else - cddb_set_email_address(p_conn, cddb_opts.email); - - if (NULL == cddb_opts.server) - cddb_set_server_name(p_conn, "freedb.freedb.org"); - else - cddb_set_server_name(p_conn, cddb_opts.server); - - if (cddb_opts.timeout >= 0) - cddb_set_timeout(p_conn, cddb_opts.timeout); - - cddb_set_server_port(p_conn, cddb_opts.port); - - if (cddb_opts.http) - cddb_http_enable(p_conn); - else - cddb_http_disable(p_conn); - - if (NULL != cddb_opts.cachedir) - cddb_cache_set_dir(p_conn, cddb_opts.cachedir); - - if (cddb_opts.disable_cache) - cddb_cache_disable(p_conn); - - disc = cddb_disc_new(); - if (!disc) { - report(stderr, "%s: unable to create CDDB disc structure", program_name); - goto cddb_destroy; - } - for(i = 0; i < i_tracks; i++) { - cddb_track_t *t = cddb_track_new(); - t->frame_offset = cdio_get_track_lba(p_cdio, i+i_first_track); - cddb_disc_add_track(disc, t); - } - - disc->length = - cdio_get_track_lba(p_cdio, CDIO_CDROM_LEADOUT_TRACK) - / CDIO_CD_FRAMES_PER_SEC; - - if (!cddb_disc_calc_discid(disc)) { - report(stderr, "%s: libcddb calc discid failed.\n", - program_name); - goto cddb_destroy; - } - - matches = cddb_query(p_conn, disc); - - if (-1 == matches) - printf("%s: %s\n", program_name, cddb_error_str(cddb_errno(p_conn))); - else { - printf("%s: Found %d matches in CDDB\n", program_name, matches); - for (i=1; i<=matches; i++) { - cddb_read(p_conn, disc); - cddb_disc_print(disc); - cddb_query_next(p_conn, disc); + if (-1 == i_cddb_matches) + printf("%s: %s\n", program_name, cddb_error_str(cddb_errno(p_conn))); + else { + printf("%s: Found %d matches in CDDB\n", program_name, i_cddb_matches); + for (i=1; i<=i_cddb_matches; i++) { + cddb_disc_print(p_cddb_disc); + cddb_query_next(p_conn, p_cddb_disc); + if (i != i_cddb_matches) cddb_read(p_conn, p_cddb_disc); + } } - } - cddb_disc_destroy(disc); - cddb_destroy: - cddb_destroy(p_conn); + cddb_disc_destroy(p_cddb_disc); + cddb_destroy(p_conn); + } } #endif diff --git a/src/cdda-player.c b/src/cdda-player.c index b997ca00..99ddc1b2 100644 --- a/src/cdda-player.c +++ b/src/cdda-player.c @@ -1,5 +1,5 @@ /* - $Id: cdda-player.c,v 1.4 2005/03/11 10:34:28 rocky Exp $ + $Id: cdda-player.c,v 1.5 2005/03/12 06:02:36 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -138,8 +138,6 @@ bool b_cdtext_genre; /* true if from CD-Text, false if from CDDB */ bool b_cdtext_category; /* true if from CD-Text, false if from CDDB */ #ifdef HAVE_CDDB -static bool init_cddb(CdIo_t *p_cdio, cddb_conn_t *p_conn, track_t i_tracks); - cddb_conn_t *p_conn = NULL; cddb_disc_t *p_cddb_disc = NULL; int i_cddb_matches = 0; @@ -331,6 +329,10 @@ read_subchannel(CdIo_t *p_cdio) cd_eject(); } +#define add_cddb_disc_info(format_str, field) \ + if (p_cddb_disc->field) \ + snprintf(field, sizeof(field)-1, format_str, p_cddb_disc->field); + static void read_toc(CdIo_t *p_cdio) { @@ -355,13 +357,19 @@ read_toc(CdIo_t *p_cdio) } else { b_cd = true; data = 0; - b_db = #ifdef HAVE_CDDB - init_cddb(p_cdio, p_conn, i_tracks) + b_db = init_cddb(p_cdio, &p_conn, &p_cddb_disc, xperror, i_first_track, + i_tracks, &i_cddb_matches); + if (b_db) { + add_cddb_disc_info("%s", artist); + add_cddb_disc_info("%s", title); + add_cddb_disc_info("%s", genre); + add_cddb_disc_info("%5d", year); + } + #else - false + b_db = false; #endif - ; for (i = i_first_track; i <= i_last_track+1; i++) { if ( !cdio_get_track_msf(p_cdio, i, &(toc[i])) ) { @@ -503,86 +511,6 @@ display_status() action(NULL); } -#define add_cddb_disc_info(format_str, field) \ - if (p_cddb_disc->field) \ - snprintf(field, sizeof(field)-1, format_str, p_cddb_disc->field); - -#ifdef HAVE_CDDB -static bool -init_cddb(CdIo_t *p_cdio, cddb_conn_t *p_conn, track_t i_tracks) -{ - track_t i; - - if (p_conn) cddb_destroy(p_conn); - p_conn = cddb_new(); - p_cddb_disc = NULL; - - if (!p_conn) { - xperror("unable to initialize libcddb"); - return false; - } - - if (NULL == cddb_opts.email) - cddb_set_email_address(p_conn, "me@home"); - else - cddb_set_email_address(p_conn, cddb_opts.email); - - if (NULL == cddb_opts.server) - cddb_set_server_name(p_conn, "freedb.freedb.org"); - else - cddb_set_server_name(p_conn, cddb_opts.server); - - if (cddb_opts.timeout >= 0) - cddb_set_timeout(p_conn, cddb_opts.timeout); - - cddb_set_server_port(p_conn, cddb_opts.port); - - if (cddb_opts.http) - cddb_http_enable(p_conn); - else - cddb_http_disable(p_conn); - - if (NULL != cddb_opts.cachedir) - cddb_cache_set_dir(p_conn, cddb_opts.cachedir); - - if (cddb_opts.disable_cache) - cddb_cache_disable(p_conn); - - p_cddb_disc = cddb_disc_new(); - if (!p_cddb_disc) { - xperror("unable to create CDDB disc structure"); - return false; - } - for(i = 0; i < i_tracks; i++) { - cddb_track_t *t = cddb_track_new(); - t->frame_offset = cdio_get_track_lba(p_cdio, i+i_first_track); - cddb_disc_add_track(p_cddb_disc, t); - } - - p_cddb_disc->length = - cdio_get_track_lba(p_cdio, CDIO_CDROM_LEADOUT_TRACK) - / CDIO_CD_FRAMES_PER_SEC; - - if (!cddb_disc_calc_discid(p_cddb_disc)) { - xperror("libcddb calc discid failed."); - return false; - } - - i_cddb_matches = cddb_query(p_conn, p_cddb_disc); - - if (-1 == i_cddb_matches) - xperror(cddb_error_str(cddb_errno(p_conn))); - - cddb_read(p_conn, p_cddb_disc); - - add_cddb_disc_info("%s", artist); - add_cddb_disc_info("%s", title); - add_cddb_disc_info("%s", genre); - add_cddb_disc_info("%5d", year); - return true; -} -#endif /*HAVE_CDDB*/ - #define add_cddb_track_info(format_str, field) \ if (t->field) \ snprintf(cd_info[i_track].field, sizeof(cd_info[i_track].field)-1, \ diff --git a/src/cddb.c b/src/cddb.c index 12dfcb94..638f1ccd 100644 --- a/src/cddb.c +++ b/src/cddb.c @@ -1,5 +1,5 @@ /* - $Id: cddb.c,v 1.2 2005/03/06 16:00:35 rocky Exp $ + $Id: cddb.c,v 1.3 2005/03/12 06:02:36 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -67,3 +67,75 @@ cddb_discid(CdIo_t *p_cdio, track_t i_tracks) return ((n % 0xff) << 24 | t << 8 | i_tracks); } + +#ifdef HAVE_CDDB +bool +init_cddb(CdIo_t *p_cdio, cddb_conn_t **pp_conn, cddb_disc_t **pp_cddb_disc, + error_fn_t errmsg, track_t i_first_track, track_t i_tracks, + int *i_cddb_matches) +{ + track_t i; + + *pp_conn = cddb_new(); + *pp_cddb_disc = NULL; + + if (!*pp_conn) { + errmsg("unable to initialize libcddb"); + return false; + } + + if (NULL == cddb_opts.email) + cddb_set_email_address(*pp_conn, "me@home"); + else + cddb_set_email_address(*pp_conn, cddb_opts.email); + + if (NULL == cddb_opts.server) + cddb_set_server_name(*pp_conn, "freedb.freedb.org"); + else + cddb_set_server_name(*pp_conn, cddb_opts.server); + + if (cddb_opts.timeout >= 0) + cddb_set_timeout(*pp_conn, cddb_opts.timeout); + + cddb_set_server_port(*pp_conn, cddb_opts.port); + + if (cddb_opts.http) + cddb_http_enable(*pp_conn); + else + cddb_http_disable(*pp_conn); + + if (NULL != cddb_opts.cachedir) + cddb_cache_set_dir(*pp_conn, cddb_opts.cachedir); + + if (cddb_opts.disable_cache) + cddb_cache_disable(*pp_conn); + + *pp_cddb_disc = cddb_disc_new(); + if (!*pp_cddb_disc) { + errmsg("unable to create CDDB disc structure"); + return false; + } + for(i = 0; i < i_tracks; i++) { + cddb_track_t *t = cddb_track_new(); + t->frame_offset = cdio_get_track_lba(p_cdio, i+i_first_track); + cddb_disc_add_track(*pp_cddb_disc, t); + } + + (*pp_cddb_disc)->length = + cdio_get_track_lba(p_cdio, CDIO_CDROM_LEADOUT_TRACK) + / CDIO_CD_FRAMES_PER_SEC; + + if (!cddb_disc_calc_discid(*pp_cddb_disc)) { + errmsg("libcddb calc discid failed."); + return false; + } + + *i_cddb_matches = cddb_query(*pp_conn, *pp_cddb_disc); + + if (-1 == *i_cddb_matches) + errmsg(cddb_error_str(cddb_errno(*pp_conn))); + + cddb_read(*pp_conn, *pp_cddb_disc); + return true; +} +#endif /*HAVE_CDDB*/ diff --git a/src/cddb.h b/src/cddb.h index 8ad32850..13a4ffd1 100644 --- a/src/cddb.h +++ b/src/cddb.h @@ -1,7 +1,7 @@ /* - $Id: cddb.h,v 1.2 2005/03/11 10:34:28 rocky Exp $ + $Id: cddb.h,v 1.3 2005/03/12 06:02:36 rocky Exp $ - Copyright (C) 2003, 2004, 2005 Rocky Bernstein + Copyright (C) 2005 Rocky Bernstein 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 @@ -39,3 +39,13 @@ cddb_opts_t cddb_opts; the number of tracks. */ unsigned long cddb_discid(CdIo_t *p_cdio, track_t i_tracks); + +#ifdef HAVE_CDDB +#include + +typedef void (*error_fn_t) (const char *msg); + +bool init_cddb(CdIo_t *p_cdio, cddb_conn_t **pp_conn, + cddb_disc_t **pp_cddb_disc, error_fn_t errmsg, + track_t i_first_track, track_t i_tracks, int *i_cddb_matches); +#endif