docs
[pspp] / tests / libpspp / i18n-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "libpspp/i18n.h"
24
25 #include "gl/xalloc.h"
26
27 #undef NDEBUG
28 #include <assert.h>
29
30 /* Returns a malloc()'d copy of INPUT with \ooo octal escapes replaced by their
31    values. */
32 static char *
33 backslash_decode (const char *input)
34 {
35   char *output = xmalloc (strlen (input) + 1);
36   char *p = output;
37   for (; *input; input++)
38     {
39       if (*input == '\\' && input[1] >= '0' && input[1] <= '7')
40         {
41           uint8_t c = 0;
42           while (input[1] >= '0' && input[1] <= '7')
43             c = c * 8 + (*++input - '0');
44           *p++ = c;
45         }
46       else
47         *p++ = *input;
48     }
49   *p = '\0';
50
51   return output;
52 }
53
54 int
55 main (int argc, char *argv[])
56 {
57   i18n_init ();
58
59   if (argc > 1 && !strcmp (argv[1], "supports_encodings"))
60     {
61       int status = 0;
62       int i;
63
64       for (i = 2; i < argc; i++)
65         if (!is_encoding_supported (argv[i]))
66           {
67             printf ("encoding \"%s\" is NOT supported\n", argv[i]);
68             status = 77;
69           }
70       i18n_done ();
71       exit (status);
72     }
73   if (argc == 5 && !strcmp (argv[1], "recode"))
74     {
75       const char *from = argv[2];
76       const char *to = argv[3];
77       char *string = backslash_decode (argv[4]);
78       char *result = recode_string (to, from, string, -1);
79       puts (result);
80       assert (strlen (result) == recode_string_len (to, from, string, -1));
81       free (string);
82       free (result);
83     }
84   else if (argc == 6 && !strcmp (argv[1], "concat"))
85     {
86       char *head = backslash_decode (argv[2]);
87       char *tail = backslash_decode (argv[3]);
88       const char *encoding = argv[4];
89       int max_len = atoi (argv[5]);
90       char *result;
91
92       result = utf8_encoding_concat (head, tail, encoding, max_len);
93       puts (result);
94
95       assert (strlen (result)
96               == utf8_encoding_concat_len (head, tail, encoding, max_len));
97
98       if (tail[0] == '\0')
99         {
100           char *result2 = utf8_encoding_trunc (head, encoding, max_len);
101           assert (!strcmp (result, result2));
102           assert (strlen (result2)
103                   == utf8_encoding_trunc_len (head, encoding, max_len));
104           free (result2);
105         }
106
107       free (result);
108       free (head);
109       free (tail);
110     }
111   else
112     {
113       fprintf (stderr, "\
114 usage: %s supports_encodings ENCODING...\n\
115 where ENCODING is the name of an encoding.\n\
116 Exits with status 0 if all the encodings are supported, 77 otherwise.\n\
117 \n\
118 usage: %s recode FROM TO STRING\n\
119 where FROM is the source encoding,\n\
120       TO is the target encoding,\n\
121       and STRING is the text to recode.\n\
122 \n\
123 usage: %s concat HEAD TAIL ENCODING MAX_LEN\n\
124 where HEAD is the first string to concatenate\n\
125       TAIL is the second string to concatenate\n\
126       ENCODING is the encoding in which to measure the result's length\n\
127       MAX_LEN is the maximum length of the result in ENCODING.\n",
128                argv[0], argv[0], argv[0]);
129       return EXIT_FAILURE;
130     }
131
132   i18n_done ();
133
134   return 0;
135 }