File: i18n-test.c

package info (click to toggle)
pspp 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,676 kB
  • sloc: ansic: 267,210; xml: 18,446; sh: 5,534; python: 2,881; makefile: 125; perl: 64
file content (135 lines) | stat: -rw-r--r-- 3,808 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* PSPP - a program for statistical analysis.
   Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "libpspp/i18n.h"

#include "gl/xalloc.h"

#undef NDEBUG
#include <assert.h>

/* Returns a malloc()'d copy of INPUT with \ooo octal escapes replaced by their
   values. */
static char *
backslash_decode (const char *input)
{
  char *output = xmalloc (strlen (input) + 1);
  char *p = output;
  for (; *input; input++)
    {
      if (*input == '\\' && input[1] >= '0' && input[1] <= '7')
        {
          uint8_t c = 0;
          while (input[1] >= '0' && input[1] <= '7')
            c = c * 8 + (*++input - '0');
          *p++ = c;
        }
      else
        *p++ = *input;
    }
  *p = '\0';

  return output;
}

int
main (int argc, char *argv[])
{
  i18n_init ();

  if (argc > 1 && !strcmp (argv[1], "supports_encodings"))
    {
      int status = 0;
      int i;

      for (i = 2; i < argc; i++)
        if (!is_encoding_supported (argv[i]))
          {
            printf ("encoding \"%s\" is NOT supported\n", argv[i]);
            status = 77;
          }
      i18n_done ();
      exit (status);
    }
  if (argc == 5 && !strcmp (argv[1], "recode"))
    {
      const char *from = argv[2];
      const char *to = argv[3];
      char *string = backslash_decode (argv[4]);
      char *result = recode_string (to, from, string, -1);
      puts (result);
      assert (strlen (result) == recode_string_len (to, from, string, -1));
      free (string);
      free (result);
    }
  else if (argc == 6 && !strcmp (argv[1], "concat"))
    {
      char *head = backslash_decode (argv[2]);
      char *tail = backslash_decode (argv[3]);
      const char *encoding = argv[4];
      int max_len = atoi (argv[5]);
      char *result;

      result = utf8_encoding_concat (head, tail, encoding, max_len);
      puts (result);

      assert (strlen (result)
              == utf8_encoding_concat_len (head, tail, encoding, max_len));

      if (tail[0] == '\0')
        {
          char *result2 = utf8_encoding_trunc (head, encoding, max_len);
          assert (!strcmp (result, result2));
          assert (strlen (result2)
                  == utf8_encoding_trunc_len (head, encoding, max_len));
          free (result2);
        }

      free (result);
      free (head);
      free (tail);
    }
  else
    {
      fprintf (stderr, "\
usage: %s supports_encodings ENCODING...\n\
where ENCODING is the name of an encoding.\n\
Exits with status 0 if all the encodings are supported, 77 otherwise.\n\
\n\
usage: %s recode FROM TO STRING\n\
where FROM is the source encoding,\n\
      TO is the target encoding,\n\
      and STRING is the text to recode.\n\
\n\
usage: %s concat HEAD TAIL ENCODING MAX_LEN\n\
where HEAD is the first string to concatenate\n\
      TAIL is the second string to concatenate\n\
      ENCODING is the encoding in which to measure the result's length\n\
      MAX_LEN is the maximum length of the result in ENCODING.\n",
               argv[0], argv[0], argv[0]);
      return EXIT_FAILURE;
    }

  i18n_done ();

  return 0;
}