1 /****************************************************************************
2 * Copyright (c) 2009,2010 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_addchstr.c,v 1.6 2010/05/01 19:13:46 tom Exp $
31 * Demonstrate the waddchstr() and waddch functions.
32 * Thomas Dickey - 2009/9/12
35 #include <test.priv.h>
42 #define AddNStr addchnstr
43 #define AddStr addchstr
44 #define MvAddNStr (void) mvaddchnstr
45 #define MvAddStr (void) mvaddchstr
46 #define MvWAddNStr (void) mvwaddchnstr
47 #define MvWAddStr (void) mvwaddchstr
48 #define WAddNStr waddchnstr
49 #define WAddStr waddchstr
63 static bool m_opt = FALSE;
64 static bool w_opt = FALSE;
65 static int n_opt = -1;
67 static chtype *temp_buffer;
68 static size_t temp_length;
70 #define TempBuffer(source_cast)
73 ChStr(const char *source)
76 size_t need = strlen(source) + 1;
80 if (need > temp_length) {
81 temp_length = need * 2;
82 temp_buffer = typeRealloc(chtype, temp_length, temp_buffer);
87 temp_buffer[n++] = UChar(*source++);
88 } while (have[0] != 0);
89 } else if (temp_buffer != 0) {
98 legend(WINDOW *win, int level, Options state, char *buffer, int length)
100 NCURSES_CONST char *showstate;
108 showstate = " (mvXXX)";
111 showstate = " (winXXX)";
114 showstate = " (mvwinXXX)";
120 "The Strings/Chars displays should match. Enter any characters, except:\n");
122 "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
124 wprintw(win, "Level %d,%s added %d characters <%s>", level,
125 showstate, length, buffer);
129 ColOf(char *buffer, int length, int margin)
134 for (n = 0, result = margin + 1; n < length; ++n) {
135 int ch = UChar(buffer[n]);
138 /* actually newline should clear the remainder of the line
139 * and move to the next line - but that seems a little awkward
150 result += (MY_TABSIZE - (result % MY_TABSIZE));
165 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
169 static bool first = TRUE;
181 int margin = (2 * MY_TABSIZE) - 1;
182 Options option = (Options) ((unsigned) (m_opt
185 | (unsigned) ((w_opt || (level > 0))
191 setlocale(LC_ALL, "");
193 putenv(strcpy(cmd, "TABSIZE=8"));
196 (void) cbreak(); /* take input chars one at a time, no wait for \n */
197 (void) noecho(); /* don't echo input */
198 keypad(stdscr, TRUE);
203 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
204 work = newwin(limit - 2, COLS - (2 * level), 1, level);
205 show = newwin(4, COLS, limit + 1, 0);
211 show = derwin(stdscr, 4, COLS, limit + 1, 0);
215 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
216 MvWVLine(work, row, col, '.', limit - 2);
218 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
219 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
222 MvWAddChStr(work, 1, 2, ChStr("String"));
223 MvWAddChStr(work, limit + 1, 2, ChStr("Chars"));
226 buffer[length = 0] = '\0';
227 legend(show, level, option, buffer, length);
233 * Show the characters added in color, to distinguish from those that
238 init_pair(1, COLOR_WHITE, COLOR_BLUE);
239 wbkgdset(work, COLOR_PAIR(1) | ' ');
242 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
243 wmove(work, row, margin + 1);
246 test_adds(level + 1);
261 /* put the whole string in, all at once */
266 for (col = 0; col < length; col += n_opt) {
267 col2 = ColOf(buffer, col, margin);
268 if (move(row, col2) != ERR) {
269 AddNStr(ChStr(buffer + col), LEN(col));
273 if (move(row, col2) != ERR) {
274 AddStr(ChStr(buffer));
280 for (col = 0; col < length; col += n_opt) {
281 col2 = ColOf(buffer, col, margin);
282 MvAddNStr(row, col2, ChStr(buffer + col), LEN(col));
285 MvAddStr(row, col2, ChStr(buffer));
290 for (col = 0; col < length; col += n_opt) {
291 col2 = ColOf(buffer, col, margin);
292 if (wmove(work, row, col2) != ERR) {
293 WAddNStr(work, ChStr(buffer + col), LEN(col));
297 if (wmove(work, row, col2) != ERR) {
298 WAddStr(work, ChStr(buffer));
304 for (col = 0; col < length; col += n_opt) {
305 col2 = ColOf(buffer, col, margin);
306 MvWAddNStr(work, row, col2, ChStr(buffer + col),
310 MvWAddStr(work, row, col2, ChStr(buffer));
315 /* do the corresponding single-character add */
317 for (col = 0; col < length; ++col) {
318 col2 = ColOf(buffer, col, margin);
321 if (move(row2, col2) != ERR) {
322 AddCh(UChar(buffer[col]));
326 MvAddCh(row2, col2, UChar(buffer[col]));
329 if (wmove(work, row2, col2) != ERR) {
330 WAddCh(work, UChar(buffer[col]));
334 MvWAddCh(work, row2, col2, UChar(buffer[col]));
346 if (ch <= 0 || ch > 255) {
350 buffer[length++] = (char) ch;
351 buffer[length] = '\0';
353 /* put the string in, one character at a time */
354 col = ColOf(buffer, length - 1, margin);
357 if (move(row, col) != ERR) {
358 AddStr(ChStr(buffer + length - 1));
362 MvAddStr(row, col, ChStr(buffer + length - 1));
365 if (wmove(work, row, col) != ERR) {
366 WAddStr(work, ChStr(buffer + length - 1));
370 MvWAddStr(work, row, col, ChStr(buffer + length - 1));
374 /* do the corresponding single-character add */
377 if (move(limit + row, col) != ERR) {
382 MvAddCh(limit + row, col, UChar(ch));
385 if (wmove(work, limit + row, col) != ERR) {
386 WAddCh(work, UChar(ch));
390 MvWAddCh(work, limit + row, col, UChar(ch));
396 legend(show, level, option, buffer, length);
413 static const char *tbl[] =
415 "Usage: test_addchstr [options]"
418 ," -f FILE read data from given file"
419 ," -n NUM limit string-adds to NUM bytes on ^N replay"
420 ," -m perform wmove/move separately from add-functions"
421 ," -w use window-parameter even when stdscr would be implied"
424 for (n = 0; n < SIZEOF(tbl); ++n)
425 fprintf(stderr, "%s\n", tbl[n]);
426 ExitProgram(EXIT_FAILURE);
430 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
434 setlocale(LC_ALL, "");
436 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
439 init_linedata(optarg);
445 n_opt = atoi(optarg);
462 ExitProgram(EXIT_SUCCESS);