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