8 #include "sub/find_subfiles.h"
11 static void strcpy_trim(char *d
, char *s
)
13 // skip leading whitespace
14 while (*s
&& isspace(*s
)) {
19 while (*s
&& !isspace(*s
)) {
25 // trim excess whitespace
26 while (*s
&& isspace(*s
)) {
36 static void strcpy_strip_ext(char *d
, char *s
)
38 char *tmp
= strrchr(s
, '.');
52 static void strcpy_get_ext(char *d
, char *s
)
54 char *tmp
= strrchr(s
, '.');
63 static int whiteonly(char *s
)
73 typedef struct subfn
{
78 static int compare_sub_priority(const void *a
, const void *b
)
80 if (((const subfn
*)a
)->priority
> ((const subfn
*)b
)->priority
) {
82 } else if (((const subfn
*)a
)->priority
< ((const subfn
*)b
)->priority
) {
85 return strcoll(((const subfn
*)a
)->fname
, ((const subfn
*)b
)->fname
);
89 static char **sub_filenames(const char *path
, const char *fname
)
91 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
92 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
94 int len
, pos
, found
, i
, j
;
95 char *sub_exts
[] = {"utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
106 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
107 + (strlen(path
) > 256 ? strlen(path
) : 256) + 2;
110 f_fname
= malloc(len
);
111 f_fname_noext
= malloc(len
);
112 f_fname_trim
= malloc(len
);
114 tmp_fname_noext
= malloc(len
);
115 tmp_fname_trim
= malloc(len
);
116 tmp_fname_ext
= malloc(len
);
118 tmpresult
= malloc(len
);
120 result
= calloc(MAX_SUBTITLE_FILES
, sizeof(*result
));
124 tmp
= strrchr(fname
,'/');
126 if(!tmp
)tmp
= strrchr(fname
,'\\');
127 if(!tmp
)tmp
= strrchr(fname
,':');
130 // extract filename & dirname from fname
132 strcpy(f_fname
, tmp
+1);
134 strncpy(f_dir
, fname
, pos
+1);
137 strcpy(f_fname
, fname
);
141 strcpy_strip_ext(f_fname_noext
, f_fname
);
142 strcpy_trim(f_fname_trim
, f_fname_noext
);
144 /* The code using sub language here is broken - it assumes strict
145 * "videoname languagename" syntax for the subtitle file, which is
146 * very unlikely to match especially if language name uses "en,de"
150 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
151 tmp_sub_id
= malloc(strlen(dvdsub_lang
) + 1);
152 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
157 // 1 = any subtitle file
158 // 2 = any sub file containing movie name
159 // 3 = sub file containing movie name and the lang extension
160 for (j
= 0; j
<= 1; j
++) {
161 d
= opendir(j
== 0 ? f_dir
: path
);
163 while ((de
= readdir(d
))) {
164 // retrieve various parts of the filename
165 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
166 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
167 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
169 // does it end with a subtitle extension?
173 for (i
= ((sub_cp
&& strncasecmp(sub_cp
, "enca", 4) != 0) ? 3 : 0); sub_exts
[i
]; i
++) {
175 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
178 for (i
= 0; sub_exts
[i
]; i
++) {
180 if (strcasecmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
186 // we have a (likely) subtitle file
189 if (!prio
&& tmp_sub_id
) {
190 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
191 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
192 // matches the movie name + lang extension
196 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
197 // matches the movie name
200 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && sub_match_fuzziness
>= 1) {
201 // contains the movie name
202 tmp
+= strlen(f_fname_trim
);
203 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
204 // with sub_id specified prefer localized subtitles
206 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
207 // without sub_id prefer "plain" name
210 // with no localized subs found, try any else instead
215 // doesn't contain the movie name
216 // don't try in the mplayer subtitle directory
217 if (j
== 0 && sub_match_fuzziness
>= 2) {
222 mp_msg(MSGT_SUBREADER
, MSGL_DBG2
, "Potential sub file: "
223 "\"%s\" Priority: %d\n", de
->d_name
, prio
);
227 if (i
< 3) // prefer UTF-8 coded
230 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
231 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
232 if ((f
= fopen(tmpresult
, "rt"))) {
234 result
[subcnt
].priority
= prio
;
235 result
[subcnt
].fname
= strdup(tmpresult
);
241 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
255 free(tmp_fname_noext
);
256 free(tmp_fname_trim
);
261 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
263 result2
= calloc(subcnt
+ 1, sizeof(*result2
));
265 for (i
= 0; i
< subcnt
; i
++)
266 result2
[i
] = result
[i
].fname
;
267 result2
[subcnt
] = NULL
;
274 char **find_text_subtitles(const char *fname
)
276 char *psub
= get_path("sub/");
277 char **tmp
= sub_filenames(psub
? psub
: "", fname
);