Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmpdcurses/pdcurses/util.c
3153 views
1
/* PDCurses */
2
3
#include <curspriv.h>
4
5
/*man-start**************************************************************
6
7
util
8
----
9
10
### Synopsis
11
12
char *unctrl(chtype c);
13
void filter(void);
14
void use_env(bool x);
15
int delay_output(int ms);
16
17
int getcchar(const cchar_t *wcval, wchar_t *wch, attr_t *attrs,
18
short *color_pair, void *opts);
19
int setcchar(cchar_t *wcval, const wchar_t *wch, const attr_t attrs,
20
short color_pair, const void *opts);
21
wchar_t *wunctrl(cchar_t *wc);
22
23
int PDC_mbtowc(wchar_t *pwc, const char *s, size_t n);
24
size_t PDC_mbstowcs(wchar_t *dest, const char *src, size_t n);
25
size_t PDC_wcstombs(char *dest, const wchar_t *src, size_t n);
26
27
### Description
28
29
unctrl() expands the text portion of the chtype c into a printable
30
string. Control characters are changed to the "^X" notation; others
31
are passed through. wunctrl() is the wide-character version of the
32
function.
33
34
filter() and use_env() are no-ops in PDCurses.
35
36
delay_output() inserts an ms millisecond pause in output.
37
38
getcchar() works in two modes: When wch is not NULL, it reads the
39
cchar_t pointed to by wcval and stores the attributes in attrs, the
40
color pair in color_pair, and the text in the wide-character string
41
wch. When wch is NULL, getcchar() merely returns the number of wide
42
characters in wcval. In either mode, the opts argument is unused.
43
44
setcchar constructs a cchar_t at wcval from the wide-character text
45
at wch, the attributes in attr and the color pair in color_pair. The
46
opts argument is unused.
47
48
Currently, the length returned by getcchar() is always 1 or 0.
49
Similarly, setcchar() will only take the first wide character from
50
wch, and ignore any others that it "should" take (i.e., combining
51
characters). Nor will it correctly handle any character outside the
52
basic multilingual plane (UCS-2).
53
54
### Return Value
55
56
wunctrl() returns NULL on failure. delay_output() always returns OK.
57
58
getcchar() returns the number of wide characters wcval points to when
59
wch is NULL; when it's not, getcchar() returns OK or ERR.
60
61
setcchar() returns OK or ERR.
62
63
### Portability
64
X/Open ncurses NetBSD
65
unctrl Y Y Y
66
filter Y Y Y
67
use_env Y Y Y
68
delay_output Y Y Y
69
getcchar Y Y Y
70
setcchar Y Y Y
71
wunctrl Y Y Y
72
PDC_mbtowc - - -
73
PDC_mbstowcs - - -
74
PDC_wcstombs - - -
75
76
**man-end****************************************************************/
77
78
#include <stdlib.h>
79
#include <string.h>
80
81
char *unctrl(chtype c)
82
{
83
static char strbuf[3] = {0, 0, 0};
84
85
chtype ic;
86
87
PDC_LOG(("unctrl() - called\n"));
88
89
ic = c & A_CHARTEXT;
90
91
if (ic >= 0x20 && ic != 0x7f) /* normal characters */
92
{
93
strbuf[0] = (char)ic;
94
strbuf[1] = '\0';
95
return strbuf;
96
}
97
98
strbuf[0] = '^'; /* '^' prefix */
99
100
if (ic == 0x7f) /* 0x7f == DEL */
101
strbuf[1] = '?';
102
else /* other control */
103
strbuf[1] = (char)(ic + '@');
104
105
return strbuf;
106
}
107
108
void filter(void)
109
{
110
PDC_LOG(("filter() - called\n"));
111
}
112
113
void use_env(bool x)
114
{
115
PDC_LOG(("use_env() - called: x %d\n", x));
116
}
117
118
int delay_output(int ms)
119
{
120
PDC_LOG(("delay_output() - called: ms %d\n", ms));
121
122
return napms(ms);
123
}
124
125
#ifdef PDC_WIDE
126
int getcchar(const cchar_t *wcval, wchar_t *wch, attr_t *attrs,
127
short *color_pair, void *opts)
128
{
129
if (!wcval)
130
return ERR;
131
132
if (wch)
133
{
134
if (!attrs || !color_pair)
135
return ERR;
136
137
*wch = (*wcval & A_CHARTEXT);
138
*attrs = (*wcval & (A_ATTRIBUTES & ~A_COLOR));
139
*color_pair = PAIR_NUMBER(*wcval & A_COLOR);
140
141
if (*wch)
142
*++wch = L'\0';
143
144
return OK;
145
}
146
else
147
return ((*wcval & A_CHARTEXT) != L'\0');
148
}
149
150
int setcchar(cchar_t *wcval, const wchar_t *wch, const attr_t attrs,
151
short color_pair, const void *opts)
152
{
153
if (!wcval || !wch)
154
return ERR;
155
156
*wcval = *wch | attrs | COLOR_PAIR(color_pair);
157
158
return OK;
159
}
160
161
wchar_t *wunctrl(cchar_t *wc)
162
{
163
static wchar_t strbuf[3] = {0, 0, 0};
164
165
cchar_t ic;
166
167
PDC_LOG(("wunctrl() - called\n"));
168
169
if (!wc)
170
return NULL;
171
172
ic = *wc & A_CHARTEXT;
173
174
if (ic >= 0x20 && ic != 0x7f) /* normal characters */
175
{
176
strbuf[0] = (wchar_t)ic;
177
strbuf[1] = L'\0';
178
return strbuf;
179
}
180
181
strbuf[0] = '^'; /* '^' prefix */
182
183
if (ic == 0x7f) /* 0x7f == DEL */
184
strbuf[1] = '?';
185
else /* other control */
186
strbuf[1] = (wchar_t)(ic + '@');
187
188
return strbuf;
189
}
190
191
int PDC_mbtowc(wchar_t *pwc, const char *s, size_t n)
192
{
193
# ifdef PDC_FORCE_UTF8
194
wchar_t key;
195
int i = -1;
196
const unsigned char *string;
197
198
if (!s || (n < 1))
199
return -1;
200
201
if (!*s)
202
return 0;
203
204
string = (const unsigned char *)s;
205
206
key = string[0];
207
208
/* Simplistic UTF-8 decoder -- only does the BMP, minimal validation */
209
210
if (key & 0x80)
211
{
212
if ((key & 0xe0) == 0xc0)
213
{
214
if (1 < n)
215
{
216
key = ((key & 0x1f) << 6) | (string[1] & 0x3f);
217
i = 2;
218
}
219
}
220
else if ((key & 0xe0) == 0xe0)
221
{
222
if (2 < n)
223
{
224
key = ((key & 0x0f) << 12) | ((string[1] & 0x3f) << 6) |
225
(string[2] & 0x3f);
226
i = 3;
227
}
228
}
229
}
230
else
231
i = 1;
232
233
if (i)
234
*pwc = key;
235
236
return i;
237
# else
238
return mbtowc(pwc, s, n);
239
# endif
240
}
241
242
size_t PDC_mbstowcs(wchar_t *dest, const char *src, size_t n)
243
{
244
# ifdef PDC_FORCE_UTF8
245
size_t i = 0, len;
246
247
if (!src || !dest)
248
return 0;
249
250
len = strlen(src);
251
252
while (*src && i < n)
253
{
254
int retval = PDC_mbtowc(dest + i, src, len);
255
256
if (retval < 1)
257
return -1;
258
259
src += retval;
260
len -= retval;
261
i++;
262
}
263
# else
264
size_t i = mbstowcs(dest, src, n);
265
# endif
266
dest[i] = 0;
267
return i;
268
}
269
270
size_t PDC_wcstombs(char *dest, const wchar_t *src, size_t n)
271
{
272
# ifdef PDC_FORCE_UTF8
273
size_t i = 0;
274
275
if (!src || !dest)
276
return 0;
277
278
while (*src && i < n)
279
{
280
chtype code = *src++;
281
282
if (code < 0x80)
283
{
284
dest[i] = code;
285
i++;
286
}
287
else
288
if (code < 0x800)
289
{
290
dest[i] = ((code & 0x07c0) >> 6) | 0xc0;
291
dest[i + 1] = (code & 0x003f) | 0x80;
292
i += 2;
293
}
294
else
295
{
296
dest[i] = ((code & 0xf000) >> 12) | 0xe0;
297
dest[i + 1] = ((code & 0x0fc0) >> 6) | 0x80;
298
dest[i + 2] = (code & 0x003f) | 0x80;
299
i += 3;
300
}
301
}
302
# else
303
size_t i = wcstombs(dest, src, n);
304
# endif
305
dest[i] = '\0';
306
return i;
307
}
308
#endif
309
310