Backport revision 106439 from trunk. I didn't realize this was broken in 1.4 as well.

Closes issue #12222.


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@109057 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Jason Parker 17 years ago
parent 284f74997d
commit 4501c61d73

@ -475,6 +475,37 @@ static int ast_filehelper(const char *filename, const void *arg2, const char *fm
return res; return res;
} }
static int is_absolute_path(const char *filename)
{
return filename[0] == '/';
}
static int fileexists_test(const char *filename, const char *fmt, const char *lang,
char *buf, int buflen)
{
if (buf == NULL) {
return -1;
}
if (ast_language_is_prefix) { /* new layout */
if (lang) {
snprintf(buf, buflen, "%s/%s", lang, filename);
} else {
snprintf(buf, buflen, "%s", filename);
}
} else { /* old layout */
strcpy(buf, filename); /* first copy the full string */
if (lang) {
/* insert the language and suffix if needed */
const char *c = strrchr(filename, '/');
int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
}
}
return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
}
/*! /*!
* \brief helper routine to locate a file with a given format * \brief helper routine to locate a file with a given format
* and language preference. * and language preference.
@ -490,57 +521,56 @@ static int fileexists_core(const char *filename, const char *fmt, const char *pr
char *buf, int buflen) char *buf, int buflen)
{ {
int res = -1; int res = -1;
int langlen; /* length of language string */ char *lang = NULL;
const char *c = strrchr(filename, '/');
int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
if (preflang == NULL) { if (buf == NULL) {
preflang = ""; return -1;
}
langlen = strlen(preflang);
if (buflen < langlen + strlen(filename) + 4) {
ast_log(LOG_WARNING, "buffer too small, allocating larger buffer\n");
buf = alloca(langlen + strlen(filename) + 4); /* room for everything */
} }
if (buf == NULL) { if (is_absolute_path(filename)) {
return 0; ast_copy_string(buf, filename, buflen);
return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
} }
for (;;) { /* We try languages in the following order:
if (ast_language_is_prefix) { /* new layout */ * preflang (may include dialect)
if (langlen) { * lang (preflang without dialect - if any)
strcpy(buf, preflang); * <none>
buf[langlen] = '/'; * default (unless the same as preflang or lang without dialect)
strcpy(buf + langlen + 1, filename); */
} else {
strcpy(buf, "en/"); /* English - fallback if no file found in preferred language */ /* Try preferred language */
strcpy(buf + 3, filename); if (!ast_strlen_zero(preflang)) {
} /* try the preflang exactly as it was requested */
} else { /* old layout */ if ((res = fileexists_test(filename, fmt, preflang, buf, buflen)) > 0) {
strcpy(buf, filename); /* first copy the full string */ return res;
if (langlen) { } else {
/* insert the language and suffix if needed */ /* try without a dialect */
strcpy(buf + offset, preflang); char *postfix = NULL;
sprintf(buf + offset + langlen, "/%s", filename + offset); postfix = lang = ast_strdupa(preflang);
strsep(&postfix, "_");
if (postfix) {
if ((res = fileexists_test(filename, fmt, lang, buf, buflen)) > 0) {
return res;
}
} }
} }
res = ast_filehelper(buf, NULL, fmt, ACTION_EXISTS); }
if (res > 0) { /* found format */
break; /* Try without any language */
} if ((res = fileexists_test(filename, fmt, NULL, buf, buflen)) > 0) {
if (langlen == 0) { /* no more formats */ return res;
break; }
}
if (preflang[langlen] == '_') { /* we are on the local suffix */ /* Finally try the default language unless it was already tried before */
langlen = 0; /* try again with no language */ if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
} else { if ((res = fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen)) > 0) {
langlen = (c = strchr(preflang, '_')) ? c - preflang : 0; return res;
} }
} }
return res; return 0;
} }
struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang) struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)

Loading…
Cancel
Save