1 /****************************************************************************
2 * Copyright (c) 1999,2000 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.5 2000/09/09 19:08:32 tom Exp $
34 * File format: text beginning in column 1 is a title; other text forms the content.
37 #include <test.priv.h>
45 #define VISIBLE_CARDS 10
49 #define CTRL(x) ((x) & 0x1f)
52 typedef struct _card {
60 static CARD *all_cards;
61 static char default_name[] = "cardfile.dat";
64 #define strdup my_strdup
68 char *p = (char *) malloc(strlen(s) + 1);
73 #endif /* not HAVE_STRDUP */
76 skip(const char *buffer)
78 while (isspace(*buffer))
86 unsigned n = strlen(buffer);
87 while (n-- && isspace(buffer[n]))
91 /*******************************************************************************/
94 add_title(const char *title)
98 for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
99 int cmp = strcmp(p->title, title);
106 card = (CARD *) calloc(1, sizeof(CARD));
107 card->title = strdup(title);
108 card->content = strdup("");
111 card->link = all_cards;
114 card->link = q->link;
122 add_content(CARD * card, const char *content)
124 unsigned total, offset;
126 content = skip(content);
127 if ((total = strlen(content)) != 0) {
128 if ((offset = strlen(card->content)) != 0) {
130 card->content = (char *) realloc(card->content, total + 1);
131 strcpy(card->content + offset++, " ");
133 card->content = (char *) malloc(total + 1);
135 strcpy(card->content + offset, content);
142 CARD *card = add_title("");
143 add_content(card, "");
148 find_card(char *title)
152 for (card = all_cards; card != 0; card = card->link)
153 if (!strcmp(card->title, title))
160 read_data(char *fname)
166 if ((fp = fopen(fname, "r")) != 0) {
167 while (fgets(buffer, sizeof(buffer), fp)) {
169 if (isspace(*buffer)) {
171 card = add_title("");
172 add_content(card, buffer);
173 } else if ((card = find_card(buffer)) == 0) {
174 card = add_title(buffer);
181 /*******************************************************************************/
184 write_data(const char *fname)
190 if (!strcmp(fname, default_name))
191 fname = "cardfile.out";
193 if ((fp = fopen(fname, "w")) != 0) {
194 for (p = all_cards; p != 0; p = p->link) {
195 FIELD **f = form_fields(p->form);
196 for (n = 0; f[n] != 0; n++) {
197 char *s = field_buffer(f[n], 0);
199 && (s = strdup(s)) != 0) {
201 fprintf(fp, "%s%s\n", n ? "\t" : "", s);
210 /*******************************************************************************/
221 for (p = all_cards; p != 0; p = p->link)
228 * Shuffle the panels to keep them in a natural hierarchy.
231 order_cards(CARD * first, int depth)
234 if (depth && first->link)
235 order_cards(first->link, depth - 1);
236 top_panel(first->panel);
241 * Return the next card in the list
244 next_card(CARD * now)
252 * Return the previous card in the list
255 prev_card(CARD * now)
258 for (p = all_cards; p != 0; p = p->link)
264 /*******************************************************************************/
267 form_virtualize(WINDOW *w)
273 return (MAX_FORM_COMMAND + 4);
275 return (MAX_FORM_COMMAND + 3);
277 return (MAX_FORM_COMMAND + 2);
280 return (MAX_FORM_COMMAND + 1);
283 return (REQ_DEL_PREV);
285 return (REQ_DEL_CHAR);
287 return (REQ_LEFT_CHAR);
289 return (REQ_RIGHT_CHAR);
293 return (REQ_NEXT_FIELD);
296 return (REQ_PREV_FIELD);
303 /*******************************************************************************/
306 cardfile(char *fname)
311 int visible_cards = count_cards();
312 int panel_wide = COLS - (visible_cards * OFFSET_CARD);
313 int panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
314 int form_wide = panel_wide - 2;
315 int form_high = panel_high - 2;
316 int x = (visible_cards - 1) * OFFSET_CARD;
319 int finished = FALSE;
322 addstr("^Q/ESC -- exit form ^W -- writes data to file\n");
323 addstr("^N -- go to next card ^P -- go to previous card\n");
324 addstr("Arrow keys move left/right within a field, up/down between fields");
326 /* make a panel for each CARD */
327 for (p = all_cards; p != 0; p = p->link) {
328 FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *));
330 win = newwin(panel_high, panel_wide, x, y);
332 p->panel = new_panel(win);
335 /* ...and a form in each panel */
336 f[0] = new_field(1, form_wide, 0, 0, 0, 0);
337 set_field_back(f[0], A_REVERSE);
338 set_field_buffer(f[0], 0, p->title);
340 f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
341 set_field_buffer(f[1], 0, p->content);
342 set_field_just(f[1], JUSTIFY_LEFT);
346 p->form = new_form(f);
347 set_form_win(p->form, win);
348 set_form_sub(p->form, derwin(win, form_high, form_wide, 1, 1));
355 order_cards(top_card = all_cards, visible_cards);
361 switch (form_driver(top_card->form, ch =
362 form_virtualize(panel_window(top_card->panel)))) {
365 case E_UNKNOWN_COMMAND:
367 case MAX_FORM_COMMAND + 1:
370 case MAX_FORM_COMMAND + 2:
371 top_card = prev_card(top_card);
372 order_cards(top_card, visible_cards);
374 case MAX_FORM_COMMAND + 3:
375 top_card = next_card(top_card);
376 order_cards(top_card, visible_cards);
378 case MAX_FORM_COMMAND + 4:
393 /*******************************************************************************/
396 main(int argc, char *argv[])
405 for (n = 1; n < argc; n++)
407 if (count_cards() == 0)
411 read_data(default_name);
412 if (count_cards() == 0)
414 cardfile(default_name);