home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 165 / WDFMT21.ZIP / KEYS.C < prev    next >
Text File  |  1990-03-20  |  10KB  |  360 lines

  1. #include "scancode.h"
  2. #include "defs.h"
  3. #include <dos.h>
  4.  
  5. extern int redirection;
  6. extern union REGS inregs, outregs;
  7. unsigned char get_input();
  8.  
  9. /*********************************************/
  10. /*                         */
  11. /* get_dec(row,col,max_didit,defalt)         */
  12. /*                         */
  13. /*  input:                     */
  14. /*    row    : vetical location         */
  15. /*    col    : left    most digit location  */
  16. /*    max_digit : max number of digits         */
  17. /*    defalt    : default value if only <CR> */
  18. /*                         */
  19. /*  out put:                     */
  20. /*    integer vlue of input             */
  21. /*                         */
  22. /*********************************************/
  23.  
  24. int get_dec(row,col,max_digit,defalt)
  25.  
  26. int row,col,max_digit,defalt;
  27.  
  28. {                        /* start of get_dec */
  29.    unsigned char in_char;
  30.    unsigned char in_arry[6];
  31.    int    power,first_time,done,i,in_val,arry_ptr,t_col,t_defalt;
  32.  
  33.    t_defalt = defalt;
  34.    power=10;
  35.    for(i=0;i<=max_digit;++i)            /* setup arry wit defalt */
  36.    {
  37.      in_arry[i] = t_defalt % power;
  38.      t_defalt = t_defalt / power;
  39.    }
  40.    supress_zero(row,col,max_digit,in_arry); /* print arry */
  41.    t_col = col+max_digit;
  42.    arry_ptr =0;
  43.    first_time = TRUE;
  44.    done = FALSE;
  45.    while(!done)
  46.    {                        /* start of while */
  47.      curs(row,t_col);
  48.      in_char = (unsigned char)get_input(redirection);             /* get input from stand input */
  49.      if(('0' <= in_char) AND (in_char <= '9'))
  50.      {                        /* if valid input */
  51.        if(first_time)
  52.        {
  53.       for(i=0;i<=max_digit;++i) in_arry[i] = 0; /* clear arry */
  54.        }
  55.        for(i=max_digit;i>=arry_ptr;--i)
  56.      in_arry[i+1]=in_arry[i];
  57.        in_arry[arry_ptr] = in_char - 0x30;  /* get dec value */
  58.        first_time = FALSE;
  59.        supress_zero(row,col,max_digit,in_arry); /* print arry */
  60.      }
  61.      else                    /* if not a number */
  62.       {                     /* start of else not a number*/
  63.        switch(in_char)
  64.     {                    /* start of switch */
  65.        case BK_SP    :            /* if back space */
  66.               if(arry_ptr<max_digit)
  67.               {
  68.                  for(i=0;i<=max_digit;++i)        /* shift arry by 1*/
  69.                 in_arry[i]=in_arry[i+1];
  70.                  in_arry[max_digit]= 0;
  71.                  supress_zero(row,col,max_digit,in_arry); /* print arry */
  72.               }
  73.               break;
  74.        case CURLF    :             /* if left arrow */
  75.               if(arry_ptr<max_digit)
  76.               {
  77.                  ++arry_ptr;
  78.                  --t_col;
  79.               }
  80.               break;
  81.        case CURRT    :             /* if right arrow */
  82.               if(arry_ptr>0)   /* if valid position */
  83.               {
  84.                  --arry_ptr;
  85.                  ++t_col;
  86.               }
  87.               break;
  88.        case INSERT    :             /* if insert key */
  89.               break;
  90.        case DELETE    :             /* if delete key */
  91.               if(arry_ptr<max_digit)
  92.               {
  93.                  for(i=arry_ptr;i<=max_digit;++i)       /* shift arry by 1*/
  94.                 in_arry[i]=in_arry[i+1];
  95.                  in_arry[max_digit]= 0;
  96.                  supress_zero(row,col,max_digit,in_arry); /* print arry */
  97.               }
  98.               break;
  99.        case CRG_RTN :             /* if <CR> key */
  100.               done = TRUE;
  101.               break;
  102.        default    :
  103.               BELL;
  104.     }                    /* end of switch */
  105.       }                     /* end of else not a number*/
  106.    }                        /* end of while */
  107.    if(first_time) in_val = defalt;        /* if no input */
  108.    else                     /* if valid input */
  109.    {                        /* change array into int */
  110.      in_val= 0;
  111.      power = 1;
  112.      for(i=0;i<=max_digit-1;++i)
  113.      {
  114.        in_val += in_arry[i] * power;
  115.        power  *= 10;
  116.      }
  117.    }                        /* end of if else */
  118.  return in_val;
  119.  }                        /* end of get_dec */
  120.  
  121. /*********************************************/
  122. /*                         */
  123. /* get_dec2(row,col,max_didit,answ,special)  */
  124. /*                         */
  125. /*  input:                     */
  126. /*    row    : vetical location         */
  127. /*    col    : left    most digit location  */
  128. /*    max_digit : max number of digits         */
  129. /*    answ    : address of the answer      */
  130. /*    special    : special char to check for  */
  131. /*                         */
  132. /*  out put:                     */
  133. /*     0    : special char not found     */
  134. /*     1    : <CR> with no in put         */
  135. /*     2    : special char found         */
  136. /*********************************************/
  137.  
  138. int get_dec2(row,col,max_digit,in_val,special)
  139.  
  140. int row,col,max_digit;
  141. int *in_val;
  142. unsigned char special;
  143.  
  144. {
  145.                       /* start of get_dec */
  146.    unsigned char in_char;
  147.    unsigned char in_arry[6];
  148.    int    power,first_time,done,i,arry_ptr,t_col,is_special;
  149.  
  150.    for(i=0;i<=max_digit;++i)
  151.    {
  152.       in_arry[i] = 0;                /* clear arry */
  153.       curs(row,col+i);printf(" ");          /* clear space */
  154.    }
  155.    is_special = 0;
  156.    arry_ptr   = 0;
  157.    first_time = TRUE;
  158.    t_col      = col + max_digit;
  159.    done       = FALSE;
  160.    while(!done)
  161.    {                        /* start of while */
  162.      curs(row,t_col);
  163.      in_char = (unsigned char)get_input(redirection);  /* get input from stand input */
  164.      if(('0' <= in_char) AND (in_char <= '9'))
  165.      {                        /* if valid input */
  166.        for(i=max_digit;i>=arry_ptr;--i)
  167.      in_arry[i+1]=in_arry[i];
  168.        in_arry[arry_ptr] = in_char - 0x30;  /* get dec value */
  169.        first_time = FALSE;
  170.        supress_zero(row,col,max_digit,in_arry); /* print arry */
  171.      }
  172.      else                    /* if not a number */
  173.       {                     /* start of else not a number*/
  174.        switch(in_char)
  175.     {                    /* start of switch */
  176.        case BK_SP    :            /* if back space */
  177.               if((arry_ptr<max_digit) AND (!first_time))
  178.               {
  179.                  for(i=0;i<=max_digit;++i)        /* shift arry by 1*/
  180.                 in_arry[i]=in_arry[i+1];
  181.                  in_arry[max_digit]= 0;
  182.                  supress_zero(row,col,max_digit,in_arry); /* print arry */
  183.               }
  184.               break;
  185.        case CURLF    :             /* if left arrow */
  186.               if((arry_ptr<max_digit) AND (!first_time))
  187.               {
  188.                  ++arry_ptr;
  189.                  --t_col;
  190.               }
  191.               break;
  192.        case CURRT    :             /* if right arrow */
  193.               if((arry_ptr>0) AND (!first_time))   /* if valid position */
  194.               {
  195.                  --arry_ptr;
  196.                  ++t_col;
  197.               }
  198.               break;
  199.        case INSERT    :             /* if insert key */
  200.               break;
  201.        case DELETE    :             /* if delete key */
  202.               if((arry_ptr<max_digit) AND (!first_time))
  203.               {
  204.                  for(i=arry_ptr;i<=max_digit;++i)       /* shift arry by 1*/
  205.                 in_arry[i]=in_arry[i+1];
  206.                  supress_zero(row,col,max_digit,in_arry); /* print arry */
  207.               }
  208.               break;
  209.        case CRG_RTN :             /* if <CR> key */
  210.               done = TRUE;
  211.               break;
  212.        default    :
  213.               if(in_char EQL special)
  214.               {
  215.                  is_special = 2;
  216.                  done = TRUE;
  217.               }
  218.               else BELL;
  219.     }                    /* end of switch */
  220.       }                     /* end of else not a number*/
  221.    }                        /* end of while */
  222.    *in_val= 0;
  223.    if(first_time AND special)
  224.      is_special = 1;                /* if no input */
  225.    else                     /* if valid input */
  226.    {                        /* change array into int */
  227.      power = 1;
  228.      for(i=0;i<=max_digit-1;++i)
  229.      {
  230.        *in_val += in_arry[i] * power;
  231.        power  *= 10;
  232.      }
  233.    }                        /* end of if else */
  234.  return is_special;
  235.  }                        /* end of get_dec */
  236.  
  237. /*********************************************/
  238. /*                         */
  239. /* suppress_zero(row,col,max_didit,in_arry)  */
  240. /*                         */
  241. /*  input:                     */
  242. /*    row    : vetical location         */
  243. /*    col    : left    most digit location  */
  244. /*    max_digit : max number of digits         */
  245. /*    in_arry    : arry to be printed         */
  246. /*                         */
  247. /*  print in_arry supressing leading zeros   */
  248. /*                         */
  249. /*********************************************/
  250.  
  251.  
  252. supress_zero(row,col,max_digit,in_arry)
  253. unsigned char in_arry[];
  254. int row,col,max_digit;
  255.  
  256.  
  257.  
  258. {                        /* start of supress_zero */
  259.    int i,none_zero;
  260.  
  261.    none_zero = FALSE;
  262.    for(i=max_digit-1;i>=0;--i)            /* print arry */
  263.    {
  264.       if((in_arry[i] != 0) OR none_zero)    /* check if zero */
  265.       {
  266.      none_zero = TRUE;
  267.      curs(row,col-i+max_digit);
  268.      printf("%d",in_arry[i]);
  269.       }                    /* end if not zero or none_zero */
  270.       else
  271.       {
  272.      curs(row,col-i+max_digit);
  273.      printf(" ");
  274.       }
  275.       if(i EQL 0)
  276.       {
  277.      curs(row,col-i+max_digit);
  278.      printf("%d",in_arry[i]);
  279.       }                    /* end if not zero or none_zero */
  280.    }                        /* end for i */
  281. }                        /* end suppress_zero */
  282.  
  283.  
  284. /*****************************************************/
  285. /*                             */
  286. /* get_input(redirection)                 */
  287. /*                             */
  288. /*  input:                         */
  289. /*     redirection :  0 = get input from key board   */
  290. /*              1 = get input from file         */
  291. /*                             */
  292. /*                             */
  293. /*  output: input from key board or file         */
  294. /*                             */
  295. /*****************************************************/
  296.  
  297. unsigned char get_input(redirection)
  298.  
  299. int redirection;
  300.  
  301. {
  302.  
  303. if (redirection)                /* where to get data ? */
  304. {                        /* get data from file */
  305.   return 0;
  306. }
  307.  
  308. else                        /* get data from keyboard */
  309. {
  310.     inregs.h.ah = 0x08;             /* set up interupt regs */
  311.     intdos(&inregs,&outregs);            /* do a dos intr #0x21 */
  312.     if(outregs.h.al EQL 0)
  313.     {                        /* if extended key board */
  314.        inregs.h.ah = 0x08;            /* set up interupt regs */
  315.        intdos(&inregs,&outregs);        /* do a dos intr #0x21 */
  316.        return(unsigned char)(outregs.h.al + 0x80);
  317.     }
  318.     return  (unsigned char)outregs.h.al;    /* return Ax low byte */
  319. }
  320. }
  321.  
  322.  
  323. /*****************************************************/
  324. /*                             */
  325. /*  getyn(redirection)                     */
  326. /*                             */
  327. /*  input  :                         */
  328. /*     redirection :  0 = get input from key board   */
  329. /*              1 = get input from file         */
  330. /*                             */
  331. /*                             */
  332. /*  output:           0 = n,N                 */
  333. /*               1 = y,Y                 */
  334. /*                             */
  335. /* this routine will get  y/n responce from input    */
  336. /*                             */
  337. /*****************************************************/
  338.  
  339. getyn(redirection)
  340.  
  341. int redirection;
  342.  
  343. {                        /* start of getyn */
  344.    int valid;
  345.    unsigned char temp;
  346.  
  347.    while(TRUE)
  348.    {                        /* start of while */
  349.       temp = get_input(redirection);
  350.       temp = toupper(temp);
  351.       switch(temp)
  352.       {                     /* start of switch */
  353.      case 'Y' : return(TRUE);
  354.      case 'N' : return(FALSE);
  355.      defalt   : BELL;
  356.       }                     /* end of switch */
  357.    }                        /* end of while */
  358. }                        /* end of getyn */
  359.  
  360.