Add test tool for iconvme.
[gnulib.git] / tests / test-iconvme.c
1 /* Recode strings between character sets, using iconv.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3    Written by Simon Josefsson.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include "iconvme.h"
23
24 int main (int ac, char *av[])
25 {
26   char *in = NULL, *out = NULL;
27   char *to = NULL, *from = NULL;
28
29   if (ac > 1)
30     from = av[1];
31
32   if (ac > 2)
33     to = av[2];
34
35   if (ac > 3)
36     in = av[3];
37
38   if (!in)
39     {
40       size_t len = 0;
41       printf ("Enter string to convert:\n\t> ");
42       if (getline (&in, &len, stdin) < 0)
43         perror ("getline");
44       if (in[strlen (in) - 1] == '\n')
45         in[strlen (in) - 1] = '\0';
46     }
47
48   if (!to)
49     {
50       size_t len = 0;
51       printf ("Enter destination code set:\n\t> ");
52       if (getline (&to, &len, stdin) < 0)
53         perror ("getline");
54       if (to[strlen (to) - 1] == '\n')
55         to[strlen (to) - 1] = '\0';
56     }
57
58   if (!from)
59     {
60       size_t len = 0;
61       printf ("Enter source code set:\n\t> ");
62       if (getline (&from, &len, stdin) < 0)
63         perror ("getline");
64       if (from[strlen (from) - 1] == '\n')
65         from[strlen (from) - 1] = '\0';
66     }
67
68   printf (" Input string: `%s'\n"
69           "From code set: `%s'\n"
70           "  To code set: `%s'\n",
71           in, from, to);
72
73   out = iconv_string (in, from, to);
74
75   if (out == NULL)
76     perror ("iconv");
77   else
78     {
79       printf ("\nOutput: `%s'\n", out);
80       free (out);
81     }
82
83   return EXIT_SUCCESS;
84 }