Mercurial > mplayer.hg
annotate libmpdemux/cddb.c @ 7447:a49219323787
moved to to avoid CVS-XXYYZZ- versions (if no given) - altought this comment is bigger than the patch :)
author | alex |
---|---|
date | Fri, 20 Sep 2002 12:50:04 +0000 |
parents | e46eeafcd4df |
children | 765afc1fcd67 |
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; | |
183 while (i < tot_trks) { | |
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. | |
290 char file_name[100]; | |
291 int file_fd; | |
292 size_t wrote=0; | |
293 | |
294 if( cddb_data==NULL || cddb_data->cache_dir==NULL ) return -1; | |
295 | |
296 sprintf( file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id); | |
297 | |
298 file_fd = creat(file_name, S_IREAD|S_IWRITE); | |
299 if( file_fd<0 ) { | |
300 perror("open"); | |
301 return -1; | |
302 } | |
303 | |
304 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size); | |
305 if( wrote<0 ) { | |
306 perror("write"); | |
307 close(file_fd); | |
308 return -1; | |
309 } | |
310 if( wrote!=cddb_data->xmcd_file_size ) { | |
311 printf("Not all the xmcd file has been written\n"); | |
312 close(file_fd); | |
313 return -1; | |
314 } | |
315 | |
316 close(file_fd); | |
317 | |
318 return 0; | |
319 } | |
320 | |
321 int | |
322 cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { | |
323 unsigned long disc_id; | |
324 char category[100]; | |
325 char *ptr=NULL, *ptr2=NULL; | |
326 int ret, status; | |
327 | |
328 if( http_hdr==NULL || cddb_data==NULL ) return -1; | |
329 | |
330 ret = sscanf( http_hdr->body, "%d ", &status); | |
331 if( ret!=1 ) { | |
332 printf("Parse error\n"); | |
333 return -1; | |
334 } | |
335 | |
336 switch(status) { | |
337 case 210: | |
338 ret = sscanf( http_hdr->body, "%d %s %08lx", &status, category, &disc_id); | |
339 if( ret!=3 ) { | |
340 printf("Parse error\n"); | |
341 return -1; | |
342 } | |
343 // Check if it's a xmcd database file | |
344 ptr = strstr(http_hdr->body, "# xmcd"); | |
345 if( ptr==NULL ) { | |
346 printf("Invalid xmcd database file returned\n"); | |
347 return -1; | |
348 } | |
349 // Ok found the beginning of the file | |
350 // look for the end | |
351 ptr2 = strstr(ptr, "\r\n.\r\n"); | |
352 if( ptr2==NULL ) { | |
353 ptr2 = strstr(ptr, "\n.\n"); | |
354 if( ptr2==NULL ) { | |
355 printf("Unable to find '.'\n"); | |
356 return -1; | |
357 } | |
358 } | |
359 // Ok found the end | |
360 // do a sanity check | |
361 if( http_hdr->body_size<(ptr2-ptr) ) { | |
362 printf("Unexpected fix me\n"); | |
363 return -1; | |
364 } | |
365 cddb_data->xmcd_file = ptr; | |
366 cddb_data->xmcd_file_size = ptr2-ptr+2; | |
367 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0'; | |
368 // Avoid the http_free function to free the xmcd file...save a mempcy... | |
369 http_hdr->body = NULL; | |
370 http_hdr->body_size = 0; | |
371 return cddb_write_cache(cddb_data); | |
372 default: | |
373 printf("Unhandled code\n"); | |
374 } | |
375 return 0; | |
376 } | |
377 | |
378 int | |
379 cddb_request_titles(cddb_data_t *cddb_data) { | |
380 char command[1024]; | |
381 sprintf( command, "cddb+read+%s+%08lx", cddb_data->category, cddb_data->disc_id); | |
382 return cddb_http_request(command, cddb_read_parse, cddb_data); | |
383 } | |
384 | |
385 int | |
386 cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { | |
387 char album_title[100]; | |
388 char *ptr = NULL; | |
389 int ret, status; | |
390 | |
391 ret = sscanf( http_hdr->body, "%d ", &status); | |
392 if( ret!=1 ) { | |
393 printf("Parse error\n"); | |
394 return -1; | |
395 } | |
396 | |
397 switch(status) { | |
398 case 200: | |
399 // Found exact match | |
400 ret = sscanf(http_hdr->body, "%d %s %08lx %s", &status, cddb_data->category, &(cddb_data->disc_id), album_title); | |
401 if( ret!=4 ) { | |
402 printf("Parse error\n"); | |
403 return -1; | |
404 } | |
405 ptr = strstr(http_hdr->body, album_title); | |
406 if( ptr!=NULL ) { | |
407 char *ptr2; | |
408 int len; | |
409 ptr2 = strstr(ptr, "\n"); | |
410 if( ptr2==NULL ) { | |
411 len = (http_hdr->body_size)-(ptr-(http_hdr->body)); | |
412 } else { | |
413 len = ptr2-ptr+1; | |
414 } | |
415 strncpy(album_title, ptr, len); | |
416 album_title[len-2]='\0'; | |
417 } | |
418 printf("Parse OK, found: %s\n", album_title); | |
419 return cddb_request_titles(cddb_data); | |
420 case 202: | |
421 // No match found | |
422 printf("Album not found\n"); | |
423 break; | |
424 case 210: | |
425 // Found exact matches, list follows | |
426 ptr = strstr(http_hdr->body, "\n"); | |
427 if( ptr==NULL ) { | |
428 printf("Unable to find end of line\n"); | |
429 return -1; | |
430 } | |
431 ptr++; | |
432 // We have a list of exact matches, so which one do | |
433 // we use? So let's take the first one. | |
434 ret = sscanf(ptr, "%s %08lx %s", cddb_data->category, &(cddb_data->disc_id), album_title); | |
435 if( ret!=3 ) { | |
436 printf("Parse error\n"); | |
437 return -1; | |
438 } | |
439 ptr = strstr(http_hdr->body, album_title); | |
440 if( ptr!=NULL ) { | |
441 char *ptr2; | |
442 int len; | |
443 ptr2 = strstr(ptr, "\n"); | |
444 if( ptr2==NULL ) { | |
445 len = (http_hdr->body_size)-(ptr-(http_hdr->body)); | |
446 } else { | |
447 len = ptr2-ptr+1; | |
448 } | |
449 strncpy(album_title, ptr, len); | |
450 album_title[len-2]='\0'; | |
451 } | |
452 printf("Parse OK, found: %s\n", album_title); | |
453 return cddb_request_titles(cddb_data); | |
454 /* | |
455 body=[210 Found exact matches, list follows (until terminating `.') | |
456 misc c711930d Santana / Supernatural | |
457 rock c711930d Santana / Supernatural | |
458 blues c711930d Santana / Supernatural | |
459 .] | |
460 */ | |
461 case 211: | |
462 // Found inexact matches, list follows | |
463 printf("No exact matches found, list follows\n"); | |
464 break; | |
465 default: | |
466 printf("Unhandled code\n"); | |
467 } | |
468 return -1; | |
469 } | |
470 | |
471 int | |
472 cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { | |
473 int max; | |
474 int ret, status; | |
475 char *ptr; | |
476 | |
477 ret = sscanf( http_hdr->body, "%d ", &status); | |
478 if( ret!=1 ) { | |
479 printf("Parse error\n"); | |
480 return -1; | |
481 } | |
482 | |
483 switch(status) { | |
484 case 210: | |
485 ptr = strstr(http_hdr->body, "max proto:"); | |
486 if( ptr==NULL ) { | |
487 printf("Parse error\n"); | |
488 return -1; | |
489 } | |
490 ret = sscanf(ptr, "max proto: %d", &max); | |
491 if( ret!=1 ) { | |
492 printf("Parse error\n"); | |
493 return -1; | |
494 } | |
495 cddb_data->freedb_proto_level = max; | |
496 return 0; | |
497 default: | |
498 printf("Unhandled code\n"); | |
499 } | |
500 return -1; | |
501 } | |
502 | |
503 int | |
504 cddb_get_proto_level(cddb_data_t *cddb_data) { | |
505 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data); | |
506 } | |
507 | |
508 int | |
509 cddb_freedb_sites_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { | |
510 int ret, status; | |
511 | |
512 ret = sscanf( http_hdr->body, "%d ", &status); | |
513 if( ret!=1 ) { | |
514 printf("Parse error\n"); | |
515 return -1; | |
516 } | |
517 | |
518 switch(status) { | |
519 case 210: | |
520 // Parse the sites | |
521 return 0; | |
522 case 401: | |
523 printf("No sites information available\n"); | |
524 break; | |
525 default: | |
526 printf("Unhandled code\n"); | |
527 } | |
528 return -1; | |
529 } | |
530 | |
531 int | |
532 cddb_get_freedb_sites(cddb_data_t *cddb_data) { | |
533 return cddb_http_request("sites", cddb_freedb_sites_parse, cddb_data); | |
534 } | |
535 | |
536 void | |
537 cddb_create_hello(cddb_data_t *cddb_data) { | |
538 char host_name[51]; | |
539 char *user_name; | |
6697
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
540 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
541 if( cddb_data->anonymous ) { // Default is anonymous |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
542 /* 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
|
543 * 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
|
544 * Software that sends this is considered spyware |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
545 * that most people don't like. |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
546 */ |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
547 user_name = "anonymous"; |
6474 | 548 strcpy(host_name, "localhost"); |
6697
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
549 } else { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
550 if( gethostname(host_name, 50)<0 ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
551 strcpy(host_name, "localhost"); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
552 } |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
553 user_name = getenv("LOGNAME"); |
6474 | 554 } |
555 sprintf( cddb_data->cddb_hello, "&hello=%s+%s+%s+%s", user_name, host_name, "MPlayer", VERSION ); | |
556 } | |
557 | |
558 int | |
559 cddb_retrieve(cddb_data_t *cddb_data) { | |
560 char offsets[1024], command[1024]; | |
561 char *ptr; | |
562 int i, time_len; | |
563 | |
564 ptr = offsets; | |
565 for( i=0; i<cddb_data->tracks ; i++ ) { | |
566 ptr += sprintf(ptr, "%d+", cdtoc[i].frame ); | |
567 } | |
568 time_len = (cdtoc[cddb_data->tracks].frame)/75; | |
569 | |
570 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER; | |
571 cddb_data->freedb_proto_level = 1; | |
572 cddb_data->xmcd_file = NULL; | |
573 | |
574 cddb_create_hello(cddb_data); | |
6475
837ca6fd4adf
Checked the return value when retrieving the protocol level.
bertrand
parents:
6474
diff
changeset
|
575 if( cddb_get_proto_level(cddb_data)<0 ) { |
837ca6fd4adf
Checked the return value when retrieving the protocol level.
bertrand
parents:
6474
diff
changeset
|
576 printf("Failed to get the protocol level\n"); |
837ca6fd4adf
Checked the return value when retrieving the protocol level.
bertrand
parents:
6474
diff
changeset
|
577 return -1; |
837ca6fd4adf
Checked the return value when retrieving the protocol level.
bertrand
parents:
6474
diff
changeset
|
578 } |
6474 | 579 |
580 //cddb_get_freedb_sites(&cddb_data); | |
581 | |
582 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id, cddb_data->tracks, offsets, time_len ); | |
583 i = cddb_http_request(command, cddb_query_parse, cddb_data); | |
584 if( i<0 ) return -1; | |
585 | |
586 if( cddb_data->cache_dir!=NULL ) { | |
587 free(cddb_data->cache_dir); | |
588 } | |
589 return 0; | |
590 } | |
591 | |
592 int | |
6697
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
593 cddb_resolve(char **xmcd_file) { |
6474 | 594 char cddb_cache_dir[] = DEFAULT_CACHE_DIR; |
595 char *home_dir = NULL; | |
596 cddb_data_t cddb_data; | |
597 | |
598 cddb_data.tracks = read_toc(); | |
599 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
|
600 cddb_data.anonymous = 1; // Don't send user info by default |
6474 | 601 |
602 home_dir = getenv("HOME"); | |
603 if( home_dir==NULL ) { | |
604 cddb_data.cache_dir = NULL; | |
605 } else { | |
606 cddb_data.cache_dir = (char*)malloc(strlen(home_dir)+strlen(cddb_cache_dir)+1); | |
607 if( cddb_data.cache_dir==NULL ) { | |
608 printf("Memory allocation failed\n"); | |
609 return -1; | |
610 } | |
611 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir ); | |
612 } | |
613 | |
614 // Check for a cached file | |
615 if( cddb_read_cache(&cddb_data)<0 ) { | |
616 // No Cache found | |
617 if( cddb_retrieve(&cddb_data)<0 ) { | |
618 return -1; | |
619 } | |
620 } | |
6697
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
621 |
6474 | 622 if( cddb_data.xmcd_file!=NULL ) { |
6697
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
623 // printf("%s\n", cddb_data.xmcd_file ); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
624 *xmcd_file = cddb_data.xmcd_file; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
625 return 0; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
626 } |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
627 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
628 return -1; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
629 } |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
630 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
631 /******************************************************************************************************************* |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
632 * |
7431
e46eeafcd4df
Moved all the cdinfo specific from cddb to a standalone file(cdinfo.c), so
bertrand
parents:
7269
diff
changeset
|
633 * xmcd parser |
6697
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
634 * |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
635 *******************************************************************************************************************/ |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
636 char* |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
637 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
|
638 char *ptr, *album; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
639 ptr = strstr(line, "DTITLE="); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
640 if( ptr!=NULL ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
641 ptr += 7; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
642 album = strstr(ptr, "/"); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
643 if( album==NULL ) return NULL; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
644 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
|
645 if( cd_info->album==NULL ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
646 return NULL; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
647 } |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
648 strcpy( cd_info->album, album+2 ); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
649 album--; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
650 album[0] = '\0'; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
651 cd_info->artist = (char*)malloc(strlen(ptr)+1); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
652 if( cd_info->artist==NULL ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
653 return NULL; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
654 } |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
655 strcpy( cd_info->artist, ptr ); |
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 return ptr; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
658 } |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
659 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
660 char* |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
661 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
|
662 char *ptr; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
663 ptr = strstr(line, "DGENRE="); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
664 if( ptr!=NULL ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
665 ptr += 7; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
666 cd_info->genre = (char*)malloc(strlen(ptr)+1); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
667 if( cd_info->genre==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->genre, ptr ); |
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 return ptr; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
673 } |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
674 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
675 char* |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
676 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
|
677 unsigned int track_nb; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
678 unsigned long sec, off; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
679 char *ptr; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
680 ptr = strstr(line, "TTITLE"); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
681 if( ptr!=NULL ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
682 ptr += 6; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
683 // Here we point to the track number |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
684 track_nb = atoi(ptr); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
685 ptr = strstr(ptr, "="); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
686 if( ptr==NULL ) return NULL; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
687 ptr++; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
688 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
689 sec = cdtoc[track_nb].frame; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
690 off = cdtoc[track_nb+1].frame-sec+1; |
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 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
|
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 cd_info_t* |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
698 cddb_parse_xmcd(char *xmcd_file) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
699 cd_info_t *cd_info = NULL; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
700 int length, pos = 0; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
701 char *ptr, *ptr2; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
702 unsigned int audiolen; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
703 if( xmcd_file==NULL ) return NULL; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
704 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
705 cd_info = cd_info_new(); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
706 if( cd_info==NULL ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
707 return NULL; |
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 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
710 length = strlen(xmcd_file); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
711 ptr = xmcd_file; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
712 while( ptr!=NULL && pos<length ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
713 // Read a line |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
714 ptr2 = ptr; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
715 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
|
716 if( ptr2[0]=='\0' ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
717 break; |
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 ptr2[0] = '\0'; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
720 // Ignore comments |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
721 if( ptr[0]!='#' ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
722 // Search for the album title |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
723 if( xmcd_parse_dtitle(cd_info, ptr) ); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
724 // Search for the genre |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
725 else if( xmcd_parse_dgenre(cd_info, ptr) ); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
726 // Search for a track title |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
727 else if( xmcd_parse_ttitle(cd_info, ptr) ); |
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 if( ptr2[1]=='\n' ) ptr2++; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
730 pos = (ptr2+1)-ptr; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
731 ptr = ptr2+1; |
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 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
|
735 cd_info->min = (unsigned int)(audiolen/(60*75)); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
736 cd_info->sec = (unsigned int)((audiolen/75)%60); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
737 cd_info->msec = (unsigned int)(audiolen%75); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
738 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
739 return cd_info; |
6474 | 740 } |
741 | |
742 stream_t* | |
743 cddb_open(char *dev, char *track) { | |
6697
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
744 stream_t *stream; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
745 cd_info_t *cd_info = NULL; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
746 cdda_priv *priv; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
747 char *xmcd_file = NULL; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
748 int ret; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
749 |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
750 ret = cddb_resolve(&xmcd_file); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
751 if( ret==0 ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
752 cd_info = cddb_parse_xmcd(xmcd_file); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
753 free(xmcd_file); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
754 cd_info_debug( cd_info ); |
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 stream = open_cdda(dev, track); |
6474 | 757 |
6697
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
758 priv = ((cdda_priv*)(stream->priv)); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
759 cd_info_debug(priv->cd_info); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
760 if( cd_info!=NULL ) { |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
761 cd_info_free(priv->cd_info); |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
762 priv->cd_info = cd_info; |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
763 } |
4cade272ce2b
Added a simple xmcd parser to retreive the tracks name.
bertrand
parents:
6475
diff
changeset
|
764 return stream; |
6474 | 765 } |
766 #endif |