This source file includes following definitions.
- ulc_possible_linebreaks
- read_file
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #include <config.h>
27
28
29 #include "unilbrk.h"
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "c-ctype.h"
35 #include "uniconv.h"
36 #include "unilbrk/ulc-common.h"
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 void
52 ulc_possible_linebreaks (const char *s, size_t n, const char *encoding,
53 char *p)
54 {
55 if (n > 0)
56 {
57 if (is_utf8_encoding (encoding))
58 u8_possible_linebreaks ((const uint8_t *) s, n, encoding, p);
59 else
60 {
61
62
63 size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
64
65 if (offsets != NULL)
66 {
67 uint8_t *t;
68 size_t m;
69
70 t = u8_conv_from_encoding (encoding, iconveh_question_mark,
71 s, n, offsets, NULL, &m);
72 if (t != NULL)
73 {
74 char *q = (char *) (m > 0 ? malloc (m) : NULL);
75
76 if (m == 0 || q != NULL)
77 {
78 size_t i;
79
80
81
82 u8_possible_linebreaks (t, m, encoding, q);
83
84
85 memset (p, UC_BREAK_PROHIBITED, n);
86 for (i = 0; i < n; i++)
87 if (offsets[i] != (size_t)(-1))
88 p[i] = q[offsets[i]];
89
90 free (q);
91 free (t);
92 free (offsets);
93 return;
94 }
95 free (t);
96 }
97 free (offsets);
98 }
99
100
101 #if C_CTYPE_ASCII
102 if (is_all_ascii (s, n))
103 {
104
105 u8_possible_linebreaks ((const uint8_t *) s, n, encoding, p);
106 return;
107 }
108 #endif
109
110
111
112
113 {
114 const char *s_end = s + n;
115 while (s < s_end)
116 {
117 *p = (*s == '\n' ? UC_BREAK_MANDATORY : UC_BREAK_PROHIBITED);
118 s++;
119 p++;
120 }
121 }
122 }
123 }
124 }
125
126
127 #ifdef TEST
128
129 #include <stdio.h>
130 #include <locale.h>
131 #include <string.h>
132
133
134
135 char *
136 read_file (FILE *stream)
137 {
138 #define BUFSIZE 4096
139 char *buf = NULL;
140 int alloc = 0;
141 int size = 0;
142 int count;
143
144 while (! feof (stream))
145 {
146 if (size + BUFSIZE > alloc)
147 {
148 alloc = alloc + alloc / 2;
149 if (alloc < size + BUFSIZE)
150 alloc = size + BUFSIZE;
151 buf = realloc (buf, alloc);
152 if (buf == NULL)
153 {
154 fprintf (stderr, "out of memory\n");
155 exit (1);
156 }
157 }
158 count = fread (buf + size, 1, BUFSIZE, stream);
159 if (count == 0)
160 {
161 if (ferror (stream))
162 {
163 perror ("fread");
164 exit (1);
165 }
166 }
167 else
168 size += count;
169 }
170 buf = realloc (buf, size + 1);
171 if (buf == NULL)
172 {
173 fprintf (stderr, "out of memory\n");
174 exit (1);
175 }
176 buf[size] = '\0';
177 return buf;
178 #undef BUFSIZE
179 }
180
181 int
182 main (int argc, char * argv[])
183 {
184 setlocale (LC_CTYPE, "");
185 if (argc == 1)
186 {
187
188 char *input = read_file (stdin);
189 int length = strlen (input);
190 char *breaks = malloc (length);
191 int i;
192
193 ulc_possible_linebreaks (input, length, locale_charset (), breaks);
194
195 for (i = 0; i < length; i++)
196 {
197 switch (breaks[i])
198 {
199 case UC_BREAK_POSSIBLE:
200 putc ('|', stdout);
201 break;
202 case UC_BREAK_MANDATORY:
203 break;
204 case UC_BREAK_PROHIBITED:
205 break;
206 default:
207 abort ();
208 }
209 putc (input[i], stdout);
210 }
211
212 free (breaks);
213
214 return 0;
215 }
216 else
217 return 1;
218 }
219
220 #endif