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