mbsnrtowcs: Work around Solaris 11.4 bug.
[gnulib.git] / lib / unicase / u8-casefold.c
blob2de75a0304c8ad9c40505097815c5393cc801303
1 /* Casefolding mapping for UTF-8 strings (locale dependent).
2 Copyright (C) 2009-2018 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
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.
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.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "unicase.h"
23 #define FUNC u8_casefold
24 #define UNIT uint8_t
25 #define U_CT_CASEFOLD u8_ct_casefold
26 #include "u-casefold.h"
29 #ifdef TEST
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 /* Read the contents of an input stream, and return it, terminated with a NUL
37 byte. */
38 char *
39 read_file (FILE *stream)
41 #define BUFSIZE 4096
42 char *buf = NULL;
43 int alloc = 0;
44 int size = 0;
45 int count;
47 while (! feof (stream))
49 if (size + BUFSIZE > alloc)
51 alloc = alloc + alloc / 2;
52 if (alloc < size + BUFSIZE)
53 alloc = size + BUFSIZE;
54 buf = realloc (buf, alloc);
55 if (buf == NULL)
57 fprintf (stderr, "out of memory\n");
58 exit (1);
61 count = fread (buf + size, 1, BUFSIZE, stream);
62 if (count == 0)
64 if (ferror (stream))
66 perror ("fread");
67 exit (1);
70 else
71 size += count;
73 buf = realloc (buf, size + 1);
74 if (buf == NULL)
76 fprintf (stderr, "out of memory\n");
77 exit (1);
79 buf[size] = '\0';
80 return buf;
81 #undef BUFSIZE
84 int
85 main (int argc, char * argv[])
87 setlocale (LC_ALL, "");
88 if (argc == 1)
90 /* Display the case folded input string. */
91 char *input = read_file (stdin);
92 int length = strlen (input);
93 size_t output_length;
94 uint8_t *output =
95 u8_casefold ((uint8_t *) input, length, uc_locale_language (),
96 NULL,
97 NULL, &output_length);
99 fwrite (output, 1, output_length, stdout);
101 return 0;
103 else
104 return 1;
107 #endif /* TEST */