diff options
author | Natanael Copa <natanael.copa@gmail.com> | 2008-10-22 13:44:07 +0200 |
---|---|---|
committer | Natanael Copa <natanael.copa@gmail.com> | 2008-10-22 13:44:07 +0200 |
commit | 6ed652d87201c190125877cf6912cd4f2e4269b5 (patch) | |
tree | 4d430843a20030d50acf625294e1cd8916a3df0b /test.c | |
download | uiconv-6ed652d87201c190125877cf6912cd4f2e4269b5.tar.bz2 uiconv-6ed652d87201c190125877cf6912cd4f2e4269b5.tar.xz |
initial commit
Diffstat (limited to 'test.c')
-rw-r--r-- | test.c | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -0,0 +1,72 @@ +#include <errno.h> +#include <libgen.h> +#include <stdio.h> +#include <string.h> + +#include "iconv.h" + +void print_hex(char *str) +{ + printf("%.2x", (unsigned char) *str); + str++; + while (*str) { + printf(" %.2x", (unsigned char) *str); + str++; + } +} + +void convert_string(iconv_t cd, char *str) +{ + char outbuf[256]; + char *outptr = outbuf; + const char *inptr = str; + size_t outsize = sizeof(outbuf); + size_t insize = strlen(str); + int ret, err = 0; + memset(outbuf, 0, sizeof(outbuf)); + print_hex(str); + printf(" -> "); + ret = iconv(cd, &inptr, &insize, &outptr, &outsize); + if (ret < 0) + err= errno; + print_hex(outbuf); + if (err) + printf(" (%s)", strerror(err)); + printf("\n"); +} + + +void convert_args(char *from, char *to, int argc, char **argv) +{ + iconv_t cd; + int i; + + printf("\n>>> %s: %s -> %s\n", basename(argv[0]), from, to); + cd = iconv_open(to, from); + if (cd < 0) { + printf("iconv_open(\"%s\", \"%s\"): %s\n", to, from, strerror(errno)); + return; + } + + for (i = 1; i < argc; i++) + convert_string(cd, argv[i]); + + iconv_close(cd); +} + + +int main(int argc, char **argv) +{ + char *codesets[] = { "ASCII", "ISO-8859-1", "UTF-8", "invalid", NULL }; + char **from, **to; + + for (from = codesets; *from; from++) + for (to = codesets; *to; to++) + convert_args(*from, *to, argc, argv); + + return 0; +} + + + + |