DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
ETI forms

Creating a field type with validation functions

Another way to create a new field type is by specifying

or both. Function new_fieldtype returns your new field type given pointers to these validation functions.

SYNOPSIS

   typedef int (* PTF_int) ();
   

FIELDTYPE * new_fieldtype (f_check, c_check) PTF_int f_check; PTF_int c_check;

The form driver automatically calls the named validation functions during form driver processing.

To create a new field type, you must write at least one of the two validation functions. Function f_check is a pointer to a function that takes two arguments: a field pointer and an argument pointer. The argument pointer is treated in the next section. f_check is called whenever the end-user tries to leave the field. It should check the field value stored in field buffer 0 and return TRUE if the field is valid or FALSE if not. If the validation function fails, your end-user remains on the offending field.

Function c_check is also a pointer to a function that takes two arguments: an integer that represents an ASCII character and an argument pointer. Function c_check is called as each character is entered by your end-user. It should check the character for validity and return TRUE if it is and FALSE if not.

Function new_fieldtype is useful for creating field types for specialized applications. For example, ``Creating a programmer-defined field type'' defines a new field type TYPE_HEX as a hex number between 0x0000 and 0xffff.

   	#include <ctype.h>
   	#include <form.h>
   	extern long strtol ();
   

#define isblank(c) ((c) == ' ')

static int padding = 4; /* pad on left to 4 digits */ static long vmin = 0x0000L; /* minimum acceptable value */ static long vmax = 0xffffL; /* maximum acceptable value */

static int fcheck_hex (f, arg) FIELD * f; char * arg; /* unnecessary here, discussed in the next section */ { 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; /* unnecessary in this example, discussed in the next section */ { return isxdigit (c); } FIELDTYPE * TYPE_HEX = new_fieldtype (fcheck_hex, ccheck_hex); /* create new field type */

Creating a programmer-defined field type

Later, you assign fields with the field type TYPE_HEX as you do with any field type and field:

   FIELD * field;
   

set_field_type (field, TYPE_HEX);

Function ccheck_hex checks that the input character is a valid hexadecimal digit, while function fcheck_hex examines the field value for valid characters and checks the range. If successful, fcheck_hex pads the field to four digits and returns TRUE. If not, it returns FALSE.


NOTE: The argument arg to functions f_check and c_check is not used in this version of the TYPE_HEX example because the new type does not require additional arguments to the set_field_type routine.

If successful, new_fieldtype returns a pointer to the new field type. If either argument to new_fieldtype is a NULL pointer, the corresponding validation is not performed. If no memory is available or both function pointers are NULL, new_fieldtype returns NULL.


Next topic: Freeing programmer-defined field types
Previous topic: Building a field type from two other field types

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004