? mpconf Index: cfg-common.h =================================================================== RCS file: /cvsroot/mplayer/main/cfg-common.h,v retrieving revision 1.82 diff -u -b -B -r1.82 cfg-common.h --- cfg-common.h 27 Jan 2003 23:41:38 -0000 1.82 +++ cfg-common.h 3 Feb 2003 08:46:36 -0000 @@ -170,6 +170,7 @@ #ifdef USE_ICONV {"subcp", &sub_cp, CONF_TYPE_STRING, 0, 0, 0, NULL}, #endif + {"subid", &sub_id, CONF_TYPE_STRING, 0, 0, 0, NULL}, {"subdelay", &sub_delay, CONF_TYPE_FLOAT, 0, 0.0, 10.0, NULL}, {"subfps", &sub_fps, CONF_TYPE_FLOAT, 0, 0.0, 10.0, NULL}, {"noautosub", &sub_auto, CONF_TYPE_FLAG, 0, 1, 0, NULL}, Index: mplayer.h =================================================================== RCS file: /cvsroot/mplayer/main/mplayer.h,v retrieving revision 1.28 diff -u -b -B -r1.28 mplayer.h --- mplayer.h 21 Jan 2003 19:12:34 -0000 1.28 +++ mplayer.h 3 Feb 2003 08:46:36 -0000 @@ -35,6 +35,7 @@ extern int sub_pos; extern int sub_unicode; extern char * sub_cp; +extern char * sub_id; extern subtitle* subtitles; extern subtitle* vo_sub; extern int suboverlap_enabled; Index: subreader.c =================================================================== RCS file: /cvsroot/mplayer/main/subreader.c,v retrieving revision 1.97 diff -u -b -B -r1.97 subreader.c --- subreader.c 2 Feb 2003 02:42:55 -0000 1.97 +++ subreader.c 3 Feb 2003 08:46:36 -0000 @@ -12,6 +12,9 @@ #include #include +#include +#include + #include "config.h" #include "mp_msg.h" #include "subreader.h" @@ -23,6 +26,8 @@ char *sub_cp=NULL; #endif +char *sub_id=NULL; // subtitle ID string (en, de, hu, cz ...) + /* Maximal length of line of a subtitle */ #define LINE_LEN 1000 @@ -1418,74 +1423,217 @@ } #endif +static void strcpy_trim(char *d, char *s) +{ + // skip leading whitespace + while (*s && !isalnum(*s)) { + s++; + } + for (;;) { + // copy word + while (*s && isalnum(*s)) { + *d = tolower(*s); + s++; d++; + } + if (*s == 0) break; + // trim excess whitespace + while (*s && !isalnum(*s)) { + s++; + } + if (*s == 0) break; + if (isalnum(*s)) { + *d++ = ' '; + } + } + *d++ = 0; +} + +static void strcpy_strip_ext(char *d, char *s) +{ + char *tmp = strrchr(s,'.'); + if (!tmp) { + strcpy(d, s); + return; + } else { + strncpy(d, s, tmp-s); + d[tmp-s] = 0; + } + while (*d) { + *d = tolower(*d); + d++; + } +} + +static void strcpy_get_ext(char *d, char *s) +{ + char *tmp = strrchr(s,'.'); + if (!tmp) { + strcpy(d, ""); + return; + } else { + strcpy(d, tmp+1); + } +} + +static int whiteonly(char *s) +{ + while (*s) { + if (isalnum(*s)) return 0; + *s++; + } + return 1; +} + char * sub_filename(char* path, char * fname ) { - char * sub_name1; - char * sub_name2; - char * aviptr1, * aviptr2, * tmp; - int i,j; - FILE * f; - int pos=0; - char * sub_exts[] = - { ".utf", - ".UTF", - ".sub", - ".SUB", - ".srt", - ".SRT", - ".smi", - ".SMI", - ".rt", - ".RT", - ".txt", - ".TXT", - ".ssa", - ".SSA", - ".aqt", - ".AQT", - ".jss", - ".JSS" }; - - - if ( fname == NULL ) return NULL; - - sub_name1=strrchr(fname,'.'); - if (!sub_name1) return NULL; - pos=sub_name1-fname; - - sub_name1=malloc(strlen(fname)+8); - strcpy(sub_name1,fname); - - sub_name2=malloc (strlen(path) + strlen(fname) + 8); - if ((tmp=strrchr(fname,'/'))) - sprintf (sub_name2, "%s%s", path, tmp+1); - else - sprintf (sub_name2, "%s%s", path, fname); + char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id; + char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult; + char *result; - aviptr1=strrchr(sub_name1,'.'); - aviptr2=strrchr(sub_name2,'.'); + int len, pos, found, i, j, match; + char * sub_exts[] = { "utf", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", NULL}; - for(j=0;j<=1;j++){ - char* sub_name=j?sub_name1:sub_name2; -#ifdef USE_ICONV - for ( i=(sub_cp?2:0);i<(sizeof(sub_exts)/sizeof(char*));i++ ) { -#else - for ( i=0;i<(sizeof(sub_exts)/sizeof(char*));i++ ) { -#endif - strcpy(j?aviptr1:aviptr2,sub_exts[i]); -// printf("trying: '%s'\n",sub_name); - if((f=fopen( sub_name,"rt" ))) { - fclose( f ); - mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected sub file: %s\n",sub_name ); - if (i<2) sub_utf8=1; - return sub_name; + FILE *f; + + DIR *d; + struct dirent *de; + + len = strlen(fname)+strlen(path)+2; + + f_dir = (char*)malloc(len); + f_fname = (char*)malloc(len); + f_fname_noext = (char*)malloc(len); + f_fname_trim = (char*)malloc(len); + + tmp_fname_noext = (char*)malloc(len); + tmp_fname_trim = (char*)malloc(len); + tmp_fname_ext = (char*)malloc(len); + + result = (char*)malloc(len); + tmpresult = (char*)malloc(len); + + tmp = strrchr(fname,'/'); + + // extract filename & dirname from fname + if (tmp) { + strcpy(f_fname, tmp+1); + pos = tmp - fname; + strncpy(f_dir, fname, pos+1); + f_dir[pos+1] = 0; + } else { + strcpy(f_fname, fname); + strcpy(f_dir, "./"); + } + + strcpy_strip_ext(f_fname_noext, f_fname); + strcpy_trim(f_fname_trim, f_fname_noext); + + tmp_sub_id = NULL; + if (sub_id && !whiteonly(sub_id)) { + tmp_sub_id = (char*)malloc(strlen(sub_id)+1); + strcpy_trim(tmp_sub_id, sub_id); + } + + // 0 = nothing + // 1 = any subtitle file + // 2 = any sub file containing movie name + // 3 = sub file containing movie name and the lang extension + match = 0; + for (j = 0; (j <= 1) && (match < 3); j++) { + d = opendir(j == 0 ? f_dir : path); + if (d) { + while ((de = readdir(d)) && (match < 3)) { + // retrieve various parts of the filename + strcpy_strip_ext(tmp_fname_noext, de->d_name); + strcpy_get_ext(tmp_fname_ext, de->d_name); + strcpy_trim(tmp_fname_trim, tmp_fname_noext); + + // does it end with a subtitle extension? + found = 0; + for (i = (sub_cp ? 1 : 0); sub_exts[i]; i++) { + if (strcmp(sub_exts[i], tmp_fname_ext) == 0) { + found = 1; + break; + } + + } + + // we have a (likely) subtitle file + if (found) { + // does it contain the movie name? + tmp = strstr(tmp_fname_trim, f_fname_trim); + if (tmp) { + tmp += strlen(f_fname_trim); + if (sub_id && strstr(tmp, sub_id)) { + if (match < 3) { + // with sub_id specified prefer localized subtitles + sprintf(tmpresult, "%s%s", f_dir, de->d_name); + if (f = fopen(tmpresult, "rt")) { + fclose(f); + strcpy(result, tmpresult); + match = 3; + } + } + } else if ((sub_id == NULL) && whiteonly(tmp)) { + if (match < 3) { + // without sub_id prefer "plain" name + sprintf(tmpresult, "%s%s", f_dir, de->d_name); + if (f = fopen(tmpresult, "rt")) { + fclose(f); + strcpy(result, tmpresult); + match = 3; + } + } + } else { + // with no localized subs found, try any else instead + if (match < 2) { + sprintf(tmpresult, "%s%s", f_dir, de->d_name); + if (f = fopen(tmpresult, "rt")) { + fclose(f); + strcpy(result, tmpresult); + match = 2; + } + } + } + } else { + // doesn't contain the movie name + // don't try in the mplayer subtitle directory + if ((match < 1) && (j == 0)) { + sprintf(tmpresult, "%s%s", f_dir, de->d_name); + if (f = fopen(tmpresult, "rt")) { + fclose(f); + strcpy(result, tmpresult); + match = 1; } } } + } + + } + closedir(d); + } - free(sub_name2); - free(sub_name1); + } + + if (tmp_sub_id) free(tmp_sub_id); + + free(f_dir); + free(f_fname); + free(f_fname_noext); + free(f_fname_trim); + + free(tmp_fname_noext); + free(tmp_fname_trim); + free(tmp_fname_ext); + + free(tmpresult); + + if (match) { + return result; + } else { + free(result); return NULL; + } } void list_sub_file(subtitle* subs){ Index: libvo/sub.h =================================================================== RCS file: /cvsroot/mplayer/main/libvo/sub.h,v retrieving revision 1.27 diff -u -b -B -r1.27 sub.h --- libvo/sub.h 24 Jan 2003 10:24:06 -0000 1.27 +++ libvo/sub.h 3 Feb 2003 08:46:36 -0000 @@ -96,6 +96,7 @@ #ifdef USE_ICONV extern char *sub_cp; #endif +extern char *sub_id; extern int sub_pos; extern int sub_width_p; extern int sub_alignment;