1 /****************************************************************************
2 * Copyright (c) 1999-2001,2002 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 ****************************************************************************/
30 * Author: Thomas E. Dickey <dickey@clark.net> 1999
32 * $Id: cardfile.c,v 1.11 2002/04/06 23:12:50 tom Exp $
34 * File format: text beginning in column 1 is a title; other text forms the content.
37 #include <test.priv.h>
39 #if HAVE_FORM_H && HAVE_PANEL_H && HAVE_LIBFORM && HAVE_LIBPANEL
46 #define VISIBLE_CARDS 10
50 #define CTRL(x) ((x) & 0x1f)
53 typedef struct _card {
61 static CARD *all_cards;
62 static char default_name[] = "cardfile.dat";
65 #define strdup my_strdup
69 char *p = (char *) malloc(strlen(s) + 1);
74 #endif /* not HAVE_STRDUP */
77 skip(const char *buffer)
79 while (isspace(UChar(*buffer)))
87 unsigned n = strlen(buffer);
88 while (n-- && isspace(UChar(buffer[n])))
92 /*******************************************************************************/
95 add_title(const char *title)
99 for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
100 int cmp = strcmp(p->title, title);
107 card = (CARD *) calloc(1, sizeof(CARD));
108 card->title = strdup(title);
109 card->content = strdup("");
112 card->link = all_cards;
115 card->link = q->link;
123 add_content(CARD * card, const char *content)
125 unsigned total, offset;
127 content = skip(content);
128 if ((total = strlen(content)) != 0) {
129 if ((offset = strlen(card->content)) != 0) {
131 card->content = (char *) realloc(card->content, total + 1);
132 strcpy(card->content + offset++, " ");
134 card->content = (char *) malloc(total + 1);
136 strcpy(card->content + offset, content);
143 CARD *card = add_title("");
144 add_content(card, "");
149 find_card(char *title)
153 for (card = all_cards; card != 0; card = card->link)
154 if (!strcmp(card->title, title))
161 read_data(char *fname)
167 if ((fp = fopen(fname, "r")) != 0) {
168 while (fgets(buffer, sizeof(buffer), fp)) {
170 if (isspace(UChar(*buffer))) {
172 card = add_title("");
173 add_content(card, buffer);
174 } else if ((card = find_card(buffer)) == 0) {
175 card = add_title(buffer);
182 /*******************************************************************************/
185 write_data(const char *fname)
191 if (!strcmp(fname, default_name))
192 fname = "cardfile.out";
194 if ((fp = fopen(fname, "w")) != 0) {
195 for (p = all_cards; p != 0; p = p->link) {
196 FIELD **f = form_fields(p->form);
197 for (n = 0; f[n] != 0; n++) {
198 char *s = field_buffer(f[n], 0);
200 && (s = strdup(s)) != 0) {
202 fprintf(fp, "%s%s\n", n ? "\t" : "", s);
211 /*******************************************************************************/
222 for (p = all_cards; p != 0; p = p->link)
229 * Shuffle the panels to keep them in a natural hierarchy.
232 order_cards(CARD * first, int depth)
235 if (depth && first->link)
236 order_cards(first->link, depth - 1);
237 top_panel(first->panel);
242 * Return the next card in the list
245 next_card(CARD * now)
253 * Return the previous card in the list
256 prev_card(CARD * now)
259 for (p = all_cards; p != 0; p = p->link)
265 /*******************************************************************************/
268 form_virtualize(WINDOW *w)
274 return (MAX_FORM_COMMAND + 4);
276 return (MAX_FORM_COMMAND + 3);
278 return (MAX_FORM_COMMAND + 2);
281 return (MAX_FORM_COMMAND + 1);
284 return (REQ_DEL_PREV);
286 return (REQ_DEL_CHAR);
288 return (REQ_LEFT_CHAR);
290 return (REQ_RIGHT_CHAR);
294 return (REQ_NEXT_FIELD);
297 return (REQ_PREV_FIELD);
304 /*******************************************************************************/
307 cardfile(char *fname)
312 int visible_cards = count_cards();
313 int panel_wide = COLS - (visible_cards * OFFSET_CARD);
314 int panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
315 int form_wide = panel_wide - 2;
316 int form_high = panel_high - 2;
317 int x = (visible_cards - 1) * OFFSET_CARD;
320 int finished = FALSE;
323 addstr("^Q/ESC -- exit form ^W -- writes data to file\n");
324 addstr("^N -- go to next card ^P -- go to previous card\n");
325 addstr("Arrow keys move left/right within a field, up/down between fields");
327 /* make a panel for each CARD */
328 for (p = all_cards; p != 0; p = p->link) {
329 FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *));
331 win = newwin(panel_high, panel_wide, x, y);
333 p->panel = new_panel(win);
336 /* ...and a form in each panel */
337 f[0] = new_field(1, form_wide, 0, 0, 0, 0);
338 set_field_back(f[0], A_REVERSE);
339 set_field_buffer(f[0], 0, p->title);
341 f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
342 set_field_buffer(f[1], 0, p->content);
343 set_field_just(f[1], JUSTIFY_LEFT);
347 p->form = new_form(f);
348 set_form_win(p->form, win);
349 set_form_sub(p->form, derwin(win, form_high, form_wide, 1, 1));
356 order_cards(top_card = all_cards, visible_cards);
362 switch (form_driver(top_card->form, ch =
363 form_virtualize(panel_window(top_card->panel)))) {
366 case E_UNKNOWN_COMMAND:
368 case MAX_FORM_COMMAND + 1:
371 case MAX_FORM_COMMAND + 2:
372 top_card = prev_card(top_card);
373 order_cards(top_card, visible_cards);
375 case MAX_FORM_COMMAND + 3:
376 top_card = next_card(top_card);
377 order_cards(top_card, visible_cards);
379 case MAX_FORM_COMMAND + 4:
399 /*******************************************************************************/
402 main(int argc, char *argv[])
411 for (n = 1; n < argc; n++)
413 if (count_cards() == 0)
417 read_data(default_name);
418 if (count_cards() == 0)
420 cardfile(default_name);
425 ExitProgram(EXIT_SUCCESS);
431 printf("This program requires the curses form and panel libraries\n");
432 ExitProgram(EXIT_FAILURE);