]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_addstr.c
ncurses 5.7 - patch 20091031
[ncurses.git] / test / test_addstr.c
1 /****************************************************************************
2  * Copyright (c) 2009 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_addstr.c,v 1.3 2009/10/10 16:01:41 tom Exp $
30  *
31  * Demonstrate the waddstr() and waddch functions.
32  * Thomas Dickey - 2009/9/12
33  */
34
35 #include <test.priv.h>
36
37 #include <linedata.h>
38
39 #define AddNStr    addnstr
40 #define AddStr     addstr
41 #define MvAddNStr  mvaddnstr
42 #define MvAddStr   mvaddstr
43 #define MvWAddNStr mvwaddnstr
44 #define MvWAddStr  mvwaddstr
45 #define WAddNStr   waddnstr
46 #define WAddStr    waddstr
47
48 #define AddCh      addch
49 #define MvAddCh    mvaddch
50 #define MvWAddCh   mvwaddch
51 #define WAddCh     waddch
52
53 #define MY_TABSIZE 8
54
55 typedef enum {
56     oDefault = 0,
57     oMove = 1,
58     oWindow = 2,
59     oMoveWindow = 3
60 } Options;
61
62 static bool m_opt = FALSE;
63 static bool w_opt = FALSE;
64 static int n_opt = -1;
65
66 static void
67 legend(WINDOW *win, int level, Options state, char *buffer, int length)
68 {
69     NCURSES_CONST char *showstate;
70
71     switch (state) {
72     default:
73     case oDefault:
74         showstate = "";
75         break;
76     case oMove:
77         showstate = " (mvXXX)";
78         break;
79     case oWindow:
80         showstate = " (winXXX)";
81         break;
82     case oMoveWindow:
83         showstate = " (mvwinXXX)";
84         break;
85     }
86
87     wmove(win, 0, 0);
88     wprintw(win,
89             "The Strings/Chars displays should match.  Enter any characters, except:\n");
90     wprintw(win,
91             "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
92     wclrtoeol(win);
93     wprintw(win, "Level %d,%s added %d characters <%s>", level,
94             showstate, length, buffer);
95 }
96
97 static int
98 ColOf(char *buffer, int length, int margin)
99 {
100     int n;
101     int result;
102
103     for (n = 0, result = margin + 1; n < length; ++n) {
104         int ch = UChar(buffer[n]);
105         switch (ch) {
106         case '\n':
107             /* actually newline should clear the remainder of the line
108              * and move to the next line - but that seems a little awkward
109              * in this example.
110              */
111         case '\r':
112             result = 0;
113             break;
114         case '\b':
115             if (result > 0)
116                 --result;
117             break;
118         case '\t':
119             result += (MY_TABSIZE - (result % MY_TABSIZE));
120             break;
121         case '\177':
122             result += 2;
123             break;
124         default:
125             ++result;
126             if (ch < 32)
127                 ++result;
128             break;
129         }
130     }
131     return result;
132 }
133
134 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
135 static void
136 test_adds(int level)
137 {
138     static bool first = TRUE;
139
140     int ch;
141     int limit;
142     int row = 1;
143     int col;
144     int row2, col2;
145     int length;
146     char buffer[BUFSIZ];
147     WINDOW *look = 0;
148     WINDOW *work = 0;
149     WINDOW *show = 0;
150     int margin = (2 * MY_TABSIZE) - 1;
151     Options option = (Options) ((unsigned) (m_opt
152                                             ? oMove
153                                             : oDefault)
154                                 | (unsigned) ((w_opt || (level > 0))
155                                               ? oWindow
156                                               : oDefault));
157
158     if (first) {
159         static char cmd[80];
160         setlocale(LC_ALL, "");
161
162         putenv(strcpy(cmd, "TABSIZE=8"));
163
164         initscr();
165         (void) cbreak();        /* take input chars one at a time, no wait for \n */
166         (void) noecho();        /* don't echo input */
167         keypad(stdscr, TRUE);
168     }
169
170     limit = LINES - 5;
171     if (level > 0) {
172         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
173         work = newwin(limit - 2, COLS - (2 * level), 1, level);
174         show = newwin(4, COLS, limit + 1, 0);
175         box(look, 0, 0);
176         wnoutrefresh(look);
177         limit -= 2;
178     } else {
179         work = stdscr;
180         show = derwin(stdscr, 4, COLS, limit + 1, 0);
181     }
182     keypad(work, TRUE);
183
184     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
185         mvwvline(work, row, col, '.', limit - 2);
186
187     mvwvline(work, row, margin, ACS_VLINE, limit - 2);
188     mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
189     limit /= 2;
190
191     mvwaddstr(work, 1, 2, "String");
192     mvwaddstr(work, limit + 1, 2, "Chars");
193     wnoutrefresh(work);
194
195     buffer[length = 0] = '\0';
196     legend(show, level, option, buffer, length);
197     wnoutrefresh(show);
198
199     doupdate();
200
201     /*
202      * Show the characters added in color, to distinguish from those that
203      * are shifted.
204      */
205     if (has_colors()) {
206         start_color();
207         init_pair(1, COLOR_WHITE, COLOR_BLUE);
208         wbkgdset(work, COLOR_PAIR(1) | ' ');
209     }
210
211     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
212         wmove(work, row, margin + 1);
213         switch (ch) {
214         case key_RECUR:
215             test_adds(level + 1);
216
217             touchwin(look);
218             touchwin(work);
219             touchwin(show);
220
221             wnoutrefresh(look);
222             wnoutrefresh(work);
223             wnoutrefresh(show);
224
225             doupdate();
226             break;
227         case key_NEWLINE:
228             if (row < limit) {
229                 ++row;
230                 /* put the whole string in, all at once */
231                 col2 = margin + 1;
232                 switch (option) {
233                 case oDefault:
234                     if (n_opt > 1) {
235                         for (col = 0; col < length; col += n_opt) {
236                             col2 = ColOf(buffer, col, margin);
237                             if (move(row, col2) != ERR) {
238                                 AddNStr(buffer + col, LEN(col));
239                             }
240                         }
241                     } else {
242                         if (move(row, col2) != ERR) {
243                             AddStr(buffer);
244                         }
245                     }
246                     break;
247                 case oMove:
248                     if (n_opt > 1) {
249                         for (col = 0; col < length; col += n_opt) {
250                             col2 = ColOf(buffer, col, margin);
251                             MvAddNStr(row, col2, buffer + col, LEN(col));
252                         }
253                     } else {
254                         MvAddStr(row, col2, buffer);
255                     }
256                     break;
257                 case oWindow:
258                     if (n_opt > 1) {
259                         for (col = 0; col < length; col += n_opt) {
260                             col2 = ColOf(buffer, col, margin);
261                             if (wmove(work, row, col2) != ERR) {
262                                 WAddNStr(work, buffer + col, LEN(col));
263                             }
264                         }
265                     } else {
266                         if (wmove(work, row, col2) != ERR) {
267                             WAddStr(work, buffer);
268                         }
269                     }
270                     break;
271                 case oMoveWindow:
272                     if (n_opt > 1) {
273                         for (col = 0; col < length; col += n_opt) {
274                             col2 = ColOf(buffer, col, margin);
275                             MvWAddNStr(work, row, col2, buffer + col, LEN(col));
276                         }
277                     } else {
278                         MvWAddStr(work, row, col2, buffer);
279                     }
280                     break;
281                 }
282
283                 /* do the corresponding single-character add */
284                 row2 = limit + row;
285                 for (col = 0; col < length; ++col) {
286                     col2 = ColOf(buffer, col, margin);
287                     switch (option) {
288                     case oDefault:
289                         if (move(row2, col2) != ERR) {
290                             AddCh(UChar(buffer[col]));
291                         }
292                         break;
293                     case oMove:
294                         MvAddCh(row2, col2, UChar(buffer[col]));
295                         break;
296                     case oWindow:
297                         if (wmove(work, row2, col2) != ERR) {
298                             WAddCh(work, UChar(buffer[col]));
299                         }
300                         break;
301                     case oMoveWindow:
302                         MvWAddCh(work, row2, col2, UChar(buffer[col]));
303                         break;
304                     }
305                 }
306             } else {
307                 beep();
308             }
309             break;
310         default:
311             if (ch <= 0 || ch > 255) {
312                 beep();
313                 break;
314             }
315             buffer[length++] = (char) ch;
316             buffer[length] = '\0';
317
318             /* put the string in, one character at a time */
319             col = ColOf(buffer, length - 1, margin);
320             switch (option) {
321             case oDefault:
322                 if (move(row, col) != ERR) {
323                     AddStr(buffer + length - 1);
324                 }
325                 break;
326             case oMove:
327                 MvAddStr(row, col, buffer + length - 1);
328                 break;
329             case oWindow:
330                 if (wmove(work, row, col) != ERR) {
331                     WAddStr(work, buffer + length - 1);
332                 }
333                 break;
334             case oMoveWindow:
335                 MvWAddStr(work, row, col, buffer + length - 1);
336                 break;
337             }
338
339             /* do the corresponding single-character add */
340             switch (option) {
341             case oDefault:
342                 if (move(limit + row, col) != ERR) {
343                     AddCh(UChar(ch));
344                 }
345                 break;
346             case oMove:
347                 MvAddCh(limit + row, col, UChar(ch));
348                 break;
349             case oWindow:
350                 if (wmove(work, limit + row, col) != ERR) {
351                     WAddCh(work, UChar(ch));
352                 }
353                 break;
354             case oMoveWindow:
355                 MvWAddCh(work, limit + row, col, UChar(ch));
356                 break;
357             }
358
359             wnoutrefresh(work);
360
361             legend(show, level, option, buffer, length);
362             wnoutrefresh(show);
363
364             doupdate();
365             break;
366         }
367     }
368     if (level > 0) {
369         delwin(show);
370         delwin(work);
371         delwin(look);
372     }
373 }
374
375 static void
376 usage(void)
377 {
378     static const char *tbl[] =
379     {
380         "Usage: test_addstr [options]"
381         ,""
382         ,"Options:"
383         ,"  -f FILE read data from given file"
384         ,"  -n NUM  limit string-adds to NUM bytes on ^N replay"
385         ,"  -m      perform wmove/move separately from add-functions"
386         ,"  -w      use window-parameter even when stdscr would be implied"
387     };
388     unsigned n;
389     for (n = 0; n < SIZEOF(tbl); ++n)
390         fprintf(stderr, "%s\n", tbl[n]);
391     ExitProgram(EXIT_FAILURE);
392 }
393
394 int
395 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
396 {
397     int ch;
398
399     setlocale(LC_ALL, "");
400
401     while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
402         switch (ch) {
403         case 'f':
404             init_linedata(optarg);
405             break;
406         case 'm':
407             m_opt = TRUE;
408             break;
409         case 'n':
410             n_opt = atoi(optarg);
411             if (n_opt == 0)
412                 n_opt = -1;
413             break;
414         case 'w':
415             w_opt = TRUE;
416             break;
417         default:
418             usage();
419             break;
420         }
421     }
422     if (optind < argc)
423         usage();
424
425     test_adds(0);
426     endwin();
427     ExitProgram(EXIT_SUCCESS);
428 }