Avoid test failures on AIX and OSF/1.
[gnulib.git] / lib / uniwbrk / ulc-wordbreaks.c
1 /* Word breaks in strings.
2    Copyright (C) 2001-2003, 2006-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 "uniwbrk.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "c-ctype.h"
27 #include "localcharset.h"
28 #include "uniconv.h"
29 #include "unilbrk/ulc-common.h"
30
31 /* Word breaks of a string in an arbitrary encoding.
32
33    We convert the input string to Unicode.
34
35    The standardized Unicode encodings are UTF-8, UCS-2, UCS-4, UTF-16,
36    UTF-16BE, UTF-16LE, UTF-7.  UCS-2 supports only characters up to
37    \U0000FFFF.  UTF-16 and variants support only characters up to
38    \U0010FFFF.  UTF-7 is way too complex and not supported by glibc-2.1.
39    UCS-4 specification leaves doubts about endianness and byte order mark.
40    glibc currently interprets it as big endian without byte order mark,
41    but this is not backed by an RFC.  So we use UTF-8. It supports
42    characters up to \U7FFFFFFF and is unambiguously defined.  */
43
44 void
45 ulc_wordbreaks (const char *s, size_t n, char *p)
46 {
47   if (n > 0)
48     {
49       const char *encoding = locale_charset ();
50
51       if (is_utf8_encoding (encoding))
52         u8_wordbreaks ((const uint8_t *) s, n, p);
53       else
54         {
55           /* Convert the string to UTF-8 and build a translation table
56              from offsets into s to offsets into the translated string.  */
57           size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
58
59           if (offsets != NULL)
60             {
61               uint8_t *t = NULL;
62               size_t m;
63               if (u8_conv_from_encoding (encoding, iconveh_question_mark,
64                                          s, n, offsets, &t, &m)
65                   == 0)
66                 {
67                   char *q = (char *) (m > 0 ? malloc (m) : NULL);
68
69                   if (m == 0 || q != NULL)
70                     {
71                       size_t i;
72
73                       /* Determine the word breaks of the UTF-8 string.  */
74                       u8_wordbreaks (t, m, q);
75
76                       /* Translate the result back to the original string.  */
77                       memset (p, 0, n);
78                       for (i = 0; i < n; i++)
79                         if (offsets[i] != (size_t)(-1))
80                           p[i] = q[offsets[i]];
81
82                       free (q);
83                       free (t);
84                       free (offsets);
85                       return;
86                     }
87                   free (t);
88                 }
89               free (offsets);
90             }
91
92           /* Impossible to convert.  */
93 #if C_CTYPE_ASCII
94           if (is_all_ascii (s, n))
95             {
96               /* ASCII is a subset of UTF-8.  */
97               u8_wordbreaks ((const uint8_t *) s, n, p);
98               return;
99             }
100 #endif
101           /* We have a non-ASCII string and cannot convert it.
102              Don't produce any word breaks.  */
103           memset (p, 0, n);
104         }
105     }
106 }
107
108
109 #ifdef TEST
110
111 #include <locale.h>
112 #include <stdio.h>
113 #include <stdlib.h>
114
115 /* Read the contents of an input stream, and return it, terminated with a NUL
116    byte. */
117 char *
118 read_file (FILE *stream)
119 {
120 #define BUFSIZE 4096
121   char *buf = NULL;
122   int alloc = 0;
123   int size = 0;
124   int count;
125
126   while (! feof (stream))
127     {
128       if (size + BUFSIZE > alloc)
129         {
130           alloc = alloc + alloc / 2;
131           if (alloc < size + BUFSIZE)
132             alloc = size + BUFSIZE;
133           buf = realloc (buf, alloc);
134           if (buf == NULL)
135             {
136               fprintf (stderr, "out of memory\n");
137               exit (1);
138             }
139         }
140       count = fread (buf + size, 1, BUFSIZE, stream);
141       if (count == 0)
142         {
143           if (ferror (stream))
144             {
145               perror ("fread");
146               exit (1);
147             }
148         }
149       else
150         size += count;
151     }
152   buf = realloc (buf, size + 1);
153   if (buf == NULL)
154     {
155       fprintf (stderr, "out of memory\n");
156       exit (1);
157     }
158   buf[size] = '\0';
159   return buf;
160 #undef BUFSIZE
161 }
162
163 int
164 main (int argc, char * argv[])
165 {
166   setlocale (LC_CTYPE, "");
167   if (argc == 1)
168     {
169       /* Display all the word breaks in the input string.  */
170       char *input = read_file (stdin);
171       int length = strlen (input);
172       char *breaks = malloc (length);
173       int i;
174
175       ulc_wordbreaks (input, length, breaks);
176
177       for (i = 0; i < length; i++)
178         {
179           switch (breaks[i])
180             {
181             case 1:
182               putc ('|', stdout);
183               break;
184             case 0:
185               break;
186             default:
187               abort ();
188             }
189           putc (input[i], stdout);
190         }
191
192       free (breaks);
193
194       return 0;
195     }
196   else
197     return 1;
198 }
199
200 #endif /* TEST */