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>
|
|
12
|
|
13 #include "subreader.h"
|
|
14
|
|
15 #define ERR (void *)-1
|
|
16
|
|
17
|
|
18 int sub_uses_time=0;
|
|
19 int sub_errs=0;
|
624
|
20 int sub_num=0; // number of subtitle structs
|
|
21 int sub_format=-1; // 0 for microdvd
|
|
22 // 1 for SubRip
|
|
23 // 2 for the third format (what's this?)
|
|
24 // 3 for SAMI (smi)
|
|
25
|
|
26 int eol(char p) {
|
|
27 return (p=='\r' || p=='\n' || p=='\0');
|
|
28 }
|
|
29
|
|
30
|
|
31 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) {
|
|
32 char line[1001];
|
|
33 int i;
|
|
34 char *s, *p;
|
|
35
|
|
36 current->start=0;
|
|
37 do {
|
|
38 if (! (fgets (line, 1000, fd))) return 0;
|
|
39 s= strstr(line, "Start=");
|
|
40 if (s) {
|
|
41 sscanf (s, "Start=%d", ¤t->start);
|
|
42 if (strstr (s, "<P><br>")) current->start=0;
|
|
43 }
|
|
44 } while ( !current->start );
|
|
45
|
|
46 if (! (fgets (line, 1000, fd))) return 0;
|
|
47 s=strstr (line, "<P>")+3;
|
|
48
|
|
49 i=0;
|
|
50 do {
|
|
51 for (p=s; !eol(*p) && strncmp(p,"<br>",4); p++);
|
|
52 if (p==s) {
|
|
53 s+=4;
|
|
54 continue;
|
|
55 }
|
|
56 current->text[i]=(char *)malloc(p-s+1);
|
|
57 strncpy (current->text[i], s, p-s);
|
|
58 current->text[i++][p-s]='\0';
|
|
59 if (!strncmp(p,"<br>",4)) s=p+4;
|
|
60 else s=p;
|
|
61 } while (!eol (*p));
|
|
62
|
|
63 current->lines=i;
|
|
64
|
|
65 if (! (fgets (line, 1000, fd))) return 0;
|
|
66 s= strstr(line, "Start=");
|
|
67 if (s) {
|
|
68 sscanf (s, "Start=%d", ¤t->end);
|
|
69 if (!strstr (s, "<P><br>")) return ERR;
|
|
70 } else return ERR;
|
|
71
|
|
72 return current;
|
|
73 }
|
258
|
74
|
|
75
|
|
76 char *sub_readtext(char *source, char **dest) {
|
|
77 int len=0;
|
|
78 char *p;
|
|
79
|
|
80 for (p=source;*p!='\r' && *p!='\n' && *p!='|'; p++,len++);
|
|
81
|
|
82 *dest= (char *)malloc (len+1);
|
|
83 if (!dest) {return ERR;}
|
|
84
|
|
85 strncpy(*dest, source, len);
|
|
86 (*dest)[len]=0;
|
|
87
|
|
88 while (*p=='\r' || *p=='\n' || *p=='|') p++;
|
|
89
|
|
90 if (*p) return p; // not-last text field
|
|
91 else return NULL; // last text field
|
|
92 }
|
|
93
|
|
94 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
|
|
95 char line[1001];
|
|
96 char line2[1001];
|
|
97 char *p, *next;
|
|
98 int i;
|
|
99
|
|
100 bzero (current, sizeof(current));
|
|
101
|
|
102 do {
|
|
103 if (!fgets (line, 1000, fd)) return NULL;
|
|
104 } while (*line=='\n' || *line == '\r' || !*line);
|
|
105
|
605
|
106 if (sscanf (line, "{%ld}{%ld}%s", &(current->start), &(current->end),line2) <2) {return ERR;}
|
258
|
107
|
|
108 p=line;
|
|
109 while (*p++!='}');
|
|
110 while (*p++!='}');
|
|
111
|
|
112 next=p, i=0;
|
|
113 while ((next =sub_readtext (next, &(current->text[i])))) {
|
270
|
114 if (current->text[i]==ERR) {return ERR;}
|
258
|
115 i++;
|
678
|
116 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;}
|
258
|
117 }
|
|
118 current->lines=i+1;
|
|
119
|
|
120 return current;
|
|
121 }
|
|
122
|
|
123 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
|
|
124 char line[1001];
|
|
125 int a1,a2,a3,a4,b1,b2,b3,b4;
|
|
126 char *p=NULL, *q=NULL;
|
|
127 int len;
|
|
128
|
|
129 bzero (current, sizeof(current));
|
|
130
|
|
131 while (!current->text[0]) {
|
|
132 if (!fgets (line, 1000, fd)) return NULL;
|
269
|
133 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
|
258
|
134 current->start = a1*360000+a2*6000+a3*100+a4;
|
|
135 current->end = b1*360000+b2*6000+b3*100+b4;
|
|
136
|
|
137 if (!fgets (line, 1000, fd)) return NULL;
|
|
138
|
|
139 p=q=line;
|
|
140 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
|
|
141 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++);
|
|
142 current->text[current->lines-1]=(char *)malloc (len+1);
|
|
143 if (!current->text[current->lines-1]) return ERR;
|
|
144 strncpy (current->text[current->lines-1], q, len);
|
270
|
145 current->text[current->lines-1][len]='\0';
|
258
|
146 if (!*p || *p=='\r' || *p=='\n') break;
|
|
147 while (*p++!=']');
|
|
148 }
|
|
149 }
|
|
150 return current;
|
|
151 }
|
|
152
|
|
153 subtitle *sub_read_line_third(FILE *fd,subtitle *current) {
|
|
154 char line[1001];
|
|
155 int a1,a2,a3,a4,b1,b2,b3,b4;
|
|
156 char *p=NULL;
|
|
157 int i,len;
|
|
158
|
|
159 bzero (current, sizeof(current));
|
|
160
|
|
161 while (!current->text[0]) {
|
|
162 if (!fgets (line, 1000, fd)) return NULL;
|
269
|
163 if ((len=sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
|
258
|
164 continue;
|
|
165 current->start = a1*360000+a2*6000+a3*100+a4/10;
|
|
166 current->end = b1*360000+b2*6000+b3*100+b4/10;
|
|
167 for (i=0; i<SUB_MAX_TEXT;) {
|
269
|
168 if (!fgets (line, 1000, fd)) break;
|
258
|
169 len=0;
|
|
170 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
|
|
171 if (len) {
|
|
172 current->text[i]=(char *)malloc (len+1);
|
|
173 if (!current->text[i]) return ERR;
|
270
|
174 strncpy (current->text[i], line, len); current->text[i][len]='\0';
|
258
|
175 i++;
|
|
176 } else {
|
|
177 break;
|
|
178 }
|
|
179 }
|
|
180 current->lines=i;
|
|
181 }
|
|
182 return current;
|
|
183 }
|
|
184
|
|
185
|
|
186 int sub_autodetect (FILE *fd) {
|
|
187 char line[1001];
|
|
188 int i,j=0;
|
|
189 // char *p;
|
|
190
|
624
|
191 while (j < 100) {
|
258
|
192 j++;
|
|
193 if (!fgets (line, 1000, fd))
|
|
194 return -1;
|
|
195
|
624
|
196 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
|
258
|
197 {sub_uses_time=0;return 0;}
|
269
|
198 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
|
258
|
199 {sub_uses_time=1;return 1;}
|
269
|
200 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
|
258
|
201 {sub_uses_time=1;return 2;}
|
624
|
202 if (strstr (line, "<SAMI>"))
|
|
203 {sub_uses_time=0; return 3;}
|
258
|
204 }
|
624
|
205
|
|
206 return -1; // too many bad lines
|
258
|
207 }
|
|
208
|
|
209
|
|
210 subtitle* sub_read_file (char *filename) {
|
|
211 FILE *fd;
|
|
212 int n_max;
|
|
213 subtitle *first;
|
624
|
214 subtitle * (*func[4])(FILE *fd,subtitle *dest)=
|
258
|
215 {
|
|
216 sub_read_line_microdvd,
|
|
217 sub_read_line_subrip,
|
624
|
218 sub_read_line_third,
|
|
219 sub_read_line_sami
|
258
|
220 };
|
|
221
|
|
222 fd=fopen (filename, "r"); if (!fd) return NULL;
|
|
223
|
|
224 sub_format=sub_autodetect (fd);
|
|
225 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;}
|
624
|
226 printf ("SUB: Detected subtitle file format: %d\n",sub_format);
|
258
|
227
|
|
228 rewind (fd);
|
|
229
|
|
230 sub_num=0;n_max=32;
|
|
231 first=(subtitle *)malloc(n_max*sizeof(subtitle));
|
|
232 if(!first) return NULL;
|
|
233
|
|
234 while(1){
|
|
235 subtitle *sub;
|
|
236 if(sub_num>=n_max){
|
|
237 n_max+=16;
|
|
238 first=realloc(first,n_max*sizeof(subtitle));
|
|
239 }
|
|
240 sub=func[sub_format](fd,&first[sub_num]);
|
|
241 if(!sub) break; // EOF
|
|
242 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
|
|
243 }
|
|
244
|
|
245 fclose(fd);
|
|
246
|
|
247 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
|
269
|
248 printf ("SUB: Read %i subtitles", sub_num);
|
624
|
249 if (sub_errs) printf (", %i bad line(s).\n", sub_errs);
|
269
|
250 else printf (".\n");
|
258
|
251
|
|
252 return first;
|
|
253 }
|
|
254
|
509
|
255 char * strreplace( char * in,char * what,char * whereof )
|
|
256 {
|
|
257 int i;
|
|
258 char * tmp;
|
|
259
|
|
260 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
|
|
261 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
|
|
262 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
|
|
263 return in;
|
|
264 }
|
|
265
|
|
266 char * sub_filename( char * fname )
|
|
267 {
|
|
268 char * sub_name = NULL;
|
|
269 char * sub_tmp = NULL;
|
|
270 int i;
|
|
271 #define SUB_EXTS 4
|
|
272 char * sub_exts[SUB_EXTS] =
|
|
273 { ".sub",
|
|
274 ".SUB",
|
|
275 ".srt",
|
|
276 ".SRT" };
|
|
277
|
|
278 if ( fname == NULL ) return NULL;
|
|
279 for( i=strlen( fname );i>0;i-- )
|
|
280 if( fname[i] == '.' )
|
|
281 {
|
|
282 sub_tmp=(char *)&fname[i];
|
|
283 break;
|
|
284 }
|
|
285 if ( i == 0 ) return NULL;
|
|
286 sub_name=strdup( fname );
|
|
287 for ( i=0;i<SUB_EXTS;i++ )
|
|
288 {
|
|
289 FILE * f;
|
|
290
|
|
291 strcpy( sub_name,fname );
|
|
292 f=fopen( strreplace( sub_name,sub_tmp,sub_exts[i] ),"rt" );
|
|
293 if ( f != NULL )
|
|
294 {
|
|
295 fclose( f );
|
|
296 printf( "SUB: Detected sub file: %s\n",sub_name );
|
|
297 return sub_name;
|
|
298 }
|
|
299 }
|
|
300 return NULL;
|
|
301 }
|
|
302
|
625
|
303 #if 0
|
258
|
304 int main(int argc, char **argv) { // for testing
|
|
305
|
|
306 int i,j;
|
|
307 subtitle *subs;
|
|
308 subtitle *egysub;
|
|
309
|
|
310 if(argc<2){
|
|
311 printf("\nUsage: subreader filename.sub\n\n");
|
|
312 exit(1);
|
|
313 }
|
|
314
|
624
|
315 subs=sub_read_file(argv[1]);
|
258
|
316 if(!subs){
|
|
317 printf("Couldn't load file... let's write a bugreport :)\n");
|
|
318 exit(1);
|
|
319 }
|
|
320
|
|
321 for(j=0;j<sub_num;j++){
|
|
322 egysub=&subs[j];
|
|
323 printf ("%i line%c (%i-%i) ",
|
|
324 egysub->lines,
|
|
325 (1==egysub->lines)?' ':'s',
|
|
326 egysub->start,
|
|
327 egysub->end);
|
|
328 for (i=0; i<egysub->lines; i++) {
|
|
329 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> ");
|
|
330 }
|
|
331 printf ("\n");
|
|
332 }
|
|
333
|
|
334 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
|
|
335 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
|
|
336 return 0;
|
|
337 }
|
625
|
338 #endif |