]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_addchstr.c
ncurses 5.9 - patch 20121026
[ncurses.git] / test / test_addchstr.c
1 /****************************************************************************
2  * Copyright (c) 2009-2010,2012 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: test_addchstr.c,v 1.15 2012/10/27 19:31:42 tom Exp $
30  *
31  * Demonstrate the waddchstr() and waddch functions.
32  * Thomas Dickey - 2009/9/12
33  */
34
35 #include <test.priv.h>
36
37 #include <linedata.h>
38
39 #undef MvAddStr
40 #undef MvWAddStr
41
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
50
51 #define AddCh      addch
52 #define WAddCh     waddch
53
54 #define MY_TABSIZE 8
55
56 typedef enum {
57     oDefault = 0,
58     oMove = 1,
59     oWindow = 2,
60     oMoveWindow = 3
61 } Options;
62
63 static bool m_opt = FALSE;
64 static bool pass_ctls = FALSE;
65 static bool w_opt = FALSE;
66 static int n_opt = -1;
67
68 static attr_t show_attr;
69 static chtype *temp_buffer;
70 static size_t temp_length;
71
72 #define TempBuffer(source_cast)
73
74 static size_t
75 ChLen(const char *source)
76 {
77     size_t result = strlen(source);
78
79     if (!pass_ctls) {
80         size_t adjust = 0;
81         size_t n;
82
83         for (n = 0; n < result; ++n) {
84             const char *s = unctrl(UChar(source[n]));
85             if (s != 0) {
86                 adjust += (strlen(s) - 1);
87             }
88         }
89         result += adjust;
90     }
91     return result;
92 }
93
94 static chtype *
95 ChStr(const char *source)
96 {
97     if (source != 0) {
98         size_t need = ChLen(source) + 1;
99         int n = 0;
100
101         if (need > temp_length) {
102             temp_length = need * 2;
103             temp_buffer = typeRealloc(chtype, temp_length, temp_buffer);
104             if (!temp_buffer)
105                 failed("TempBuffer");
106         }
107         do {
108             const char *s;
109             chtype ch = UChar(*source++);
110             if (!pass_ctls && (s = unctrl(ch)) != 0) {
111                 while (*s != '\0') {
112                     temp_buffer[n++] = UChar(*s++);
113                 }
114             } else {
115                 temp_buffer[n++] = ch;
116             }
117         } while (source[0] != 0);
118         temp_buffer[n] = 0;
119     } else if (temp_buffer != 0) {
120         free(temp_buffer);
121         temp_buffer = 0;
122         temp_length = 0;
123     }
124     return temp_buffer;
125 }
126
127 /* color the strings drawn in the workspace */
128 static chtype *
129 ChStr2(const char *source)
130 {
131     size_t len = ChLen(source);
132     size_t n;
133     chtype *result = ChStr(source);
134     for (n = 0; n < len; ++n) {
135         result[n] |= show_attr;
136     }
137     return result;
138 }
139
140 static void
141 legend(WINDOW *win, int level, Options state, char *buffer, int length)
142 {
143     const char *showstate;
144
145     switch (state) {
146     default:
147     case oDefault:
148         showstate = "";
149         break;
150     case oMove:
151         showstate = " (mvXXX)";
152         break;
153     case oWindow:
154         showstate = " (winXXX)";
155         break;
156     case oMoveWindow:
157         showstate = " (mvwinXXX)";
158         break;
159     }
160
161     wmove(win, 0, 0);
162     wprintw(win,
163             "The Strings/Chars displays should match.  Enter any characters, except:\n");
164     wprintw(win,
165             "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
166     wclrtoeol(win);
167     wprintw(win, "Level %d,%s added %d characters <%s>", level,
168             showstate, length, buffer);
169 }
170
171 static int
172 ColOf(char *buffer, int length, int margin)
173 {
174     int n;
175     int result;
176
177     for (n = 0, result = margin + 1; n < length; ++n) {
178         int ch = UChar(buffer[n]);
179         switch (ch) {
180         case '\n':
181             /* actually newline should clear the remainder of the line
182              * and move to the next line - but that seems a little awkward
183              * in this example.
184              */
185         case '\r':
186             result = 0;
187             break;
188         case '\b':
189             if (result > 0)
190                 --result;
191             break;
192         case '\t':
193             result += (MY_TABSIZE - (result % MY_TABSIZE));
194             break;
195         case '\177':
196             result += 2;
197             break;
198         default:
199             ++result;
200             if (ch < 32)
201                 ++result;
202             break;
203         }
204     }
205     return result;
206 }
207
208 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
209 static void
210 test_adds(int level)
211 {
212     static bool first = TRUE;
213
214     int ch;
215     int limit;
216     int row = 1;
217     int col;
218     int row2, col2;
219     int length;
220     char buffer[BUFSIZ];
221     WINDOW *look = 0;
222     WINDOW *work = 0;
223     WINDOW *show = 0;
224     int margin = (2 * MY_TABSIZE) - 1;
225     Options option = (Options) ((unsigned) (m_opt
226                                             ? oMove
227                                             : oDefault)
228                                 | (unsigned) ((w_opt || (level > 0))
229                                               ? oWindow
230                                               : oDefault));
231
232     if (first) {
233         static char cmd[80];
234         setlocale(LC_ALL, "");
235
236         putenv(strcpy(cmd, "TABSIZE=8"));
237
238         initscr();
239         (void) cbreak();        /* take input chars one at a time, no wait for \n */
240         (void) noecho();        /* don't echo input */
241         keypad(stdscr, TRUE);
242     }
243
244     limit = LINES - 5;
245     if (level > 0) {
246         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
247         work = newwin(limit - 2, COLS - (2 * level), 1, level);
248         show = newwin(4, COLS, limit + 1, 0);
249         box(look, 0, 0);
250         wnoutrefresh(look);
251         limit -= 2;
252     } else {
253         work = stdscr;
254         show = derwin(stdscr, 4, COLS, limit + 1, 0);
255     }
256     keypad(work, TRUE);
257
258     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
259         MvWVLine(work, row, col, '.', limit - 2);
260
261     MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
262     MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
263     limit /= 2;
264
265     MvWAddChStr(work, 1, 2, ChStr("String"));
266     MvWAddChStr(work, limit + 1, 2, ChStr("Chars"));
267     wnoutrefresh(work);
268
269     buffer[length = 0] = '\0';
270     legend(show, level, option, buffer, length);
271     wnoutrefresh(show);
272
273     doupdate();
274
275     /*
276      * Show the characters added in color, to distinguish from those that
277      * are shifted.
278      */
279     if (has_colors()) {
280         start_color();
281         init_pair(1, COLOR_WHITE, COLOR_BLUE);
282         show_attr = (attr_t) COLOR_PAIR(1);
283         wbkgdset(work, show_attr | ' ');
284     } else {
285         show_attr = A_STANDOUT;
286     }
287
288     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
289         wmove(work, row, margin + 1);
290         switch (ch) {
291         case key_RECUR:
292             test_adds(level + 1);
293
294             touchwin(look);
295             touchwin(work);
296             touchwin(show);
297
298             wnoutrefresh(look);
299             wnoutrefresh(work);
300             wnoutrefresh(show);
301
302             doupdate();
303             break;
304         case key_NEWLINE:
305             if (row < limit) {
306                 ++row;
307                 /* put the whole string in, all at once */
308                 col2 = margin + 1;
309                 switch (option) {
310                 case oDefault:
311                     if (n_opt > 1) {
312                         for (col = 0; col < length; col += n_opt) {
313                             col2 = ColOf(buffer, col, margin);
314                             if (move(row, col2) != ERR) {
315                                 AddNStr(ChStr2(buffer + col), LEN(col));
316                             }
317                         }
318                     } else {
319                         if (move(row, col2) != ERR) {
320                             AddStr(ChStr2(buffer));
321                         }
322                     }
323                     break;
324                 case oMove:
325                     if (n_opt > 1) {
326                         for (col = 0; col < length; col += n_opt) {
327                             col2 = ColOf(buffer, col, margin);
328                             MvAddNStr(row, col2, ChStr2(buffer + col), LEN(col));
329                         }
330                     } else {
331                         MvAddStr(row, col2, ChStr2(buffer));
332                     }
333                     break;
334                 case oWindow:
335                     if (n_opt > 1) {
336                         for (col = 0; col < length; col += n_opt) {
337                             col2 = ColOf(buffer, col, margin);
338                             if (wmove(work, row, col2) != ERR) {
339                                 WAddNStr(work, ChStr2(buffer + col), LEN(col));
340                             }
341                         }
342                     } else {
343                         if (wmove(work, row, col2) != ERR) {
344                             WAddStr(work, ChStr2(buffer));
345                         }
346                     }
347                     break;
348                 case oMoveWindow:
349                     if (n_opt > 1) {
350                         for (col = 0; col < length; col += n_opt) {
351                             col2 = ColOf(buffer, col, margin);
352                             MvWAddNStr(work, row, col2, ChStr2(buffer + col),
353                                        LEN(col));
354                         }
355                     } else {
356                         MvWAddStr(work, row, col2, ChStr2(buffer));
357                     }
358                     break;
359                 }
360
361                 /* do the corresponding single-character add */
362                 row2 = limit + row;
363                 for (col = 0; col < length; ++col) {
364                     col2 = ColOf(buffer, col, margin);
365                     switch (option) {
366                     case oDefault:
367                         if (move(row2, col2) != ERR) {
368                             AddCh(UChar(buffer[col]));
369                         }
370                         break;
371                     case oMove:
372                         MvAddCh(row2, col2, UChar(buffer[col]));
373                         break;
374                     case oWindow:
375                         if (wmove(work, row2, col2) != ERR) {
376                             WAddCh(work, UChar(buffer[col]));
377                         }
378                         break;
379                     case oMoveWindow:
380                         MvWAddCh(work, row2, col2, UChar(buffer[col]));
381                         break;
382                     }
383                 }
384             } else {
385                 beep();
386             }
387             break;
388         case KEY_BACKSPACE:
389             ch = '\b';
390             /* FALLTHRU */
391         default:
392             if (ch <= 0 || ch > 255) {
393                 beep();
394                 break;
395             }
396             buffer[length++] = (char) ch;
397             buffer[length] = '\0';
398
399             /* put the string in, one character at a time */
400             col = ColOf(buffer, length - 1, margin);
401             switch (option) {
402             case oDefault:
403                 if (move(row, col) != ERR) {
404                     AddStr(ChStr2(buffer + length - 1));
405                 }
406                 break;
407             case oMove:
408                 MvAddStr(row, col, ChStr2(buffer + length - 1));
409                 break;
410             case oWindow:
411                 if (wmove(work, row, col) != ERR) {
412                     WAddStr(work, ChStr2(buffer + length - 1));
413                 }
414                 break;
415             case oMoveWindow:
416                 MvWAddStr(work, row, col, ChStr2(buffer + length - 1));
417                 break;
418             }
419
420             /* do the corresponding single-character add */
421             switch (option) {
422             case oDefault:
423                 if (move(limit + row, col) != ERR) {
424                     AddCh(UChar(ch));
425                 }
426                 break;
427             case oMove:
428                 MvAddCh(limit + row, col, UChar(ch));
429                 break;
430             case oWindow:
431                 if (wmove(work, limit + row, col) != ERR) {
432                     WAddCh(work, UChar(ch));
433                 }
434                 break;
435             case oMoveWindow:
436                 MvWAddCh(work, limit + row, col, UChar(ch));
437                 break;
438             }
439
440             wnoutrefresh(work);
441
442             legend(show, level, option, buffer, length);
443             wnoutrefresh(show);
444
445             doupdate();
446             break;
447         }
448     }
449     if (level > 0) {
450         delwin(show);
451         delwin(work);
452         delwin(look);
453     }
454 }
455
456 static void
457 usage(void)
458 {
459     static const char *tbl[] =
460     {
461         "Usage: test_addchstr [options]"
462         ,""
463         ,"Options:"
464         ,"  -f FILE read data from given file"
465         ,"  -n NUM  limit string-adds to NUM bytes on ^N replay"
466         ,"  -m      perform wmove/move separately from add-functions"
467         ,"  -p      pass-thru control characters without using unctrl()"
468         ,"  -w      use window-parameter even when stdscr would be implied"
469     };
470     unsigned n;
471     for (n = 0; n < SIZEOF(tbl); ++n)
472         fprintf(stderr, "%s\n", tbl[n]);
473     ExitProgram(EXIT_FAILURE);
474 }
475
476 int
477 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
478 {
479     int ch;
480
481     setlocale(LC_ALL, "");
482
483     while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
484         switch (ch) {
485         case 'f':
486             init_linedata(optarg);
487             break;
488         case 'm':
489             m_opt = TRUE;
490             break;
491         case 'n':
492             n_opt = atoi(optarg);
493             if (n_opt == 0)
494                 n_opt = -1;
495             break;
496         case 'p':
497             pass_ctls = TRUE;
498             break;
499         case 'w':
500             w_opt = TRUE;
501             break;
502         default:
503             usage();
504             break;
505         }
506     }
507     if (optind < argc)
508         usage();
509
510     test_adds(0);
511     endwin();
512     ExitProgram(EXIT_SUCCESS);
513 }