New module 'unicase/u16-suffix-context'.
[gnulib.git] / lib / unicase / u8-tolower.c
1 /* Lowercase 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 <stddef.h>
24
25 #include "unicasemap.h"
26 #include "special-casing.h"
27
28 uint8_t *
29 u8_tolower (const uint8_t *s, size_t n, const char *iso639_language,
30             uninorm_t nf,
31             uint8_t *resultbuf, size_t *lengthp)
32 {
33   return u8_casemap (s, n, iso639_language,
34                      uc_tolower, offsetof (struct special_casing_rule, lower[0]),
35                      nf,
36                      resultbuf, lengthp);
37 }
38
39
40 #ifdef TEST
41
42 #include <locale.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 /* Read the contents of an input stream, and return it, terminated with a NUL
48    byte. */
49 char *
50 read_file (FILE *stream)
51 {
52 #define BUFSIZE 4096
53   char *buf = NULL;
54   int alloc = 0;
55   int size = 0;
56   int count;
57
58   while (! feof (stream))
59     {
60       if (size + BUFSIZE > alloc)
61         {
62           alloc = alloc + alloc / 2;
63           if (alloc < size + BUFSIZE)
64             alloc = size + BUFSIZE;
65           buf = realloc (buf, alloc);
66           if (buf == NULL)
67             {
68               fprintf (stderr, "out of memory\n");
69               exit (1);
70             }
71         }
72       count = fread (buf + size, 1, BUFSIZE, stream);
73       if (count == 0)
74         {
75           if (ferror (stream))
76             {
77               perror ("fread");
78               exit (1);
79             }
80         }
81       else
82         size += count;
83     }
84   buf = realloc (buf, size + 1);
85   if (buf == NULL)
86     {
87       fprintf (stderr, "out of memory\n");
88       exit (1);
89     }
90   buf[size] = '\0';
91   return buf;
92 #undef BUFSIZE
93 }
94
95 int
96 main (int argc, char * argv[])
97 {
98   setlocale (LC_ALL, "");
99   if (argc == 1)
100     {
101       /* Display the lower case of the input string.  */
102       char *input = read_file (stdin);
103       int length = strlen (input);
104       size_t output_length;
105       uint8_t *output =
106         u8_tolower ((uint8_t *) input, length, uc_locale_language (),
107                     NULL,
108                     NULL, &output_length);
109
110       fwrite (output, 1, output_length, stdout);
111
112       return 0;
113     }
114   else
115     return 1;
116 }
117
118 #endif /* TEST */