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