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;
|
|
20 int sub_num=0; // number of subtitle structs
|
|
21 int sub_format=-1; // 0 for microdvd, 1 for SubRip, 2 for the third format
|
|
22
|
|
23
|
|
24 char *sub_readtext(char *source, char **dest) {
|
|
25 int len=0;
|
|
26 char *p;
|
|
27
|
|
28 for (p=source;*p!='\r' && *p!='\n' && *p!='|'; p++,len++);
|
|
29
|
|
30 *dest= (char *)malloc (len+1);
|
|
31 if (!dest) {return ERR;}
|
|
32
|
|
33 strncpy(*dest, source, len);
|
|
34 (*dest)[len]=0;
|
|
35
|
|
36 while (*p=='\r' || *p=='\n' || *p=='|') p++;
|
|
37
|
|
38 if (*p) return p; // not-last text field
|
|
39 else return NULL; // last text field
|
|
40 }
|
|
41
|
|
42
|
|
43
|
|
44 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
|
|
45 char line[1001];
|
|
46 char line2[1001];
|
|
47 char *p, *next;
|
|
48 int i;
|
|
49
|
|
50 bzero (current, sizeof(current));
|
|
51
|
|
52 do {
|
|
53 if (!fgets (line, 1000, fd)) return NULL;
|
|
54 } while (*line=='\n' || *line == '\r' || !*line);
|
|
55
|
269
|
56 if (sscanf (line, "{%d}{%d}%s", &(current->start), &(current->end),line2) <2) {return ERR;}
|
258
|
57
|
|
58 p=line;
|
|
59 while (*p++!='}');
|
|
60 while (*p++!='}');
|
|
61
|
|
62 next=p, i=0;
|
|
63 while ((next =sub_readtext (next, &(current->text[i])))) {
|
270
|
64 if (current->text[i]==ERR) {return ERR;}
|
258
|
65 i++;
|
270
|
66 if (i>SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;}
|
258
|
67 }
|
|
68 current->lines=i+1;
|
|
69
|
|
70 return current;
|
|
71 }
|
|
72
|
|
73 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
|
|
74 char line[1001];
|
|
75 int a1,a2,a3,a4,b1,b2,b3,b4;
|
|
76 char *p=NULL, *q=NULL;
|
|
77 int len;
|
|
78
|
|
79 bzero (current, sizeof(current));
|
|
80
|
|
81 while (!current->text[0]) {
|
|
82 if (!fgets (line, 1000, fd)) return NULL;
|
269
|
83 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
|
258
|
84 current->start = a1*360000+a2*6000+a3*100+a4;
|
|
85 current->end = b1*360000+b2*6000+b3*100+b4;
|
|
86
|
|
87 if (!fgets (line, 1000, fd)) return NULL;
|
|
88
|
|
89 p=q=line;
|
|
90 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
|
|
91 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++);
|
|
92 current->text[current->lines-1]=(char *)malloc (len+1);
|
|
93 if (!current->text[current->lines-1]) return ERR;
|
|
94 strncpy (current->text[current->lines-1], q, len);
|
270
|
95 current->text[current->lines-1][len]='\0';
|
258
|
96 if (!*p || *p=='\r' || *p=='\n') break;
|
|
97 while (*p++!=']');
|
|
98 }
|
|
99 }
|
|
100 return current;
|
|
101 }
|
|
102
|
|
103 subtitle *sub_read_line_third(FILE *fd,subtitle *current) {
|
|
104 char line[1001];
|
|
105 int a1,a2,a3,a4,b1,b2,b3,b4;
|
|
106 char *p=NULL;
|
|
107 int i,len;
|
|
108
|
|
109 bzero (current, sizeof(current));
|
|
110
|
|
111 while (!current->text[0]) {
|
|
112 if (!fgets (line, 1000, fd)) return NULL;
|
269
|
113 if ((len=sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
|
258
|
114 continue;
|
|
115 current->start = a1*360000+a2*6000+a3*100+a4/10;
|
|
116 current->end = b1*360000+b2*6000+b3*100+b4/10;
|
|
117 for (i=0; i<SUB_MAX_TEXT;) {
|
269
|
118 if (!fgets (line, 1000, fd)) break;
|
258
|
119 len=0;
|
|
120 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
|
|
121 if (len) {
|
|
122 current->text[i]=(char *)malloc (len+1);
|
|
123 if (!current->text[i]) return ERR;
|
270
|
124 strncpy (current->text[i], line, len); current->text[i][len]='\0';
|
258
|
125 i++;
|
|
126 } else {
|
|
127 break;
|
|
128 }
|
|
129 }
|
|
130 current->lines=i;
|
|
131 }
|
|
132 return current;
|
|
133 }
|
|
134
|
|
135
|
|
136 int sub_autodetect (FILE *fd) {
|
|
137 char line[1001];
|
|
138 int i,j=0;
|
|
139 // char *p;
|
|
140
|
|
141 while (1) {
|
|
142 j++;
|
|
143 if (!fgets (line, 1000, fd))
|
|
144 return -1;
|
|
145
|
|
146 // if (sscanf (line, "{%i}{%i}", &i, &i, p)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
|
269
|
147 if (sscanf (line, "{%d}{%d}", &i, &i)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
|
258
|
148 {sub_uses_time=0;return 0;}
|
269
|
149 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
|
258
|
150 {sub_uses_time=1;return 1;}
|
269
|
151 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
|
258
|
152 {sub_uses_time=1;return 2;}
|
|
153 if (j>100) return -1; // too many bad lines or bad coder
|
|
154 }
|
|
155 }
|
|
156
|
|
157
|
|
158 subtitle* sub_read_file (char *filename) {
|
|
159 FILE *fd;
|
|
160 int n_max;
|
|
161 subtitle *first;
|
|
162 subtitle * (*func[3])(FILE *fd,subtitle *dest)=
|
|
163 {
|
|
164 sub_read_line_microdvd,
|
|
165 sub_read_line_subrip,
|
|
166 sub_read_line_third
|
|
167 };
|
|
168
|
|
169 fd=fopen (filename, "r"); if (!fd) return NULL;
|
|
170
|
|
171 sub_format=sub_autodetect (fd);
|
|
172 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;}
|
|
173 printf ("SUB: Detected subtitle file format: %i\n",sub_format);
|
|
174
|
|
175 rewind (fd);
|
|
176
|
|
177 sub_num=0;n_max=32;
|
|
178 first=(subtitle *)malloc(n_max*sizeof(subtitle));
|
|
179 if(!first) return NULL;
|
|
180
|
|
181 while(1){
|
|
182 subtitle *sub;
|
|
183 if(sub_num>=n_max){
|
|
184 n_max+=16;
|
|
185 first=realloc(first,n_max*sizeof(subtitle));
|
|
186 }
|
|
187 sub=func[sub_format](fd,&first[sub_num]);
|
|
188 if(!sub) break; // EOF
|
|
189 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
|
|
190 }
|
|
191
|
|
192 fclose(fd);
|
|
193
|
|
194 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
|
269
|
195 printf ("SUB: Read %i subtitles", sub_num);
|
|
196 if (sub_errs) printf (", %i error(s).\n", sub_errs);
|
|
197 else printf (".\n");
|
258
|
198
|
|
199 return first;
|
|
200 }
|
|
201
|
|
202 #if 0
|
|
203 int main(int argc, char **argv) { // for testing
|
|
204
|
|
205 int i,j;
|
|
206 subtitle *subs;
|
|
207 subtitle *egysub;
|
|
208
|
|
209 if(argc<2){
|
|
210 printf("\nUsage: subreader filename.sub\n\n");
|
|
211 exit(1);
|
|
212 }
|
|
213
|
|
214 subs=sub_get_subtitles(argv[1]);
|
|
215 if(!subs){
|
|
216 printf("Couldn't load file... let's write a bugreport :)\n");
|
|
217 exit(1);
|
|
218 }
|
|
219
|
|
220 for(j=0;j<sub_num;j++){
|
|
221 egysub=&subs[j];
|
|
222 printf ("%i line%c (%i-%i) ",
|
|
223 egysub->lines,
|
|
224 (1==egysub->lines)?' ':'s',
|
|
225 egysub->start,
|
|
226 egysub->end);
|
|
227 for (i=0; i<egysub->lines; i++) {
|
|
228 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> ");
|
|
229 }
|
|
230 printf ("\n");
|
|
231 }
|
|
232
|
|
233 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
|
|
234 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
|
|
235 return 0;
|
|
236 }
|
|
237 #endif
|