home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * GET_FNAME.C posted on Compuserve as GETFN.C
- * Copyright 1992 Edward Mulroy
- * Rights granted for all private and commercial uses
- *
- * This file is to illustrate simple data entry from the keyboard.
- * It filters the keys to construct a valid filename.
- */
-
- #include <stdio.h>
- #include <conio.h> /* for prototype of getch() */
- #include <string.h>
- #include <ctype.h>
-
- /*
- * get a key. if a special key, the upper byte has the value, else
- * it's returned as the normal key that was pressed
- */
- int get_key( void)
- {
- int i;
-
- if ( !(i = getch()))
- i = getch() << 8;
-
- return i;
- }
-
-
- char *ok_chars = ".-_!$&@#\"%~"; /* non alpha-numeric filename char's */
-
- /*
- * read a filename from the keyboard
- * do not accept paths or non-filename char's
- * enforce an 8 char limit on the name, 3 on the extension
- */
- void get_fname( char *s1)
- {
- int ch, len, name_len, ext_len, has_dot;
- char *s = s1;
-
- /* clear the entry area, space back to the start of the field */
- fputs( " \b\b\b\b\b\b\b\b\b\b\b\b", stdout);
- len = name_len = ext_len = has_dot = 0;
-
- do
- {
- ch = getch();
-
- if ( ch > '~') /* if a delete (^bksp) or function key, skip */
- continue;
-
- ch = toupper( ch); /* force input to upper case */
-
- /* if a char which is ok in a filename, add it */
- if ( isalnum( ch) || (strchr( ok_chars, ch) != NULL))
- {
- if ( ch == '.') /* handle the '.' indicating an ext follows */
- if ( has_dot)
- continue; /* don't accept two dots in a filename */
- else
- has_dot = 1;
- else if ( has_dot)
- if ( ext_len == 3) /* don't allow extensions > 3 char's */
- continue;
- else
- ++ext_len; /* if already has '.', adjust ext length */
- else if ( name_len == 8) /* don't allow names > 8 char's */
- continue;
- else
- ++name_len; /* else adjust base name length */
-
- *s++ = ch; /* add it to the string */
- ++len; /* adjust string length */
- putchar( ch); /* echo the char */
-
- /* if the last char in the field, don't space out more */
- if ( ext_len == 3)
- putchar( '\b');
- }
- else if ( (ch == '\b') && len) /* delete the char if any in string */
- {
- if ( s[ -1] == '.') /* adjust counts or '.' flag */
- has_dot = 0;
- else if ( has_dot && ext_len)
- --ext_len;
- else if ( !has_dot && name_len)
- --name_len;
-
- --len; /* adjust length and string position */
- --s;
-
- if ( ext_len < 2) /* blank the deleted char & move the cursor */
- putchar( '\b');
-
- fputs( " \b", stdout);
- }
- } while ( ch != '\r');
-
- *s = '\0';
- putchar( '\n');
- }
-
-
- #define DEMO
-
- #ifdef DEMO
- char fname[ 14];
-
- int main()
- {
- fputs( "Enter Filename: ", stdout);
- get_fname( fname);
- printf( "\n\nYou entered \"%s\"\n", fname);
- return 0;
- }
- #endif
-
-