diff options
| author | 2007-10-27 07:11:26 (JST) | |
|---|---|---|
| committer | 2007-11-06 08:13:30 (JST) | |
| commit | af0819830445e39584a0137034562086a55deaf2 (patch) | |
| tree | a9da08806d706be633b63d0fc2f9dbe12824536b | |
| parent | aa5cc328f4894ba6972842b4edbca3017f169050 (diff) | |
| download | cgit-af0819830445e39584a0137034562086a55deaf2.zip cgit-af0819830445e39584a0137034562086a55deaf2.tar.gz | |
Add iconv_msg function.
| -rw-r--r-- | parsing.c | 58 |
1 files changed, 58 insertions, 0 deletions
| @@ -6,6 +6,8 @@ | |||
| 6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
| 7 | */ | 7 | */ |
| 8 | 8 | ||
| 9 | #include <iconv.h> | ||
| 10 | |||
| 9 | #include "cgit.h" | 11 | #include "cgit.h" |
| 10 | 12 | ||
| 11 | int next_char(FILE *f) | 13 | int next_char(FILE *f) |
| @@ -176,6 +178,62 @@ void cgit_parse_url(const char *url) | |||
| 176 | } | 178 | } |
| 177 | } | 179 | } |
| 178 | 180 | ||
| 181 | static char *iconv_msg(char *msg, const char *encoding) | ||
| 182 | { | ||
| 183 | iconv_t msg_conv = iconv_open(PAGE_ENCODING, encoding); | ||
| 184 | size_t inlen = strlen(msg); | ||
| 185 | char *in; | ||
| 186 | char *out; | ||
| 187 | size_t inleft; | ||
| 188 | size_t outleft; | ||
| 189 | char *buf; | ||
| 190 | char *ret; | ||
| 191 | size_t buf_sz; | ||
| 192 | int again, fail; | ||
| 193 | |||
| 194 | if(msg_conv == (iconv_t)-1) | ||
| 195 | return NULL; | ||
| 196 | |||
| 197 | buf_sz = inlen * 2; | ||
| 198 | buf = xmalloc(buf_sz+1); | ||
| 199 | do { | ||
| 200 | in = msg; | ||
| 201 | inleft = inlen; | ||
| 202 | |||
| 203 | out = buf; | ||
| 204 | outleft = buf_sz; | ||
| 205 | iconv(msg_conv, &in, &inleft, &out, &outleft); | ||
| 206 | |||
| 207 | if(inleft == 0) { | ||
| 208 | fail = 0; | ||
| 209 | again = 0; | ||
| 210 | } else if(inleft != 0 && errno == E2BIG) { | ||
| 211 | fail = 0; | ||
| 212 | again = 1; | ||
| 213 | |||
| 214 | buf_sz *= 2; | ||
| 215 | free(buf); | ||
| 216 | buf = xmalloc(buf_sz+1); | ||
| 217 | } else { | ||
| 218 | fail = 1; | ||
| 219 | again = 0; | ||
| 220 | } | ||
| 221 | } while(again && !fail); | ||
| 222 | |||
| 223 | if(fail) { | ||
| 224 | free(buf); | ||
| 225 | ret = NULL; | ||
| 226 | } else { | ||
| 227 | buf = xrealloc(buf, out - buf); | ||
| 228 | *out = 0; | ||
| 229 | ret = buf; | ||
| 230 | } | ||
| 231 | |||
| 232 | iconv_close(msg_conv); | ||
| 233 | |||
| 234 | return ret; | ||
| 235 | } | ||
| 236 | |||
| 179 | char *substr(const char *head, const char *tail) | 237 | char *substr(const char *head, const char *tail) |
| 180 | { | 238 | { |
| 181 | char *buf; | 239 | char *buf; |
