summaryrefslogtreecommitdiffstats
path: root/test.c
diff options
context:
space:
mode:
authorNatanael Copa <natanael.copa@gmail.com>2008-10-22 13:44:07 +0200
committerNatanael Copa <natanael.copa@gmail.com>2008-10-22 13:44:07 +0200
commit6ed652d87201c190125877cf6912cd4f2e4269b5 (patch)
tree4d430843a20030d50acf625294e1cd8916a3df0b /test.c
downloaduiconv-6ed652d87201c190125877cf6912cd4f2e4269b5.tar.bz2
uiconv-6ed652d87201c190125877cf6912cd4f2e4269b5.tar.xz
initial commit
Diffstat (limited to 'test.c')
-rw-r--r--test.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..1cb8011
--- /dev/null
+++ b/test.c
@@ -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;
+}
+
+
+
+