Mercurial > mplayer.hg
annotate subreader.c @ 851:8a99c95b0f56
support for new opendivx decore API
author | arpi_esp |
---|---|
date | Tue, 22 May 2001 23:17:31 +0000 |
parents | 2cc743142e79 |
children | 48d144812f24 |
rev | line source |
---|---|
258 | 1 /* |
2 * Subtitle reader with format autodetection | |
3 * | |
4 * Written by laaz | |
5 * Some code cleanup & realloc() by A'rpi/ESP-team | |
6 */ | |
7 | |
8 | |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
11 #include <string.h> | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
12 #include <ctype.h> |
258 | 13 |
14 #include "subreader.h" | |
15 | |
16 #define ERR (void *)-1 | |
17 | |
18 | |
19 int sub_uses_time=0; | |
20 int sub_errs=0; | |
624 | 21 int sub_num=0; // number of subtitle structs |
22 int sub_format=-1; // 0 for microdvd | |
23 // 1 for SubRip | |
24 // 2 for the third format (what's this?) | |
25 // 3 for SAMI (smi) | |
818 | 26 // 4 for vplayer format |
850 | 27 // 5 for RT format |
624 | 28 |
29 int eol(char p) { | |
30 return (p=='\r' || p=='\n' || p=='\0'); | |
31 } | |
32 | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
33 static inline void trail_space(char *s) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
34 int i; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
35 while (isspace(*s)) strcpy(s, s + 1); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
36 i = strlen(s) - 1; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
37 while (i > 0 && isspace(s[i])) s[i--] = '\0'; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
38 } |
624 | 39 |
40 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) { | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
41 static char line[1001]; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
42 static char *s = NULL; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
43 char text[1000], *p, *q; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
44 int state; |
624 | 45 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
46 current->lines = current->start = current->end = 0; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
47 state = 0; |
624 | 48 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
49 /* read the first line */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
50 if (!s) |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
51 if (!(s = fgets(line, 1000, fd))) return 0; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
52 |
624 | 53 do { |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
54 switch (state) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
55 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
56 case 0: /* find "START=" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
57 s = strstr (s, "Start="); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
58 if (s) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
59 current->start = strtol (s + 6, &s, 0) / 10; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
60 state = 1; continue; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
61 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
62 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
63 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
64 case 1: /* find "<P" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
65 if ((s = strstr (s, "<P"))) { s += 2; state = 2; continue; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
66 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
67 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
68 case 2: /* find ">" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
69 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
70 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
71 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
72 case 3: /* get all text until '<' appears */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
73 if (*s == '\0') { break; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
74 else if (*s == '<') { state = 4; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
75 else if (!strncasecmp (s, " ", 6)) { *p++ = ' '; s += 6; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
76 else if (*s == '\r') { s++; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
77 else if (!strncasecmp (s, "<br>", 4) || *s == '\n') { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
78 *p = '\0'; p = text; trail_space (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
79 if (text[0] != '\0') |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
80 current->text[current->lines++] = strdup (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
81 if (*s == '\n') s++; else s += 4; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
82 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
83 else *p++ = *s++; |
624 | 84 continue; |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
85 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
86 case 4: /* get current->end or skip <TAG> */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
87 q = strstr (s, "Start="); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
88 if (q) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
89 current->end = strtol (q + 6, &q, 0) / 10 - 1; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
90 *p = '\0'; trail_space (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
91 if (text[0] != '\0') |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
92 current->text[current->lines++] = strdup (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
93 if (current->lines > 0) { state = 99; break; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
94 state = 0; continue; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
95 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
96 s = strchr (s, '>'); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
97 if (s) { s++; state = 3; continue; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
98 break; |
624 | 99 } |
100 | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
101 /* read next line */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
102 if (state != 99 && !(s = fgets (line, 1000, fd))) return 0; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
103 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
104 } while (state != 99); |
624 | 105 |
106 return current; | |
107 } | |
258 | 108 |
109 | |
110 char *sub_readtext(char *source, char **dest) { | |
111 int len=0; | |
112 char *p; | |
113 | |
114 for (p=source;*p!='\r' && *p!='\n' && *p!='|'; p++,len++); | |
115 | |
116 *dest= (char *)malloc (len+1); | |
117 if (!dest) {return ERR;} | |
118 | |
119 strncpy(*dest, source, len); | |
120 (*dest)[len]=0; | |
121 | |
122 while (*p=='\r' || *p=='\n' || *p=='|') p++; | |
123 | |
124 if (*p) return p; // not-last text field | |
125 else return NULL; // last text field | |
126 } | |
127 | |
128 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) { | |
129 char line[1001]; | |
130 char line2[1001]; | |
131 char *p, *next; | |
132 int i; | |
133 | |
134 bzero (current, sizeof(current)); | |
135 | |
136 do { | |
137 if (!fgets (line, 1000, fd)) return NULL; | |
138 } while (*line=='\n' || *line == '\r' || !*line); | |
139 | |
605 | 140 if (sscanf (line, "{%ld}{%ld}%s", &(current->start), &(current->end),line2) <2) {return ERR;} |
258 | 141 |
142 p=line; | |
143 while (*p++!='}'); | |
144 while (*p++!='}'); | |
145 | |
146 next=p, i=0; | |
147 while ((next =sub_readtext (next, &(current->text[i])))) { | |
270 | 148 if (current->text[i]==ERR) {return ERR;} |
258 | 149 i++; |
678 | 150 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;} |
258 | 151 } |
152 current->lines=i+1; | |
153 | |
154 return current; | |
155 } | |
156 | |
157 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) { | |
158 char line[1001]; | |
159 int a1,a2,a3,a4,b1,b2,b3,b4; | |
160 char *p=NULL, *q=NULL; | |
161 int len; | |
162 | |
163 bzero (current, sizeof(current)); | |
164 | |
165 while (!current->text[0]) { | |
166 if (!fgets (line, 1000, fd)) return NULL; | |
269 | 167 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue; |
258 | 168 current->start = a1*360000+a2*6000+a3*100+a4; |
169 current->end = b1*360000+b2*6000+b3*100+b4; | |
170 | |
171 if (!fgets (line, 1000, fd)) return NULL; | |
172 | |
173 p=q=line; | |
174 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) { | |
175 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++); | |
176 current->text[current->lines-1]=(char *)malloc (len+1); | |
177 if (!current->text[current->lines-1]) return ERR; | |
178 strncpy (current->text[current->lines-1], q, len); | |
270 | 179 current->text[current->lines-1][len]='\0'; |
258 | 180 if (!*p || *p=='\r' || *p=='\n') break; |
181 while (*p++!=']'); | |
182 } | |
183 } | |
184 return current; | |
185 } | |
186 | |
187 subtitle *sub_read_line_third(FILE *fd,subtitle *current) { | |
188 char line[1001]; | |
189 int a1,a2,a3,a4,b1,b2,b3,b4; | |
190 char *p=NULL; | |
191 int i,len; | |
192 | |
193 bzero (current, sizeof(current)); | |
194 | |
195 while (!current->text[0]) { | |
196 if (!fgets (line, 1000, fd)) return NULL; | |
269 | 197 if ((len=sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) |
258 | 198 continue; |
199 current->start = a1*360000+a2*6000+a3*100+a4/10; | |
200 current->end = b1*360000+b2*6000+b3*100+b4/10; | |
201 for (i=0; i<SUB_MAX_TEXT;) { | |
269 | 202 if (!fgets (line, 1000, fd)) break; |
258 | 203 len=0; |
204 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++); | |
205 if (len) { | |
206 current->text[i]=(char *)malloc (len+1); | |
207 if (!current->text[i]) return ERR; | |
270 | 208 strncpy (current->text[i], line, len); current->text[i][len]='\0'; |
258 | 209 i++; |
210 } else { | |
211 break; | |
212 } | |
213 } | |
214 current->lines=i; | |
215 } | |
216 return current; | |
217 } | |
218 | |
818 | 219 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) { |
220 char line[1001]; | |
221 char line2[1001]; | |
222 int a1,a2,a3,b1,b2,b3; | |
223 int setime,etime; | |
224 char *p=NULL, *q=NULL, *l=NULL,*next; | |
225 int i,len,len2; | |
226 | |
227 bzero (current, sizeof(current)); | |
228 | |
229 while (!current->text[0]) { | |
230 if (!fgets (line, 1000, fd)) return NULL; | |
231 if ((len=sscanf (line, "%d:%d:%d:",&a1,&a2,&a3)) < 3) | |
232 continue; | |
233 if (!fgets (line2, 1000, fd)) return NULL; | |
234 if ((len2=sscanf (line2, "%d:%d:%d:",&b1,&b2,&b3)) < 3) | |
235 continue; | |
236 // przewiñ o linijkê do ty³u: | |
237 fseek(fd,-strlen(line2),SEEK_CUR); | |
238 | |
239 current->start = a1*360000+a2*6000+a3*100; | |
240 current->end = b1*360000+b2*6000+b3*100; | |
241 // teraz czas na wkopiowanie stringu | |
242 p=line; p+=9;i=0; | |
243 if (*p!='|') { | |
244 // | |
245 next = p,i=0; | |
246 while ((next =sub_readtext (next, &(current->text[i])))) { | |
247 if (current->text[i]==ERR) {return ERR;} | |
248 i++; | |
249 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;} | |
250 } | |
251 current->lines=i+1; | |
252 } | |
253 } | |
254 return current; | |
255 } | |
256 | |
850 | 257 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) { |
258 //TODO: This format uses quite rich (sub/super)set of xhtml | |
259 // I couldn't check it since DTD is not included. | |
260 // WARNING: full XML parses can be required for proper parsing | |
261 char line[1001]; | |
262 int a1,a2,a3,a4,b1,b2,b3,b4; | |
263 char *p=NULL,*next=NULL; | |
264 int i,len,plen; | |
265 | |
266 bzero (current, sizeof(current)); | |
267 | |
268 while (!current->text[0]) { | |
269 if (!fgets (line, 1000, fd)) return NULL; | |
270 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0 | |
271 //to describe the same moment in time. Maybe there are even more formats in use. | |
272 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) | |
273 plen=a1=a2=a3=a4=b1=b2=b3=b4=0; | |
274 if ( | |
275 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) && | |
276 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) && | |
277 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) && | |
278 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&b4,&plen)) < 6) && | |
279 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4,&plen)) < 8) | |
280 ) | |
281 continue; | |
282 current->start = a1*360000+a2*6000+a3*100+a4/10; | |
283 current->end = b1*360000+b2*6000+b3*100+b4/10; | |
284 p=line; p+=plen;i=0; | |
285 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml? | |
286 next = strstr(line,"<clear/>")+8;i=0; | |
287 while ((next =sub_readtext (next, &(current->text[i])))) { | |
288 if (current->text[i]==ERR) {return ERR;} | |
289 i++; | |
290 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;} | |
291 } | |
292 current->lines=i+1; | |
293 } | |
294 return current; | |
295 } | |
296 | |
818 | 297 |
258 | 298 |
299 int sub_autodetect (FILE *fd) { | |
300 char line[1001]; | |
301 int i,j=0; | |
302 // char *p; | |
303 | |
624 | 304 while (j < 100) { |
258 | 305 j++; |
306 if (!fgets (line, 1000, fd)) | |
307 return -1; | |
308 | |
624 | 309 if (sscanf (line, "{%d}{%d}", &i, &i)==2) |
258 | 310 {sub_uses_time=0;return 0;} |
269 | 311 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
258 | 312 {sub_uses_time=1;return 1;} |
269 | 313 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
258 | 314 {sub_uses_time=1;return 2;} |
624 | 315 if (strstr (line, "<SAMI>")) |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
316 {sub_uses_time=1; return 3;} |
818 | 317 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3) |
318 {sub_uses_time=1;return 4;} | |
850 | 319 //TODO: just checking if first line of sub starts with "<" is WAY |
320 // to weak test for RT | |
321 if (strcmp("<",line)) | |
322 {sub_uses_time=1;return 5;} | |
258 | 323 } |
624 | 324 |
325 return -1; // too many bad lines | |
258 | 326 } |
327 | |
328 | |
329 subtitle* sub_read_file (char *filename) { | |
330 FILE *fd; | |
331 int n_max; | |
332 subtitle *first; | |
850 | 333 subtitle * (*func[6])(FILE *fd,subtitle *dest)= |
258 | 334 { |
335 sub_read_line_microdvd, | |
336 sub_read_line_subrip, | |
624 | 337 sub_read_line_third, |
818 | 338 sub_read_line_sami, |
850 | 339 sub_read_line_vplayer, |
340 sub_read_line_rt | |
258 | 341 }; |
342 | |
343 fd=fopen (filename, "r"); if (!fd) return NULL; | |
344 | |
345 sub_format=sub_autodetect (fd); | |
346 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;} | |
624 | 347 printf ("SUB: Detected subtitle file format: %d\n",sub_format); |
258 | 348 |
349 rewind (fd); | |
350 | |
351 sub_num=0;n_max=32; | |
352 first=(subtitle *)malloc(n_max*sizeof(subtitle)); | |
353 if(!first) return NULL; | |
354 | |
355 while(1){ | |
356 subtitle *sub; | |
357 if(sub_num>=n_max){ | |
358 n_max+=16; | |
359 first=realloc(first,n_max*sizeof(subtitle)); | |
360 } | |
361 sub=func[sub_format](fd,&first[sub_num]); | |
362 if(!sub) break; // EOF | |
363 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid | |
364 } | |
365 | |
366 fclose(fd); | |
367 | |
368 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
269 | 369 printf ("SUB: Read %i subtitles", sub_num); |
624 | 370 if (sub_errs) printf (", %i bad line(s).\n", sub_errs); |
269 | 371 else printf (".\n"); |
258 | 372 |
373 return first; | |
374 } | |
375 | |
509 | 376 char * strreplace( char * in,char * what,char * whereof ) |
377 { | |
378 int i; | |
379 char * tmp; | |
380 | |
381 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL; | |
382 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i]; | |
383 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0; | |
384 return in; | |
385 } | |
386 | |
387 char * sub_filename( char * fname ) | |
388 { | |
389 char * sub_name = NULL; | |
390 char * sub_tmp = NULL; | |
391 int i; | |
850 | 392 #define SUB_EXTS 10 |
509 | 393 char * sub_exts[SUB_EXTS] = |
394 { ".sub", | |
395 ".SUB", | |
396 ".srt", | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
397 ".SRT", |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
398 ".smi", |
850 | 399 ".SMI", |
400 ".rt", | |
401 ".RT", | |
402 ".txt", | |
403 ".TXT"}; | |
509 | 404 |
405 if ( fname == NULL ) return NULL; | |
406 for( i=strlen( fname );i>0;i-- ) | |
407 if( fname[i] == '.' ) | |
408 { | |
409 sub_tmp=(char *)&fname[i]; | |
410 break; | |
411 } | |
412 if ( i == 0 ) return NULL; | |
413 sub_name=strdup( fname ); | |
414 for ( i=0;i<SUB_EXTS;i++ ) | |
415 { | |
416 FILE * f; | |
417 | |
418 strcpy( sub_name,fname ); | |
419 f=fopen( strreplace( sub_name,sub_tmp,sub_exts[i] ),"rt" ); | |
420 if ( f != NULL ) | |
421 { | |
422 fclose( f ); | |
423 printf( "SUB: Detected sub file: %s\n",sub_name ); | |
424 return sub_name; | |
425 } | |
426 } | |
427 return NULL; | |
428 } | |
429 | |
625 | 430 #if 0 |
258 | 431 int main(int argc, char **argv) { // for testing |
432 | |
433 int i,j; | |
434 subtitle *subs; | |
435 subtitle *egysub; | |
436 | |
437 if(argc<2){ | |
438 printf("\nUsage: subreader filename.sub\n\n"); | |
439 exit(1); | |
440 } | |
441 | |
624 | 442 subs=sub_read_file(argv[1]); |
258 | 443 if(!subs){ |
444 printf("Couldn't load file... let's write a bugreport :)\n"); | |
445 exit(1); | |
446 } | |
447 | |
448 for(j=0;j<sub_num;j++){ | |
449 egysub=&subs[j]; | |
450 printf ("%i line%c (%i-%i) ", | |
451 egysub->lines, | |
452 (1==egysub->lines)?' ':'s', | |
453 egysub->start, | |
454 egysub->end); | |
455 for (i=0; i<egysub->lines; i++) { | |
456 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> "); | |
457 } | |
458 printf ("\n"); | |
459 } | |
460 | |
461 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
462 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs); | |
463 return 0; | |
464 } | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
465 #endif |