1/* Titlecase mapping for UTF-8 strings (locale dependent).
2   Copyright (C) 2009-2010 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#define FUNC u8_totitle
24#define UNIT uint8_t
25#define U_CT_TOTITLE u8_ct_totitle
26#include "u-totitle.h"
27
28
29#ifdef TEST
30
31#include <locale.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36/* Read the contents of an input stream, and return it, terminated with a NUL
37   byte. */
38char *
39read_file (FILE *stream)
40{
41#define BUFSIZE 4096
42  char *buf = NULL;
43  int alloc = 0;
44  int size = 0;
45  int count;
46
47  while (! feof (stream))
48    {
49      if (size + BUFSIZE > alloc)
50        {
51          alloc = alloc + alloc / 2;
52          if (alloc < size + BUFSIZE)
53            alloc = size + BUFSIZE;
54          buf = realloc (buf, alloc);
55          if (buf == NULL)
56            {
57              fprintf (stderr, "out of memory\n");
58              exit (1);
59            }
60        }
61      count = fread (buf + size, 1, BUFSIZE, stream);
62      if (count == 0)
63        {
64          if (ferror (stream))
65            {
66              perror ("fread");
67              exit (1);
68            }
69        }
70      else
71        size += count;
72    }
73  buf = realloc (buf, size + 1);
74  if (buf == NULL)
75    {
76      fprintf (stderr, "out of memory\n");
77      exit (1);
78    }
79  buf[size] = '\0';
80  return buf;
81#undef BUFSIZE
82}
83
84int
85main (int argc, char * argv[])
86{
87  setlocale (LC_ALL, "");
88  if (argc == 1)
89    {
90      /* Display the upper case of the input string.  */
91      char *input = read_file (stdin);
92      int length = strlen (input);
93      size_t output_length;
94      uint8_t *output =
95        u8_toupper ((uint8_t *) input, length, uc_locale_language (),
96                    NULL,
97                    NULL, &output_length);
98
99      fwrite (output, 1, output_length, stdout);
100
101      return 0;
102    }
103  else
104    return 1;
105}
106
107#endif /* TEST */
108