This source file includes following definitions.
- u8_width_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 "unistr.h"
32 #include "uniwidth.h"
33
34 int
35 u8_width_linebreaks (const uint8_t *s, size_t n,
36 int width, int start_column, int at_end_columns,
37 const char *o, const char *encoding,
38 char *p)
39 {
40 const uint8_t *s_end;
41 char *last_p;
42 int last_column;
43 int piece_width;
44
45 u8_possible_linebreaks (s, n, encoding, p);
46
47 s_end = s + n;
48 last_p = NULL;
49 last_column = start_column;
50 piece_width = 0;
51 while (s < s_end)
52 {
53 ucs4_t uc;
54 int count = u8_mbtouc_unsafe (&uc, s, s_end - s);
55
56
57 if (o != NULL && *o != UC_BREAK_UNDEFINED)
58 *p = *o;
59
60 if (*p == UC_BREAK_POSSIBLE || *p == UC_BREAK_MANDATORY)
61 {
62
63 if (last_p != NULL && last_column + piece_width > width)
64 {
65
66 *last_p = UC_BREAK_POSSIBLE;
67 last_column = 0;
68 }
69 }
70
71 if (*p == UC_BREAK_MANDATORY)
72 {
73
74
75 last_p = NULL;
76 last_column = 0;
77 piece_width = 0;
78 }
79 else
80 {
81
82 int w;
83
84 if (*p == UC_BREAK_POSSIBLE)
85 {
86
87 last_p = p;
88 last_column += piece_width;
89 piece_width = 0;
90
91
92 }
93
94 *p = UC_BREAK_PROHIBITED;
95
96 w = uc_width (uc, encoding);
97 if (w >= 0)
98 piece_width += w;
99 }
100
101 s += count;
102 p += count;
103 if (o != NULL)
104 o += count;
105 }
106
107
108 if (last_p != NULL && last_column + piece_width + at_end_columns > width)
109 {
110
111 *last_p = UC_BREAK_POSSIBLE;
112 last_column = 0;
113 }
114
115 return last_column + piece_width;
116 }
117
118
119 #ifdef TEST
120
121 #include <stdio.h>
122 #include <stdlib.h>
123 #include <string.h>
124
125
126
127 char *
128 read_file (FILE *stream)
129 {
130 #define BUFSIZE 4096
131 char *buf = NULL;
132 int alloc = 0;
133 int size = 0;
134 int count;
135
136 while (! feof (stream))
137 {
138 if (size + BUFSIZE > alloc)
139 {
140 alloc = alloc + alloc / 2;
141 if (alloc < size + BUFSIZE)
142 alloc = size + BUFSIZE;
143 buf = realloc (buf, alloc);
144 if (buf == NULL)
145 {
146 fprintf (stderr, "out of memory\n");
147 exit (1);
148 }
149 }
150 count = fread (buf + size, 1, BUFSIZE, stream);
151 if (count == 0)
152 {
153 if (ferror (stream))
154 {
155 perror ("fread");
156 exit (1);
157 }
158 }
159 else
160 size += count;
161 }
162 buf = realloc (buf, size + 1);
163 if (buf == NULL)
164 {
165 fprintf (stderr, "out of memory\n");
166 exit (1);
167 }
168 buf[size] = '\0';
169 return buf;
170 #undef BUFSIZE
171 }
172
173 int
174 main (int argc, char * argv[])
175 {
176 if (argc == 2)
177 {
178
179 int width = atoi (argv[1]);
180 char *input = read_file (stdin);
181 int length = strlen (input);
182 char *breaks = malloc (length);
183 int i;
184
185 u8_width_linebreaks ((uint8_t *) input, length, width, 0, 0, NULL, "UTF-8", breaks);
186
187 for (i = 0; i < length; i++)
188 {
189 switch (breaks[i])
190 {
191 case UC_BREAK_POSSIBLE:
192 putc ('\n', stdout);
193 break;
194 case UC_BREAK_MANDATORY:
195 break;
196 case UC_BREAK_PROHIBITED:
197 break;
198 default:
199 abort ();
200 }
201 putc (input[i], stdout);
202 }
203
204 free (breaks);
205
206 return 0;
207 }
208 else
209 return 1;
210 }
211
212 #endif