New module 'unicase/ulc-casecoll'.
[gnulib.git] / lib / unicase / u8-casefold.c
1 /* Casefolding mapping for UTF-8 strings (locale dependent).
2    Copyright (C) 2009 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5    This program is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Lesser General Public License as published
7    by the Free Software Foundation; either version 3 of the License, or
8    (at your option) 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "unicase.h"
22
23 #include <errno.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26
27 #include "unicasemap.h"
28 #include "special-casing.h"
29 #include "casefold.h"
30
31 #define FUNC u8_casefold
32 #define UNIT uint8_t
33 #define U_CASEMAP u8_casemap
34 #define U_NORMALIZE u8_normalize
35 #include "u-casefold.h"
36
37
38 #ifdef TEST
39
40 #include <locale.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 /* Read the contents of an input stream, and return it, terminated with a NUL
46    byte. */
47 char *
48 read_file (FILE *stream)
49 {
50 #define BUFSIZE 4096
51   char *buf = NULL;
52   int alloc = 0;
53   int size = 0;
54   int count;
55
56   while (! feof (stream))
57     {
58       if (size + BUFSIZE > alloc)
59         {
60           alloc = alloc + alloc / 2;
61           if (alloc < size + BUFSIZE)
62             alloc = size + BUFSIZE;
63           buf = realloc (buf, alloc);
64           if (buf == NULL)
65             {
66               fprintf (stderr, "out of memory\n");
67               exit (1);
68             }
69         }
70       count = fread (buf + size, 1, BUFSIZE, stream);
71       if (count == 0)
72         {
73           if (ferror (stream))
74             {
75               perror ("fread");
76               exit (1);
77             }
78         }
79       else
80         size += count;
81     }
82   buf = realloc (buf, size + 1);
83   if (buf == NULL)
84     {
85       fprintf (stderr, "out of memory\n");
86       exit (1);
87     }
88   buf[size] = '\0';
89   return buf;
90 #undef BUFSIZE
91 }
92
93 int
94 main (int argc, char * argv[])
95 {
96   setlocale (LC_ALL, "");
97   if (argc == 1)
98     {
99       /* Display the case folded input string.  */
100       char *input = read_file (stdin);
101       int length = strlen (input);
102       size_t output_length;
103       uint8_t *output =
104         u8_casefold ((uint8_t *) input, length, uc_locale_language (),
105                     NULL,
106                     NULL, &output_length);
107
108       fwrite (output, 1, output_length, stdout);
109
110       return 0;
111     }
112   else
113     return 1;
114 }
115
116 #endif /* TEST */