k++; /* treat `xhtml-' and `html-' the same */
if (!ustricmp(k, L"html-restrict-charset")) {
- char *csname = utoa_dup(uadv(k), CS_ASCII);
- ret.restrict_charset = charset_from_localenc(csname);
- sfree(csname);
+ ret.restrict_charset = charset_from_ustr(&p->fpos, uadv(k));
} else if (!ustricmp(k, L"html-output-charset")) {
- char *csname = utoa_dup(uadv(k), CS_ASCII);
- ret.output_charset = charset_from_localenc(csname);
- sfree(csname);
+ ret.output_charset = charset_from_ustr(&p->fpos, uadv(k));
} else if (!ustricmp(k, L"html-version")) {
wchar_t *vername = uadv(k);
static const struct {
sfree(ret.filename);
ret.filename = dupstr(adv(p->origkeyword));
} else if (!ustricmp(p->keyword, L"info-charset")) {
- char *csname = utoa_dup(uadv(p->keyword), CS_ASCII);
- ret.charset = charset_from_localenc(csname);
- sfree(csname);
+ ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"info-max-file-size")) {
ret.maxfilesize = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"info-width")) {
ret.th = snewn(ep - wp + 1, wchar_t);
memcpy(ret.th, wp, (ep - wp + 1) * sizeof(wchar_t));
} else if (!ustricmp(p->keyword, L"man-charset")) {
- char *csname = utoa_dup(uadv(p->keyword), CS_ASCII);
- ret.charset = charset_from_localenc(csname);
- sfree(csname);
+ ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"man-headnumbers")) {
ret.headnumbers = utob(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"man-mindepth")) {
if (!ustricmp(p->keyword, L"text-indent")) {
ret.indent = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"text-charset")) {
- char *csname = utoa_dup(uadv(p->keyword), CS_ASCII);
- ret.charset = charset_from_localenc(csname);
- sfree(csname);
+ ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"text-filename")) {
sfree(ret.filename);
ret.filename = dupstr(adv(p->origkeyword));
sfree(sp);
flags = FILEPOS;
break;
+ case err_charset:
+ fpos = *va_arg(ap, filepos *);
+ wsp = va_arg(ap, wchar_t *);
+ sp = utoa_locale_dup(wsp);
+ sprintf(error, "character set `%.200s' not recognised", sp);
+ flags = FILEPOS;
+ sfree(sp);
+ break;
case err_whatever:
sp = va_arg(ap, char *);
vsprintf(error, sp, ap);
err_infonodechar, /* colon/comma in node name in info */
err_text_codeline, /* \c line too long in text backend */
err_htmlver, /* unrecognised HTML version keyword */
+ err_charset, /* unrecognised character set name */
err_whatever /* random error of another type */
};
wchar_t *ustrlow(wchar_t *s);
wchar_t *ustrftime(const wchar_t *wfmt, const struct tm *timespec);
int cvt_ok(int charset, const wchar_t *s);
+int charset_from_ustr(filepos *fpos, const wchar_t *name);
/*
* wcwidth.c
assert(cfg->type == para_Config);
if (!ustricmp(cfg->keyword, L"input-charset")) {
- char *csname = utoa_dup(uadv(cfg->keyword), CS_ASCII);
- in->charset = charset_from_localenc(csname);
- sfree(csname);
+ in->charset = charset_from_ustr(&cfg->fpos, uadv(cfg->keyword));
}
}
}
return TRUE;
}
+
+/*
+ * Wrapper around charset_from_localenc which accepts the charset
+ * name as a wide string (since that happens to be more useful).
+ * Also throws a Halibut error and falls back to CS_ASCII if the
+ * charset is unrecognised, meaning the rest of the program can
+ * rely on always getting a valid charset id back from this
+ * function.
+ */
+int charset_from_ustr(filepos *fpos, const wchar_t *name)
+{
+ char *csname;
+ int charset;
+
+ csname = utoa_dup(name, CS_ASCII);
+ charset = charset_from_localenc(csname);
+
+ if (charset == CS_NONE) {
+ charset = CS_ASCII;
+ error(err_charset, fpos, name);
+ }
+
+ sfree(csname);
+ return charset;
+}