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