Mercurial > mplayer.hg
annotate subreader.c @ 1024:ee73458f8f69
Kernel version checking
author | nickols_k |
---|---|
date | Tue, 05 Jun 2001 07:56:40 +0000 |
parents | 0b1b0bac67e9 |
children | 48cd2c5a9542 |
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 | |
921 | 24 // 2 for SubViewer |
624 | 25 // 3 for SAMI (smi) |
818 | 26 // 4 for vplayer format |
850 | 27 // 5 for RT format |
921 | 28 // 6 for ssa (Sub Station Alpha) |
624 | 29 |
30 int eol(char p) { | |
31 return (p=='\r' || p=='\n' || p=='\0'); | |
32 } | |
33 | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
34 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
|
35 int i; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
36 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
|
37 i = strlen(s) - 1; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
38 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
|
39 } |
624 | 40 |
41 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
|
42 static char line[1001]; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
43 static char *s = NULL; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
44 char text[1000], *p, *q; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
45 int state; |
624 | 46 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
47 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
|
48 state = 0; |
624 | 49 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
50 /* read the first line */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
51 if (!s) |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
52 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
|
53 |
624 | 54 do { |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
55 switch (state) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
56 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
57 case 0: /* find "START=" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
58 s = strstr (s, "Start="); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
59 if (s) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
60 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
|
61 state = 1; continue; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
62 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
63 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
64 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
65 case 1: /* find "<P" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
66 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
|
67 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
68 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
69 case 2: /* find ">" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
70 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
|
71 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
72 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
73 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
|
74 if (*s == '\0') { break; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
75 else if (*s == '<') { state = 4; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
76 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
|
77 else if (*s == '\r') { s++; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
78 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
|
79 *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
|
80 if (text[0] != '\0') |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
81 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
|
82 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
|
83 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
84 else *p++ = *s++; |
624 | 85 continue; |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
86 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
87 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
|
88 q = strstr (s, "Start="); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
89 if (q) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
90 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
|
91 *p = '\0'; trail_space (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
92 if (text[0] != '\0') |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
93 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
|
94 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
|
95 state = 0; continue; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
96 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
97 s = strchr (s, '>'); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
98 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
|
99 break; |
624 | 100 } |
101 | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
102 /* read next line */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
103 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
|
104 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
105 } while (state != 99); |
624 | 106 |
107 return current; | |
108 } | |
258 | 109 |
110 | |
111 char *sub_readtext(char *source, char **dest) { | |
112 int len=0; | |
932 | 113 char *p=source; |
258 | 114 |
932 | 115 while ( !eol(*p) && *p!= '|' ) { |
116 p++,len++; | |
117 } | |
258 | 118 |
119 *dest= (char *)malloc (len+1); | |
120 if (!dest) {return ERR;} | |
121 | |
122 strncpy(*dest, source, len); | |
123 (*dest)[len]=0; | |
124 | |
125 while (*p=='\r' || *p=='\n' || *p=='|') p++; | |
126 | |
127 if (*p) return p; // not-last text field | |
128 else return NULL; // last text field | |
129 } | |
130 | |
131 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) { | |
132 char line[1001]; | |
133 char line2[1001]; | |
134 char *p, *next; | |
135 int i; | |
136 | |
137 bzero (current, sizeof(current)); | |
138 | |
139 do { | |
140 if (!fgets (line, 1000, fd)) return NULL; | |
932 | 141 } while (sscanf (line, "{%ld}{%ld}%[^\r\n]", &(current->start), &(current->end),line2) <3); |
258 | 142 |
932 | 143 p=line2; |
258 | 144 |
145 next=p, i=0; | |
932 | 146 while (next =sub_readtext (next, &(current->text[i]))) { |
270 | 147 if (current->text[i]==ERR) {return ERR;} |
258 | 148 i++; |
678 | 149 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;} |
258 | 150 } |
932 | 151 current->lines= ++i; |
258 | 152 |
153 return current; | |
154 } | |
155 | |
156 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) { | |
157 char line[1001]; | |
158 int a1,a2,a3,a4,b1,b2,b3,b4; | |
159 char *p=NULL, *q=NULL; | |
160 int len; | |
161 | |
162 bzero (current, sizeof(current)); | |
163 | |
164 while (!current->text[0]) { | |
165 if (!fgets (line, 1000, fd)) return NULL; | |
269 | 166 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue; |
258 | 167 current->start = a1*360000+a2*6000+a3*100+a4; |
168 current->end = b1*360000+b2*6000+b3*100+b4; | |
169 | |
170 if (!fgets (line, 1000, fd)) return NULL; | |
171 | |
172 p=q=line; | |
173 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) { | |
174 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++); | |
175 current->text[current->lines-1]=(char *)malloc (len+1); | |
176 if (!current->text[current->lines-1]) return ERR; | |
177 strncpy (current->text[current->lines-1], q, len); | |
270 | 178 current->text[current->lines-1][len]='\0'; |
258 | 179 if (!*p || *p=='\r' || *p=='\n') break; |
180 while (*p++!=']'); | |
181 } | |
182 } | |
183 return current; | |
184 } | |
185 | |
186 subtitle *sub_read_line_third(FILE *fd,subtitle *current) { | |
187 char line[1001]; | |
188 int a1,a2,a3,a4,b1,b2,b3,b4; | |
189 char *p=NULL; | |
190 int i,len; | |
191 | |
192 bzero (current, sizeof(current)); | |
193 | |
194 while (!current->text[0]) { | |
195 if (!fgets (line, 1000, fd)) return NULL; | |
269 | 196 if ((len=sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) |
258 | 197 continue; |
198 current->start = a1*360000+a2*6000+a3*100+a4/10; | |
199 current->end = b1*360000+b2*6000+b3*100+b4/10; | |
200 for (i=0; i<SUB_MAX_TEXT;) { | |
269 | 201 if (!fgets (line, 1000, fd)) break; |
258 | 202 len=0; |
203 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++); | |
204 if (len) { | |
205 current->text[i]=(char *)malloc (len+1); | |
206 if (!current->text[i]) return ERR; | |
270 | 207 strncpy (current->text[i], line, len); current->text[i][len]='\0'; |
258 | 208 i++; |
209 } else { | |
210 break; | |
211 } | |
212 } | |
213 current->lines=i; | |
214 } | |
215 return current; | |
216 } | |
217 | |
818 | 218 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) { |
219 char line[1001]; | |
220 char line2[1001]; | |
221 int a1,a2,a3,b1,b2,b3; | |
222 int setime,etime; | |
223 char *p=NULL, *q=NULL, *l=NULL,*next; | |
858 | 224 int i,len,len2,plen; |
818 | 225 |
226 bzero (current, sizeof(current)); | |
227 | |
228 while (!current->text[0]) { | |
229 if (!fgets (line, 1000, fd)) return NULL; | |
858 | 230 if ((len=sscanf (line, "%d:%d:%d:%n",&a1,&a2,&a3,&plen)) < 3) |
818 | 231 continue; |
232 if (!fgets (line2, 1000, fd)) return NULL; | |
233 if ((len2=sscanf (line2, "%d:%d:%d:",&b1,&b2,&b3)) < 3) | |
234 continue; | |
235 // przewiñ o linijkê do ty³u: | |
236 fseek(fd,-strlen(line2),SEEK_CUR); | |
237 | |
238 current->start = a1*360000+a2*6000+a3*100; | |
239 current->end = b1*360000+b2*6000+b3*100; | |
896
d46de26aef48
there is another format that get detected as vplayers.
eyck
parents:
892
diff
changeset
|
240 if ((current->end - current->start) > 1000) {current->end = current->start + 1000;} // not too long though. |
818 | 241 // teraz czas na wkopiowanie stringu |
858 | 242 p=line; p+=plen;i=0; |
818 | 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 | |
921 | 297 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) { |
298 int hour1, min1, sec1, hunsec1, | |
299 hour2, min2, sec2, hunsec2, nothing; | |
300 | |
301 char line[1000], | |
302 line2[1000]; | |
303 do { | |
304 if (!fgets (line, 1000, fd)) return NULL; | |
305 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d," | |
306 "*Default,%d,%d,%d,%d,,%[^\n\r]", ¬hing, &hour1, &min1, | |
307 &sec1, &hunsec1, | |
308 &hour2, &min2, &sec2, &hunsec2, ¬hing, | |
309 ¬hing, ¬hing, ¬hing, line2) < 14); | |
310 current->lines=1; | |
311 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1; | |
312 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2; | |
313 current->text[0]=(char *) malloc(strlen(line2)+1); | |
314 strcpy(current->text[0],line2); | |
818 | 315 |
921 | 316 return current; |
317 } | |
258 | 318 |
319 int sub_autodetect (FILE *fd) { | |
320 char line[1001]; | |
321 int i,j=0; | |
322 // char *p; | |
323 | |
624 | 324 while (j < 100) { |
258 | 325 j++; |
326 if (!fgets (line, 1000, fd)) | |
327 return -1; | |
328 | |
624 | 329 if (sscanf (line, "{%d}{%d}", &i, &i)==2) |
258 | 330 {sub_uses_time=0;return 0;} |
269 | 331 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
258 | 332 {sub_uses_time=1;return 1;} |
269 | 333 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
258 | 334 {sub_uses_time=1;return 2;} |
624 | 335 if (strstr (line, "<SAMI>")) |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
336 {sub_uses_time=1; return 3;} |
818 | 337 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3) |
338 {sub_uses_time=1;return 4;} | |
850 | 339 //TODO: just checking if first line of sub starts with "<" is WAY |
913
18c43d261c35
corrected strcmp() bug, now it works again with every subs (it was broken)
laaz
parents:
896
diff
changeset
|
340 // too weak test for RT |
18c43d261c35
corrected strcmp() bug, now it works again with every subs (it was broken)
laaz
parents:
896
diff
changeset
|
341 // Please someone who knows the format of RT... FIX IT!!! |
921 | 342 // It may conflict with other sub formats in the future (actually it doesn't) |
913
18c43d261c35
corrected strcmp() bug, now it works again with every subs (it was broken)
laaz
parents:
896
diff
changeset
|
343 if ( *line == '<' ) |
850 | 344 {sub_uses_time=1;return 5;} |
921 | 345 |
346 // I have only seen only 1 piece of .ssa file. | |
347 // It may be not correct (tell me if it's not) | |
348 if (!memcmp(line, "Dialogue: Marked", 16)) | |
349 {sub_uses_time=1; return 6;} | |
258 | 350 } |
624 | 351 |
352 return -1; // too many bad lines | |
258 | 353 } |
354 | |
355 | |
356 subtitle* sub_read_file (char *filename) { | |
357 FILE *fd; | |
358 int n_max; | |
359 subtitle *first; | |
921 | 360 subtitle * (*func[7])(FILE *fd,subtitle *dest)= |
258 | 361 { |
362 sub_read_line_microdvd, | |
363 sub_read_line_subrip, | |
624 | 364 sub_read_line_third, |
818 | 365 sub_read_line_sami, |
850 | 366 sub_read_line_vplayer, |
921 | 367 sub_read_line_rt, |
368 sub_read_line_ssa | |
258 | 369 }; |
370 | |
371 fd=fopen (filename, "r"); if (!fd) return NULL; | |
372 | |
373 sub_format=sub_autodetect (fd); | |
374 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;} | |
624 | 375 printf ("SUB: Detected subtitle file format: %d\n",sub_format); |
258 | 376 |
377 rewind (fd); | |
378 | |
379 sub_num=0;n_max=32; | |
380 first=(subtitle *)malloc(n_max*sizeof(subtitle)); | |
381 if(!first) return NULL; | |
382 | |
383 while(1){ | |
384 subtitle *sub; | |
385 if(sub_num>=n_max){ | |
386 n_max+=16; | |
387 first=realloc(first,n_max*sizeof(subtitle)); | |
388 } | |
389 sub=func[sub_format](fd,&first[sub_num]); | |
390 if(!sub) break; // EOF | |
391 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid | |
392 } | |
393 | |
394 fclose(fd); | |
395 | |
396 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
269 | 397 printf ("SUB: Read %i subtitles", sub_num); |
624 | 398 if (sub_errs) printf (", %i bad line(s).\n", sub_errs); |
269 | 399 else printf (".\n"); |
258 | 400 |
401 return first; | |
402 } | |
403 | |
892 | 404 #if 0 |
509 | 405 char * strreplace( char * in,char * what,char * whereof ) |
406 { | |
407 int i; | |
408 char * tmp; | |
409 | |
410 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL; | |
411 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i]; | |
412 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0; | |
413 return in; | |
414 } | |
892 | 415 #endif |
509 | 416 |
892 | 417 char * sub_filename(char* path, char * fname ) |
509 | 418 { |
892 | 419 char * sub_name1; |
420 char * sub_name2; | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
421 char * aviptr1, * aviptr2, * tmp; |
892 | 422 int i,j; |
423 FILE * f; | |
424 int pos=0; | |
425 char * sub_exts[] = | |
509 | 426 { ".sub", |
427 ".SUB", | |
428 ".srt", | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
429 ".SRT", |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
430 ".smi", |
850 | 431 ".SMI", |
432 ".rt", | |
433 ".RT", | |
434 ".txt", | |
435 ".TXT"}; | |
892 | 436 |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
437 |
509 | 438 if ( fname == NULL ) return NULL; |
892 | 439 |
440 sub_name1=strrchr(fname,'.'); | |
441 if (!sub_name1) return NULL; | |
442 pos=sub_name1-fname; | |
443 | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
444 sub_name1=malloc(strlen(fname)+8); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
445 strcpy(sub_name1,fname); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
446 |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
447 sub_name2=malloc (strlen(path) + strlen(fname) + 8); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
448 if (tmp=strrchr(fname,'/')) |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
449 sprintf (sub_name2, "%s%s", path, tmp+1); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
450 else |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
451 sprintf (sub_name2, "%s%s", path, fname); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
452 |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
453 aviptr1=strrchr(sub_name1,'.'); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
454 aviptr2=strrchr(sub_name2,'.'); |
892 | 455 |
456 for(j=0;j<=1;j++){ | |
457 char* sub_name=j?sub_name1:sub_name2; | |
458 for ( i=0;i<(sizeof(sub_exts)/sizeof(char*));i++ ) { | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
459 strcpy(j?aviptr1:aviptr2,sub_exts[i]); |
935 | 460 // printf("trying: '%s'\n",sub_name); |
892 | 461 if((f=fopen( sub_name,"rt" ))) { |
509 | 462 fclose( f ); |
463 printf( "SUB: Detected sub file: %s\n",sub_name ); | |
464 return sub_name; | |
892 | 465 } |
509 | 466 } |
892 | 467 } |
468 | |
509 | 469 return NULL; |
470 } | |
471 | |
625 | 472 #if 0 |
258 | 473 int main(int argc, char **argv) { // for testing |
474 | |
475 int i,j; | |
476 subtitle *subs; | |
477 subtitle *egysub; | |
478 | |
479 if(argc<2){ | |
480 printf("\nUsage: subreader filename.sub\n\n"); | |
481 exit(1); | |
482 } | |
483 | |
624 | 484 subs=sub_read_file(argv[1]); |
258 | 485 if(!subs){ |
486 printf("Couldn't load file... let's write a bugreport :)\n"); | |
487 exit(1); | |
488 } | |
489 | |
490 for(j=0;j<sub_num;j++){ | |
491 egysub=&subs[j]; | |
492 printf ("%i line%c (%i-%i) ", | |
493 egysub->lines, | |
494 (1==egysub->lines)?' ':'s', | |
495 egysub->start, | |
496 egysub->end); | |
497 for (i=0; i<egysub->lines; i++) { | |
498 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> "); | |
499 } | |
500 printf ("\n"); | |
501 } | |
502 | |
503 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
504 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs); | |
505 return 0; | |
506 } | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
507 #endif |