- 论坛徽章:
- 0
|
两天前买了该书,翻了翻,发现其中错误不少。
其中 page: 217的
set_field_type(field,TYPE_HEX,'*',0X0100L,0X1000)
一句,作者说 '*'是填充字符。其实有误,应该是一个精度数。不知道大家认同否。
下面提供一个测试程序:
#include <form.h>;
#include <ctype.h>;
#include <varargs.h>;
extern long strtol ();
#define isblank(c) ((c) == ' ')
typedef struct {
int padding;
long vmin, vmax;
} HEX;
static char *make_hex (ap)
va_list *ap;
{
HEX * n = (HEX * ) malloc (sizeof (HEX));
if (n) {
n ->;padding = va_arg (*ap, int);
n ->;vmin = va_arg (*ap, long);
n ->;vmax = va_arg (*ap, long);
}
return (char *) n;
}
static char *copy_hex (arg)
char *arg;
{
HEX * n = (HEX * ) malloc (sizeof (HEX));
if (n)
*n = *((HEX * ) arg);
return (char *) n;
}
static void free_hex (arg)
char *arg;
{
free (arg);
}
static int fcheck_hex (f, arg)
FIELD *f;
char *arg;
{
HEX * n = (HEX * ) arg;
int padding = n ->;padding;
long vmin = n ->;vmin;
long vmax = n ->;vmax;
char buf[80];
char *x = field_buffer (f, 0);
while (*x && isblank (*x))
++x;
if (*x) {
char *t = x;
while (*x && isxdigit (*x))
++x;
while (*x && isblank (*x))
++x;
if (!*x) {
long v = strtol (t, (char **) 0, 16);
if (v >;= vmin && v <= vmax) {
sprintf (buf, "%.*lx", padding, v);
set_field_buffer (f, 0, buf);
return TRUE;
}
}
}
return FALSE;
}
static int ccheck_hex (c, arg)
int c;
char *arg;
{
return isxdigit (c);
}
保存为:nt.c
#include <form.h>;
#include <locale.h>;
#include "nt.c"
int main()
{
FIELD * field[3];
FORM * my_form;
int ch;
FIELDTYPE * TYPE_HEX = NULL ;
setlocale( LC_CTYPE, "" );
/* Initialize curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
meta( stdscr, TRUE );
TYPE_HEX = new_fieldtype (fcheck_hex, ccheck_hex);
set_fieldtype_arg (TYPE_HEX, make_hex, copy_hex, free_hex);
/* Initialize the fields */
field[0] = new_field(1, 5, 4, 18, 0, 0);
field[1] = new_field(1, 5, 6, 18, 0, 0);
field[2] = NULL;
set_field_type(field[0], TYPE_HEX,5, 0x0000L, 0x1000L) ;
//set_field_type(field[0], TYPE_HEX) ;
/* Set field options */
set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */
field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when thi
s */
/* Field is filled up */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* Create the form and post it */
my_form = new_form(field);
post_form(my_form);
refresh();
mvprintw(4, 10, "Value 1:" ;
mvprintw(6, 10, "Value 2:" ;
refresh();
/* Loop through to get user requests */
while ((ch = getch()) != KEY_F(1)) {
switch (ch) {
case KEY_DOWN:
/* Go to next field */
form_driver(my_form, REQ_NEXT_FIELD);
/* Go to the end of the present buffer */
/* Leaves nicely at the last character */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* Go to previous field */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* If this is a normal character, it gets */
/* Printed */
form_driver(my_form, ch);
break;
}
}
/* Un post form and free the memory */
free_fieldtype(TYPE_HEX) ;
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}
保存为form.c
cc form.c -o form -lform -lcurses
大家可以调试一下。 |
|