>From 014d97b7cf46e56d31b15163d48037ede88f9408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Date: Sat, 20 Nov 2010 21:07:43 +0100 Subject: [PATCH 2/3] Make load of n subtitles directories possible. --- sub/subreader.c | 164 +++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 104 insertions(+), 60 deletions(-) diff --git a/sub/subreader.c b/sub/subreader.c index c532dc1..1c38412 100644 --- a/sub/subreader.c +++ b/sub/subreader.c @@ -1877,8 +1877,38 @@ typedef struct subfn { int priority; char *fname; + int noerr; } subfn; +struct sub_list { + struct subfn *subs; + int sid; +}; + +/** + * \brief Append a subtitle to a list + * \param dst Destination subtitles list + * \param fname Subtitle filename + * \param priority Subtitle priority + * \param noerr Subtitle raising error flag on open failure + */ +static int append_sub(struct sub_list *dst, const char *fname, + int priority, int noerr) +{ + struct subfn *sub; + + if (dst->sid >= MAX_SUBTITLE_FILES) + return -1; + sub = &dst->subs[dst->sid]; + sub->fname = strdup(fname); + sub->priority = priority; + sub->noerr = noerr; + if (!sub->fname) + return -1; + dst->sid++; + return 0; +} + static int compare_sub_priority(const void *a, const void *b) { if (((const subfn*)a)->priority > ((const subfn*)b)->priority) { @@ -1890,17 +1920,20 @@ static int compare_sub_priority(const void *a, const void *b) } } -static char** sub_filenames(const char* path, const char *fname) +/** + * \brief Append all the subtitles in the given path matching fname + * \param path Look for subtitles in this directory + * \param fname Subtitle filename (pattern) + * \param limit_fuzziness Ignore flag when sub_fuziness == 2 + */ +static void append_dir_subtitles(struct sub_list *slist, const char *path, + const char *fname, int limit_fuzziness) { - char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id; + char *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id; char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult; - int len, pos, found, i, j; + int len, found, i; char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL}; - subfn *result; - char **result2; - - int subcnt; FILE *f; @@ -1910,8 +1943,7 @@ static char** sub_filenames(const char* path, const char *fname) len = (strlen(fname) > 256 ? strlen(fname) : 256) +(strlen(path) > 256 ? strlen(path) : 256)+2; - f_dir = malloc(len); - f_fname = malloc(len); + f_fname = strdup(mp_basename(fname)); f_fname_noext = malloc(len); f_fname_trim = malloc(len); @@ -1921,27 +1953,6 @@ static char** sub_filenames(const char* path, const char *fname) tmpresult = malloc(len); - result = calloc(MAX_SUBTITLE_FILES, sizeof(*result)); - - subcnt = 0; - - tmp = strrchr(fname,'/'); -#if HAVE_DOS_PATHS - if(!tmp)tmp = strrchr(fname,'\\'); - if(!tmp)tmp = strrchr(fname,':'); -#endif - - // 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); @@ -1955,9 +1966,9 @@ static char** sub_filenames(const char* path, const char *fname) // 1 = any subtitle file // 2 = any sub file containing movie name // 3 = sub file containing movie name and the lang extension - for (j = 0; j <= 1; j++) { - d = opendir(j == 0 ? f_dir : path); + d = opendir(path); if (d) { + mp_msg(MSGT_SUBREADER, MSGL_INFO, "Load subtitles in %s\n", path); while ((de = readdir(d))) { // retrieve various parts of the filename strcpy_strip_ext(tmp_fname_noext, de->d_name); @@ -2013,8 +2024,7 @@ static char** sub_filenames(const char* path, const char *fname) } if (!prio) { // doesn't contain the movie name - // don't try in the mplayer subtitle directory - if ((j == 0) && (sub_match_fuzziness >= 2)) { + if (!limit_fuzziness && sub_match_fuzziness >= 2) { prio = 1; } } @@ -2026,27 +2036,22 @@ static char** sub_filenames(const char* path, const char *fname) prio++; } #endif - sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name); + sprintf(tmpresult, "%s/%s", path, de->d_name); // fprintf(stderr, "%s priority %d\n", tmpresult, prio); if ((f = fopen(tmpresult, "rt"))) { - fclose(f); - result[subcnt].priority = prio; - result[subcnt].fname = strdup(tmpresult); - subcnt++; + fclose(f); + if (append_sub(slist, tmpresult, prio, 1) < 0) + break; } } } - if (subcnt >= MAX_SUBTITLE_FILES) break; } closedir(d); } - } - free(tmp_sub_id); - free(f_dir); free(f_fname); free(f_fname_noext); free(f_fname_trim); @@ -2056,19 +2061,34 @@ static char** sub_filenames(const char* path, const char *fname) free(tmp_fname_ext); free(tmpresult); +} - qsort(result, subcnt, sizeof(subfn), compare_sub_priority); - - result2 = calloc(subcnt + 1, sizeof(*result2)); +/** + * \brief Append a new series of subtitles by tracking a given directory + * \param dir Look for subtitles in this directory + * \param fname Subtitle filename (pattern) + * \param slist Subtitles list where new subtitles are appended + */ +static int track_sub_directory(const char *dir, const char *fname, + struct sub_list *slist) +{ + char *path, *tmp; - for (i = 0; i < subcnt; i++) { - result2[i] = result[i].fname; + path = mp_dirname(fname); + if (!path) + return -1; + tmp = realloc(path, strlen(path) + strlen(dir) + 1); + if (!tmp) { + free(path); + return -1; } - result2[subcnt] = NULL; + path = tmp; + strcat(path, dir); - free(result); + append_dir_subtitles(slist, path, fname, 0); - return result2; + free(path); + return 0; } /** @@ -2076,23 +2096,47 @@ static char** sub_filenames(const char* path, const char *fname) * \param fname Path to subtitle filename * \param fps FPS parameter for the add subtitle function * \param add_f Add subtitle function to call for each sub + * + * Subtitles are tracked and scored in various places according to the user + * options, sorted, and then added by calling the add_f function. */ void load_subtitles(const char *fname, int fps, void add_f(char *, float, int)) { int i; + char *mp_subdir; + struct sub_list slist; + + // Load subtitles specified by sub option first if (sub_name) for (i = 0; sub_name[i]; i++) add_f(sub_name[i], fps, 0); - if (sub_auto) { // auto load sub file ... - char *psub = get_path("sub/"); - char **tmp = sub_filenames((psub ? psub : ""), fname); - free(psub); // release the buffer created by get_path() above - for (i = 0; tmp[i]; i++) { - add_f(tmp[i], fps, 1); - free(tmp[i]); - } - free(tmp); + + // Stop here if automatic detection disabled + if (!sub_auto || !fname) + return; + + slist.sid = 0; + slist.subs = calloc(MAX_SUBTITLE_FILES, sizeof(*slist.subs)); + if (!slist.subs) + return; + + // Load subtitles from current media directory + track_sub_directory("", fname, &slist); + + // Load subtitles in ~/.mplayer/sub + mp_subdir = get_path("sub/"); + if (mp_subdir) + append_dir_subtitles(&slist, mp_subdir, fname, 1); + free(mp_subdir); + + // Sort subs by priority and append them + qsort(slist.subs, slist.sid, sizeof(*slist.subs), compare_sub_priority); + for (i = 0; i < slist.sid; i++) { + struct subfn *sub = &slist.subs[i]; + add_f(sub->fname, fps, sub->noerr); + free(sub->fname); } + free(slist.subs); } void list_sub_file(sub_data* subd){ -- 1.7.3.3