3.4 User Edit Routine Example



As an example, suppose that a field named city must begin with a letter. A user edit routine enforcing that convention would be:

#include <stdio.h>

#include <ctype.h>

#include <cbase/form.h>

char *

user_edit (type, edit_name, old_value, new_value, exit_char)

int type;

char *edit_name;

char *old_value;

char *new_value;

int exit_char;

{

if (type == U_USEREDIT && strcmp (edit_name, "city") == 0) {

if (*new_value == '\0' || isalpha (*new_value))

return (NULL);

return ("First position must be a letter");

}

else

return (NULL);

}

This user edit routine allows the field to be completely blank; but if it is not, the first character must be a letter. Remember that the user edit routine is called with U_USEREDIT each time the cursor passes through the field, so this edit check is made each time the cursor leaves the city field.

If the operator should be required to enter data into the field, the input required field in the Field Description form should be set to yes. This also allows the operator to skip past the field if desired, but the operator must enter data into the field before form will allow the operator to store the record.

If the user edit routine is instead coded as follows, the operator must enter data into the field every time the field is entered. It would not be possible to leave the field at all without first entering something into it.

#include <stdio.h>

#include <ctype.h>

#include <cbase/form.h>

char *

user_edit (type, edit_name, old_value, new_value, exit_char)

int type;

char *edit_name;

char *old_value;

char *new_value;

int exit_char;

{

if (type == U_USEREDIT && strcmp (edit_name, "city") == 0) {

if (isalpha (*new_value))

return (NULL);

return ("First position must be a letter");

}

else

return (NULL);

}

The examples above show just the user edit routine. The following demonstrates how to create a new form program with it. A main routine must be provided that calls form. The complete user edit routine would look like this:

#include <stdio.h>

#include <ctype.h>

#include <cbase/form.h>

char *

user_edit (type, edit_name, old_value, new_value, exit_char)

int type;

char *edit_name;

char *old_value;

char *new_value;

int exit_char;

{

if (type == U_USEREDIT && strcmp (edit_name, "city") == 0) {

if (*new_value == '\0' || isalpha (*new_value))

return (NULL);

return ("First position must be a letter");

}

else

return (NULL);

}

main (argc, argv)

int argc;

char *argv[];

{

form (argc, argv);

exit (0);

}

If this is stored in a text file named cityform.c, the command:

cl -c -AL -Ic:\cbase\include cityform.c

cl /F 5000 /Fe..\bin\cityform cityform.obj /link /NOE c:\cbase\lib\libcbase.lib

creates a new form program named cityform which performs the city field edit check along with all the standard editing functions of form. The formfile also must be modified slightly. The city field must have the user edit field set to yes in the Field Description form so that the user edit routine is called each time the city field is exited. Also, an edit_name of city should be provided in the edit field name field in the field description form. Form passes this name as the second parameter to the user edit routine.

Assuming the logical formfile is named sub, the user could then enter data into the subscriber file with the command:

cityform -qfuad sub

and additional editing on the city field would be performed.

As a second user edit routine example, a program could check for the correct state abbreviation in a state field. A user edit routine for this function would be as follows:

#include <stdio.h>

#include <cbase/form.h>

static char *states[] = {

"AK",

"AL",

/* ... */

"WA",

"WY",

NULL };

char *

user_edit (type, edit_name, old_value, new_value, exit_char) int type;

char *edit_name;

char *old_value;

char *new_value;

int exit_char;

{

int i;

if (type == U_USEREDIT && strcmp (edit_name, "state") == 0) {

/* allow an empty field */

if (*new_value == '\0')

return (NULL);

/* search table */

for (i = 0; states[i] != NULL; i++) {

if (strcmp (states[i], new_value) == 0)

return (NULL);

}

/* no match found */

return ("Illegal state abbreviation");

}

else

return (NULL);

}

main (argc, argv)

int argc;

char *argv[];

{

form (argc, argv);

exit (0);

}

Again, this routine allows the entry field to be left empty if the operator so desires. This program would be compiled much like the first example was. The user edit field in the field description form must be set to yes from the state field, and the edit name field would be entered as state.

As an aside, you could also perform this edit check without any programming by creating a small RMSfile in which the key field of each record was one of the state abbreviations, and specify this RMSfile as the Validation File. Then form would check the contents of the state field against the contents of this file.