Mercurial > mplayer.hg
comparison playtreeparser.c @ 4308:d752f99fd535
Objectization of the play_tree_parser for more flexiblity
author | albeu |
---|---|
date | Tue, 22 Jan 2002 22:46:12 +0000 |
parents | 22fadd4022b5 |
children | 7d24c9bd2ae5 |
comparison
equal
deleted
inserted
replaced
4307:c494cc258eb8 | 4308:d752f99fd535 |
---|---|
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <fcntl.h> | 12 #include <fcntl.h> |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 #include "playtree.h" | 14 #include "playtree.h" |
15 #include "playtreeparser.h" | |
15 #include "libmpdemux/stream.h" | 16 #include "libmpdemux/stream.h" |
16 #include "mp_msg.h" | 17 #include "mp_msg.h" |
17 | 18 |
18 extern play_tree_t* | 19 extern play_tree_t* |
19 asx_parser_build_tree(char* buffer); | 20 asx_parser_build_tree(char* buffer, int ref); |
20 | 21 |
21 | 22 |
22 static char* buffer = NULL; | 23 play_tree_t* |
23 static int buffer_size = 0, buffer_end = 0; | 24 parse_asx(play_tree_parser_t* p) { |
24 | |
25 | |
26 | |
27 play_tree_t* | |
28 parse_asx(stream_t* stream) { | |
29 int r; | 25 int r; |
30 int comments = 0,read = 1,eof = 0; | 26 int comments = 0,read = 1,eof = 0; |
31 | 27 |
32 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n"); | 28 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n"); |
33 | 29 |
34 while(1) { | 30 while(1) { |
35 if(read && eof) // Eof reached before anything useful | 31 if(read && eof) // Eof reached before anything useful |
36 return NULL; | 32 return NULL; |
37 if(read) { | 33 if(read) { |
38 if(buffer_size - buffer_end < 50) buffer_size += 255; | 34 if(p->buffer_size - p->buffer_end < 50) p->buffer_size += 255; |
39 buffer = (char*)realloc(buffer,buffer_size*sizeof(char)); | 35 p->buffer = (char*)realloc(p->buffer,p->buffer_size*sizeof(char)); |
40 if(buffer == NULL) { | 36 if(p->buffer == NULL) { |
41 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",buffer_size*sizeof(char)); | 37 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",p->buffer_size*sizeof(char)); |
42 buffer_size = buffer_end = 0; | 38 p->buffer_size = p->buffer_end = 0; |
43 return NULL; | 39 return NULL; |
44 } | 40 } |
45 | 41 |
46 r = stream_read(stream,buffer+buffer_end,buffer_size-buffer_end-1); | 42 r = stream_read(p->stream,p->buffer+p->buffer_end,p->buffer_size-p->buffer_end-1); |
47 if(r < 0) { | 43 if(r < 0) { |
48 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't read from stream r=%d\n",r); | 44 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't read from stream r=%d\n",r); |
49 return NULL; | 45 return NULL; |
50 } else if(r == 0) | 46 } else if(r == 0) |
51 eof = 1; | 47 eof = 1; |
52 buffer_end += r; | 48 p->buffer_end += r; |
53 buffer[buffer_end] = '\0'; | 49 p->buffer[p->buffer_end] = '\0'; |
54 } | 50 } |
55 | 51 |
56 if(comments) { // Jump comments | 52 if(comments) { // Jump comments |
57 int e; | 53 int e; |
58 char* end = strstr(buffer,"-->"); | 54 char* end = strstr(p->buffer,"-->"); |
59 if(!end) { | 55 if(!end) { |
60 if(buffer[buffer_end-1] != '-') | 56 if(p->buffer[p->buffer_end-1] != '-') |
61 buffer_end = 0; // Drop buffer content if last char isn't '-' | 57 p->buffer_end = 0; // Drop buffer content if last char isn't '-' |
62 continue; | 58 continue; |
63 } | 59 } |
64 comments = 0; | 60 comments = 0; |
65 e = end - buffer + 3; | 61 e = end - p->buffer + 3; |
66 if(e >= buffer_end) { // > seems impossible | 62 if(e >= p->buffer_end) { // > seems impossible |
67 buffer_end = 0; // Drop buffer content | 63 p->buffer_end = 0; // Drop buffer content |
68 read = 1; | 64 read = 1; |
69 continue; | 65 continue; |
70 } | 66 } |
71 buffer_end -= e; | 67 p->buffer_end -= e; |
72 memmove(buffer,end+3,buffer_end); // Drop comments | 68 memmove(p->buffer,end+3,p->buffer_end); // Drop comments |
73 continue; | 69 continue; |
74 } | 70 } |
75 | 71 |
76 for(r= 0 ; r < buffer_end ; r++) { | 72 for(r= 0 ; r < p->buffer_end ; r++) { |
77 if(strchr(" \n\r\t",buffer[r]) != NULL) // Jump space | 73 if(strchr(" \n\r\t",p->buffer[r]) != NULL) // Jump space |
78 continue; | 74 continue; |
79 if(buffer[r] != '<') { | 75 if(p->buffer[r] != '<') { |
80 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",buffer[r]); | 76 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",p->buffer[r]); |
81 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",buffer); | 77 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer); |
82 return NULL; | 78 return NULL; |
83 } | 79 } |
84 break; // Stop on first '<' | 80 break; // Stop on first '<' |
85 } | 81 } |
86 if(r > buffer_end-4) { // We need more | 82 if(r > p->buffer_end-4) { // We need more |
87 if(r > 0) { // Drop unuseful beggining | 83 if(r > 0) { // Drop unuseful beggining |
88 buffer_end -= r; | 84 p->buffer_end -= r; |
89 memmove(buffer,&buffer[r],buffer_end); | 85 memmove(p->buffer,&p->buffer[r],p->buffer_end); |
90 } | 86 } |
91 read = 1; | 87 read = 1; |
92 continue; | 88 continue; |
93 } | 89 } |
94 | 90 |
95 if(strncmp(&buffer[r],"<!--",4) == 0) { // Comments | 91 if(strncmp(&p->buffer[r],"<!--",4) == 0) { // Comments |
96 read = 0; | 92 read = 0; |
97 comments = 1; | 93 comments = 1; |
98 continue; | 94 continue; |
99 } | 95 } |
100 | 96 |
101 if(strncasecmp(&buffer[r],"<ASX",4) != 0) // First element is not a comment nor an asx : end | 97 if(strncasecmp(&p->buffer[r],"<ASX",4) != 0) // First element is not a comment nor an asx : end |
102 return NULL; | 98 return NULL; |
103 | 99 |
104 | 100 |
105 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n"); | 101 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n"); |
106 break; | 102 break; |
107 } | 103 } |
108 // We have an asx : load it in memory and parse | 104 // We have an asx : load it in memory and parse |
109 | 105 |
110 while(!eof) { | 106 while(!eof) { |
111 if(buffer_size - buffer_end < 50) buffer_size += 255; | 107 if(p->buffer_size - p->buffer_end < 50) p->buffer_size += 255; |
112 buffer = (char*)realloc(buffer,buffer_size*sizeof(char)); | 108 p->buffer = (char*)realloc(p->buffer,p->buffer_size*sizeof(char)); |
113 if(buffer == NULL) { | 109 if(p->buffer == NULL) { |
114 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",buffer_size*sizeof(char)); | 110 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",p->buffer_size*sizeof(char)); |
115 buffer_size = buffer_end = 0; | 111 p->buffer_size = p->buffer_end = 0; |
116 return NULL; | 112 return NULL; |
117 } | 113 } |
118 r = stream_read(stream,buffer+buffer_end,buffer_size-buffer_end-1); | 114 r = stream_read(p->stream,p->buffer+p->buffer_end,p->buffer_size-p->buffer_end-1); |
119 if(r > 0) | 115 if(r > 0) |
120 buffer_end += r; | 116 p->buffer_end += r; |
121 if(r <= 0) | 117 if(r <= 0) |
122 break; | 118 break; |
123 buffer[buffer_end] = '\0'; | 119 p->buffer[p->buffer_end] = '\0'; |
124 } | 120 } |
125 | 121 |
126 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file : [%s]\n",buffer); | 122 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file : [%s]\n",p->buffer); |
127 return asx_parser_build_tree(buffer); | 123 return asx_parser_build_tree(p->buffer,p->deep); |
128 | 124 } |
129 | 125 |
130 } | 126 play_tree_t* |
131 | 127 parse_textplain(play_tree_parser_t* p) { |
132 play_tree_t* | |
133 parse_textplain(stream_t *stream) { | |
134 char* end; | 128 char* end; |
135 char* file; | 129 char* file; |
136 int eof = 0,r,p_end=-1,resize = 0; | 130 int eof = 0,r,p_end=-1,resize = 0; |
137 play_tree_t *list = NULL, *entry = NULL; | 131 play_tree_t *list = NULL, *entry = NULL; |
138 | 132 |
139 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext...\n"); | 133 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext...\n"); |
140 | 134 |
141 if(buffer_size < 255 && ! stream->eof) { | 135 if(p->buffer_size < 255 && ! p->stream->eof) { |
142 buffer_size = 255; | 136 p->buffer_size = 255; |
143 buffer = (char*)realloc(buffer,buffer_size*sizeof(char)); | 137 p->buffer = (char*)realloc(p->buffer,p->buffer_size*sizeof(char)); |
144 if(buffer == NULL) { | 138 if(p->buffer == NULL) { |
145 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",buffer_size*sizeof(char)); | 139 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",p->buffer_size*sizeof(char)); |
146 buffer_size = buffer_end = 0; | 140 p->buffer_size = p->buffer_end = 0; |
147 return NULL; | 141 return NULL; |
148 } | 142 } |
149 } | 143 } |
150 | 144 |
151 | 145 |
152 while(!eof) { | 146 while(!eof) { |
153 if(resize) { | 147 if(resize) { |
154 buffer_size += 255; | 148 p->buffer_size += 255; |
155 buffer = (char*)realloc(buffer,buffer_size*sizeof(char)); | 149 p->buffer = (char*)realloc(p->buffer,p->buffer_size*sizeof(char)); |
156 resize = 0; | 150 resize = 0; |
157 if(buffer == NULL) { | 151 if(p->buffer == NULL) { |
158 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",buffer_size*sizeof(char)); | 152 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",p->buffer_size*sizeof(char)); |
159 buffer_size = buffer_end = 0; | 153 p->buffer_size = p->buffer_end = 0; |
160 if(list) play_tree_free_list(list,1); | 154 if(list) play_tree_free_list(list,1); |
161 return NULL; | 155 return NULL; |
162 } | 156 } |
163 } | 157 } |
164 if(!stream->eof) { | 158 if(!p->stream->eof) { |
165 r = stream_read(stream,buffer+buffer_end,buffer_size-buffer_end-1); | 159 r = stream_read(p->stream,p->buffer+p->buffer_end,p->buffer_size-p->buffer_end-1); |
166 if(r < 0) { | 160 if(r < 0) { |
167 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't read from stream r=%d\n",r); | 161 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't read from stream r=%d\n",r); |
168 return NULL; | 162 return NULL; |
169 } else if(r == 0) | 163 } else if(r == 0) |
170 eof = 1; | 164 eof = 1; |
171 buffer_end += r; | 165 p->buffer_end += r; |
172 buffer[buffer_end] = '\0'; | 166 p->buffer[p->buffer_end] = '\0'; |
173 } else eof = 1; | 167 } else eof = 1; |
174 r = 0; | 168 r = 0; |
175 while(1) { | 169 while(1) { |
176 p_end = r; | 170 p_end = r; |
177 for( ; buffer[r] != '\0' ; r++) { | 171 for( ; p->buffer[r] != '\0' ; r++) { |
178 if(strchr(" \n\r\t",buffer[r]) != NULL) | 172 if(strchr(" \n\r\t",p->buffer[r]) != NULL) |
179 continue; | 173 continue; |
180 break; | 174 break; |
181 } | 175 } |
182 if(buffer[r] == '\0') { | 176 if(p->buffer[r] == '\0') { |
183 p_end = r; | 177 p_end = r; |
184 if(!eof) | 178 if(!eof) |
185 resize = 1; | 179 resize = 1; |
186 break; | 180 break; |
187 } | 181 } |
188 end = strchr(&buffer[r],'\n'); | 182 end = strchr(&p->buffer[r],'\n'); |
189 if(!end) { | 183 if(!end) { |
190 if(!eof) { | 184 if(!eof) { |
191 p_end = r; | 185 p_end = r; |
192 if(p_end == 0) { | 186 if(p_end == 0) { |
193 resize = 1; | 187 resize = 1; |
194 } | 188 } |
195 break; | 189 break; |
196 } | 190 } |
197 entry = play_tree_new(); | 191 entry = play_tree_new(); |
198 play_tree_add_file(entry,&buffer[r]); | 192 play_tree_add_file(entry,&p->buffer[r]); |
199 r = buffer_end; | 193 r = p->buffer_end; |
200 | 194 |
201 } | 195 } |
202 else { | 196 else { |
203 if(r > 0 && buffer[r-1] == '\r') r--; | 197 if(r > 0 && p->buffer[r-1] == '\r') r--; |
204 file = (char*)malloc((end-(&buffer[r])+1)*sizeof(char)); | 198 file = (char*)malloc((end-(&p->buffer[r])+1)*sizeof(char)); |
205 if(file == NULL) { | 199 if(file == NULL) { |
206 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",buffer_size*sizeof(char)); | 200 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Can't allocate %d bytes of memory\n",p->buffer_size*sizeof(char)); |
207 buffer_size = buffer_end = 0; | 201 p->buffer_size = p->buffer_end = 0; |
208 if(list) play_tree_free_list(list,1); | 202 if(list) play_tree_free_list(list,1); |
209 return NULL; | 203 return NULL; |
210 } | 204 } |
211 // TODO : Check if the given file exist and is readable (or it'a an url) | 205 // TODO : Check if the given file exist and is readable (or it'a an url) |
212 strncpy(file,&buffer[r],end-(&buffer[r])); | 206 strncpy(file,&p->buffer[r],end-(&p->buffer[r])); |
213 file[end-(&buffer[r])] = '\0'; | 207 file[end-(&p->buffer[r])] = '\0'; |
214 entry = play_tree_new(); | 208 entry = play_tree_new(); |
215 play_tree_add_file(entry,file); | 209 play_tree_add_file(entry,file); |
216 free(file); | 210 free(file); |
217 r += end-(&buffer[r]); | 211 r += end-(&p->buffer[r]); |
218 p_end = r; | 212 p_end = r; |
219 } | 213 } |
220 if(entry) { | 214 if(entry) { |
221 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding file %s to playlist\n",entry->files[0]); | 215 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding file %s to playlist\n",entry->files[0]); |
222 if(list) play_tree_append_entry(list,entry); | 216 if(list) play_tree_append_entry(list,entry); |
223 else list = entry; | 217 else list = entry; |
224 entry = NULL; | 218 entry = NULL; |
225 } | 219 } |
226 } | 220 } |
227 if(!eof && p_end > 0 && p_end < buffer_end) { | 221 if(!eof && p_end > 0 && p_end < p->buffer_end) { |
228 memmove(buffer,&buffer[p_end],buffer_end-p_end); | 222 memmove(p->buffer,&p->buffer[p_end],p->buffer_end-p_end); |
229 buffer_end -= p_end; | 223 p->buffer_end -= p_end; |
230 } else if(!eof && !resize && p_end == buffer_end) { | 224 } else if(!eof && !resize && p_end == p->buffer_end) { |
231 buffer_end = 0; | 225 p->buffer_end = 0; |
232 buffer[0] = '\0'; | 226 p->buffer[0] = '\0'; |
233 } | 227 } |
234 } | 228 } |
235 | 229 |
236 if(!list) return NULL; | 230 if(!list) return NULL; |
237 entry = play_tree_new(); | 231 entry = play_tree_new(); |
239 return entry; | 233 return entry; |
240 } | 234 } |
241 | 235 |
242 play_tree_t* | 236 play_tree_t* |
243 parse_playtree(stream_t *stream) { | 237 parse_playtree(stream_t *stream) { |
244 play_tree_t* tree = NULL; | 238 play_tree_parser_t* p; |
245 | 239 play_tree_t* ret; |
240 | |
246 #ifdef MP_DEBUG | 241 #ifdef MP_DEBUG |
247 assert(stream != NULL); | 242 assert(stream != NULL); |
248 assert(stream->type == STREAMTYPE_PLAYLIST); | 243 assert(stream->type == STREAMTYPE_PLAYLIST); |
249 #endif | 244 #endif |
250 | 245 |
251 while(1) { | 246 p = play_tree_parser_new(stream,0); |
252 tree = parse_asx(stream); | 247 if(!p) |
253 if(tree) break; | 248 return NULL; |
254 // Here come the others formats ( textplain must stay the last one ) | 249 |
255 tree = parse_textplain(stream); | 250 ret = play_tree_parser_get_play_tree(p); |
256 if(tree) break; | 251 play_tree_parser_free(p); |
257 break; | 252 |
258 } | 253 return ret; |
259 | |
260 if(tree) | |
261 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist succefully parsed\n"); | |
262 else mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while parsing playlist\n"); | |
263 | |
264 if(tree) | |
265 tree = play_tree_cleanup(tree); | |
266 | |
267 if(!tree) mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Warning empty playlist\n"); | |
268 | |
269 if(buffer) free(buffer); | |
270 buffer = NULL; | |
271 buffer_end = buffer_size = 0; | |
272 return tree; | |
273 } | 254 } |
274 | 255 |
275 play_tree_t* | 256 play_tree_t* |
276 parse_playlist_file(char* file) { | 257 parse_playlist_file(char* file) { |
277 stream_t *stream; | 258 stream_t *stream; |
297 free_stream(stream); | 278 free_stream(stream); |
298 | 279 |
299 return ret; | 280 return ret; |
300 | 281 |
301 } | 282 } |
283 | |
284 | |
285 play_tree_parser_t* | |
286 play_tree_parser_new(stream_t* stream,int deep) { | |
287 play_tree_parser_t* p; | |
288 | |
289 p = (play_tree_parser_t*)calloc(1,sizeof(play_tree_parser_t)); | |
290 if(!p) | |
291 return NULL; | |
292 p->stream = stream; | |
293 p->deep = deep; | |
294 | |
295 return p; | |
296 | |
297 } | |
298 | |
299 void | |
300 play_tree_parser_free(play_tree_parser_t* p) { | |
301 | |
302 #ifdef MP_DEBUG | |
303 assert(p != NULL); | |
304 #endif | |
305 | |
306 if(p->buffer) free(p->buffer); | |
307 free(p->buffer); | |
308 } | |
309 | |
310 play_tree_t* | |
311 play_tree_parser_get_play_tree(play_tree_parser_t* p) { | |
312 play_tree_t* tree = NULL; | |
313 | |
314 #ifdef MP_DEBUG | |
315 assert(p != NULL); | |
316 #endif | |
317 | |
318 while(1) { | |
319 tree = parse_asx(p); | |
320 if(tree) break; | |
321 // Here come the others formats ( textplain must stay the last one ) | |
322 tree = parse_textplain(p); | |
323 if(tree) break; | |
324 break; | |
325 } | |
326 | |
327 if(tree) | |
328 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist succefully parsed\n"); | |
329 else mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while parsing playlist\n"); | |
330 | |
331 if(tree) | |
332 tree = play_tree_cleanup(tree); | |
333 | |
334 if(!tree) mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Warning empty playlist\n"); | |
335 | |
336 if(p->buffer) free(p->buffer); | |
337 p->buffer = NULL; | |
338 p->buffer_end = p->buffer_size = 0; | |
339 | |
340 return tree; | |
341 } |