diff options
author | wm4 <wm4@nowhere> | 2015-08-01 23:25:50 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2015-08-01 23:49:37 +0200 |
commit | 11f2be2bcc264aa4f24cdad036231e34d09f5630 (patch) | |
tree | 6e0b8a258cf6d8de882bd21d7d61a644ca379dd9 /misc | |
parent | 17fe9d7c0d33ead1d13535952d8491aaf68d3c46 (diff) | |
download | mpv-11f2be2bcc264aa4f24cdad036231e34d09f5630.tar.bz2 mpv-11f2be2bcc264aa4f24cdad036231e34d09f5630.tar.xz |
charset_conv: make it possible to return an allocated string as guess
uchardet is written in C++, and thus doesn't appreciate the value of
using static strings, and internally stores the guessed charset as
allocated std::string. Add a minimal hack to deal with this. (I don't
appreciate that the code is potentially harder to understand by
returning either a static or allocated string, but I do appreciate for
not having to litter the existing code with strdups.)
Diffstat (limited to 'misc')
-rw-r--r-- | misc/charset_conv.c | 12 | ||||
-rw-r--r-- | misc/charset_conv.h | 4 |
2 files changed, 10 insertions, 6 deletions
diff --git a/misc/charset_conv.c b/misc/charset_conv.c index 31f53ccecb..343fb7fd90 100644 --- a/misc/charset_conv.c +++ b/misc/charset_conv.c @@ -150,8 +150,9 @@ static const char *libguess_guess(struct mp_log *log, bstr buf, // If user_cp doesn't refer to any known auto-detection (for example because // it's a real iconv codepage), user_cp is returned without even looking at // the buf data. -const char *mp_charset_guess(struct mp_log *log, bstr buf, const char *user_cp, - int flags) +// The return value may (but doesn't have to) be allocated under talloc_ctx. +const char *mp_charset_guess(void *talloc_ctx, struct mp_log *log, bstr buf, + const char *user_cp, int flags) { if (!mp_charset_requires_guess(user_cp)) return user_cp; @@ -225,8 +226,11 @@ const char *mp_charset_guess(struct mp_log *log, bstr buf, const char *user_cp, bstr mp_charset_guess_and_conv_to_utf8(struct mp_log *log, bstr buf, const char *user_cp, int flags) { - return mp_iconv_to_utf8(log, buf, mp_charset_guess(log, buf, user_cp, flags), - flags); + void *tmp = talloc_new(NULL); + const char *cp = mp_charset_guess(log, tmp, buf, user_cp, flags); + bstr res = mp_iconv_to_utf8(log, buf, cp, flags); + talloc_free(tmp); + return res; } // Use iconv to convert buf to UTF-8. diff --git a/misc/charset_conv.h b/misc/charset_conv.h index 93bd91cffe..bd76ae007a 100644 --- a/misc/charset_conv.h +++ b/misc/charset_conv.h @@ -14,8 +14,8 @@ enum { bool mp_charset_is_utf8(const char *user_cp); bool mp_charset_requires_guess(const char *user_cp); -const char *mp_charset_guess(struct mp_log *log, bstr buf, const char *user_cp, - int flags); +const char *mp_charset_guess(void *talloc_ctx, struct mp_log *log, bstr buf, + const char *user_cp, int flags); bstr mp_charset_guess_and_conv_to_utf8(struct mp_log *log, bstr buf, const char *user_cp, int flags); bstr mp_iconv_to_utf8(struct mp_log *log, bstr buf, const char *cp, int flags); |