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