4 * Copyright (C) 2002 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * Implementation follow the freedb.howto1.06.txt specification
7 * from http://freedb.freedb.org
9 * discid computation by Jeremy D. Zawodny
10 * Copyright (c) 1998-2000 Jeremy D. Zawodny <Jeremy@Zawodny.com>
12 * This file is part of MPlayer.
14 * MPlayer is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * MPlayer is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
40 #if defined(__MINGW32__) || defined(__CYGWIN__)
47 #include <sys/ioctl.h>
49 #include <sys/types.h>
55 #if defined(__linux__)
56 #include <linux/cdrom.h>
57 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
59 #elif defined(__MINGW32__) || defined(__CYGWIN__)
60 #include <ddk/ntddcdrm.h>
61 #elif defined(__bsdi__)
63 #elif defined(__APPLE__) || defined(__DARWIN__)
64 #include <IOKit/storage/IOCDTypes.h>
65 #include <IOKit/storage/IOCDMediaBSDClient.h>
73 #include "libavutil/common.h"
75 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
76 #define DEFAULT_CACHE_DIR "/.cddb/"
79 char cddb_hello
[1024];
80 unsigned long disc_id
;
84 int freedb_proto_level
;
88 size_t xmcd_file_size
;
93 unsigned int min
, sec
, frame
;
96 static cd_toc_t cdtoc
[100];
97 static int cdtoc_last_track
;
99 static int read_toc(const char *dev
)
101 int first
= 0, last
= -1;
103 #if defined(__MINGW32__) || defined(__CYGWIN__)
109 sprintf(device
, "\\\\.\\%s", dev
);
110 drive
= CreateFile(device
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
111 OPEN_EXISTING
, 0, 0);
113 if (!DeviceIoControl(drive
, IOCTL_CDROM_READ_TOC
, NULL
, 0, &toc
,
114 sizeof(CDROM_TOC
), &r
, 0)) {
115 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
119 first
= toc
.FirstTrack
- 1; last
= toc
.LastTrack
;
120 for (i
= first
; i
<= last
; i
++) {
121 cdtoc
[i
].min
= toc
.TrackData
[i
].Address
[1];
122 cdtoc
[i
].sec
= toc
.TrackData
[i
].Address
[2];
123 cdtoc
[i
].frame
= toc
.TrackData
[i
].Address
[3];
129 drive
= open(dev
, O_RDONLY
| O_NONBLOCK
);
134 #if defined(__linux__) || defined(__bsdi__)
136 struct cdrom_tochdr tochdr
;
137 ioctl(drive
, CDROMREADTOCHDR
, &tochdr
);
138 first
= tochdr
.cdth_trk0
- 1; last
= tochdr
.cdth_trk1
;
140 for (i
= first
; i
<= last
; i
++) {
141 struct cdrom_tocentry tocentry
;
142 tocentry
.cdte_track
= (i
== last
) ? 0xAA : i
+ 1;
143 tocentry
.cdte_format
= CDROM_MSF
;
144 ioctl(drive
, CDROMREADTOCENTRY
, &tocentry
);
145 cdtoc
[i
].min
= tocentry
.cdte_addr
.msf
.minute
;
146 cdtoc
[i
].sec
= tocentry
.cdte_addr
.msf
.second
;
147 cdtoc
[i
].frame
= tocentry
.cdte_addr
.msf
.frame
;
149 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
151 struct ioc_toc_header tochdr
;
152 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
153 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
155 for (i
= first
; i
<= last
; i
++) {
156 struct ioc_read_toc_single_entry tocentry
;
157 tocentry
.track
= (i
== last
) ? 0xAA : i
+ 1;
158 tocentry
.address_format
= CD_MSF_FORMAT
;
159 ioctl(drive
, CDIOREADTOCENTRY
, &tocentry
);
160 cdtoc
[i
].min
= tocentry
.entry
.addr
.msf
.minute
;
161 cdtoc
[i
].sec
= tocentry
.entry
.addr
.msf
.second
;
162 cdtoc
[i
].frame
= tocentry
.entry
.addr
.msf
.frame
;
164 #elif defined(__NetBSD__) || defined(__OpenBSD__)
166 struct ioc_toc_header tochdr
;
167 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
168 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
170 for (i
= first
; i
<= last
; i
++) {
171 struct ioc_read_toc_entry tocentry
;
172 struct cd_toc_entry toc_buffer
;
173 tocentry
.starting_track
= (i
== last
) ? 0xAA : i
+ 1;
174 tocentry
.address_format
= CD_MSF_FORMAT
;
175 tocentry
.data
= &toc_buffer
;
176 tocentry
.data_len
= sizeof(toc_buffer
);
177 ioctl(drive
, CDIOREADTOCENTRYS
, &tocentry
);
178 cdtoc
[i
].min
= toc_buffer
.addr
.msf
.minute
;
179 cdtoc
[i
].sec
= toc_buffer
.addr
.msf
.second
;
180 cdtoc
[i
].frame
= toc_buffer
.addr
.msf
.frame
;
182 #elif defined(__APPLE__) || defined(__DARWIN__)
184 dk_cd_read_toc_t tochdr
;
186 uint8_t buf2
[100 * sizeof(CDTOCDescriptor
) + sizeof(CDTOC
)];
187 memset(&tochdr
, 0, sizeof(tochdr
));
188 tochdr
.bufferLength
= sizeof(buf
);
189 tochdr
.buffer
= &buf
;
190 if (!ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
191 && tochdr
.bufferLength
== sizeof(buf
)) {
196 memset(&tochdr
, 0, sizeof(tochdr
));
197 tochdr
.bufferLength
= sizeof(buf2
);
198 tochdr
.buffer
= &buf2
;
199 tochdr
.format
= kCDTOCFormatTOC
;
200 if (ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
201 || tochdr
.bufferLength
< sizeof(CDTOC
))
205 CDTOC
*cdToc
= (CDTOC
*)buf2
;
206 CDTrackInfo lastTrack
;
207 dk_cd_read_track_info_t trackInfoParams
;
208 for (i
= first
; i
< last
; ++i
) {
209 CDMSF msf
= CDConvertTrackNumberToMSF(i
+ 1, cdToc
);
210 cdtoc
[i
].min
= msf
.minute
;
211 cdtoc
[i
].sec
= msf
.second
;
212 cdtoc
[i
].frame
= msf
.frame
;
214 memset(&trackInfoParams
, 0, sizeof(trackInfoParams
));
215 trackInfoParams
.addressType
= kCDTrackInfoAddressTypeTrackNumber
;
216 trackInfoParams
.bufferLength
= sizeof(lastTrack
);
217 trackInfoParams
.address
= last
;
218 trackInfoParams
.buffer
= &lastTrack
;
219 if (!ioctl(drive
, DKIOCCDREADTRACKINFO
, &trackInfoParams
)) {
220 CDMSF msf
= CDConvertLBAToMSF(be2me_32(lastTrack
.trackStartAddress
)
221 + be2me_32(lastTrack
.trackSize
));
222 cdtoc
[last
].min
= msf
.minute
;
223 cdtoc
[last
].sec
= msf
.second
;
224 cdtoc
[last
].frame
= msf
.frame
;
231 for (i
= first
; i
<= last
; i
++)
232 cdtoc
[i
].frame
+= (cdtoc
[i
].min
* 60 + cdtoc
[i
].sec
) * 75;
237 \brief Reads TOC from CD in the given device and prints the number of tracks
238 and the length of each track in minute:second:frame format.
239 \param *dev the device to analyse
240 \return if the command line -identify is given, returns the last track of
241 the TOC or -1 if the TOC can't be read,
242 otherwise just returns 0 and let cddb_resolve the TOC
244 int cdd_identify(const char *dev
)
246 cdtoc_last_track
= 0;
247 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
)) {
248 int i
, min
, sec
, frame
;
249 cdtoc_last_track
= read_toc(dev
);
250 if (cdtoc_last_track
< 0) {
251 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
254 mp_msg(MSGT_GLOBAL
, MSGL_INFO
, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track
);
255 for (i
= 1; i
<= cdtoc_last_track
; i
++) {
256 frame
= cdtoc
[i
].frame
- cdtoc
[i
-1].frame
;
261 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
,
262 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i
, min
, sec
, frame
);
265 return cdtoc_last_track
;
268 static unsigned int cddb_sum(int n
)
280 static unsigned long cddb_discid(int tot_trks
)
282 unsigned int i
, t
= 0, n
= 0;
285 while (i
< (unsigned int)tot_trks
) {
286 n
= n
+ cddb_sum((cdtoc
[i
].min
* 60) + cdtoc
[i
].sec
);
289 t
= ((cdtoc
[tot_trks
].min
* 60) + cdtoc
[tot_trks
].sec
) -
290 ((cdtoc
[0].min
* 60) + cdtoc
[0].sec
);
291 return (n
% 0xff) << 24 | t
<< 8 | tot_trks
;
296 static int cddb_http_request(char *command
,
297 int (*reply_parser
)(HTTP_header_t
*, cddb_data_t
*),
298 cddb_data_t
*cddb_data
)
303 HTTP_header_t
*http_hdr
;
305 if (reply_parser
== NULL
|| command
== NULL
|| cddb_data
== NULL
)
308 sprintf(request
, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
309 cddb_data
->freedb_server
, command
, cddb_data
->cddb_hello
,
310 cddb_data
->freedb_proto_level
);
311 mp_msg(MSGT_OPEN
, MSGL_INFO
,"Request[%s]\n", request
);
313 url
= url_new(request
);
315 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "not a valid URL\n");
319 fd
= http_send_request(url
,0);
321 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to send the HTTP request.\n");
325 http_hdr
= http_read_response(fd
);
326 if (http_hdr
== NULL
) {
327 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to read the HTTP response.\n");
331 http_debug_hdr(http_hdr
);
332 mp_msg(MSGT_OPEN
, MSGL_INFO
,"body=[%s]\n", http_hdr
->body
);
334 switch (http_hdr
->status_code
) {
336 ret
= reply_parser(http_hdr
, cddb_data
);
339 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Not Found.\n");
342 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unknown error code\n");
351 static int cddb_read_cache(cddb_data_t
*cddb_data
)
358 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
361 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
363 file_fd
= open(file_name
, O_RDONLY
| O_BINARY
);
365 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No cache found.\n");
369 ret
= fstat(file_fd
, &stats
);
374 file_size
= stats
.st_size
< UINT_MAX
? stats
.st_size
: UINT_MAX
- 1;
377 cddb_data
->xmcd_file
= malloc(file_size
+ 1);
378 if (cddb_data
->xmcd_file
== NULL
) {
379 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
383 cddb_data
->xmcd_file_size
= read(file_fd
, cddb_data
->xmcd_file
, file_size
);
384 if (cddb_data
->xmcd_file_size
!= file_size
) {
385 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all the xmcd file has been read.\n");
389 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = 0;
396 static int cddb_write_cache(cddb_data_t
*cddb_data
)
398 // We have the file, save it for cache.
403 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
406 // Check if the CDDB cache dir exist
407 if (!mp_path_exists(cddb_data
->cache_dir
)) {
408 // Directory not present, create it.
409 ret
= mkdir(cddb_data
->cache_dir
, 0755);
411 if (ret
< 0 && errno
!= EEXIST
) {
416 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to create directory %s.\n",
417 cddb_data
->cache_dir
);
422 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
424 file_fd
= creat(file_name
, S_IRUSR
| S_IWUSR
);
430 wrote
= write(file_fd
, cddb_data
->xmcd_file
, cddb_data
->xmcd_file_size
);
436 if ((unsigned int) wrote
!= cddb_data
->xmcd_file_size
) {
437 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all of the xmcd file has been written.\n");
447 static int cddb_read_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
449 unsigned long disc_id
;
451 char *ptr
= NULL
, *ptr2
= NULL
;
454 if (http_hdr
== NULL
|| cddb_data
== NULL
)
457 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
459 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
465 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx", &status
,
468 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
471 // Check if it's a xmcd database file
472 ptr
= strstr(http_hdr
->body
, "# xmcd");
474 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
475 "Invalid xmcd database file returned.\n");
479 // Ok found the beginning of the file
481 ptr2
= strstr(ptr
, "\n.\r\n");
483 ptr2
= strstr(ptr
, "\n.\n");
487 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, "Unable to find '.'\n");
488 ptr2
= ptr
+ strlen(ptr
); //return -1;
492 if (http_hdr
->body_size
< (unsigned int)(ptr2
- ptr
)) {
493 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unexpected FIXME\n");
496 cddb_data
->xmcd_file
= ptr
;
497 cddb_data
->xmcd_file_size
= ptr2
- ptr
;
498 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = '\0';
499 return cddb_write_cache(cddb_data
);
501 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
506 static int cddb_request_titles(cddb_data_t
*cddb_data
)
509 sprintf(command
, "cddb+read+%s+%08lx",
510 cddb_data
->category
, cddb_data
->disc_id
);
511 return cddb_http_request(command
, cddb_read_parse
, cddb_data
);
514 static int cddb_parse_matches_list(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
516 char album_title
[100];
520 ptr
= strstr(http_hdr
->body
, "\n");
522 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Unable to find end of line.\n");
526 // We have a list of exact/inexact matches, so which one do we use?
527 // So let's take the first one.
528 ret
= sscanf(ptr
, "%99s %08lx %99s", cddb_data
->category
,
529 &(cddb_data
->disc_id
), album_title
);
531 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
534 ptr
= strstr(http_hdr
->body
, album_title
);
538 ptr2
= strstr(ptr
, "\n");
540 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
544 len
= FFMIN(sizeof(album_title
) - 1, len
);
545 strncpy(album_title
, ptr
, len
);
546 album_title
[len
]='\0';
548 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
552 static int cddb_query_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
554 char album_title
[100];
558 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
560 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
567 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx %99s", &status
,
568 cddb_data
->category
, &(cddb_data
->disc_id
), album_title
);
570 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
573 ptr
= strstr(http_hdr
->body
, album_title
);
577 ptr2
= strstr(ptr
, "\n");
579 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
583 len
= FFMIN(sizeof(album_title
) - 1, len
);
584 strncpy(album_title
, ptr
, len
);
585 album_title
[len
]='\0';
587 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
588 return cddb_request_titles(cddb_data
);
591 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Album not found.\n");
594 // Found exact matches, list follows
595 cddb_parse_matches_list(http_hdr
, cddb_data
);
596 return cddb_request_titles(cddb_data
);
598 body=[210 Found exact matches, list follows (until terminating `.')
599 misc c711930d Santana / Supernatural
600 rock c711930d Santana / Supernatural
601 blues c711930d Santana / Supernatural
605 // Found inexact matches, list follows
606 cddb_parse_matches_list(http_hdr
, cddb_data
);
607 return cddb_request_titles(cddb_data
);
609 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
,
610 "Server returns: Command syntax error\n");
613 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
618 static int cddb_proto_level_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
624 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
626 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
632 ptr
= strstr(http_hdr
->body
, "max proto:");
634 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
637 ret
= sscanf(ptr
, "max proto: %d", &max
);
639 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
642 cddb_data
->freedb_proto_level
= max
;
645 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
650 static int cddb_get_proto_level(cddb_data_t
*cddb_data
)
652 return cddb_http_request("stat", cddb_proto_level_parse
, cddb_data
);
655 static void cddb_create_hello(cddb_data_t
*cddb_data
)
660 if (cddb_data
->anonymous
) { // Default is anonymous
661 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
662 * We don't send current user/host name in hello to prevent spam.
663 * Software that sends this is considered spyware
664 * that most people don't like.
666 user_name
= "anonymous";
667 strcpy(host_name
, "localhost");
669 if (gethostname(host_name
, 50) < 0) {
670 strcpy(host_name
, "localhost");
672 user_name
= getenv("LOGNAME");
674 sprintf(cddb_data
->cddb_hello
, "&hello=%s+%s+%s",
675 user_name
, host_name
, mplayer_version
);
678 static int cddb_retrieve(cddb_data_t
*cddb_data
)
680 char offsets
[1024], command
[1024];
682 unsigned int i
, time_len
;
686 for (i
= 0; i
< cddb_data
->tracks
; i
++) {
687 ptr
+= sprintf(ptr
, "%d+", cdtoc
[i
].frame
);
688 if (ptr
-offsets
> sizeof offsets
- 40) break;
691 time_len
= (cdtoc
[cddb_data
->tracks
].frame
)/75;
693 cddb_data
->freedb_server
= DEFAULT_FREEDB_SERVER
;
694 cddb_data
->freedb_proto_level
= 1;
695 cddb_data
->xmcd_file
= NULL
;
697 cddb_create_hello(cddb_data
);
698 if (cddb_get_proto_level(cddb_data
) < 0) {
699 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to get the protocol level.\n");
703 sprintf(command
, "cddb+query+%08lx+%d+%s%d", cddb_data
->disc_id
,
704 cddb_data
->tracks
, offsets
, time_len
);
705 ret
= cddb_http_request(command
, cddb_query_parse
, cddb_data
);
709 free(cddb_data
->cache_dir
);
713 int cddb_resolve(const char *dev
, char **xmcd_file
)
715 char cddb_cache_dir
[] = DEFAULT_CACHE_DIR
;
716 char *home_dir
= NULL
;
717 cddb_data_t cddb_data
;
719 if (cdtoc_last_track
<= 0) {
720 cdtoc_last_track
= read_toc(dev
);
721 if (cdtoc_last_track
< 0) {
722 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
726 cddb_data
.tracks
= cdtoc_last_track
;
727 cddb_data
.disc_id
= cddb_discid(cddb_data
.tracks
);
728 cddb_data
.anonymous
= 1; // Don't send user info by default
730 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDB_DISCID=%08lx\n",
733 // Check if there is a CD in the drive
734 // FIXME: That's not really a good way to check
735 if (cddb_data
.disc_id
== 0) {
736 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No CD in the drive.\n");
740 home_dir
= getenv("HOME");
742 if (home_dir
== NULL
)
743 home_dir
= getenv("USERPROFILE");
744 if (home_dir
== NULL
)
745 home_dir
= getenv("HOMEPATH");
746 // Last resort, store the cddb cache in the mplayer directory
747 if (home_dir
== NULL
)
748 home_dir
= (char *)get_path("");
750 if (home_dir
== NULL
) {
751 cddb_data
.cache_dir
= NULL
;
753 cddb_data
.cache_dir
= malloc(strlen(home_dir
)
754 + strlen(cddb_cache_dir
) + 1);
755 if (cddb_data
.cache_dir
== NULL
) {
756 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
759 sprintf(cddb_data
.cache_dir
, "%s%s", home_dir
, cddb_cache_dir
);
762 // Check for a cached file
763 if (cddb_read_cache(&cddb_data
) < 0) {
765 if (cddb_retrieve(&cddb_data
) < 0) {
770 if (cddb_data
.xmcd_file
!= NULL
) {
771 // printf("%s\n", cddb_data.xmcd_file);
772 *xmcd_file
= cddb_data
.xmcd_file
;
782 static char *xmcd_parse_dtitle(cd_info_t
*cd_info
, char *line
)
785 ptr
= strstr(line
, "DTITLE=");
788 album
= strstr(ptr
, "/");
791 cd_info
->album
= malloc(strlen(album
+ 2) + 1);
792 if (cd_info
->album
== NULL
) {
795 strcpy(cd_info
->album
, album
+ 2);
798 cd_info
->artist
= malloc(strlen(ptr
) + 1);
799 if (cd_info
->artist
== NULL
) {
802 strcpy(cd_info
->artist
, ptr
);
807 static char *xmcd_parse_dgenre(cd_info_t
*cd_info
, char *line
)
810 ptr
= strstr(line
, "DGENRE=");
813 cd_info
->genre
= malloc(strlen(ptr
)+1);
814 if (cd_info
->genre
== NULL
) {
817 strcpy(cd_info
->genre
, ptr
);
822 static char *xmcd_parse_ttitle(cd_info_t
*cd_info
, char *line
)
824 unsigned int track_nb
;
825 unsigned long sec
, off
;
827 ptr
= strstr(line
, "TTITLE");
830 // Here we point to the track number
831 track_nb
= atoi(ptr
);
832 ptr
= strstr(ptr
, "=");
837 sec
= cdtoc
[track_nb
].frame
;
838 off
= cdtoc
[track_nb
+ 1].frame
- sec
+ 1;
840 cd_info_add_track(cd_info
, ptr
, track_nb
+ 1,
841 (unsigned int) (off
/ (60 * 75)),
842 (unsigned int) ((off
/ 75) % 60),
843 (unsigned int) (off
% 75),
849 cd_info_t
*cddb_parse_xmcd(char *xmcd_file
)
851 cd_info_t
*cd_info
= NULL
;
854 unsigned int audiolen
;
855 if (xmcd_file
== NULL
)
858 cd_info
= cd_info_new();
859 if (cd_info
== NULL
) {
863 length
= strlen(xmcd_file
);
865 while (ptr
!= NULL
&& pos
< length
) {
868 while(ptr2
[0] != '\0' && ptr2
[0] != '\r' && ptr2
[0] != '\n')
870 if (ptr2
[0] == '\0') {
876 // Search for the album title
877 if (xmcd_parse_dtitle(cd_info
, ptr
))
879 // Search for the genre
880 else if (xmcd_parse_dgenre(cd_info
, ptr
))
882 // Search for a track title
883 else if (xmcd_parse_ttitle(cd_info
, ptr
))
884 audiolen
++; // <-- audiolen++ to shut up gcc warning
888 pos
= (ptr2
+ 1) - ptr
;
892 audiolen
= cdtoc
[cd_info
->nb_tracks
].frame
-cdtoc
[0].frame
;
893 cd_info
->min
= (unsigned int) (audiolen
/ (60 * 75));
894 cd_info
->sec
= (unsigned int) ((audiolen
/ 75) % 60);
895 cd_info
->msec
= (unsigned int) (audiolen
% 75);