From d87fabeacea32abba73d67f991f9e523991369c7 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 27 Jun 2004 13:18:32 +0000 Subject: Sort out error handling everywhere a charset name is converted into an integer charset ID. [originally from svn r4317] --- ustring.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'ustring.c') diff --git a/ustring.c b/ustring.c index 8cf8554..13ee635 100644 --- a/ustring.c +++ b/ustring.c @@ -459,3 +459,28 @@ int cvt_ok(int charset, const wchar_t *s) } 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; +} -- cgit v1.1