annotate libmpdemux/cddb.c @ 8764:081116fcc50a

voxvare audio fix patch by Charles Philip Chan <cpchan@sympatico.ca>
author arpi
date Sat, 04 Jan 2003 11:18:42 +0000
parents f1fc23f6203b
children c61e70388f6b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
1 /*
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
2 * CDDB HTTP protocol
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
4 * (C) 2002, MPlayer team.
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
5 *
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
6 * Implementation follow the freedb.howto1.06.txt specification
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
7 * from http://freedb.freedb.org
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
8 *
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
9 * discid computation by Jeremy D. Zawodny
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
10 * Copyright (c) 1998-2000 Jeremy D. Zawodny <Jeremy@Zawodny.com>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
11 * Code release under GPL
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
12 *
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
13 */
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
14
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
15 #include "config.h"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
16
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
17 #if defined(HAVE_CDDA) && defined(STREAMING)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
18
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
19 #include <stdio.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
20 #include <stdlib.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
21 #include <fcntl.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
22 #include <stdarg.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
23 #include <errno.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
24 #include <netdb.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
25 #include <unistd.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
26 #include <string.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
27 #include <sys/ioctl.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
28 #include <sys/types.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
29 #include <sys/stat.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
30
8609
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
31 #if defined(__FreeBSD__) || defined(__bsdi__)
7269
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
32 #define SYS_BSD 1
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
33 #endif
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
34
8609
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
35 #if defined(__NetBSD__)
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
36 #define SYS_NBSD 1
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
37 #endif
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
38
7269
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
39 #if defined(__OpenBSD__)
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
40 #define SYS_OBSD 1
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
41 #endif
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
42
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
43 #if defined(__linux__)
7269
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
44 #include <linux/cdrom.h>
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
45 #elif defined(SYS_BSD)
7269
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
46 #include <sys/cdio.h>
8609
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
47 #elif defined(SYS_NBSD)
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
48 #include <sys/cdio.h>
7269
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
49 #elif defined(SYS_OBSD)
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
50 #include <util.h>
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
51 #include <sys/cdio.h>
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
52 #endif
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
53
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
54 #include "cdd.h"
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
55 #include "../version.h"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
56 #include "stream.h"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
57 #include "network.h"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
58
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
59 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
60 #define DEFAULT_CACHE_DIR "/.cddb/"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
61
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
62 stream_t* open_cdda(char *dev, char *track);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
63
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
64 static cd_toc_t cdtoc[100];
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
65
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
66 #if defined(__linux__)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
67 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
68 read_toc(void) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
69 int drive = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
70 struct cdrom_tochdr tochdr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
71 struct cdrom_tocentry tocentry;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
72 int i;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
73
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
74 ioctl(drive, CDROMREADTOCHDR, &tochdr);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
75 for (i = tochdr.cdth_trk0; i <= tochdr.cdth_trk1; i++) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
76 tocentry.cdte_track = i;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
77 tocentry.cdte_format = CDROM_MSF;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
78 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
79 cdtoc[i-1].min = tocentry.cdte_addr.msf.minute;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
80 cdtoc[i-1].sec = tocentry.cdte_addr.msf.second;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
81 cdtoc[i-1].frame = tocentry.cdte_addr.msf.frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
82 cdtoc[i-1].frame += cdtoc[i-1].min*60*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
83 cdtoc[i-1].frame += cdtoc[i-1].sec*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
84 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
85 tocentry.cdte_track = 0xAA;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
86 tocentry.cdte_format = CDROM_MSF;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
87 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
88 cdtoc[tochdr.cdth_trk1].min = tocentry.cdte_addr.msf.minute;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
89 cdtoc[tochdr.cdth_trk1].sec = tocentry.cdte_addr.msf.second;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
90 cdtoc[tochdr.cdth_trk1].frame = tocentry.cdte_addr.msf.frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
91 cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].min*60*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
92 cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].sec*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
93 close(drive);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
94 return tochdr.cdth_trk1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
95 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
96
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
97 #elif defined(SYS_BSD)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
98 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
99 read_toc(void) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
100 int drive = open("/dev/acd0c", O_RDONLY | O_NONBLOCK);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
101 struct ioc_toc_header tochdr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
102 struct ioc_read_toc_single_entry tocentry;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
103 int i;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
104
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
105 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
106 for (i = tochdr.starting_track; i <= tochdr.ending_track; i++) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
107 tocentry.track = i;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
108 tocentry.address_format = CD_MSF_FORMAT;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
109 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
110 cdtoc[i-1].min = tocentry.entry.addr.msf.minute;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
111 cdtoc[i-1].sec = tocentry.entry.addr.msf.second;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
112 cdtoc[i-1].frame = tocentry.entry.addr.msf.frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
113 cdtoc[i-1].frame += cdtoc[i-1].min*60*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
114 cdtoc[i-1].frame += cdtoc[i-1].sec*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
115 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
116 tocentry.track = 0xAA;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
117 tocentry.address_format = CD_MSF_FORMAT;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
118 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
119 cdtoc[tochdr.ending_track].min = tocentry.entry.addr.msf.minute;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
120 cdtoc[tochdr.ending_track].sec = tocentry.entry.addr.msf.second;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
121 cdtoc[tochdr.ending_track].frame = tocentry.entry.addr.msf.frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
122 cdtoc[tochdr.ending_track].frame += cdtoc[tochdr.ending_track].min*60*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
123 cdtoc[tochdr.ending_track].frame += cdtoc[tochdr.ending_track].sec*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
124 close(drive);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
125 return tochdr.ending_track;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
126 }
7269
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
127
8609
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
128 #elif defined(SYS_NBSD)
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
129 int
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
130 read_toc(void) {
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
131 int drive;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
132 struct ioc_toc_header tochdr;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
133 struct ioc_read_toc_entry tocentry;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
134 int i;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
135 struct cd_toc_entry toc_buffer;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
136
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
137 drive = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
138 if (!drive)
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
139 return -1;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
140
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
141 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
142 for (i = tochdr.starting_track; i <= tochdr.ending_track; i++) {
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
143 tocentry.starting_track = i;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
144 tocentry.address_format = CD_MSF_FORMAT;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
145 tocentry.data = &toc_buffer;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
146 tocentry.data_len = sizeof(toc_buffer);
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
147 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
148 cdtoc[i-1].min = toc_buffer.addr.msf.minute;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
149 cdtoc[i-1].sec = toc_buffer.addr.msf.second;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
150 cdtoc[i-1].frame = toc_buffer.addr.msf.frame;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
151 cdtoc[i-1].frame += cdtoc[i-1].min*60*75;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
152 cdtoc[i-1].frame += cdtoc[i-1].sec*75;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
153 }
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
154 tocentry.starting_track = 0xAA;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
155 tocentry.address_format = CD_MSF_FORMAT;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
156 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
157 cdtoc[tochdr.ending_track].min = toc_buffer.addr.msf.minute;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
158 cdtoc[tochdr.ending_track].sec = toc_buffer.addr.msf.second;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
159 cdtoc[tochdr.ending_track].frame = toc_buffer.addr.msf.frame;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
160 cdtoc[tochdr.ending_track].frame += cdtoc[tochdr.ending_track].min*60*75;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
161 cdtoc[tochdr.ending_track].frame += cdtoc[tochdr.ending_track].sec*75;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
162 close(drive);
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
163 return tochdr.ending_track;
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
164 }
1d98280b9ad1 The following patch allows the MPlayer "cdparanoia" support to work on
arpi
parents: 8557
diff changeset
165
7269
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
166 #elif defined(SYS_OBSD)
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
167 int
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
168 read_toc(void) {
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
169 int drive;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
170 struct ioc_toc_header tochdr;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
171 struct ioc_read_toc_entry tocentry;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
172 int i;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
173 char *drivename;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
174 char *realdev;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
175 struct cd_toc_entry toc_buffer;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
176
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
177 drivename = getenv("CDROM");
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
178 if (!drivename)
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
179 drivename = "cd0";
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
180 drive = opendev(drivename, O_RDONLY, OPENDEV_PART, &realdev);
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
181 if (!drive)
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
182 return -1;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
183
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
184 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
185 for (i = tochdr.starting_track; i <= tochdr.ending_track; i++) {
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
186 tocentry.starting_track = i;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
187 tocentry.address_format = CD_MSF_FORMAT;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
188 tocentry.data = &toc_buffer;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
189 tocentry.data_len = sizeof(toc_buffer);
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
190 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
191 cdtoc[i-1].min = toc_buffer.addr.msf.minute;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
192 cdtoc[i-1].sec = toc_buffer.addr.msf.second;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
193 cdtoc[i-1].frame = toc_buffer.addr.msf.frame;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
194 cdtoc[i-1].frame += cdtoc[i-1].min*60*75;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
195 cdtoc[i-1].frame += cdtoc[i-1].sec*75;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
196 }
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
197 tocentry.starting_track = 0xAA;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
198 tocentry.address_format = CD_MSF_FORMAT;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
199 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
200 cdtoc[tochdr.ending_track].min = toc_buffer.addr.msf.minute;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
201 cdtoc[tochdr.ending_track].sec = toc_buffer.addr.msf.second;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
202 cdtoc[tochdr.ending_track].frame = toc_buffer.addr.msf.frame;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
203 cdtoc[tochdr.ending_track].frame += cdtoc[tochdr.ending_track].min*60*75;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
204 cdtoc[tochdr.ending_track].frame += cdtoc[tochdr.ending_track].sec*75;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
205 close(drive);
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
206 return tochdr.ending_track;
a5f1baaf7714 Applied patch for OpenBDS from Marc Espie (submitted by Bj«Órn Sandell <biorn@dce.chalmers.se>)
bertrand
parents: 6697
diff changeset
207 }
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
208 #endif
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
209
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
210 unsigned int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
211 cddb_sum(int n) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
212 unsigned int ret;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
213
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
214 ret = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
215 while (n > 0) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
216 ret += (n % 10);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
217 n /= 10;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
218 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
219 return ret;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
220 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
221
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
222 unsigned long
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
223 cddb_discid(int tot_trks) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
224 unsigned int i, t = 0, n = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
225
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
226 i = 0;
7746
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
227 while (i < (unsigned int)tot_trks) {
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
228 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
229 i++;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
230 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
231 t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
232 ((cdtoc[0].min * 60) + cdtoc[0].sec);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
233 return ((n % 0xff) << 24 | t << 8 | tot_trks);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
234 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
235
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
236
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
237
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
238 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
239 cddb_http_request(char *command, int (*reply_parser)(HTTP_header_t*,cddb_data_t*), cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
240 char request[4096];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
241 int fd, ret = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
242 URL_t *url;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
243 HTTP_header_t *http_hdr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
244
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
245 if( reply_parser==NULL || command==NULL || cddb_data==NULL ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
246
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
247 sprintf( request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d", cddb_data->freedb_server, command, cddb_data->cddb_hello, cddb_data->freedb_proto_level );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
248 printf("Request[%s]\n", request );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
249
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
250 url = url_new(request);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
251 if( url==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
252 printf("Not a valid URL\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
253 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
254 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
255
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
256 fd = http_send_request(url);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
257 if( fd<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
258 printf("failed to send the http request\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
259 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
260 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
261
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
262 http_hdr = http_read_response( fd );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
263 if( http_hdr==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
264 printf("Failed to read the http response\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
265 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
266 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
267
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
268 http_debug_hdr(http_hdr);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
269 printf("body=[%s]\n", http_hdr->body );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
270
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
271 switch(http_hdr->status_code) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
272 case 200:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
273 ret = reply_parser(http_hdr, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
274 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
275 case 400:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
276 printf("Not Found\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
277 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
278 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
279 printf("Unknown Error code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
280 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
281
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
282 http_free( http_hdr );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
283 url_free( url );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
284
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
285 return ret;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
286 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
287
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
288 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
289 cddb_read_cache(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
290 char file_name[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
291 struct stat stats;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
292 int file_fd, ret;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
293 size_t file_size;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
294
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
295 if( cddb_data==NULL || cddb_data->cache_dir==NULL ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
296
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
297 sprintf( file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
298
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
299 file_fd = open(file_name, O_RDONLY);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
300 if( file_fd<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
301 printf("No cache found\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
302 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
303 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
304
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
305 ret = fstat( file_fd, &stats );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
306 if( ret<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
307 perror("fstat");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
308 file_size = 4096;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
309 } else {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
310 file_size = stats.st_size;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
311 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
312
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
313 cddb_data->xmcd_file = (char*)malloc(file_size);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
314 if( cddb_data->xmcd_file==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
315 printf("Memory allocation failed\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
316 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
317 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
318 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
319 cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
320 if( cddb_data->xmcd_file_size!=file_size ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
321 printf("Not all the xmcd file has been read\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
322 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
323 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
324 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
325
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
326 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
327
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
328 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
329 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
330
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
331 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
332 cddb_write_cache(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
333 // We have the file, save it for cache.
7721
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
334 struct stat file_stat;
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
335 char file_name[100];
7721
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
336 int file_fd, ret;
7746
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
337 int wrote=0;
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
338
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
339 if( cddb_data==NULL || cddb_data->cache_dir==NULL ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
340
7721
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
341 // Check if the CDDB cache dir exist
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
342 ret = stat( cddb_data->cache_dir, &file_stat );
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
343 if( ret<0 ) {
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
344 // Directory not present, create it.
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
345 ret = mkdir( cddb_data->cache_dir, 0755 );
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
346 if( ret<0 ) {
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
347 perror("mkdir");
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
348 printf("Failed to create directory %s\n", cddb_data->cache_dir );
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
349 return -1;
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
350 }
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
351 }
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
352
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
353 sprintf( file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id );
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
354
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
355 file_fd = creat(file_name, S_IREAD|S_IWRITE);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
356 if( file_fd<0 ) {
7721
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
357 perror("create");
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
358 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
359 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
360
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
361 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
362 if( wrote<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
363 perror("write");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
364 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
365 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
366 }
7746
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
367 if( (unsigned int)wrote!=cddb_data->xmcd_file_size ) {
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
368 printf("Not all the xmcd file has been written\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
369 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
370 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
371 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
372
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
373 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
374
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
375 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
376 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
377
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
378 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
379 cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
380 unsigned long disc_id;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
381 char category[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
382 char *ptr=NULL, *ptr2=NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
383 int ret, status;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
384
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
385 if( http_hdr==NULL || cddb_data==NULL ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
386
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
387 ret = sscanf( http_hdr->body, "%d ", &status);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
388 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
389 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
390 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
391 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
392
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
393 switch(status) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
394 case 210:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
395 ret = sscanf( http_hdr->body, "%d %s %08lx", &status, category, &disc_id);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
396 if( ret!=3 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
397 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
398 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
399 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
400 // Check if it's a xmcd database file
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
401 ptr = strstr(http_hdr->body, "# xmcd");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
402 if( ptr==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
403 printf("Invalid xmcd database file returned\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
404 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
405 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
406 // Ok found the beginning of the file
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
407 // look for the end
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
408 ptr2 = strstr(ptr, "\r\n.\r\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
409 if( ptr2==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
410 ptr2 = strstr(ptr, "\n.\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
411 if( ptr2==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
412 printf("Unable to find '.'\n");
8557
97cf659a4c38 - fixed uninitialized offsets[]
arpi
parents: 7953
diff changeset
413 ptr2=ptr+strlen(ptr); //return -1;
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
414 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
415 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
416 // Ok found the end
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
417 // do a sanity check
7953
60624e692b95 GCC warning fixes
bertrand
parents: 7746
diff changeset
418 if( http_hdr->body_size<(unsigned int)(ptr2-ptr) ) {
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
419 printf("Unexpected fix me\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
420 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
421 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
422 cddb_data->xmcd_file = ptr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
423 cddb_data->xmcd_file_size = ptr2-ptr+2;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
424 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
425 // Avoid the http_free function to free the xmcd file...save a mempcy...
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
426 http_hdr->body = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
427 http_hdr->body_size = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
428 return cddb_write_cache(cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
429 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
430 printf("Unhandled code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
431 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
432 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
433 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
434
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
435 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
436 cddb_request_titles(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
437 char command[1024];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
438 sprintf( command, "cddb+read+%s+%08lx", cddb_data->category, cddb_data->disc_id);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
439 return cddb_http_request(command, cddb_read_parse, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
440 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
441
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
442 int
8746
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
443 cddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
444 char album_title[100];
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
445 char *ptr = NULL;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
446 int ret;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
447
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
448 ptr = strstr(http_hdr->body, "\n");
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
449 if( ptr==NULL ) {
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
450 printf("Unable to find end of line\n");
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
451 return -1;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
452 }
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
453 ptr++;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
454 // We have a list of exact/inexact matches, so which one do we use?
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
455 // So let's take the first one.
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
456 ret = sscanf(ptr, "%s %08lx %s", cddb_data->category, &(cddb_data->disc_id), album_title);
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
457 if( ret!=3 ) {
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
458 printf("Parse error\n");
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
459 return -1;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
460 }
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
461 ptr = strstr(http_hdr->body, album_title);
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
462 if( ptr!=NULL ) {
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
463 char *ptr2;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
464 int len;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
465 ptr2 = strstr(ptr, "\n");
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
466 if( ptr2==NULL ) {
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
467 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
468 } else {
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
469 len = ptr2-ptr+1;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
470 }
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
471 strncpy(album_title, ptr, len);
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
472 album_title[len-2]='\0';
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
473 }
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
474 printf("Parse OK, found: %s\n", album_title);
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
475 return 0;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
476 }
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
477
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
478 int
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
479 cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
480 char album_title[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
481 char *ptr = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
482 int ret, status;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
483
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
484 ret = sscanf( http_hdr->body, "%d ", &status);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
485 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
486 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
487 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
488 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
489
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
490 switch(status) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
491 case 200:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
492 // Found exact match
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
493 ret = sscanf(http_hdr->body, "%d %s %08lx %s", &status, cddb_data->category, &(cddb_data->disc_id), album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
494 if( ret!=4 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
495 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
496 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
497 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
498 ptr = strstr(http_hdr->body, album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
499 if( ptr!=NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
500 char *ptr2;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
501 int len;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
502 ptr2 = strstr(ptr, "\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
503 if( ptr2==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
504 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
505 } else {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
506 len = ptr2-ptr+1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
507 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
508 strncpy(album_title, ptr, len);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
509 album_title[len-2]='\0';
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
510 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
511 printf("Parse OK, found: %s\n", album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
512 return cddb_request_titles(cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
513 case 202:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
514 // No match found
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
515 printf("Album not found\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
516 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
517 case 210:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
518 // Found exact matches, list follows
8746
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
519 cddb_parse_matches_list(http_hdr, cddb_data);
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
520 return cddb_request_titles(cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
521 /*
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
522 body=[210 Found exact matches, list follows (until terminating `.')
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
523 misc c711930d Santana / Supernatural
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
524 rock c711930d Santana / Supernatural
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
525 blues c711930d Santana / Supernatural
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
526 .]
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
527 */
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
528 case 211:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
529 // Found inexact matches, list follows
8746
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
530 cddb_parse_matches_list(http_hdr, cddb_data);
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
531 return cddb_request_titles(cddb_data);
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
532 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
533 printf("Unhandled code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
534 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
535 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
536 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
537
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
538 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
539 cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
540 int max;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
541 int ret, status;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
542 char *ptr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
543
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
544 ret = sscanf( http_hdr->body, "%d ", &status);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
545 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
546 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
547 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
548 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
549
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
550 switch(status) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
551 case 210:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
552 ptr = strstr(http_hdr->body, "max proto:");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
553 if( ptr==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
554 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
555 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
556 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
557 ret = sscanf(ptr, "max proto: %d", &max);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
558 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
559 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
560 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
561 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
562 cddb_data->freedb_proto_level = max;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
563 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
564 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
565 printf("Unhandled code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
566 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
567 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
568 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
569
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
570 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
571 cddb_get_proto_level(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
572 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
573 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
574
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
575 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
576 cddb_freedb_sites_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
577 int ret, status;
7746
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
578
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
579 ret = sscanf( http_hdr->body, "%d ", &status);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
580 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
581 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
582 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
583 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
584
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
585 switch(status) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
586 case 210:
7746
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
587 // TODO: Parse the sites
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
588 ret = cddb_data->anonymous; // For gcc complaining about unused parameter.
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
589 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
590 case 401:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
591 printf("No sites information available\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
592 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
593 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
594 printf("Unhandled code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
595 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
596 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
597 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
598
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
599 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
600 cddb_get_freedb_sites(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
601 return cddb_http_request("sites", cddb_freedb_sites_parse, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
602 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
603
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
604 void
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
605 cddb_create_hello(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
606 char host_name[51];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
607 char *user_name;
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
608
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
609 if( cddb_data->anonymous ) { // Default is anonymous
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
610 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
611 * We don't send current user/host name in hello to prevent spam.
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
612 * Software that sends this is considered spyware
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
613 * that most people don't like.
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
614 */
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
615 user_name = "anonymous";
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
616 strcpy(host_name, "localhost");
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
617 } else {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
618 if( gethostname(host_name, 50)<0 ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
619 strcpy(host_name, "localhost");
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
620 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
621 user_name = getenv("LOGNAME");
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
622 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
623 sprintf( cddb_data->cddb_hello, "&hello=%s+%s+%s+%s", user_name, host_name, "MPlayer", VERSION );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
624 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
625
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
626 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
627 cddb_retrieve(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
628 char offsets[1024], command[1024];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
629 char *ptr;
7746
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
630 unsigned int i, time_len;
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
631 int ret;
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
632
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
633 ptr = offsets;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
634 for( i=0; i<cddb_data->tracks ; i++ ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
635 ptr += sprintf(ptr, "%d+", cdtoc[i].frame );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
636 }
8557
97cf659a4c38 - fixed uninitialized offsets[]
arpi
parents: 7953
diff changeset
637 ptr[0]=0;
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
638 time_len = (cdtoc[cddb_data->tracks].frame)/75;
8557
97cf659a4c38 - fixed uninitialized offsets[]
arpi
parents: 7953
diff changeset
639
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
640 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
641 cddb_data->freedb_proto_level = 1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
642 cddb_data->xmcd_file = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
643
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
644 cddb_create_hello(cddb_data);
6475
837ca6fd4adf Checked the return value when retrieving the protocol level.
bertrand
parents: 6474
diff changeset
645 if( cddb_get_proto_level(cddb_data)<0 ) {
837ca6fd4adf Checked the return value when retrieving the protocol level.
bertrand
parents: 6474
diff changeset
646 printf("Failed to get the protocol level\n");
837ca6fd4adf Checked the return value when retrieving the protocol level.
bertrand
parents: 6474
diff changeset
647 return -1;
837ca6fd4adf Checked the return value when retrieving the protocol level.
bertrand
parents: 6474
diff changeset
648 }
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
649
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
650 //cddb_get_freedb_sites(&cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
651
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
652 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id, cddb_data->tracks, offsets, time_len );
7746
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
653 ret = cddb_http_request(command, cddb_query_parse, cddb_data);
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
654 if( ret<0 ) return -1;
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
655
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
656 if( cddb_data->cache_dir!=NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
657 free(cddb_data->cache_dir);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
658 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
659 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
660 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
661
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
662 int
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
663 cddb_resolve(char **xmcd_file) {
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
664 char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
665 char *home_dir = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
666 cddb_data_t cddb_data;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
667
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
668 cddb_data.tracks = read_toc();
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
669 cddb_data.disc_id = cddb_discid(cddb_data.tracks);
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
670 cddb_data.anonymous = 1; // Don't send user info by default
7746
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
671
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
672 // Check if there is a CD in the drive
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
673 // FIXME: That's not really a good way to check
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
674 if( cddb_data.disc_id==8256 ) {
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
675 printf("No CD in the drive\n");
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
676 return -1;
15ce89ba92cf Don't start a CDDB request to the CDDB server if there is no CD in the drive,
bertrand
parents: 7721
diff changeset
677 }
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
678
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
679 home_dir = getenv("HOME");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
680 if( home_dir==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
681 cddb_data.cache_dir = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
682 } else {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
683 cddb_data.cache_dir = (char*)malloc(strlen(home_dir)+strlen(cddb_cache_dir)+1);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
684 if( cddb_data.cache_dir==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
685 printf("Memory allocation failed\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
686 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
687 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
688 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
689 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
690
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
691 // Check for a cached file
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
692 if( cddb_read_cache(&cddb_data)<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
693 // No Cache found
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
694 if( cddb_retrieve(&cddb_data)<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
695 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
696 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
697 }
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
698
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
699 if( cddb_data.xmcd_file!=NULL ) {
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
700 // printf("%s\n", cddb_data.xmcd_file );
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
701 *xmcd_file = cddb_data.xmcd_file;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
702 return 0;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
703 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
704
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
705 return -1;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
706 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
707
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
708 /*******************************************************************************************************************
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
709 *
7431
e46eeafcd4df Moved all the cdinfo specific from cddb to a standalone file(cdinfo.c), so
bertrand
parents: 7269
diff changeset
710 * xmcd parser
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
711 *
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
712 *******************************************************************************************************************/
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
713 char*
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
714 xmcd_parse_dtitle(cd_info_t *cd_info, char *line) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
715 char *ptr, *album;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
716 ptr = strstr(line, "DTITLE=");
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
717 if( ptr!=NULL ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
718 ptr += 7;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
719 album = strstr(ptr, "/");
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
720 if( album==NULL ) return NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
721 cd_info->album = (char*)malloc(strlen(album+2)+1);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
722 if( cd_info->album==NULL ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
723 return NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
724 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
725 strcpy( cd_info->album, album+2 );
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
726 album--;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
727 album[0] = '\0';
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
728 cd_info->artist = (char*)malloc(strlen(ptr)+1);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
729 if( cd_info->artist==NULL ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
730 return NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
731 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
732 strcpy( cd_info->artist, ptr );
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
733 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
734 return ptr;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
735 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
736
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
737 char*
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
738 xmcd_parse_dgenre(cd_info_t *cd_info, char *line) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
739 char *ptr;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
740 ptr = strstr(line, "DGENRE=");
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
741 if( ptr!=NULL ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
742 ptr += 7;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
743 cd_info->genre = (char*)malloc(strlen(ptr)+1);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
744 if( cd_info->genre==NULL ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
745 return NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
746 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
747 strcpy( cd_info->genre, ptr );
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
748 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
749 return ptr;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
750 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
751
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
752 char*
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
753 xmcd_parse_ttitle(cd_info_t *cd_info, char *line) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
754 unsigned int track_nb;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
755 unsigned long sec, off;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
756 char *ptr;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
757 ptr = strstr(line, "TTITLE");
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
758 if( ptr!=NULL ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
759 ptr += 6;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
760 // Here we point to the track number
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
761 track_nb = atoi(ptr);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
762 ptr = strstr(ptr, "=");
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
763 if( ptr==NULL ) return NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
764 ptr++;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
765
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
766 sec = cdtoc[track_nb].frame;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
767 off = cdtoc[track_nb+1].frame-sec+1;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
768
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
769 cd_info_add_track( cd_info, ptr, track_nb+1, (unsigned int)(off/(60*75)), (unsigned int)((off/75)%60), (unsigned int)(off%75), sec, off );
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
770 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
771 return ptr;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
772 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
773
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
774 cd_info_t*
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
775 cddb_parse_xmcd(char *xmcd_file) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
776 cd_info_t *cd_info = NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
777 int length, pos = 0;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
778 char *ptr, *ptr2;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
779 unsigned int audiolen;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
780 if( xmcd_file==NULL ) return NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
781
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
782 cd_info = cd_info_new();
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
783 if( cd_info==NULL ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
784 return NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
785 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
786
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
787 length = strlen(xmcd_file);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
788 ptr = xmcd_file;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
789 while( ptr!=NULL && pos<length ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
790 // Read a line
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
791 ptr2 = ptr;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
792 while( ptr2[0]!='\0' && ptr2[0]!='\r' && ptr2[0]!='\n' ) ptr2++;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
793 if( ptr2[0]=='\0' ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
794 break;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
795 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
796 ptr2[0] = '\0';
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
797 // Ignore comments
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
798 if( ptr[0]!='#' ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
799 // Search for the album title
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
800 if( xmcd_parse_dtitle(cd_info, ptr) );
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
801 // Search for the genre
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
802 else if( xmcd_parse_dgenre(cd_info, ptr) );
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
803 // Search for a track title
7953
60624e692b95 GCC warning fixes
bertrand
parents: 7746
diff changeset
804 else if( xmcd_parse_ttitle(cd_info, ptr) ) audiolen++; // <-- audiolen++ to shut up gcc warning
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
805 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
806 if( ptr2[1]=='\n' ) ptr2++;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
807 pos = (ptr2+1)-ptr;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
808 ptr = ptr2+1;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
809 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
810
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
811 audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
812 cd_info->min = (unsigned int)(audiolen/(60*75));
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
813 cd_info->sec = (unsigned int)((audiolen/75)%60);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
814 cd_info->msec = (unsigned int)(audiolen%75);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
815
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
816 return cd_info;
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
817 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
818
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
819 stream_t*
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
820 cddb_open(char *dev, char *track) {
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
821 stream_t *stream;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
822 cd_info_t *cd_info = NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
823 cdda_priv *priv;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
824 char *xmcd_file = NULL;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
825 int ret;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
826
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
827 ret = cddb_resolve(&xmcd_file);
8746
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
828 // if( ret<0 ) {
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
829 // return NULL;
f1fc23f6203b - If CDDB fail, try CDDA
bertrand
parents: 8609
diff changeset
830 // }
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
831 if( ret==0 ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
832 cd_info = cddb_parse_xmcd(xmcd_file);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
833 free(xmcd_file);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
834 }
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
835 stream = open_cdda(dev, track);
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
836
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
837 priv = ((cdda_priv*)(stream->priv));
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
838 if( cd_info!=NULL ) {
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
839 cd_info_free(priv->cd_info);
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
840 priv->cd_info = cd_info;
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
841 }
7721
765afc1fcd67 Create the ~/.cddb directory if not present.
bertrand
parents: 7431
diff changeset
842 cd_info_debug( cd_info );
6697
4cade272ce2b Added a simple xmcd parser to retreive the tracks name.
bertrand
parents: 6475
diff changeset
843 return stream;
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
844 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
845 #endif