1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
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.
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.
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/>. */
23 #include "libpspp/i18n.h"
25 #include "gl/xalloc.h"
30 /* Returns a malloc()'d copy of INPUT with \ooo octal escapes replaced by their
33 backslash_decode (const char *input)
35 char *output = xmalloc (strlen (input) + 1);
37 for (; *input; input++)
39 if (*input == '\\' && input[1] >= '0' && input[1] <= '7')
42 while (input[1] >= '0' && input[1] <= '7')
43 c = c * 8 + (*++input - '0');
55 main (int argc, char *argv[])
59 if (argc > 1 && !strcmp (argv[1], "supports_encodings"))
64 for (i = 2; i < argc; i++)
65 if (!is_encoding_supported (argv[i]))
67 printf ("encoding \"%s\" is NOT supported\n", argv[i]);
73 if (argc == 5 && !strcmp (argv[1], "recode"))
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);
80 assert (strlen (result) == recode_string_len (to, from, string, -1));
84 else if (argc == 6 && !strcmp (argv[1], "concat"))
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]);
92 result = utf8_encoding_concat (head, tail, encoding, max_len);
95 assert (strlen (result)
96 == utf8_encoding_concat_len (head, tail, encoding, max_len));
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));
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\
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\
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]);