find_subfiles: allow subtitle search in multiple directories
[mplayer/greg.git] / sub / find_subfiles.c
bloba5834ab733ddaecda3667af8a75a1fe5e9f9c417
1 #include <dirent.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
6 #include "mp_msg.h"
7 #include "path.h"
8 #include "mpcommon.h"
9 #include "sub/find_subfiles.h"
10 #include "sub/sub.h"
12 static void strcpy_trim(char *d, char *s)
14 // skip leading whitespace
15 while (*s && isspace(*s)) {
16 s++;
18 for (;;) {
19 // copy word
20 while (*s && !isspace(*s)) {
21 *d = tolower(*s);
22 s++; d++;
24 if (*s == 0)
25 break;
26 // trim excess whitespace
27 while (*s && isspace(*s)) {
28 s++;
30 if (*s == 0)
31 break;
32 *d++ = ' ';
34 *d = 0;
37 static void strcpy_strip_ext(char *d, char *s)
39 char *tmp = strrchr(s, '.');
40 if (!tmp) {
41 strcpy(d, s);
42 return;
43 } else {
44 strncpy(d, s, tmp-s);
45 d[tmp-s] = 0;
47 while (*d) {
48 *d = tolower(*d);
49 d++;
53 static void strcpy_get_ext(char *d, char *s)
55 char *tmp = strrchr(s, '.');
56 if (!tmp) {
57 strcpy(d, "");
58 return;
59 } else {
60 strcpy(d, tmp+1);
64 static int whiteonly(char *s)
66 while (*s) {
67 if (!isspace(*s))
68 return 0;
69 s++;
71 return 1;
74 typedef struct subfn {
75 int priority;
76 char *fname;
77 } subfn;
79 static int compare_sub_priority(const void *a, const void *b)
81 if (((const subfn*)a)->priority > ((const subfn*)b)->priority) {
82 return -1;
83 } else if (((const subfn*)a)->priority < ((const subfn*)b)->priority) {
84 return 1;
85 } else {
86 return strcoll(((const subfn*)a)->fname, ((const subfn*)b)->fname);
90 /**
91 * @brief Append all the subtitles in the given path matching fname
92 * @param slist pointer to the subtitles list tallocated
93 * @param nsub pointer to the number of subtitles
94 * @param path Look for subtitles in this directory
95 * @param fname Subtitle filename (pattern)
96 * @param limit_fuzziness Ignore flag when sub_fuziness == 2
98 static void append_dir_subtitles(struct subfn **slist, int *nsub,
99 struct bstr path, const char *fname,
100 int limit_fuzziness)
102 char *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
103 char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
105 int len, found, i;
106 char *sub_exts[] = {"utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
107 FILE *f;
109 DIR *d;
110 struct dirent *de;
112 len = (strlen(fname) > 256 ? strlen(fname) : 256)
113 + (path.len > 256 ? path.len : 256) + 2;
115 f_fname = mp_basename(fname);
116 f_fname_noext = malloc(len);
117 f_fname_trim = malloc(len);
119 tmp_fname_noext = malloc(len);
120 tmp_fname_trim = malloc(len);
121 tmp_fname_ext = malloc(len);
123 tmpresult = malloc(len);
125 strcpy_strip_ext(f_fname_noext, f_fname);
126 strcpy_trim(f_fname_trim, f_fname_noext);
128 /* The code using sub language here is broken - it assumes strict
129 * "videoname languagename" syntax for the subtitle file, which is
130 * very unlikely to match especially if language name uses "en,de"
131 * syntax... */
132 tmp_sub_id = NULL;
133 #if 0
134 if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
135 tmp_sub_id = malloc(strlen(dvdsub_lang) + 1);
136 strcpy_trim(tmp_sub_id, dvdsub_lang);
138 #endif
140 // 0 = nothing
141 // 1 = any subtitle file
142 // 2 = any sub file containing movie name
143 // 3 = sub file containing movie name and the lang extension
144 char *path0 = bstrdup0(NULL, path);
145 d = opendir(path0);
146 talloc_free(path0);
147 if (d) {
148 mp_msg(MSGT_SUBREADER, MSGL_INFO, "Load subtitles in %.*s\n", BSTR_P(path));
149 while ((de = readdir(d))) {
150 // retrieve various parts of the filename
151 strcpy_strip_ext(tmp_fname_noext, de->d_name);
152 strcpy_get_ext(tmp_fname_ext, de->d_name);
153 strcpy_trim(tmp_fname_trim, tmp_fname_noext);
155 // does it end with a subtitle extension?
156 found = 0;
157 #ifdef CONFIG_ICONV
158 #ifdef CONFIG_ENCA
159 for (i = ((sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0); sub_exts[i]; i++) {
160 #else
161 for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
162 #endif
163 #else
164 for (i = 0; sub_exts[i]; i++) {
165 #endif
166 if (strcasecmp(sub_exts[i], tmp_fname_ext) == 0) {
167 found = 1;
168 break;
172 // we have a (likely) subtitle file
173 if (found) {
174 int prio = 0;
175 if (!prio && tmp_sub_id) {
176 sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id);
177 if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) {
178 // matches the movie name + lang extension
179 prio = 5;
182 if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
183 // matches the movie name
184 prio = 4;
186 if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && sub_match_fuzziness >= 1) {
187 // contains the movie name
188 tmp += strlen(f_fname_trim);
189 if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
190 // with sub_id specified prefer localized subtitles
191 prio = 3;
192 } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
193 // without sub_id prefer "plain" name
194 prio = 3;
195 } else {
196 // with no localized subs found, try any else instead
197 prio = 2;
200 if (!prio) {
201 // doesn't contain the movie name
202 // don't try in the mplayer subtitle directory
203 if (!limit_fuzziness && sub_match_fuzziness >= 2) {
204 prio = 1;
208 mp_msg(MSGT_SUBREADER, MSGL_DBG2, "Potential sub file: "
209 "\"%s\" Priority: %d\n", de->d_name, prio);
210 if (prio) {
211 prio += prio;
212 #ifdef CONFIG_ICONV
213 if (i < 3) // prefer UTF-8 coded
214 prio++;
215 #endif
216 char *subpath = mp_path_join(*slist, path, BSTR(de->d_name));
217 if ((f = fopen(subpath, "rt"))) {
218 MP_GROW_ARRAY(*slist, *nsub);
219 struct subfn *sub = *slist + (*nsub)++;
221 fclose(f);
222 sub->priority = prio;
223 sub->fname = subpath;
224 } else
225 talloc_free(subpath);
229 closedir(d);
232 free(tmp_sub_id);
234 free(f_fname_noext);
235 free(f_fname_trim);
237 free(tmp_fname_noext);
238 free(tmp_fname_trim);
239 free(tmp_fname_ext);
241 free(tmpresult);
244 char **find_text_subtitles(const char *fname)
246 struct subfn *slist = talloc_array_ptrtype(NULL, slist, 1);
247 int n = 0;
249 // Load subtitles from current media directory
250 append_dir_subtitles(&slist, &n, mp_dirname(fname), fname, 0);
252 // Load subtitles in ~/.mplayer/sub limiting sub fuzziness
253 char *mp_subdir = get_path("sub/");
254 if (mp_subdir)
255 append_dir_subtitles(&slist, &n, BSTR(mp_subdir), fname, 1);
256 free(mp_subdir);
258 // Sort subs by priority and append them
259 qsort(slist, n, sizeof(*slist), compare_sub_priority);
261 char **subnames = talloc_array_ptrtype(NULL, subnames, n);
262 for (int i = 0; i < n; i++)
263 subnames[i] = talloc_strdup(subnames, slist[i].fname);
265 talloc_free(slist);
266 return subnames;
269 char **find_vob_subtitles(const char *fname)
271 char **vobs = talloc_array_ptrtype(NULL, vobs, 1);
272 int n = 0;
274 // Potential vobsub in the media directory
275 struct bstr bname = BSTR(mp_basename(fname));
276 int pdot = bstrrchr(bname, '.');
277 if (pdot >= 0)
278 bname.len = pdot;
279 vobs[n++] = mp_path_join(vobs, mp_dirname(fname), bname);
281 // Potential vobsub in ~/.mplayer/sub
282 char *mp_subdir = get_path("sub/");
283 if (mp_subdir) {
284 MP_GROW_ARRAY(vobs, n);
285 vobs[n++] = mp_path_join(vobs, BSTR(mp_subdir), bname);
288 free(mp_subdir);
289 MP_RESIZE_ARRAY(NULL, vobs, n);
290 return vobs;