1 /****************************************************************************
2 * Copyright (c) 2010-2011,2012 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.20 2012/12/16 00:12:04 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); \
94 failed("TempBuffer"); \
99 have[0] = source_cast; \
103 && (temp = unctrl((chtype) have[0])) != 0 \
104 && strlen(temp) > 1) { \
105 while (*temp != '\0') { \
107 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
110 setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
112 } while (have[0] != 0); \
113 } else if (temp_buffer != 0) { \
121 ChWLen(const wchar_t *source)
123 size_t result = wcslen(source);
130 for (n = 0; n < result; ++n) {
131 if (source[n] < 256 && (s = unctrl((chtype) source[n])) != 0) {
132 adjust += (strlen(s) - 1);
141 ChStr(const char *source)
143 TempBuffer(strlen(source), UChar(*source++));
147 ChWStr(const wchar_t *source)
149 TempBuffer(ChWLen(source), *source++);
153 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
155 const char *showstate;
163 showstate = " (mvXXX)";
166 showstate = " (winXXX)";
169 showstate = " (mvwinXXX)";
175 "The Strings/Chars displays should match. Enter any characters, except:\n");
177 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
179 wprintw(win, "Level %d,%s added %d characters <", level,
181 waddwstr(win, buffer);
186 ColOf(wchar_t *buffer, int length, int margin)
191 for (n = 0, result = margin + 1; n < length; ++n) {
195 /* actually newline should clear the remainder of the line
196 * and move to the next line - but that seems a little awkward
207 result += (MY_TABSIZE - (result % MY_TABSIZE));
213 result += wcwidth(ch);
223 ConvertCh(chtype source, cchar_t *target)
225 wchar_t tmp_wchar[2];
227 tmp_wchar[0] = (wchar_t) source;
229 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
237 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
242 if (ConvertCh(ch, &tmp_cchar)) {
243 code = mvwadd_wch(win, y, x, &tmp_cchar);
245 code = mvwaddch(win, y, x, ch);
251 MvAddCh(int y, int x, chtype ch)
256 if (ConvertCh(ch, &tmp_cchar)) {
257 code = mvadd_wch(y, x, &tmp_cchar);
259 code = mvaddch(y, x, ch);
265 WAddCh(WINDOW *win, chtype ch)
270 if (ConvertCh(ch, &tmp_cchar)) {
271 code = wadd_wch(win, &tmp_cchar);
273 code = waddch(win, ch);
284 if (ConvertCh(ch, &tmp_cchar)) {
285 code = add_wch(&tmp_cchar);
292 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
294 test_add_wchstr(int level)
296 static bool first = TRUE;
304 wchar_t buffer[BUFSIZ];
308 int margin = (2 * MY_TABSIZE) - 1;
309 Options option = ((m_opt ? oMove : oDefault)
310 | ((w_opt || (level > 0)) ? oWindow : oDefault));
314 setlocale(LC_ALL, "");
316 putenv(strcpy(cmd, "TABSIZE=8"));
319 (void) cbreak(); /* take input chars one at a time, no wait for \n */
320 (void) noecho(); /* don't echo input */
321 keypad(stdscr, TRUE);
324 * Show the characters added in color, to distinguish from those that
329 init_pair(1, COLOR_WHITE, COLOR_BLUE);
335 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
336 work = newwin(limit - 2, COLS - (2 * level), 1, level);
337 show = newwin(4, COLS, limit + 1, 0);
343 show = derwin(stdscr, 4, COLS, limit + 1, 0);
347 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
348 MvWVLine(work, row, col, '.', limit - 2);
350 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
351 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
354 (void) mvwadd_wchstr(work, 1, 2, ChStr("String"));
355 (void) mvwadd_wchstr(work, limit + 1, 2, ChStr("Chars"));
358 buffer[length = 0] = '\0';
359 legend(show, level, option, buffer, length);
365 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
368 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
369 wmove(work, row, margin + 1);
372 test_add_wchstr(level + 1);
389 /* put the whole string in, all at once */
394 for (col = 0; col < length; col += n_opt) {
395 col2 = ColOf(buffer, col, margin);
396 if (move(row, col2) != ERR) {
397 AddNStr(ChWStr(buffer + col), LEN(col));
401 if (move(row, col2) != ERR) {
402 AddStr(ChWStr(buffer));
408 for (col = 0; col < length; col += n_opt) {
409 col2 = ColOf(buffer, col, margin);
410 MvAddNStr(row, col2, ChWStr(buffer + col), LEN(col));
413 MvAddStr(row, col2, ChWStr(buffer));
418 for (col = 0; col < length; col += n_opt) {
419 col2 = ColOf(buffer, col, margin);
420 if (wmove(work, row, col2) != ERR) {
421 WAddNStr(work, ChWStr(buffer + col), LEN(col));
425 if (wmove(work, row, col2) != ERR) {
426 WAddStr(work, ChWStr(buffer));
432 for (col = 0; col < length; col += n_opt) {
433 col2 = ColOf(buffer, col, margin);
434 MvWAddNStr(work, row, col2, ChWStr(buffer +
438 MvWAddStr(work, row, col2, ChWStr(buffer));
443 /* do the corresponding single-character add */
445 for (col = 0; col < length; ++col) {
446 col2 = ColOf(buffer, col, margin);
449 if (move(row2, col2) != ERR) {
450 AddCh((chtype) buffer[col]);
454 MvAddCh(row2, col2, (chtype) buffer[col]);
457 if (wmove(work, row2, col2) != ERR) {
458 WAddCh(work, (chtype) buffer[col]);
462 MvWAddCh(work, row2, col2, (chtype) buffer[col]);
471 buffer[length++] = ch;
472 buffer[length] = '\0';
474 /* put the string in, one character at a time */
475 col = ColOf(buffer, length - 1, margin);
478 if (move(row, col) != ERR) {
479 AddStr(ChWStr(buffer + length - 1));
483 MvAddStr(row, col, ChWStr(buffer + length - 1));
486 if (wmove(work, row, col) != ERR) {
487 WAddStr(work, ChWStr(buffer + length - 1));
491 MvWAddStr(work, row, col, ChWStr(buffer + length - 1));
495 /* do the corresponding single-character add */
498 if (move(limit + row, col) != ERR) {
503 MvAddCh(limit + row, col, (chtype) ch);
506 if (wmove(work, limit + row, col) != ERR) {
507 WAddCh(work, (chtype) ch);
511 MvWAddCh(work, limit + row, col, (chtype) ch);
517 legend(show, level, option, buffer, length);
534 static const char *tbl[] =
536 "Usage: test_add_wchstr [options]"
539 ," -f FILE read data from given file"
540 ," -n NUM limit string-adds to NUM bytes on ^N replay"
541 ," -m perform wmove/move separately from add-functions"
542 ," -p pass-thru control characters without using unctrl()"
543 ," -w use window-parameter even when stdscr would be implied"
546 for (n = 0; n < SIZEOF(tbl); ++n)
547 fprintf(stderr, "%s\n", tbl[n]);
548 ExitProgram(EXIT_FAILURE);
552 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
556 setlocale(LC_ALL, "");
558 while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
561 init_linedata(optarg);
567 n_opt = atoi(optarg);
587 ExitProgram(EXIT_SUCCESS);
593 printf("This program requires the wide-ncurses library\n");
594 ExitProgram(EXIT_FAILURE);