Re: XForms: Example of fl_set_input_filter

Steve Lamont (spl@szechuan.ucsd.edu)
Tue, 24 Jun 97 06:28:38 PDT

To subscribers of the xforms list from spl@szechuan.ucsd.edu (Steve Lamont) :

> Can someone show me an example of the use to the function:
>
> fl_set_input_filter

Sure. Here's a bit of code that filters an input field for a "legal"
file name.

First, this is where we create the form and establish the filter:

FD_rename_object *rename_object = create_form_rename_object();

fl_set_input_filter( rename_object->object_name,
name_filter );

[...]

fl_show_form( rename_object->rename_object,
FL_PLACE_MOUSE, FL_TRANSIENT,
"Rename Object..." );

[...]

and this is the filter itself:

static int name_filter( FL_OBJECT *obj,
const char *old, const char *cur,
int c )

{

int valid;

switch ( c ) {

case '\t': /* Characters we definitely don't want */
case ' ': /* since they create "troublesome" file names */
case '/': /* which have to be quoted and will cause */
case '`': /* trouble for unsophisticated lusers */
case '\'':
case '"':
case '[':
case ']':
case '*':
case '~':
case '&':
case '^':
case ';':
case '<':
case '>':
case '?':
case '|': {

valid = FL_INVALID;
break;

}
default: { /* Could be valid, if printable */

if ( isprint( c ) )
valid = FL_VALID;
else
valid = FL_INVALID;
break;

}

}

return valid;

}

This probably also could have been done with a regular expression
that would look like

"[A-Za-z0-9_+=!@#$%-:]*"

The purpose of the filter is to make sure the luser doesn't enter a
file name that will cause trouble if they have to type it from the
shell. For instance

foo_bar

is legal but

foo?bar

is not since it contains a character with special meaning to the
shell.

> Thank you.

De nada.

spl

_________________________________________________
To unsubscribe, send the message "unsubscribe" to
xforms-request@bob.usuf2.usuhs.mil or see
http://bob.usuf2.usuhs.mil/mailserv/xforms.html
Xforms Home Page: http://bragg.phys.uwm.edu/xforms
List Archive: http://bob.usuf2.usuhs.mil/mailserv/list-archives/