home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / c / sclib31 / examples / linedit2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-31  |  2.5 KB  |  105 lines

  1. #include <scl1.h>
  2. #include <scl1keys.h>
  3. #include <scl1clor.h>
  4.  
  5.  /****************************************************************
  6.      Shows the use of LineEditor function using a mask to define
  7.      acceptable characters. A '@' accepts letters and # accepts
  8.      digits, * accepts any character. */
  9.  
  10. char mask[]="@@##@";    /* mask to be used */
  11.  
  12. unsigned int ExitKeys[]={ENTER,ESC,0};   /* LineEd exit keys */
  13.  
  14. main()
  15. {
  16. LEData led;
  17. int Mess;
  18. char buffer[6];
  19.  
  20. Cls(WHITE_BLACK,CLS_ALL);               /* clear screen */
  21. memset(buffer,0,sizeof(buffer));        /* initialize buffer */
  22.  
  23. /* initialize Line Editor structure */
  24.  
  25. LineEditor(LE_INIT,&led);
  26.  
  27. /* modify prompt and field position */
  28.  
  29. led.FRow=12;
  30. led.FCol=35;
  31. led.FLength=5;  /* the field's screen length is of 5 characters */
  32. led.FSize=5;
  33.  
  34. led.CType=CC_ANY;            /* type of valid characters */
  35. led.Buffer=buffer;           /* use our buffer */
  36. led.ExitKeys=ExitKeys;     /* our defined exit keys */
  37. PushCursor();              /* save cursor */
  38. LineEditor(LE_DRAW,&led);  /* draw */
  39.  
  40.   /* Main loop: send ACTIVE message, LineEditor constantly
  41.      returns information */
  42.  
  43. do
  44.     {
  45.     Mess=LineEditor(LE_ACTIVE,&led);
  46.     if(Mess==LE_ILLEGAL_KEY)        /* illegal key? */
  47.         TSound(440,10);                /* beep */
  48.  
  49.     CheckMask(&led);                /* call our function to check mask */
  50.  
  51.     /* loop until an exit key is pressed */
  52.  
  53.     }while(Mess != LE_EXIT_KEY);
  54.  
  55. Cls(WHITE_BLACK,CLS_ALL);               /* clear screen */
  56. }
  57.  
  58. CheckMask(LEData *led)
  59. {
  60. int i;
  61.  
  62. /* each time called this function will check the string in LineEditor
  63.    and compare it with the mask */
  64.  
  65. i=strlen(led->Buffer)-1;
  66. while(i >= 0)
  67.     {
  68.     switch(mask[i])
  69.         {           /* digits only */
  70.         case '#':
  71.             if(!isdigit(led->Buffer[i]))
  72.                 goto Invalid;
  73.             break;
  74.         case '@':    /* letters only */
  75.  
  76.             if(!isalpha(led->Buffer[i]))
  77.                 goto Invalid;
  78.             break;
  79.  
  80.         case '*':    /* accept anything */
  81.             return;
  82.         }
  83.     --i;
  84.     }
  85. return;
  86.  
  87. /* in case an invalid character is detected we'll move LineEditor
  88.    position to the invalid character. If insert mode is on (InFlag)
  89.    we'll send the CHARS_DOWN message to LineEditor so that all
  90.    characters that follow the invalid character are moved down. If
  91.    insert mode is off we'll copy a space character to the current
  92.    position */
  93.  
  94. Invalid:
  95.     {
  96.     led->Position=i;
  97.     if(led->InsFlag)
  98.         LineEditor(LE_CHARS_DOWN,led);
  99.     else
  100.         led->Buffer[i]=' ';
  101.  
  102.     LineEditor(LE_DRAW,led);    /* redraw */
  103.     TSound(440,10);
  104.     }
  105. }