comparison subreader.c @ 624:44b764d348a0

added support for SAMI subtitle support (.smi)
author laaz
date Tue, 24 Apr 2001 21:39:18 +0000
parents abd7391a31e3
children 2f321fe55bdb
comparison
equal deleted inserted replaced
623:950e2d2613e3 624:44b764d348a0
15 #define ERR (void *)-1 15 #define ERR (void *)-1
16 16
17 17
18 int sub_uses_time=0; 18 int sub_uses_time=0;
19 int sub_errs=0; 19 int sub_errs=0;
20 int sub_num=0; // number of subtitle structs 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 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", &current->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", &current->end);
69 if (!strstr (s, "<P><br>")) return ERR;
70 } else return ERR;
71
72 return current;
73 }
22 74
23 75
24 char *sub_readtext(char *source, char **dest) { 76 char *sub_readtext(char *source, char **dest) {
25 int len=0; 77 int len=0;
26 char *p; 78 char *p;
36 while (*p=='\r' || *p=='\n' || *p=='|') p++; 88 while (*p=='\r' || *p=='\n' || *p=='|') p++;
37 89
38 if (*p) return p; // not-last text field 90 if (*p) return p; // not-last text field
39 else return NULL; // last text field 91 else return NULL; // last text field
40 } 92 }
41
42
43 93
44 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) { 94 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
45 char line[1001]; 95 char line[1001];
46 char line2[1001]; 96 char line2[1001];
47 char *p, *next; 97 char *p, *next;
136 int sub_autodetect (FILE *fd) { 186 int sub_autodetect (FILE *fd) {
137 char line[1001]; 187 char line[1001];
138 int i,j=0; 188 int i,j=0;
139 // char *p; 189 // char *p;
140 190
141 while (1) { 191 while (j < 100) {
142 j++; 192 j++;
143 if (!fgets (line, 1000, fd)) 193 if (!fgets (line, 1000, fd))
144 return -1; 194 return -1;
145 195
146 // if (sscanf (line, "{%i}{%i}", &i, &i, p)==2) // ha valaki tudja miert 2, mondja mar el nekem ;) 196 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
147 if (sscanf (line, "{%d}{%d}", &i, &i)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
148 {sub_uses_time=0;return 0;} 197 {sub_uses_time=0;return 0;}
149 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) 198 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
150 {sub_uses_time=1;return 1;} 199 {sub_uses_time=1;return 1;}
151 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) 200 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
152 {sub_uses_time=1;return 2;} 201 {sub_uses_time=1;return 2;}
153 if (j>100) return -1; // too many bad lines or bad coder 202 if (strstr (line, "<SAMI>"))
154 } 203 {sub_uses_time=0; return 3;}
204 }
205
206 return -1; // too many bad lines
155 } 207 }
156 208
157 209
158 subtitle* sub_read_file (char *filename) { 210 subtitle* sub_read_file (char *filename) {
159 FILE *fd; 211 FILE *fd;
160 int n_max; 212 int n_max;
161 subtitle *first; 213 subtitle *first;
162 subtitle * (*func[3])(FILE *fd,subtitle *dest)= 214 subtitle * (*func[4])(FILE *fd,subtitle *dest)=
163 { 215 {
164 sub_read_line_microdvd, 216 sub_read_line_microdvd,
165 sub_read_line_subrip, 217 sub_read_line_subrip,
166 sub_read_line_third 218 sub_read_line_third,
219 sub_read_line_sami
167 }; 220 };
168 221
169 fd=fopen (filename, "r"); if (!fd) return NULL; 222 fd=fopen (filename, "r"); if (!fd) return NULL;
170 223
171 sub_format=sub_autodetect (fd); 224 sub_format=sub_autodetect (fd);
172 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;} 225 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); 226 printf ("SUB: Detected subtitle file format: %d\n",sub_format);
174 227
175 rewind (fd); 228 rewind (fd);
176 229
177 sub_num=0;n_max=32; 230 sub_num=0;n_max=32;
178 first=(subtitle *)malloc(n_max*sizeof(subtitle)); 231 first=(subtitle *)malloc(n_max*sizeof(subtitle));
191 244
192 fclose(fd); 245 fclose(fd);
193 246
194 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); 247 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
195 printf ("SUB: Read %i subtitles", sub_num); 248 printf ("SUB: Read %i subtitles", sub_num);
196 if (sub_errs) printf (", %i error(s).\n", sub_errs); 249 if (sub_errs) printf (", %i bad line(s).\n", sub_errs);
197 else printf (".\n"); 250 else printf (".\n");
198 251
199 return first; 252 return first;
200 } 253 }
201 254
245 } 298 }
246 } 299 }
247 return NULL; 300 return NULL;
248 } 301 }
249 302
250 #if 0
251 int main(int argc, char **argv) { // for testing 303 int main(int argc, char **argv) { // for testing
252 304
253 int i,j; 305 int i,j;
254 subtitle *subs; 306 subtitle *subs;
255 subtitle *egysub; 307 subtitle *egysub;
257 if(argc<2){ 309 if(argc<2){
258 printf("\nUsage: subreader filename.sub\n\n"); 310 printf("\nUsage: subreader filename.sub\n\n");
259 exit(1); 311 exit(1);
260 } 312 }
261 313
262 subs=sub_get_subtitles(argv[1]); 314 subs=sub_read_file(argv[1]);
263 if(!subs){ 315 if(!subs){
264 printf("Couldn't load file... let's write a bugreport :)\n"); 316 printf("Couldn't load file... let's write a bugreport :)\n");
265 exit(1); 317 exit(1);
266 } 318 }
267 319
280 332
281 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); 333 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
282 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs); 334 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
283 return 0; 335 return 0;
284 } 336 }
285 #endif