comparison subreader.c @ 1081:48cd2c5a9542

new sub format...
author szabii
date Sat, 09 Jun 2001 20:09:18 +0000
parents 0b1b0bac67e9
children d40f2b686846
comparison
equal deleted inserted replaced
1080:36b883428aeb 1081:48cd2c5a9542
1 /* 1 /*
2 * Subtitle reader with format autodetection 2 * Subtitle reader with format autodetection
3 * 3 *
4 * Written by laaz 4 * Written by laaz
5 * Some code cleanup & realloc() by A'rpi/ESP-team 5 * Some code cleanup & realloc() by A'rpi/ESP-team
6 * dunnowhat sub format by szabi
6 */ 7 */
7 8
8 9
9 #include <stdio.h> 10 #include <stdio.h>
10 #include <stdlib.h> 11 #include <stdlib.h>
24 // 2 for SubViewer 25 // 2 for SubViewer
25 // 3 for SAMI (smi) 26 // 3 for SAMI (smi)
26 // 4 for vplayer format 27 // 4 for vplayer format
27 // 5 for RT format 28 // 5 for RT format
28 // 6 for ssa (Sub Station Alpha) 29 // 6 for ssa (Sub Station Alpha)
30 // 7 for ... erm ... dunnowhat. tell me if you know
29 31
30 int eol(char p) { 32 int eol(char p) {
31 return (p=='\r' || p=='\n' || p=='\0'); 33 return (p=='\r' || p=='\n' || p=='\0');
32 } 34 }
33 35
141 } while (sscanf (line, "{%ld}{%ld}%[^\r\n]", &(current->start), &(current->end),line2) <3); 143 } while (sscanf (line, "{%ld}{%ld}%[^\r\n]", &(current->start), &(current->end),line2) <3);
142 144
143 p=line2; 145 p=line2;
144 146
145 next=p, i=0; 147 next=p, i=0;
146 while (next =sub_readtext (next, &(current->text[i]))) { 148 while ((next =sub_readtext (next, &(current->text[i])))) {
147 if (current->text[i]==ERR) {return ERR;} 149 if (current->text[i]==ERR) {return ERR;}
148 i++; 150 i++;
149 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;} 151 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;}
150 } 152 }
151 current->lines= ++i; 153 current->lines= ++i;
152 154
153 return current; 155 return current;
154 } 156 }
217 219
218 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) { 220 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) {
219 char line[1001]; 221 char line[1001];
220 char line2[1001]; 222 char line2[1001];
221 int a1,a2,a3,b1,b2,b3; 223 int a1,a2,a3,b1,b2,b3;
222 int setime,etime; 224 char *p=NULL, *next;
223 char *p=NULL, *q=NULL, *l=NULL,*next;
224 int i,len,len2,plen; 225 int i,len,len2,plen;
225 226
226 bzero (current, sizeof(current)); 227 bzero (current, sizeof(current));
227 228
228 while (!current->text[0]) { 229 while (!current->text[0]) {
244 // 245 //
245 next = p,i=0; 246 next = p,i=0;
246 while ((next =sub_readtext (next, &(current->text[i])))) { 247 while ((next =sub_readtext (next, &(current->text[i])))) {
247 if (current->text[i]==ERR) {return ERR;} 248 if (current->text[i]==ERR) {return ERR;}
248 i++; 249 i++;
249 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;} 250 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;}
250 } 251 }
251 current->lines=i+1; 252 current->lines=i+1;
252 } 253 }
253 } 254 }
254 return current; 255 return current;
285 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml? 286 // 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 next = strstr(line,"<clear/>")+8;i=0;
287 while ((next =sub_readtext (next, &(current->text[i])))) { 288 while ((next =sub_readtext (next, &(current->text[i])))) {
288 if (current->text[i]==ERR) {return ERR;} 289 if (current->text[i]==ERR) {return ERR;}
289 i++; 290 i++;
290 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;} 291 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;}
291 } 292 }
292 current->lines=i+1; 293 current->lines=i+1;
293 } 294 }
294 return current; 295 return current;
295 } 296 }
314 strcpy(current->text[0],line2); 315 strcpy(current->text[0],line2);
315 316
316 return current; 317 return current;
317 } 318 }
318 319
320 subtitle *sub_read_line_dunnowhat(FILE *fd,subtitle *current) {
321 char line[1001];
322 char text[1001];
323
324 bzero (current, sizeof(current));
325
326 if (!fgets (line, 1000, fd))
327 return NULL;
328 if (sscanf (line, "%ld,%ld,\"%[^\"]", &(current->start),
329 &(current->end), text) <3)
330 return ERR;
331 current->text[0] = strdup(text);
332 current->lines = 1;
333
334 return current;
335 }
336
319 int sub_autodetect (FILE *fd) { 337 int sub_autodetect (FILE *fd) {
320 char line[1001]; 338 char line[1001];
321 int i,j=0; 339 int i,j=0;
322 // char *p; 340 // char *p;
323 341
345 363
346 // I have only seen only 1 piece of .ssa file. 364 // I have only seen only 1 piece of .ssa file.
347 // It may be not correct (tell me if it's not) 365 // It may be not correct (tell me if it's not)
348 if (!memcmp(line, "Dialogue: Marked", 16)) 366 if (!memcmp(line, "Dialogue: Marked", 16))
349 {sub_uses_time=1; return 6;} 367 {sub_uses_time=1; return 6;}
368 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3)
369 {sub_uses_time=0;return 7;}
350 } 370 }
351 371
352 return -1; // too many bad lines 372 return -1; // too many bad lines
353 } 373 }
354 374
355 375
356 subtitle* sub_read_file (char *filename) { 376 subtitle* sub_read_file (char *filename) {
357 FILE *fd; 377 FILE *fd;
358 int n_max; 378 int n_max;
359 subtitle *first; 379 subtitle *first;
360 subtitle * (*func[7])(FILE *fd,subtitle *dest)= 380 subtitle * (*func[])(FILE *fd,subtitle *dest)=
361 { 381 {
362 sub_read_line_microdvd, 382 sub_read_line_microdvd,
363 sub_read_line_subrip, 383 sub_read_line_subrip,
364 sub_read_line_third, 384 sub_read_line_third,
365 sub_read_line_sami, 385 sub_read_line_sami,
366 sub_read_line_vplayer, 386 sub_read_line_vplayer,
367 sub_read_line_rt, 387 sub_read_line_rt,
368 sub_read_line_ssa 388 sub_read_line_ssa,
389 sub_read_line_dunnowhat
369 }; 390 };
370 391
371 fd=fopen (filename, "r"); if (!fd) return NULL; 392 fd=fopen (filename, "r"); if (!fd) return NULL;
372 393
373 sub_format=sub_autodetect (fd); 394 sub_format=sub_autodetect (fd);
430 ".smi", 451 ".smi",
431 ".SMI", 452 ".SMI",
432 ".rt", 453 ".rt",
433 ".RT", 454 ".RT",
434 ".txt", 455 ".txt",
435 ".TXT"}; 456 ".TXT",
457 ".ssa",
458 ".SSA"};
436 459
437 460
438 if ( fname == NULL ) return NULL; 461 if ( fname == NULL ) return NULL;
439 462
440 sub_name1=strrchr(fname,'.'); 463 sub_name1=strrchr(fname,'.');
443 466
444 sub_name1=malloc(strlen(fname)+8); 467 sub_name1=malloc(strlen(fname)+8);
445 strcpy(sub_name1,fname); 468 strcpy(sub_name1,fname);
446 469
447 sub_name2=malloc (strlen(path) + strlen(fname) + 8); 470 sub_name2=malloc (strlen(path) + strlen(fname) + 8);
448 if (tmp=strrchr(fname,'/')) 471 if ((tmp=strrchr(fname,'/')))
449 sprintf (sub_name2, "%s%s", path, tmp+1); 472 sprintf (sub_name2, "%s%s", path, tmp+1);
450 else 473 else
451 sprintf (sub_name2, "%s%s", path, fname); 474 sprintf (sub_name2, "%s%s", path, fname);
452 475
453 aviptr1=strrchr(sub_name1,'.'); 476 aviptr1=strrchr(sub_name1,'.');
487 exit(1); 510 exit(1);
488 } 511 }
489 512
490 for(j=0;j<sub_num;j++){ 513 for(j=0;j<sub_num;j++){
491 egysub=&subs[j]; 514 egysub=&subs[j];
492 printf ("%i line%c (%i-%i) ", 515 printf ("%i line%c (%li-%li) ",
493 egysub->lines, 516 egysub->lines,
494 (1==egysub->lines)?' ':'s', 517 (1==egysub->lines)?' ':'s',
495 egysub->start, 518 egysub->start,
496 egysub->end); 519 egysub->end);
497 for (i=0; i<egysub->lines; i++) { 520 for (i=0; i<egysub->lines; i++) {