home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume10 / tourney / part01 / get.c next >
C/C++ Source or Header  |  1990-03-06  |  5KB  |  299 lines

  1. /* %W% general getstring routine                     */
  2. /* author: Jack Alexander - this routine may be freely distibuted */
  3. #include    <curses.h>
  4. #include    "get.h"
  5.  
  6. get_num(x,y,digits,low,high,number)
  7. int    x,y,digits,low,high,*number;
  8. {
  9.     char    buf[80];
  10.     int    retval;
  11.  
  12.     buf[0]='\0';
  13.     noecho();
  14.     raw();
  15.     while(1)
  16.         switch(getst(digits,x,y,buf,digits+1,NULL,NUM_ONLY,NULL)) {
  17.             case GET_ESCAPE:
  18.                 return(GET_ESCAPE);
  19.             case GET_DOWN:
  20.             case GET_RETURN:
  21.                 retval=atoi(buf);
  22.                 if(retval < low || retval>high)
  23.                     break;
  24.                 *number = retval;
  25.                 echo();
  26.                 noraw();
  27.                 return(0);
  28.             default:
  29.                 break;
  30.         }
  31. }
  32.  
  33. /* a few hints...    */
  34. /* idx = current index into string */
  35. /* cx = current screen x position */
  36. /* cy = current screen y position */
  37. /* ix = initial screen x (upon call, where cursor is requested to be placed) */
  38. /* iy = initial screen y " " " ... */
  39. /* fw = field width */
  40. /* mx = max length of string */
  41. /* sl = string length */
  42. /* valid = string of acceptable characters within the type passed in gettype */
  43. /*         if NULL, then all type of characters passed in gettype are allowed */
  44.  
  45. int    idx, cx, cy, ix, iy, fw, mx, sl, rcode;
  46.  
  47. getst(len,sx,sy,str,fl,retlen,gettype,valid)
  48. int    len, sx, sy, fl, *retlen, gettype;
  49. char    *str, *valid;
  50. {
  51.     int    c;
  52.  
  53.  
  54.     rcode = INITIAL;
  55.     idx=0;
  56.     cx=ix=sx;
  57.     cy=iy=sy;
  58.     mx = len;
  59.     fw = fl;
  60.     str[mx]='\0';
  61.     sl=strlen(str);
  62. #ifdef undef
  63.     init();
  64. #endif
  65.     while(1) {
  66.         if(gettype==SHOW) {    /* want only to display the string */
  67.             drawstr(str,0);    /* draw the string */
  68. #ifdef undef
  69.             de_init();
  70. #endif
  71.             return;
  72.         }
  73.         drawstr(str,1);    /* draw the string */
  74.         c=getch();
  75.         switch(c) {
  76.             case GET_CLEAR:
  77.                 clearall(str);
  78.                 break;
  79.             case GET_ESCAPE:
  80.                 rcode=GET_ESCAPE;
  81.                 break;
  82.             case GET_UP:
  83.                 rcode=dec_y_pos();
  84.                 break;
  85.             case GET_DOWN:
  86.                 rcode=inc_y_pos();
  87.                 break;
  88.             case GET_LEFT:
  89.                 rcode=dec_x_pos();
  90.                 break;
  91.             case GET_RIGHT:
  92.                 rcode=inc_x_pos(1);
  93.                 break;
  94.             case GET_DELETE:
  95.             case GET_DELETE2:
  96.                 remove_char(str);
  97.                 break;
  98.             case GET_TAB:
  99.             case GET_RETURN:
  100.                 rcode = GET_RETURN;
  101.                 break;
  102.             default:
  103.                 switch(gettype) {
  104.                     case NUM_ONLY: /* numbers only (0-9) */
  105.                         if(c>='0' && c<='9')
  106.                             if(in(c,valid))
  107.                                 add_char(str,c);
  108.                         break;
  109.                     case LETTER_ONLY:
  110.                     /* letters a-z and A-Z, and ' ' only */
  111.                         if(c==' ' || (c>='a' && c<='z') || (c>='A' && c<='Z'))
  112.                             if(in(c,valid))
  113.                                 add_char(str,c);
  114.                         break;
  115.                     case ALL_ALPHA:/* any printable chars */
  116.                         if(c>=' ' && c<=0x7f)
  117.                             if(in(c,valid))
  118.                                 add_char(str,c);
  119.                         break;
  120.                     default:
  121.                         if(c>=' ' && c<=0x7f)
  122.                             add_char(str,c);
  123.                         break;
  124.                 }
  125.                 break;
  126.         }
  127.         if(rcode != INITIAL) {
  128. #ifdef undef
  129.             de_init();
  130. #endif
  131.             if(retlen != NULL)
  132.                 *retlen = sl;
  133.             return(rcode);
  134.         }
  135.     }
  136. }
  137.  
  138. dec_y_pos()
  139. {
  140.     if(!idx)
  141.         return(GET_UP);
  142.     if(iy >= cy) {
  143.         idx = 0;
  144.         cx = ix;
  145.         return(INITIAL);
  146.     }
  147.     idx -= fw;
  148.     cy--;
  149.     return(INITIAL);
  150. }
  151.  
  152. inc_y_pos()
  153. {
  154.     if(idx>=mx-1 || idx==sl)
  155.         return(GET_DOWN);
  156.     if(idx+fw >=  mx) {
  157.         cx += mx-idx - (mx-sl);
  158.         idx += mx-idx - (mx-sl);
  159. #ifdef undef
  160.         cx += (mx - idx -fw);
  161.         idx += (mx - idx - fw);
  162. #endif
  163.         return(INITIAL);
  164.     }
  165.     if(idx+fw > sl) {
  166.         cx += sl - idx;
  167.         idx += sl - idx;
  168.         return(INITIAL);
  169.     }
  170.     idx += fw;
  171.     cy++;
  172.     return(INITIAL);
  173. }
  174.  
  175. inc_x_pos(flag)
  176. int    flag;
  177. {
  178.     if(flag && idx==sl)
  179.         return(GET_RIGHT);
  180.     if(idx == mx)
  181.         return(GET_RIGHT);
  182.     cx++;
  183.     idx++;
  184.     if(cx >= (ix+fw)) {
  185.         cx = ix;
  186.         cy++;
  187.     }
  188.     return(INITIAL);
  189. }
  190.  
  191. dec_x_pos()
  192. {
  193.     if(idx==0)
  194.         return(GET_LEFT);
  195.     idx--;
  196.     cx--;
  197.     if(cx < ix) {
  198.         cx = fw+ix-1;
  199.         cy--;
  200.     }
  201.     return(INITIAL);
  202. }
  203.  
  204. add_char(str, c)
  205. char    *str;
  206. char    c;
  207. {
  208.     register    int    i;
  209.  
  210.     if(inc_x_pos(0)!=INITIAL)
  211.         return;
  212.     for(i=mx-1;i>=idx;i--)
  213.         str[i]=str[i-1];
  214.     str[idx - 1] = c;
  215.     if(sl!=mx)
  216.         sl++;
  217. }
  218.  
  219. remove_char(str)
  220. char    *str;
  221. {
  222.     register int i;
  223.  
  224.     if(idx >= sl) {        /* remove char behind cursor */
  225.         if(dec_x_pos()!=INITIAL)
  226.             return;
  227.         str[sl-1]='\0';
  228.     }
  229.     else 
  230.         for(i=idx;i<mx;i++)
  231.             str[i]=str[i+1];
  232.     sl--;
  233. }
  234.  
  235. drawstr(str,refr)
  236. char    str[];
  237. int    refr;
  238. {
  239.     int    i, j, s, yo;
  240.  
  241.     s=sl;
  242.     move(iy,ix);
  243.     for(i=0,yo=1;i<mx;) {
  244.         if(i>=s) {
  245.             addch('.');
  246.             i++;
  247.         }
  248.         else if((s-i) < fw) {
  249.             printw("%s",&str[i]);
  250.             i += s-i;
  251.         }
  252.         else {
  253.             for(j=0;j<fw;j++)
  254.                 addch(str[i++]);
  255.         }
  256.         if((i % fw)==0) {
  257.             move(iy + yo,ix);
  258.             yo++;
  259.         }
  260.     }
  261.     move(cy,cx);
  262.     if(refr)
  263.         refresh();
  264. }
  265.  
  266. init()
  267. {
  268.     noecho();
  269.     raw();
  270. }
  271.  
  272. de_init()
  273. {
  274.     noraw();
  275.     echo();
  276. }
  277.  
  278. in(ch,allow)
  279. char    ch, allow[];
  280. {
  281.     register int    i;
  282.  
  283.     if(allow == NULL)
  284.         return(1);
  285.     for(i=0;i<strlen(allow);i++)
  286.         if(ch==allow[i])
  287.             return(1);
  288.     return(0);
  289. }
  290.  
  291. clearall(str)
  292. char    str[];
  293. {
  294.     sl=idx=0;
  295.     cx=ix;
  296.     cy=iy;
  297.     str[0]='\0';
  298. }
  299.