home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 14view / getstr.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  921 b   |  48 lines

  1. /*
  2.  *    getstr -- get a string from the keyboard
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <local\std.h>
  8. #include <local\video.h>
  9. #include <local\keydefs.h>
  10.  
  11. char *
  12. getstr(buf, width)
  13. char *buf;
  14. int width;
  15. {
  16.     int row, col;
  17.     char *cp;
  18.  
  19.     /* function prototypes */
  20.     extern int putcur(int, int, int);
  21.     extern int readcur(int *, int *, int);
  22.     extern int writec(char, int, int);
  23.     extern int getkey();
  24.  
  25.     /* gather keyboard input into a string buffer */
  26.     cp = buf;
  27.     while ((*cp = getkey()) != K_RETURN && cp - buf < width) {
  28.         switch (*cp) {
  29.         case K_CTRLH:
  30.             /* destructive backspace */
  31.             if (cp > buf) {
  32.                 readcur(&row, &col, Vpage);
  33.                 putcur(row, col - 1, Vpage);
  34.                 writec(' ', 1, Vpage);
  35.                 --cp;
  36.             }
  37.             continue;
  38.         case K_ESC:
  39.             /* cancel string input operation */
  40.             return (char *)NULL;
  41.         }
  42.         put_ch(*cp, Vpage);
  43.         ++cp;
  44.     }
  45.     *cp = '\0';
  46.     return (buf);
  47. }
  48.