@matheusbsbs / matheus-bagliano / commits / b45974a655
Fork 0

Move encoding conversion routine out of mailinfo to utf8.c

This moves the body of convert_to_utf8() routine used in mailinfo to the utf8.c i18n library. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 23, 2006 at 23:36 UTC b45974a655e0e41441e5db64c091000171435096
3 files changed +69 -30
builtin-mailinfo.c
+9 -30
@@ -4,6 +4,7 @@
4 */
5 #include "cache.h"
6 #include "builtin.h"
7 +#include "utf8.h"
8
9 static FILE *cmitmsg, *patchfile, *fin, *fout;
10
@@ -510,40 +511,18 @@ static int decode_b_segment(char *in, char *ot, char *ep)
511
512 static void convert_to_utf8(char *line, char *charset)
513 {
513 -#ifndef NO_ICONV
514 - char *in, *out;
515 - size_t insize, outsize, nrc;
516 - char outbuf[4096]; /* cheat */
514 static char latin_one[] = "latin1";
515 char *input_charset = *charset ? charset : latin_one;
519 - iconv_t conv = iconv_open(metainfo_charset, input_charset);
520 -
521 - if (conv == (iconv_t) -1) {
522 - static int warned_latin1_once = 0;
523 - if (input_charset != latin_one) {
524 - fprintf(stderr, "cannot convert from %s to %s\n",
525 - input_charset, metainfo_charset);
526 - *charset = 0;
527 - }
528 - else if (!warned_latin1_once) {
529 - warned_latin1_once = 1;
530 - fprintf(stderr, "tried to convert from %s to %s, "
531 - "but your iconv does not work with it.\n",
532 - input_charset, metainfo_charset);
533 - }
516 + char *out = reencode_string(line, metainfo_charset, input_charset);
517 +
518 + if (!out) {
519 + fprintf(stderr, "cannot convert from %s to %s\n",
520 + input_charset, metainfo_charset);
521 + *charset = 0;
522 return;
523 }
536 - in = line;
537 - insize = strlen(in);
538 - out = outbuf;
539 - outsize = sizeof(outbuf);
540 - nrc = iconv(conv, &in, &insize, &out, &outsize);
541 - iconv_close(conv);
542 - if (nrc == (size_t) -1)
543 - return;
544 - *out = 0;
545 - strcpy(line, outbuf);
546 -#endif
524 + strcpy(line, out);
525 + free(out);
526 }
527
528 static int decode_header_bq(char *it)
utf8.c
+54
@@ -276,3 +276,57 @@ void print_wrapped_text(const char *text, int indent, int indent2, int width)
276 }
277 }
278 }
279 +
280 +/*
281 + * Given a buffer and its encoding, return it re-encoded
282 + * with iconv. If the conversion fails, returns NULL.
283 + */
284 +#ifndef NO_ICONV
285 +char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding)
286 +{
287 + iconv_t conv;
288 + size_t insz, outsz, outalloc;
289 + char *out, *outpos, *cp;
290 +
291 + if (!in_encoding)
292 + return NULL;
293 + conv = iconv_open(out_encoding, in_encoding);
294 + if (conv == (iconv_t) -1)
295 + return NULL;
296 + insz = strlen(in);
297 + outsz = insz;
298 + outalloc = outsz + 1; /* for terminating NUL */
299 + out = xmalloc(outalloc);
300 + outpos = out;
301 + cp = (char *)in;
302 +
303 + while (1) {
304 + size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz);
305 +
306 + if (cnt == -1) {
307 + size_t sofar;
308 + if (errno != E2BIG) {
309 + free(out);
310 + iconv_close(conv);
311 + return NULL;
312 + }
313 + /* insz has remaining number of bytes.
314 + * since we started outsz the same as insz,
315 + * it is likely that insz is not enough for
316 + * converting the rest.
317 + */
318 + sofar = outpos - out;
319 + outalloc = sofar + insz * 2 + 32;
320 + out = xrealloc(out, outalloc);
321 + outpos = out + sofar;
322 + outsz = outalloc - sofar - 1;
323 + }
324 + else {
325 + *outpos = '\0';
326 + break;
327 + }
328 + }
329 + iconv_close(conv);
330 + return out;
331 +}
332 +#endif
utf8.h
+6
@@ -5,4 +5,10 @@ int utf8_width(const char **start);
5 int is_utf8(const char *text);
6 void print_wrapped_text(const char *text, int indent, int indent2, int len);
7
8 +#ifndef NO_ICONV
9 +char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);
10 +#else
11 +#define reencode_string(a,b,c) NULL
12 +#endif
13 +
14 #endif