home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file22 / getfn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  3.4 KB  |  120 lines

  1.  
  2.   /*
  3.    * GET_FNAME.C posted on Compuserve as GETFN.C
  4.    * Copyright 1992  Edward Mulroy
  5.    * Rights granted for all private and commercial uses
  6.    *
  7.    * This file is to illustrate simple data entry from the keyboard.
  8.    * It filters the keys to construct a valid filename.
  9.    */
  10.  
  11.   #include <stdio.h>
  12.   #include <conio.h>    /* for prototype of getch() */
  13.   #include <string.h>
  14.   #include <ctype.h>
  15.  
  16.   /*
  17.    * get a key.  if a special key, the upper byte has the value, else
  18.    * it's returned as the normal key that was pressed
  19.    */
  20.   int get_key( void)
  21.     {
  22.     int i;
  23.  
  24.     if ( !(i = getch()))
  25.       i = getch() << 8;
  26.  
  27.     return i;
  28.     }
  29.  
  30.  
  31.   char *ok_chars = ".-_!$&@#\"%~"; /* non alpha-numeric filename char's */
  32.  
  33.   /*
  34.    * read a filename from the keyboard
  35.    * do not accept paths or non-filename char's
  36.    * enforce an 8 char limit on the name, 3 on the extension
  37.    */
  38.   void get_fname( char *s1)
  39.     {
  40.     int ch, len, name_len, ext_len, has_dot;
  41.     char *s = s1;
  42.  
  43.     /* clear the entry area, space back to the start of the field */
  44.     fputs( "            \b\b\b\b\b\b\b\b\b\b\b\b", stdout);
  45.     len = name_len = ext_len = has_dot = 0;
  46.  
  47.     do
  48.       {
  49.       ch = getch();
  50.  
  51.       if ( ch > '~')         /* if a delete (^bksp) or function key, skip */
  52.         continue;
  53.  
  54.       ch = toupper( ch);                     /* force input to upper case */
  55.  
  56.       /* if a char which is ok in a filename, add it */
  57.       if ( isalnum( ch) || (strchr( ok_chars, ch) != NULL))
  58.         {
  59.         if ( ch == '.')       /* handle the '.' indicating an ext follows */
  60.           if ( has_dot)
  61.             continue;              /* don't accept two dots in a filename */
  62.           else
  63.             has_dot = 1;
  64.         else if ( has_dot)
  65.           if ( ext_len == 3)         /* don't allow extensions > 3 char's */
  66.             continue;
  67.           else
  68.             ++ext_len;           /* if already has '.', adjust ext length */
  69.         else if ( name_len == 8)          /* don't allow names > 8 char's */
  70.           continue;
  71.         else
  72.           ++name_len;                     /* else adjust base name length */
  73.  
  74.         *s++ = ch;                                /* add it to the string */
  75.         ++len;                                    /* adjust string length */
  76.         putchar( ch);                                    /* echo the char */
  77.  
  78.                    /* if the last char in the field, don't space out more */
  79.         if ( ext_len == 3)
  80.           putchar( '\b');
  81.         }
  82.       else if ( (ch == '\b') && len)  /* delete the char if any in string */
  83.         {
  84.         if ( s[ -1] == '.')                  /* adjust counts or '.' flag */
  85.           has_dot = 0;
  86.         else if ( has_dot && ext_len)
  87.           --ext_len;
  88.         else if ( !has_dot && name_len)
  89.           --name_len;
  90.  
  91.         --len;                       /* adjust length and string position */
  92.         --s;
  93.  
  94.         if ( ext_len < 2)     /* blank the deleted char & move the cursor */
  95.           putchar( '\b');
  96.  
  97.         fputs( " \b", stdout);
  98.         }
  99.       } while ( ch != '\r');
  100.  
  101.     *s = '\0';
  102.     putchar( '\n');
  103.     }
  104.  
  105.  
  106.   #define DEMO
  107.  
  108.   #ifdef DEMO
  109.   char fname[ 14];
  110.  
  111.   int main()
  112.     {
  113.     fputs( "Enter Filename: ", stdout);
  114.     get_fname( fname);
  115.     printf( "\n\nYou entered \"%s\"\n", fname);
  116.     return 0;
  117.     }
  118.   #endif
  119.         
  120.