home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / cursesp.zip / STRGET.C < prev    next >
C/C++ Source or Header  |  1991-12-03  |  7KB  |  189 lines

  1. /****************************************************************/
  2. /* Getstr() routines of the PCcurses package                    */
  3. /*                                                              */
  4. /****************************************************************/
  5. /* This version of curses is based on ncurses, a curses version */
  6. /* originally written by Pavel Curtis at Cornell University.    */
  7. /* I have made substantial changes to make it run on IBM PC's,  */
  8. /* and therefore consider myself free to make it public domain. */
  9. /*              Bjorn Larsson (...mcvax!enea!infovax!bl)        */
  10. /****************************************************************/
  11. /* 1.0: Release:                                        870515  */
  12. /* 1.2: Max limits of by 1. Block nest error in function        */
  13. /*      backchar(). Fixed thanks to S. Creps:           881002  */
  14. /* 1.3: MSC -W3, Turbo'C' -w -w-pro checkes:            881005  */
  15. /****************************************************************/
  16.  
  17. #include <curses.h>
  18. #include <curspriv.h>
  19.  
  20. static  char    *backchar(char*);
  21.  
  22. char _curses_strget_rcsid[] = "@(#)strget.c v1.3 - 881005";
  23.  
  24. static  bool     oldecho;
  25. static  bool     oldcbreak;
  26. static  bool     oldnodelay;
  27. static  char    *strbeg;
  28. static  WINDOW  *w;
  29. static  int      xbeg;
  30.  
  31. /****************************************************************/
  32. /* Wgetstr(win,str) reads in a string (terminated by \n or \r)  */
  33. /* to the buffer pointed to by 'str', and displays the input    */
  34. /* in window 'win'. The user's erase and kill characters are    */
  35. /* active.                                                      */
  36. /****************************************************************/
  37.  
  38. int wgetstr(WINDOW *win, char *str)
  39.   {
  40.   w          = win;
  41.   strbeg      = str;        /* keep start for backspacing */
  42.   oldcbreak       = _cursvar.cbreak;    /* remember states */
  43.   oldecho         = _cursvar.echo;
  44.   oldnodelay      = w->_nodelay;
  45.   _cursvar.echo   = FALSE;        /* we do echo ourselves */
  46.   _cursvar.cbreak = TRUE;        /* no wait for chars */
  47.   w->_nodelay   = FALSE;        /* don't return 'NOCHARS' */
  48.   xbeg = w->_curx;            /* remember screen start x-position */
  49.  
  50.   wrefresh(w);                /* sets cursor at right place */
  51.   while ((*str = (char) getch()) != '\n')
  52.     {
  53.     if (*str == '\r')
  54.       break;
  55.     if (*str == _DCCHAR)
  56.       {
  57.       if (str > strbeg)
  58.     str = backchar(str);
  59.       } /* if */
  60.     else
  61.       if (*str == _DLCHAR)
  62.     while (str > strbeg)
  63.       str = backchar(str);
  64.       else
  65.     {
  66.         if (oldecho)            /* check if echo */
  67.           {
  68.       waddch(w,*str++);
  69.       wrefresh(w);
  70.           }
  71.     else
  72.       str++;
  73.     } /* else */
  74.       } /* while */
  75.  
  76.   *str = '\0';
  77.   _cursvar.echo   = oldecho;
  78.   _cursvar.cbreak = oldcbreak;
  79.   win->_nodelay   = oldnodelay;
  80.   return(OK);
  81.   } /* wgetstr */
  82. /****************************************************************/
  83. /* Getstr(str) reads in a string (terminated by \n or \r) to    */
  84. /* the buffer pointed to by 'str', and displays the input in    */
  85. /* stdscr. The user's erase and kill characters are active.     */
  86. /****************************************************************/
  87.  
  88. int getstr(str)
  89.   char *str;
  90.   {
  91.   return(wgetstr(stdscr,str));
  92.   } /* getstr */
  93.  
  94. /****************************************************************/
  95. /* Mvgetstr(y,x,str) moves the stdscr cursor to a new position, */
  96. /* then reads in a string (terminated by \n or \r) to the buf-  */
  97. /* fer pointed to by 'str', and displays the input in stdscr.   */
  98. /* The user's erase and kill characters are active.             */
  99. /****************************************************************/
  100.  
  101. int mvgetstr(y,x,str)
  102.   int y;
  103.   int x;
  104.   char *str;
  105.   {
  106.   if (wmove(stdscr,y,x) == ERR)
  107.     return(ERR);
  108.   return(wgetstr(stdscr,str));
  109.   } /* mvgetstr */
  110.  
  111. /****************************************************************/
  112. /* Mvwgetstr(win,y,x,str) moves the 'win' cursor to a new       */
  113. /* position, then reads in a string (terminated by \n or \r)    */
  114. /* to the buffer pointed to by 'str', and displays the input in */
  115. /* stdscr. The user's erase and kill characters are active.     */
  116. /****************************************************************/
  117.  
  118. int mvwgetstr(win,y,x,str)
  119.   WINDOW *win;
  120.   int     y;
  121.   int     x;
  122.   char   *str;
  123.   {
  124.   if (wmove(win,y,x) == ERR)
  125.     return(ERR);
  126.   return(wgetstr(win,str));
  127.   } /* mvwgetstr */
  128.  
  129. /****************************************************************/
  130. /* Backchar() does a character delete with screen erase, even   */
  131. /* up to previous lines. It will not back-scroll if the begi-   */
  132. /* ning of the string has scrolled off the window. Steps back   */
  133. /* pointer 's', and returns the new value.                      */
  134. /****************************************************************/
  135.  
  136. static char *backchar(s)
  137.   char    *s;
  138.   {
  139.   static int nbs;
  140.   static int x;
  141.   static char *p;
  142.   static int ts;
  143.  
  144.   x =  xbeg;
  145.   ts =  w->_tabsize;
  146.  
  147.   s--;                                          /* step back string */
  148.   nbs = 1;                                      /* step at least one pos */
  149.   if ((*s < ' ') || (*s == 0x7f))               /* ctrl-char has size 2 */
  150.     nbs++;
  151.   if (*s == '\t')                               /* tabs are very special */
  152.     {
  153.     for (p = strbeg; p < s ;p++)                /* find x-pos of last char */
  154.       {
  155.       if (*p == '\t')                           /* go to next tab */
  156.         x = ((x/ts)+1) * ts;
  157.       else
  158.         {
  159.         if ((*p < ' ') || (*p == 0x7f))         /* control character */
  160.           x += 2;
  161.         else                                    /* normal char */
  162.           x++;
  163.         } /* else */
  164.       if (x >= w->_maxx)                        /* go to next line? */
  165.         x = 0;
  166.       } /* for */
  167.     if (!(w->_curx))                            /* if step-over newline */
  168.       nbs = w->_maxx - x;
  169.     else                                        /* in-line tab */
  170.       nbs = w->_curx - x;                       /* positions to erase */
  171.     } /* if */
  172.  
  173.   while(nbs--)                                  /* do so many */
  174.     {
  175.     if (w->_curx > 0)                           /* if not at line beginning */
  176.       waddstr(w,"\b \b");
  177.     else
  178.       if (w->_cury)                             /* if not on top line */
  179.         {
  180.         mvwaddch(w,w->_cury-1,w->_maxx -1,' '); /* put space at line end */
  181.         wmove(w,w->_cury-1,w->_maxx - 1);       /* and go there again */
  182.         } /* else */
  183.     } /* while */
  184.  
  185.   wrefresh(w);                                  /* redraw screen */
  186.   *(s+1) = '\0';                                /* make string terminated */
  187.   return(s);
  188.   } /* backchar */
  189.