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