diff options
| author | Junio C Hamano <junkio@cox.net> | 2006-12-28 19:03:02 -0800 |
|---|---|---|
| committer | Junio C Hamano <junkio@cox.net> | 2006-12-28 19:03:02 -0800 |
| commit | eff73751bb94b0241fd2204effb8680fe9973cbf (patch) | |
| tree | 5fe6f0f55988d64a7b8b841c62b1c996a3fda5bf /utf8.c | |
| parent | 013672bc589da395fcba9bf62d699e70f3764689 (diff) | |
| parent | 7255ff0446217ad723dad6088504a18f6afb15db (diff) | |
| download | git-eff73751bb94b0241fd2204effb8680fe9973cbf.tar.xz | |
Merge branch 'jc/utf8'
* jc/utf8:
t3900: test conversion to non UTF-8 as well
Rename t3900 test vector file
UTF-8: introduce i18n.logoutputencoding.
Teach log family --encoding
i18n.logToUTF8: convert commit log message to UTF-8
Move encoding conversion routine out of mailinfo to utf8.c
Conflicts:
commit.c
Diffstat (limited to 'utf8.c')
| -rw-r--r-- | utf8.c | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -276,3 +276,57 @@ void print_wrapped_text(const char *text, int indent, int indent2, int width) } } } + +/* + * Given a buffer and its encoding, return it re-encoded + * with iconv. If the conversion fails, returns NULL. + */ +#ifndef NO_ICONV +char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding) +{ + iconv_t conv; + size_t insz, outsz, outalloc; + char *out, *outpos, *cp; + + if (!in_encoding) + return NULL; + conv = iconv_open(out_encoding, in_encoding); + if (conv == (iconv_t) -1) + return NULL; + insz = strlen(in); + outsz = insz; + outalloc = outsz + 1; /* for terminating NUL */ + out = xmalloc(outalloc); + outpos = out; + cp = (char *)in; + + while (1) { + size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz); + + if (cnt == -1) { + size_t sofar; + if (errno != E2BIG) { + free(out); + iconv_close(conv); + return NULL; + } + /* insz has remaining number of bytes. + * since we started outsz the same as insz, + * it is likely that insz is not enough for + * converting the rest. + */ + sofar = outpos - out; + outalloc = sofar + insz * 2 + 32; + out = xrealloc(out, outalloc); + outpos = out + sofar; + outsz = outalloc - sofar - 1; + } + else { + *outpos = '\0'; + break; + } + } + iconv_close(conv); + return out; +} +#endif |
