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.13 2010/12/12 01:28:24 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 pass_ctls
= FALSE
;
65 static bool w_opt
= FALSE
;
66 static int n_opt
= -1;
68 static attr_t show_attr
;
69 static chtype
*temp_buffer
;
70 static size_t temp_length
;
72 #define TempBuffer(source_cast)
75 ChLen(const char *source
)
77 size_t result
= strlen(source
);
83 for (n
= 0; n
< result
; ++n
) {
84 const char *s
= unctrl(UChar(source
[n
]));
86 adjust
+= (strlen(s
) - 1);
95 ChStr(const char *source
)
98 size_t need
= ChLen(source
) + 1;
101 if (need
> temp_length
) {
102 temp_length
= need
* 2;
103 temp_buffer
= typeRealloc(chtype
, temp_length
, temp_buffer
);
107 chtype ch
= UChar(*source
++);
108 if (!pass_ctls
&& (s
= unctrl(ch
)) != 0) {
110 temp_buffer
[n
++] = UChar(*s
++);
113 temp_buffer
[n
++] = ch
;
115 } while (source
[0] != 0);
117 } else if (temp_buffer
!= 0) {
125 /* color the strings drawn in the workspace */
127 ChStr2(const char *source
)
129 size_t len
= ChLen(source
);
131 chtype
*result
= ChStr(source
);
132 for (n
= 0; n
< len
; ++n
) {
133 result
[n
] |= show_attr
;
139 legend(WINDOW
*win
, int level
, Options state
, char *buffer
, int length
)
141 const char *showstate
;
149 showstate
= " (mvXXX)";
152 showstate
= " (winXXX)";
155 showstate
= " (mvwinXXX)";
161 "The Strings/Chars displays should match. Enter any characters, except:\n");
163 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
165 wprintw(win
, "Level %d,%s added %d characters <%s>", level
,
166 showstate
, length
, buffer
);
170 ColOf(char *buffer
, int length
, int margin
)
175 for (n
= 0, result
= margin
+ 1; n
< length
; ++n
) {
176 int ch
= UChar(buffer
[n
]);
179 /* actually newline should clear the remainder of the line
180 * and move to the next line - but that seems a little awkward
191 result
+= (MY_TABSIZE
- (result
% MY_TABSIZE
));
206 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
210 static bool first
= TRUE
;
222 int margin
= (2 * MY_TABSIZE
) - 1;
223 Options option
= (Options
) ((unsigned) (m_opt
226 | (unsigned) ((w_opt
|| (level
> 0))
232 setlocale(LC_ALL
, "");
234 putenv(strcpy(cmd
, "TABSIZE=8"));
237 (void) cbreak(); /* take input chars one at a time, no wait for \n */
238 (void) noecho(); /* don't echo input */
239 keypad(stdscr
, TRUE
);
244 look
= newwin(limit
, COLS
- (2 * (level
- 1)), 0, level
- 1);
245 work
= newwin(limit
- 2, COLS
- (2 * level
), 1, level
);
246 show
= newwin(4, COLS
, limit
+ 1, 0);
252 show
= derwin(stdscr
, 4, COLS
, limit
+ 1, 0);
256 for (col
= margin
+ 1; col
< COLS
; col
+= MY_TABSIZE
)
257 MvWVLine(work
, row
, col
, '.', limit
- 2);
259 MvWVLine(work
, row
, margin
, ACS_VLINE
, limit
- 2);
260 MvWVLine(work
, row
, margin
+ 1, ACS_VLINE
, limit
- 2);
263 MvWAddChStr(work
, 1, 2, ChStr("String"));
264 MvWAddChStr(work
, limit
+ 1, 2, ChStr("Chars"));
267 buffer
[length
= 0] = '\0';
268 legend(show
, level
, option
, buffer
, length
);
274 * Show the characters added in color, to distinguish from those that
279 init_pair(1, COLOR_WHITE
, COLOR_BLUE
);
280 show_attr
= COLOR_PAIR(1);
281 wbkgdset(work
, show_attr
| ' ');
283 show_attr
= A_STANDOUT
;
286 while ((ch
= read_linedata(work
)) != ERR
&& !isQUIT(ch
)) {
287 wmove(work
, row
, margin
+ 1);
290 test_adds(level
+ 1);
305 /* put the whole string in, all at once */
310 for (col
= 0; col
< length
; col
+= n_opt
) {
311 col2
= ColOf(buffer
, col
, margin
);
312 if (move(row
, col2
) != ERR
) {
313 AddNStr(ChStr2(buffer
+ col
), LEN(col
));
317 if (move(row
, col2
) != ERR
) {
318 AddStr(ChStr2(buffer
));
324 for (col
= 0; col
< length
; col
+= n_opt
) {
325 col2
= ColOf(buffer
, col
, margin
);
326 MvAddNStr(row
, col2
, ChStr2(buffer
+ col
), LEN(col
));
329 MvAddStr(row
, col2
, ChStr2(buffer
));
334 for (col
= 0; col
< length
; col
+= n_opt
) {
335 col2
= ColOf(buffer
, col
, margin
);
336 if (wmove(work
, row
, col2
) != ERR
) {
337 WAddNStr(work
, ChStr2(buffer
+ col
), LEN(col
));
341 if (wmove(work
, row
, col2
) != ERR
) {
342 WAddStr(work
, ChStr2(buffer
));
348 for (col
= 0; col
< length
; col
+= n_opt
) {
349 col2
= ColOf(buffer
, col
, margin
);
350 MvWAddNStr(work
, row
, col2
, ChStr2(buffer
+ col
),
354 MvWAddStr(work
, row
, col2
, ChStr2(buffer
));
359 /* do the corresponding single-character add */
361 for (col
= 0; col
< length
; ++col
) {
362 col2
= ColOf(buffer
, col
, margin
);
365 if (move(row2
, col2
) != ERR
) {
366 AddCh(UChar(buffer
[col
]));
370 MvAddCh(row2
, col2
, UChar(buffer
[col
]));
373 if (wmove(work
, row2
, col2
) != ERR
) {
374 WAddCh(work
, UChar(buffer
[col
]));
378 MvWAddCh(work
, row2
, col2
, UChar(buffer
[col
]));
390 if (ch
<= 0 || ch
> 255) {
394 buffer
[length
++] = (char) ch
;
395 buffer
[length
] = '\0';
397 /* put the string in, one character at a time */
398 col
= ColOf(buffer
, length
- 1, margin
);
401 if (move(row
, col
) != ERR
) {
402 AddStr(ChStr2(buffer
+ length
- 1));
406 MvAddStr(row
, col
, ChStr2(buffer
+ length
- 1));
409 if (wmove(work
, row
, col
) != ERR
) {
410 WAddStr(work
, ChStr2(buffer
+ length
- 1));
414 MvWAddStr(work
, row
, col
, ChStr2(buffer
+ length
- 1));
418 /* do the corresponding single-character add */
421 if (move(limit
+ row
, col
) != ERR
) {
426 MvAddCh(limit
+ row
, col
, UChar(ch
));
429 if (wmove(work
, limit
+ row
, col
) != ERR
) {
430 WAddCh(work
, UChar(ch
));
434 MvWAddCh(work
, limit
+ row
, col
, UChar(ch
));
440 legend(show
, level
, option
, buffer
, length
);
457 static const char *tbl
[] =
459 "Usage: test_addchstr [options]"
462 ," -f FILE read data from given file"
463 ," -n NUM limit string-adds to NUM bytes on ^N replay"
464 ," -m perform wmove/move separately from add-functions"
465 ," -p pass-thru control characters without using unctrl()"
466 ," -w use window-parameter even when stdscr would be implied"
469 for (n
= 0; n
< SIZEOF(tbl
); ++n
)
470 fprintf(stderr
, "%s\n", tbl
[n
]);
471 ExitProgram(EXIT_FAILURE
);
475 main(int argc GCC_UNUSED
, char *argv
[]GCC_UNUSED
)
479 setlocale(LC_ALL
, "");
481 while ((ch
= getopt(argc
, argv
, "f:mn:pw")) != -1) {
484 init_linedata(optarg
);
490 n_opt
= atoi(optarg
);
510 ExitProgram(EXIT_SUCCESS
);