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])))) {
|
|
64 if (current->text[i]==ERR || current->text[i]==ERR) {return ERR;}
|
|
65 i++;
|
|
66 if (i>SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");return ERR;}
|
|
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);
|
|
95 if (!*p || *p=='\r' || *p=='\n') break;
|
|
96 while (*p++!=']');
|
|
97 }
|
|
98 }
|
|
99 return current;
|
|
100 }
|
|
101
|
|
102 subtitle *sub_read_line_third(FILE *fd,subtitle *current) {
|
|
103 char line[1001];
|
|
104 int a1,a2,a3,a4,b1,b2,b3,b4;
|
|
105 char *p=NULL;
|
|
106 int i,len;
|
|
107
|
|
108 bzero (current, sizeof(current));
|
|
109
|
|
110 while (!current->text[0]) {
|
|
111 if (!fgets (line, 1000, fd)) return NULL;
|
269
|
112 if ((len=sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
|
258
|
113 continue;
|
|
114 current->start = a1*360000+a2*6000+a3*100+a4/10;
|
|
115 current->end = b1*360000+b2*6000+b3*100+b4/10;
|
|
116 for (i=0; i<SUB_MAX_TEXT;) {
|
269
|
117 if (!fgets (line, 1000, fd)) break;
|
258
|
118 len=0;
|
|
119 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
|
|
120 if (len) {
|
|
121 current->text[i]=(char *)malloc (len+1);
|
|
122 if (!current->text[i]) return ERR;
|
|
123 strncpy (current->text[i], line, len);
|
|
124 i++;
|
|
125 } else {
|
|
126 break;
|
|
127 }
|
|
128 }
|
|
129 current->lines=i;
|
|
130 }
|
|
131 return current;
|
|
132 }
|
|
133
|
|
134
|
|
135 int sub_autodetect (FILE *fd) {
|
|
136 char line[1001];
|
|
137 int i,j=0;
|
|
138 // char *p;
|
|
139
|
|
140 while (1) {
|
|
141 j++;
|
|
142 if (!fgets (line, 1000, fd))
|
|
143 return -1;
|
|
144
|
|
145 // if (sscanf (line, "{%i}{%i}", &i, &i, p)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
|
269
|
146 if (sscanf (line, "{%d}{%d}", &i, &i)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
|
258
|
147 {sub_uses_time=0;return 0;}
|
269
|
148 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
|
258
|
149 {sub_uses_time=1;return 1;}
|
269
|
150 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
|
258
|
151 {sub_uses_time=1;return 2;}
|
|
152 if (j>100) return -1; // too many bad lines or bad coder
|
|
153 }
|
|
154 }
|
|
155
|
|
156
|
|
157 subtitle* sub_read_file (char *filename) {
|
|
158 FILE *fd;
|
|
159 int n_max;
|
|
160 subtitle *first;
|
|
161 subtitle * (*func[3])(FILE *fd,subtitle *dest)=
|
|
162 {
|
|
163 sub_read_line_microdvd,
|
|
164 sub_read_line_subrip,
|
|
165 sub_read_line_third
|
|
166 };
|
|
167
|
|
168 fd=fopen (filename, "r"); if (!fd) return NULL;
|
|
169
|
|
170 sub_format=sub_autodetect (fd);
|
|
171 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;}
|
|
172 printf ("SUB: Detected subtitle file format: %i\n",sub_format);
|
|
173
|
|
174 rewind (fd);
|
|
175
|
|
176 sub_num=0;n_max=32;
|
|
177 first=(subtitle *)malloc(n_max*sizeof(subtitle));
|
|
178 if(!first) return NULL;
|
|
179
|
|
180 while(1){
|
|
181 subtitle *sub;
|
|
182 if(sub_num>=n_max){
|
|
183 n_max+=16;
|
|
184 first=realloc(first,n_max*sizeof(subtitle));
|
|
185 }
|
|
186 sub=func[sub_format](fd,&first[sub_num]);
|
|
187 if(!sub) break; // EOF
|
|
188 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
|
|
189 }
|
|
190
|
|
191 fclose(fd);
|
|
192
|
|
193 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
|
269
|
194 printf ("SUB: Read %i subtitles", sub_num);
|
|
195 if (sub_errs) printf (", %i error(s).\n", sub_errs);
|
|
196 else printf (".\n");
|
258
|
197
|
|
198 return first;
|
|
199 }
|
|
200
|
|
201 #if 0
|
|
202 int main(int argc, char **argv) { // for testing
|
|
203
|
|
204 int i,j;
|
|
205 subtitle *subs;
|
|
206 subtitle *egysub;
|
|
207
|
|
208 if(argc<2){
|
|
209 printf("\nUsage: subreader filename.sub\n\n");
|
|
210 exit(1);
|
|
211 }
|
|
212
|
|
213 subs=sub_get_subtitles(argv[1]);
|
|
214 if(!subs){
|
|
215 printf("Couldn't load file... let's write a bugreport :)\n");
|
|
216 exit(1);
|
|
217 }
|
|
218
|
|
219 for(j=0;j<sub_num;j++){
|
|
220 egysub=&subs[j];
|
|
221 printf ("%i line%c (%i-%i) ",
|
|
222 egysub->lines,
|
|
223 (1==egysub->lines)?' ':'s',
|
|
224 egysub->start,
|
|
225 egysub->end);
|
|
226 for (i=0; i<egysub->lines; i++) {
|
|
227 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> ");
|
|
228 }
|
|
229 printf ("\n");
|
|
230 }
|
|
231
|
|
232 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
|
|
233 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
|
|
234 return 0;
|
|
235 }
|
|
236 #endif
|