subs: move text sub loading logic down to find_subfiles.c
[mplayer/greg.git] / sub / find_subfiles.c
blob19ee0f065b441c91189049bbe4d4020f3a4b988d
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 "sub/find_subfiles.h"
9 #include "sub/sub.h"
11 static void strcpy_trim(char *d, char *s)
13 // skip leading whitespace
14 while (*s && isspace(*s)) {
15 s++;
17 for (;;) {
18 // copy word
19 while (*s && !isspace(*s)) {
20 *d = tolower(*s);
21 s++; d++;
23 if (*s == 0)
24 break;
25 // trim excess whitespace
26 while (*s && isspace(*s)) {
27 s++;
29 if (*s == 0)
30 break;
31 *d++ = ' ';
33 *d = 0;
36 static void strcpy_strip_ext(char *d, char *s)
38 char *tmp = strrchr(s, '.');
39 if (!tmp) {
40 strcpy(d, s);
41 return;
42 } else {
43 strncpy(d, s, tmp-s);
44 d[tmp-s] = 0;
46 while (*d) {
47 *d = tolower(*d);
48 d++;
52 static void strcpy_get_ext(char *d, char *s)
54 char *tmp = strrchr(s, '.');
55 if (!tmp) {
56 strcpy(d, "");
57 return;
58 } else {
59 strcpy(d, tmp+1);
63 static int whiteonly(char *s)
65 while (*s) {
66 if (!isspace(*s))
67 return 0;
68 s++;
70 return 1;
73 typedef struct subfn {
74 int priority;
75 char *fname;
76 } subfn;
78 static int compare_sub_priority(const void *a, const void *b)
80 if (((const subfn*)a)->priority > ((const subfn*)b)->priority) {
81 return -1;
82 } else if (((const subfn*)a)->priority < ((const subfn*)b)->priority) {
83 return 1;
84 } else {
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};
96 subfn *result;
97 char **result2;
99 int subcnt;
101 FILE *f;
103 DIR *d;
104 struct dirent *de;
106 len = (strlen(fname) > 256 ? strlen(fname) : 256)
107 + (strlen(path) > 256 ? strlen(path) : 256) + 2;
109 f_dir = malloc(len);
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));
122 subcnt = 0;
124 tmp = strrchr(fname,'/');
125 #if HAVE_DOS_PATHS
126 if(!tmp)tmp = strrchr(fname,'\\');
127 if(!tmp)tmp = strrchr(fname,':');
128 #endif
130 // extract filename & dirname from fname
131 if (tmp) {
132 strcpy(f_fname, tmp+1);
133 pos = tmp - fname;
134 strncpy(f_dir, fname, pos+1);
135 f_dir[pos+1] = 0;
136 } else {
137 strcpy(f_fname, fname);
138 strcpy(f_dir, "./");
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"
147 * syntax... */
148 tmp_sub_id = NULL;
149 #if 0
150 if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
151 tmp_sub_id = malloc(strlen(dvdsub_lang) + 1);
152 strcpy_trim(tmp_sub_id, dvdsub_lang);
154 #endif
156 // 0 = nothing
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);
162 if (d) {
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?
170 found = 0;
171 #ifdef CONFIG_ICONV
172 #ifdef CONFIG_ENCA
173 for (i = ((sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0); sub_exts[i]; i++) {
174 #else
175 for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
176 #endif
177 #else
178 for (i = 0; sub_exts[i]; i++) {
179 #endif
180 if (strcasecmp(sub_exts[i], tmp_fname_ext) == 0) {
181 found = 1;
182 break;
186 // we have a (likely) subtitle file
187 if (found) {
188 int prio = 0;
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
193 prio = 5;
196 if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
197 // matches the movie name
198 prio = 4;
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
205 prio = 3;
206 } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
207 // without sub_id prefer "plain" name
208 prio = 3;
209 } else {
210 // with no localized subs found, try any else instead
211 prio = 2;
214 if (!prio) {
215 // doesn't contain the movie name
216 // don't try in the mplayer subtitle directory
217 if (j == 0 && sub_match_fuzziness >= 2) {
218 prio = 1;
222 mp_msg(MSGT_SUBREADER, MSGL_DBG2, "Potential sub file: "
223 "\"%s\" Priority: %d\n", de->d_name, prio);
224 if (prio) {
225 prio += prio;
226 #ifdef CONFIG_ICONV
227 if (i < 3) // prefer UTF-8 coded
228 prio++;
229 #endif
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"))) {
233 fclose(f);
234 result[subcnt].priority = prio;
235 result[subcnt].fname = strdup(tmpresult);
236 subcnt++;
241 if (subcnt >= MAX_SUBTITLE_FILES) break;
243 closedir(d);
248 free(tmp_sub_id);
250 free(f_dir);
251 free(f_fname);
252 free(f_fname_noext);
253 free(f_fname_trim);
255 free(tmp_fname_noext);
256 free(tmp_fname_trim);
257 free(tmp_fname_ext);
259 free(tmpresult);
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;
269 free(result);
271 return result2;
274 char **find_text_subtitles(const char *fname)
276 char *psub = get_path("sub/");
277 char **tmp = sub_filenames(psub ? psub : "", fname);
278 free(psub);
279 return tmp;