comparison subreader.c @ 2151:a9d91476085a

modifications to use iconv(3) function to recode text of subs (autodetect) added option -subcp <cpname> # for example cp1250, latin2 etc.
author atlka
date Wed, 10 Oct 2001 13:07:42 +0000
parents fa62c11857e8
children f723e4e8f4fd
comparison
equal deleted inserted replaced
2150:93e0ed86705e 2151:a9d91476085a
10 #include <stdio.h> 10 #include <stdio.h>
11 #include <stdlib.h> 11 #include <stdlib.h>
12 #include <string.h> 12 #include <string.h>
13 #include <ctype.h> 13 #include <ctype.h>
14 14
15 #include "config.h"
15 #include "subreader.h" 16 #include "subreader.h"
16 17
17 #define ERR (void *)-1 18 #define ERR (void *)-1
18 19
20 #ifdef USE_ICONV
21 #include <iconv.h>
22 char *sub_cp=NULL;
23 #endif
19 24
20 int sub_uses_time=0; 25 int sub_uses_time=0;
21 int sub_errs=0; 26 int sub_errs=0;
22 int sub_num=0; // number of subtitle structs 27 int sub_num=0; // number of subtitle structs
23 int sub_format=-1; // 0 for microdvd 28 int sub_format=-1; // 0 for microdvd
397 {sub_uses_time=0;return 7;} 402 {sub_uses_time=0;return 7;}
398 } 403 }
399 404
400 return -1; // too many bad lines 405 return -1; // too many bad lines
401 } 406 }
402 407
408 extern int sub_utf8;
409
410 #ifdef USE_ICONV
411 static iconv_t icdsc;
412
413 void subcp_open (void)
414 {
415 char *tocp = "UTF-8";
416 icdsc = (iconv_t)(-1);
417 if (sub_cp){
418 if ((icdsc = iconv_open (tocp, sub_cp)) != (iconv_t)(-1)){
419 printf ("SUB: opened iconv descriptor.\n");
420 sub_utf8 = 2;
421 } else
422 printf ("SUB: error opening iconv descriptor.\n");
423 }
424 }
425
426 void subcp_close (void)
427 {
428 if (icdsc != (iconv_t)(-1)){
429 (void) iconv_close (icdsc);
430 printf ("SUB: closed iconv descriptor.\n");
431 }
432 }
433
434 #define ICBUFFSIZE 512
435 static char icbuffer[ICBUFFSIZE];
436
437 subtitle* subcp_recode (subtitle *sub)
438 {
439 int l=sub->lines;
440 size_t ileft, oleft, otlen;
441 char *op, *ip, *ot;
442
443 while (l){
444 op = icbuffer;
445 ip = sub->text[--l];
446 ileft = strlen(ip);
447 oleft = ICBUFFSIZE - 1;
448
449 if (iconv(icdsc, (const char **) &ip, &ileft,
450 &op, &oleft) == (size_t)(-1)) {
451 printf ("SUB: error recoding line.\n");
452 l++;
453 break;
454 }
455 if (!(ot = (char *)malloc(op - icbuffer + 1))){
456 printf ("SUB: error allocating mem.\n");
457 l++;
458 break;
459 }
460 *op='\0' ;
461 strcpy (ot, icbuffer);
462 free (sub->text[l]);
463 sub->text[l] = ot;
464 }
465 if (l){
466 for (l = sub->lines; l;)
467 free (sub->text[--l]);
468 return ERR;
469 }
470 return sub;
471 }
472
473 #endif
403 474
404 subtitle* sub_read_file (char *filename) { 475 subtitle* sub_read_file (char *filename) {
405 FILE *fd; 476 FILE *fd;
406 int n_max; 477 int n_max;
407 subtitle *first; 478 subtitle *first;
423 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;} 494 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;}
424 printf ("SUB: Detected subtitle file format: %d\n",sub_format); 495 printf ("SUB: Detected subtitle file format: %d\n",sub_format);
425 496
426 rewind (fd); 497 rewind (fd);
427 498
499 #ifdef USE_ICONV
500 subcp_open();
501 #endif
502
428 sub_num=0;n_max=32; 503 sub_num=0;n_max=32;
429 first=(subtitle *)malloc(n_max*sizeof(subtitle)); 504 first=(subtitle *)malloc(n_max*sizeof(subtitle));
430 if(!first) return NULL; 505 if(!first) return NULL;
431 506
432 while(1){ 507 while(1){
435 n_max+=16; 510 n_max+=16;
436 first=realloc(first,n_max*sizeof(subtitle)); 511 first=realloc(first,n_max*sizeof(subtitle));
437 } 512 }
438 sub=func[sub_format](fd,&first[sub_num]); 513 sub=func[sub_format](fd,&first[sub_num]);
439 if(!sub) break; // EOF 514 if(!sub) break; // EOF
515 #ifdef USE_ICONV
516 if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub);
517 #endif
440 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid 518 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
441 } 519 }
442 520
443 fclose(fd); 521 fclose(fd);
522
523 #ifdef USE_ICONV
524 subcp_close();
525 #endif
444 526
445 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); 527 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
446 printf ("SUB: Read %i subtitles", sub_num); 528 printf ("SUB: Read %i subtitles", sub_num);
447 if (sub_errs) printf (", %i bad line(s).\n", sub_errs); 529 if (sub_errs) printf (", %i bad line(s).\n", sub_errs);
448 else printf (".\n"); 530 else printf (".\n");
463 } 545 }
464 #endif 546 #endif
465 547
466 char * sub_filename(char* path, char * fname ) 548 char * sub_filename(char* path, char * fname )
467 { 549 {
468 extern int sub_utf8;
469 char * sub_name1; 550 char * sub_name1;
470 char * sub_name2; 551 char * sub_name2;
471 char * aviptr1, * aviptr2, * tmp; 552 char * aviptr1, * aviptr2, * tmp;
472 int i,j; 553 int i,j;
473 FILE * f; 554 FILE * f;
507 aviptr1=strrchr(sub_name1,'.'); 588 aviptr1=strrchr(sub_name1,'.');
508 aviptr2=strrchr(sub_name2,'.'); 589 aviptr2=strrchr(sub_name2,'.');
509 590
510 for(j=0;j<=1;j++){ 591 for(j=0;j<=1;j++){
511 char* sub_name=j?sub_name1:sub_name2; 592 char* sub_name=j?sub_name1:sub_name2;
593 #ifdef USE_ICONV
594 for ( i=(sub_cp?2:0);i<(sizeof(sub_exts)/sizeof(char*));i++ ) {
595 #else
512 for ( i=0;i<(sizeof(sub_exts)/sizeof(char*));i++ ) { 596 for ( i=0;i<(sizeof(sub_exts)/sizeof(char*));i++ ) {
597 #endif
513 strcpy(j?aviptr1:aviptr2,sub_exts[i]); 598 strcpy(j?aviptr1:aviptr2,sub_exts[i]);
514 // printf("trying: '%s'\n",sub_name); 599 // printf("trying: '%s'\n",sub_name);
515 if((f=fopen( sub_name,"rt" ))) { 600 if((f=fopen( sub_name,"rt" ))) {
516 fclose( f ); 601 fclose( f );
517 printf( "SUB: Detected sub file: %s\n",sub_name ); 602 printf( "SUB: Detected sub file: %s\n",sub_name );