ncursesw-morphos/test/cardfile.c

535 lines
12 KiB
C
Raw Normal View History

1999-10-24 12:32:42 +08:00
/****************************************************************************
2002-10-13 11:35:53 +08:00
* Copyright (c) 1999-2001,2002 Free Software Foundation, Inc. *
1999-10-24 12:32:42 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
/*
* Author: Thomas E. Dickey <dickey@clark.net> 1999
*
2002-10-13 11:35:53 +08:00
* $Id: cardfile.c,v 1.19 2002/09/01 17:59:48 tom Exp $
1999-10-24 12:32:42 +08:00
*
* File format: text beginning in column 1 is a title; other text forms the content.
*/
#include <test.priv.h>
2002-10-13 11:35:53 +08:00
#if HAVE_FORM_H && HAVE_PANEL_H && HAVE_LIBFORM && HAVE_LIBPANEL
1999-10-24 12:32:42 +08:00
#include <form.h>
#include <panel.h>
#include <ctype.h>
#define VISIBLE_CARDS 10
#define OFFSET_CARD 2
#ifndef CTRL
#define CTRL(x) ((x) & 0x1f)
#endif
typedef struct _card {
2000-07-09 10:46:08 +08:00
struct _card *link;
PANEL *panel;
FORM *form;
char *title;
char *content;
1999-10-24 12:32:42 +08:00
} CARD;
static CARD *all_cards;
static char default_name[] = "cardfile.dat";
#if !HAVE_STRDUP
#define strdup my_strdup
2000-07-09 10:46:08 +08:00
static char *
strdup(char *s)
1999-10-24 12:32:42 +08:00
{
2000-07-09 10:46:08 +08:00
char *p = (char *) malloc(strlen(s) + 1);
1999-10-24 12:32:42 +08:00
if (p)
strcpy(p, s);
2000-07-09 10:46:08 +08:00
return (p);
1999-10-24 12:32:42 +08:00
}
#endif /* not HAVE_STRDUP */
2000-10-21 12:42:11 +08:00
static const char *
skip(const char *buffer)
1999-10-24 12:32:42 +08:00
{
2002-10-13 11:35:53 +08:00
while (isspace(UChar(*buffer)))
1999-10-24 12:32:42 +08:00
buffer++;
return buffer;
}
2000-07-09 10:46:08 +08:00
static void
trim(char *buffer)
1999-10-24 12:32:42 +08:00
{
unsigned n = strlen(buffer);
2002-10-13 11:35:53 +08:00
while (n-- && isspace(UChar(buffer[n])))
2000-07-09 10:46:08 +08:00
buffer[n] = 0;
1999-10-24 12:32:42 +08:00
}
/*******************************************************************************/
2000-07-09 10:46:08 +08:00
static CARD *
add_title(const char *title)
1999-10-24 12:32:42 +08:00
{
CARD *card, *p, *q;
2000-07-09 10:46:08 +08:00
for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
1999-10-24 12:32:42 +08:00
int cmp = strcmp(p->title, title);
if (cmp == 0)
return p;
if (cmp > 0)
break;
}
2000-07-09 10:46:08 +08:00
card = (CARD *) calloc(1, sizeof(CARD));
1999-10-24 12:32:42 +08:00
card->title = strdup(title);
card->content = strdup("");
2000-07-09 10:46:08 +08:00
if (q == 0) {
1999-10-24 12:32:42 +08:00
card->link = all_cards;
all_cards = card;
2000-07-09 10:46:08 +08:00
} else {
card->link = q->link;
1999-10-24 12:32:42 +08:00
q->link = card;
}
return card;
}
2000-07-09 10:46:08 +08:00
static void
2000-10-21 12:42:11 +08:00
add_content(CARD * card, const char *content)
1999-10-24 12:32:42 +08:00
{
unsigned total, offset;
content = skip(content);
2000-07-09 10:46:08 +08:00
if ((total = strlen(content)) != 0) {
if ((offset = strlen(card->content)) != 0) {
1999-10-24 12:32:42 +08:00
total += 1 + offset;
2000-07-09 10:46:08 +08:00
card->content = (char *) realloc(card->content, total + 1);
1999-10-24 12:32:42 +08:00
strcpy(card->content + offset++, " ");
2000-07-09 10:46:08 +08:00
} else {
2002-10-13 11:35:53 +08:00
if (card->content != 0)
free(card->content);
2000-07-09 10:46:08 +08:00
card->content = (char *) malloc(total + 1);
1999-10-24 12:32:42 +08:00
}
strcpy(card->content + offset, content);
}
}
2000-07-09 10:46:08 +08:00
static CARD *
new_card(void)
{
CARD *card = add_title("");
add_content(card, "");
return card;
}
static CARD *
find_card(char *title)
1999-10-24 12:32:42 +08:00
{
CARD *card;
for (card = all_cards; card != 0; card = card->link)
if (!strcmp(card->title, title))
break;
return card;
}
2000-07-09 10:46:08 +08:00
static void
read_data(char *fname)
1999-10-24 12:32:42 +08:00
{
FILE *fp;
CARD *card = 0;
char buffer[BUFSIZ];
2000-07-09 10:46:08 +08:00
if ((fp = fopen(fname, "r")) != 0) {
while (fgets(buffer, sizeof(buffer), fp)) {
1999-10-24 12:32:42 +08:00
trim(buffer);
2002-10-13 11:35:53 +08:00
if (isspace(UChar(*buffer))) {
1999-10-24 12:32:42 +08:00
if (card == 0)
card = add_title("");
add_content(card, buffer);
2000-07-09 10:46:08 +08:00
} else if ((card = find_card(buffer)) == 0) {
1999-10-24 12:32:42 +08:00
card = add_title(buffer);
}
}
fclose(fp);
}
}
/*******************************************************************************/
2000-07-09 10:46:08 +08:00
static void
write_data(const char *fname)
1999-10-24 12:32:42 +08:00
{
FILE *fp;
CARD *p = 0;
int n;
if (!strcmp(fname, default_name))
fname = "cardfile.out";
2000-07-09 10:46:08 +08:00
if ((fp = fopen(fname, "w")) != 0) {
for (p = all_cards; p != 0; p = p->link) {
1999-10-24 12:32:42 +08:00
FIELD **f = form_fields(p->form);
2000-07-09 10:46:08 +08:00
for (n = 0; f[n] != 0; n++) {
1999-10-24 12:32:42 +08:00
char *s = field_buffer(f[n], 0);
if (s != 0
2000-07-09 10:46:08 +08:00
&& (s = strdup(s)) != 0) {
1999-10-24 12:32:42 +08:00
trim(s);
fprintf(fp, "%s%s\n", n ? "\t" : "", s);
free(s);
}
}
}
fclose(fp);
}
}
/*******************************************************************************/
/*
* Count the cards
*/
2000-07-09 10:46:08 +08:00
static int
count_cards(void)
1999-10-24 12:32:42 +08:00
{
CARD *p;
int count = 0;
for (p = all_cards; p != 0; p = p->link)
count++;
return count;
}
/*
* Shuffle the panels to keep them in a natural hierarchy.
*/
2000-07-09 10:46:08 +08:00
static void
order_cards(CARD * first, int depth)
1999-10-24 12:32:42 +08:00
{
2000-07-09 10:46:08 +08:00
if (first) {
1999-10-24 12:32:42 +08:00
if (depth && first->link)
2000-07-09 10:46:08 +08:00
order_cards(first->link, depth - 1);
1999-10-24 12:32:42 +08:00
top_panel(first->panel);
}
}
/*
* Return the next card in the list
*/
2000-07-09 10:46:08 +08:00
static CARD *
next_card(CARD * now)
1999-10-24 12:32:42 +08:00
{
if (now->link)
2000-07-09 10:46:08 +08:00
now = now->link;
1999-10-24 12:32:42 +08:00
return now;
}
/*
* Return the previous card in the list
*/
2000-07-09 10:46:08 +08:00
static CARD *
prev_card(CARD * now)
1999-10-24 12:32:42 +08:00
{
CARD *p;
for (p = all_cards; p != 0; p = p->link)
if (p->link == now)
return p;
return now;
}
/*******************************************************************************/
2000-07-09 10:46:08 +08:00
static int
form_virtualize(WINDOW *w)
1999-10-24 12:32:42 +08:00
{
2000-07-09 10:46:08 +08:00
int c = wgetch(w);
1999-10-24 12:32:42 +08:00
2000-07-09 10:46:08 +08:00
switch (c) {
1999-10-24 12:32:42 +08:00
case CTRL('W'):
2000-07-09 10:46:08 +08:00
return (MAX_FORM_COMMAND + 4);
1999-10-24 12:32:42 +08:00
case CTRL('N'):
2000-07-09 10:46:08 +08:00
return (MAX_FORM_COMMAND + 3);
1999-10-24 12:32:42 +08:00
case CTRL('P'):
2000-07-09 10:46:08 +08:00
return (MAX_FORM_COMMAND + 2);
1999-10-24 12:32:42 +08:00
case CTRL('Q'):
case 033:
2000-07-09 10:46:08 +08:00
return (MAX_FORM_COMMAND + 1);
1999-10-24 12:32:42 +08:00
case KEY_BACKSPACE:
2000-07-09 10:46:08 +08:00
return (REQ_DEL_PREV);
1999-10-24 12:32:42 +08:00
case KEY_DC:
2000-07-09 10:46:08 +08:00
return (REQ_DEL_CHAR);
1999-10-24 12:32:42 +08:00
case KEY_LEFT:
2000-07-09 10:46:08 +08:00
return (REQ_LEFT_CHAR);
1999-10-24 12:32:42 +08:00
case KEY_RIGHT:
2000-07-09 10:46:08 +08:00
return (REQ_RIGHT_CHAR);
1999-10-24 12:32:42 +08:00
case KEY_DOWN:
case KEY_NEXT:
2000-07-09 10:46:08 +08:00
return (REQ_NEXT_FIELD);
1999-10-24 12:32:42 +08:00
case KEY_UP:
case KEY_PREVIOUS:
2000-07-09 10:46:08 +08:00
return (REQ_PREV_FIELD);
1999-10-24 12:32:42 +08:00
default:
2000-07-09 10:46:08 +08:00
return (c);
1999-10-24 12:32:42 +08:00
}
}
2002-10-13 11:35:53 +08:00
static FIELD **
make_fields(CARD * p, int form_high, int form_wide)
{
FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *));
f[0] = new_field(1, form_wide, 0, 0, 0, 0);
set_field_back(f[0], A_REVERSE);
set_field_buffer(f[0], 0, p->title);
f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
set_field_buffer(f[1], 0, p->content);
set_field_just(f[1], JUSTIFY_LEFT);
f[2] = 0;
return f;
}
static void
show_legend(void)
{
erase();
move(LINES - 3, 0);
addstr("^Q/ESC -- exit form ^W -- writes data to file\n");
addstr("^N -- go to next card ^P -- go to previous card\n");
addstr("Arrow keys move left/right within a field, up/down between fields");
}
#if (defined(KEY_RESIZE) && HAVE_WRESIZE) || NO_LEAKS
static void
free_form_fields(FIELD ** f)
{
int n;
for (n = 0; f[n] != 0; ++n) {
free_field(f[n]);
}
free(f);
}
#endif
1999-10-24 12:32:42 +08:00
/*******************************************************************************/
2000-07-09 10:46:08 +08:00
static void
cardfile(char *fname)
1999-10-24 12:32:42 +08:00
{
WINDOW *win;
CARD *p;
CARD *top_card;
int visible_cards = count_cards();
int panel_wide = COLS - (visible_cards * OFFSET_CARD);
int panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
int form_wide = panel_wide - 2;
int form_high = panel_high - 2;
2002-10-13 11:35:53 +08:00
int y = (visible_cards - 1) * OFFSET_CARD;
int x = 0;
int ch = ERR;
int last_ch;
1999-10-24 12:32:42 +08:00
int finished = FALSE;
2002-10-13 11:35:53 +08:00
show_legend();
1999-10-24 12:32:42 +08:00
/* make a panel for each CARD */
2000-07-09 10:46:08 +08:00
for (p = all_cards; p != 0; p = p->link) {
1999-10-24 12:32:42 +08:00
2002-10-13 11:35:53 +08:00
win = newwin(panel_high, panel_wide, y, x);
1999-10-24 12:32:42 +08:00
keypad(win, TRUE);
p->panel = new_panel(win);
box(win, 0, 0);
2002-10-13 11:35:53 +08:00
p->form = new_form(make_fields(p, form_high, form_wide));
1999-10-24 12:32:42 +08:00
set_form_win(p->form, win);
set_form_sub(p->form, derwin(win, form_high, form_wide, 1, 1));
post_form(p->form);
2002-10-13 11:35:53 +08:00
y -= OFFSET_CARD;
x += OFFSET_CARD;
1999-10-24 12:32:42 +08:00
}
order_cards(top_card = all_cards, visible_cards);
2000-07-09 10:46:08 +08:00
while (!finished) {
1999-10-24 12:32:42 +08:00
update_panels();
doupdate();
2002-10-13 11:35:53 +08:00
last_ch = ch;
ch = form_virtualize(panel_window(top_card->panel));
switch (form_driver(top_card->form, ch)) {
1999-10-24 12:32:42 +08:00
case E_OK:
break;
case E_UNKNOWN_COMMAND:
switch (ch) {
2000-07-09 10:46:08 +08:00
case MAX_FORM_COMMAND + 1:
1999-10-24 12:32:42 +08:00
finished = TRUE;
break;
2000-07-09 10:46:08 +08:00
case MAX_FORM_COMMAND + 2:
1999-10-24 12:32:42 +08:00
top_card = prev_card(top_card);
order_cards(top_card, visible_cards);
break;
2000-07-09 10:46:08 +08:00
case MAX_FORM_COMMAND + 3:
1999-10-24 12:32:42 +08:00
top_card = next_card(top_card);
order_cards(top_card, visible_cards);
break;
2000-07-09 10:46:08 +08:00
case MAX_FORM_COMMAND + 4:
1999-10-24 12:32:42 +08:00
write_data(fname);
break;
2002-10-13 11:35:53 +08:00
#if defined(KEY_RESIZE) && HAVE_WRESIZE
case KEY_RESIZE:
/* resizeterm already did "something" reasonable, but it cannot
* know much about layout. So let's make it nicer.
*/
panel_wide = COLS - (visible_cards * OFFSET_CARD);
panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
form_wide = panel_wide - 2;
form_high = panel_high - 2;
y = (visible_cards - 1) * OFFSET_CARD;
x = 0;
show_legend();
for (p = all_cards; p != 0; p = p->link) {
FIELD **oldf = form_fields(p->form);
WINDOW *olds = form_sub(p->form);
win = form_win(p->form);
/* move and resize the card as needed
* FIXME: if the windows are shrunk too much, this won't do
*/
mvwin(win, y, x);
wresize(win, panel_high, panel_wide);
/* reconstruct each form. Forms are not resizable, and
* there appears to be no good way to reload the text in
* a resized window.
*/
werase(win);
unpost_form(p->form);
free_form(p->form);
p->form = new_form(make_fields(p, form_high, form_wide));
set_form_win(p->form, win);
set_form_sub(p->form, derwin(win, form_high, form_wide,
1, 1));
post_form(p->form);
free_form_fields(oldf);
delwin(olds);
box(win, 0, 0);
y -= OFFSET_CARD;
x += OFFSET_CARD;
}
break;
#endif
1999-10-24 12:32:42 +08:00
default:
beep();
break;
}
break;
default:
flash();
break;
}
}
2002-10-13 11:35:53 +08:00
#if NO_LEAKS
while (all_cards != 0) {
FIELD **f;
int count;
p = all_cards;
all_cards = all_cards->link;
f = form_fields(p->form);
count = field_count(p->form);
unpost_form(p->form); /* ...so we can free it */
free_form(p->form); /* this also disconnects the fields */
free_form_fields(f);
del_panel(p->panel);
free(p->title);
free(p->content);
free(p);
}
#endif
1999-10-24 12:32:42 +08:00
}
/*******************************************************************************/
2000-07-09 10:46:08 +08:00
int
main(int argc, char *argv[])
1999-10-24 12:32:42 +08:00
{
int n;
2002-10-13 11:35:53 +08:00
setlocale(LC_ALL, "");
1999-10-24 12:32:42 +08:00
initscr();
cbreak();
noecho();
2000-07-09 10:46:08 +08:00
if (argc > 1) {
1999-10-24 12:32:42 +08:00
for (n = 1; n < argc; n++)
read_data(argv[n]);
2000-07-09 10:46:08 +08:00
if (count_cards() == 0)
new_card();
1999-10-24 12:32:42 +08:00
cardfile(argv[1]);
2000-07-09 10:46:08 +08:00
} else {
1999-10-24 12:32:42 +08:00
read_data(default_name);
2000-07-09 10:46:08 +08:00
if (count_cards() == 0)
new_card();
1999-10-24 12:32:42 +08:00
cardfile(default_name);
}
endwin();
2002-10-13 11:35:53 +08:00
ExitProgram(EXIT_SUCCESS);
1999-10-24 12:32:42 +08:00
}
2002-10-13 11:35:53 +08:00
#else
int
main(void)
{
printf("This program requires the curses form and panel libraries\n");
ExitProgram(EXIT_FAILURE);
}
#endif