1 /****************************************************************************
2 * Copyright (c) 2009,2010,2011 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: test_add_wchstr.c,v 1.15 2011/01/15 18:15:11 tom Exp $
31 * Demonstrate the waddwchstr() and wadd_wch functions.
32 * Thomas Dickey - 2009/9/12
34 * Note: to provide inputs for *add_wch(), we use setcchar(). A quirk of the
35 * X/Open definition for that function is that the string contains no
36 * characters with negative width. Any control character (such as tab) falls
37 * into that category. So it follows that *add_wch() cannot render a tab
38 * character because there is no legal way to construct a cchar_t containing
39 * one. X/Open does not document this, and it would be logical to assume that
40 * *addwchstr() has the same limitation, but it uses a wchar_t string directly,
41 * and does not document how tabs are handled.
44 #include <test.priv.h>
56 /* definitions to make it simpler to compare with test_addstr.c */
57 #define AddNStr add_wchnstr
58 #define AddStr add_wchstr
59 #define MvAddNStr (void) mvadd_wchnstr
60 #define MvAddStr (void) mvadd_wchstr
61 #define MvWAddNStr (void) mvwadd_wchnstr
62 #define MvWAddStr (void) mvwadd_wchstr
63 #define WAddNStr wadd_wchnstr
64 #define WAddStr wadd_wchstr
75 static bool m_opt
= FALSE
;
76 static bool pass_ctls
= FALSE
;
77 static bool w_opt
= FALSE
;
78 static int n_opt
= -1;
80 static cchar_t
*temp_buffer
;
81 static size_t temp_length
;
83 #define TempBuffer(source_len, source_cast) \
86 size_t need = source_len + 1; \
90 if (need > temp_length) { \
91 temp_length = need * 2; \
92 temp_buffer = typeRealloc(cchar_t, temp_length, temp_buffer); \
97 have[0] = source_cast; \
101 && (temp = unctrl((chtype) have[0])) != 0 \
102 && strlen(temp) > 1) { \
103 while (*temp != '\0') { \
105 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
108 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
110 } while (have[0] != 0); \
111 } else if (temp_buffer != 0) { \
119 ChWLen(const wchar_t *source
)
121 size_t result
= wcslen(source
);
128 for (n
= 0; n
< result
; ++n
) {
129 if (source
[n
] < 256 && (s
= unctrl((chtype
) source
[n
])) != 0) {
130 adjust
+= (strlen(s
) - 1);
139 ChStr(const char *source
)
141 TempBuffer(strlen(source
), UChar(*source
++));
145 ChWStr(const wchar_t *source
)
147 TempBuffer(ChWLen(source
), *source
++);
151 legend(WINDOW
*win
, int level
, Options state
, wchar_t *buffer
, int length
)
153 const char *showstate
;
161 showstate
= " (mvXXX)";
164 showstate
= " (winXXX)";
167 showstate
= " (mvwinXXX)";
173 "The Strings/Chars displays should match. Enter any characters, except:\n");
175 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
177 wprintw(win
, "Level %d,%s added %d characters <", level
,
179 waddwstr(win
, buffer
);
184 ColOf(wchar_t *buffer
, int length
, int margin
)
189 for (n
= 0, result
= margin
+ 1; n
< length
; ++n
) {
193 /* actually newline should clear the remainder of the line
194 * and move to the next line - but that seems a little awkward
205 result
+= (MY_TABSIZE
- (result
% MY_TABSIZE
));
211 result
+= wcwidth(ch
);
221 ConvertCh(chtype source
, cchar_t
*target
)
223 wchar_t tmp_wchar
[2];
225 tmp_wchar
[0] = (wchar_t) source
;
227 if (setcchar(target
, tmp_wchar
, A_NORMAL
, 0, (void *) 0) == ERR
) {
235 MvWAddCh(WINDOW
*win
, int y
, int x
, chtype ch
)
240 if (ConvertCh(ch
, &tmp_cchar
)) {
241 code
= mvwadd_wch(win
, y
, x
, &tmp_cchar
);
243 code
= mvwaddch(win
, y
, x
, ch
);
249 MvAddCh(int y
, int x
, chtype ch
)
254 if (ConvertCh(ch
, &tmp_cchar
)) {
255 code
= mvadd_wch(y
, x
, &tmp_cchar
);
257 code
= mvaddch(y
, x
, ch
);
263 WAddCh(WINDOW
*win
, chtype ch
)
268 if (ConvertCh(ch
, &tmp_cchar
)) {
269 code
= wadd_wch(win
, &tmp_cchar
);
271 code
= waddch(win
, ch
);
282 if (ConvertCh(ch
, &tmp_cchar
)) {
283 code
= add_wch(&tmp_cchar
);
290 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
292 test_add_wchstr(int level
)
294 static bool first
= TRUE
;
302 wchar_t buffer
[BUFSIZ
];
306 int margin
= (2 * MY_TABSIZE
) - 1;
307 Options option
= ((m_opt
? oMove
: oDefault
)
308 | ((w_opt
|| (level
> 0)) ? oWindow
: oDefault
));
312 setlocale(LC_ALL
, "");
314 putenv(strcpy(cmd
, "TABSIZE=8"));
317 (void) cbreak(); /* take input chars one at a time, no wait for \n */
318 (void) noecho(); /* don't echo input */
319 keypad(stdscr
, TRUE
);
324 look
= newwin(limit
, COLS
- (2 * (level
- 1)), 0, level
- 1);
325 work
= newwin(limit
- 2, COLS
- (2 * level
), 1, level
);
326 show
= newwin(4, COLS
, limit
+ 1, 0);
332 show
= derwin(stdscr
, 4, COLS
, limit
+ 1, 0);
336 for (col
= margin
+ 1; col
< COLS
; col
+= MY_TABSIZE
)
337 MvWVLine(work
, row
, col
, '.', limit
- 2);
339 MvWVLine(work
, row
, margin
, ACS_VLINE
, limit
- 2);
340 MvWVLine(work
, row
, margin
+ 1, ACS_VLINE
, limit
- 2);
343 (void) mvwadd_wchstr(work
, 1, 2, ChStr("String"));
344 (void) mvwadd_wchstr(work
, limit
+ 1, 2, ChStr("Chars"));
347 buffer
[length
= 0] = '\0';
348 legend(show
, level
, option
, buffer
, length
);
354 * Show the characters added in color, to distinguish from those that
359 init_pair(1, COLOR_WHITE
, COLOR_BLUE
);
360 wbkgdset(work
, COLOR_PAIR(1) | ' ');
363 while ((ch
= read_linedata(work
)) != ERR
&& !isQUIT(ch
)) {
364 wmove(work
, row
, margin
+ 1);
367 test_add_wchstr(level
+ 1);
382 /* put the whole string in, all at once */
387 for (col
= 0; col
< length
; col
+= n_opt
) {
388 col2
= ColOf(buffer
, col
, margin
);
389 if (move(row
, col2
) != ERR
) {
390 AddNStr(ChWStr(buffer
+ col
), LEN(col
));
394 if (move(row
, col2
) != ERR
) {
395 AddStr(ChWStr(buffer
));
401 for (col
= 0; col
< length
; col
+= n_opt
) {
402 col2
= ColOf(buffer
, col
, margin
);
403 MvAddNStr(row
, col2
, ChWStr(buffer
+ col
), LEN(col
));
406 MvAddStr(row
, col2
, ChWStr(buffer
));
411 for (col
= 0; col
< length
; col
+= n_opt
) {
412 col2
= ColOf(buffer
, col
, margin
);
413 if (wmove(work
, row
, col2
) != ERR
) {
414 WAddNStr(work
, ChWStr(buffer
+ col
), LEN(col
));
418 if (wmove(work
, row
, col2
) != ERR
) {
419 WAddStr(work
, ChWStr(buffer
));
425 for (col
= 0; col
< length
; col
+= n_opt
) {
426 col2
= ColOf(buffer
, col
, margin
);
427 MvWAddNStr(work
, row
, col2
, ChWStr(buffer
+
431 MvWAddStr(work
, row
, col2
, ChWStr(buffer
));
436 /* do the corresponding single-character add */
438 for (col
= 0; col
< length
; ++col
) {
439 col2
= ColOf(buffer
, col
, margin
);
442 if (move(row2
, col2
) != ERR
) {
443 AddCh((chtype
) buffer
[col
]);
447 MvAddCh(row2
, col2
, (chtype
) buffer
[col
]);
450 if (wmove(work
, row2
, col2
) != ERR
) {
451 WAddCh(work
, (chtype
) buffer
[col
]);
455 MvWAddCh(work
, row2
, col2
, (chtype
) buffer
[col
]);
464 buffer
[length
++] = ch
;
465 buffer
[length
] = '\0';
467 /* put the string in, one character at a time */
468 col
= ColOf(buffer
, length
- 1, margin
);
471 if (move(row
, col
) != ERR
) {
472 AddStr(ChWStr(buffer
+ length
- 1));
476 MvAddStr(row
, col
, ChWStr(buffer
+ length
- 1));
479 if (wmove(work
, row
, col
) != ERR
) {
480 WAddStr(work
, ChWStr(buffer
+ length
- 1));
484 MvWAddStr(work
, row
, col
, ChWStr(buffer
+ length
- 1));
488 /* do the corresponding single-character add */
491 if (move(limit
+ row
, col
) != ERR
) {
496 MvAddCh(limit
+ row
, col
, (chtype
) ch
);
499 if (wmove(work
, limit
+ row
, col
) != ERR
) {
500 WAddCh(work
, (chtype
) ch
);
504 MvWAddCh(work
, limit
+ row
, col
, (chtype
) ch
);
510 legend(show
, level
, option
, buffer
, length
);
527 static const char *tbl
[] =
529 "Usage: test_add_wchstr [options]"
532 ," -f FILE read data from given file"
533 ," -n NUM limit string-adds to NUM bytes on ^N replay"
534 ," -m perform wmove/move separately from add-functions"
535 ," -p pass-thru control characters without using unctrl()"
536 ," -w use window-parameter even when stdscr would be implied"
539 for (n
= 0; n
< SIZEOF(tbl
); ++n
)
540 fprintf(stderr
, "%s\n", tbl
[n
]);
541 ExitProgram(EXIT_FAILURE
);
545 main(int argc GCC_UNUSED
, char *argv
[]GCC_UNUSED
)
549 setlocale(LC_ALL
, "");
551 while ((ch
= getopt(argc
, argv
, "f:mn:pw")) != -1) {
554 init_linedata(optarg
);
560 n_opt
= atoi(optarg
);
580 ExitProgram(EXIT_SUCCESS
);
586 printf("This program requires the wide-ncurses library\n");
587 ExitProgram(EXIT_FAILURE
);