home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP10-8.ZIP / TCALC.ZIP / TCINPUT.C < prev    next >
Text File  |  1990-09-26  |  4KB  |  211 lines

  1. /* Turbo C++ - (C) Copyright 1987,1988,1990 by Borland International */
  2.  
  3. #include <string.h>
  4. #include <mem.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <bios.h>
  9. #include <conio.h>
  10. #include <string.h>
  11. #include "tcalc.h"
  12.  
  13. int getkey(void)
  14. /* Uses the BIOS to read the next keyboard character */
  15. {
  16.  int key, lo, hi;
  17.  
  18.  key = bioskey(0);
  19.  lo = key & 0X00FF;
  20.  hi = (key & 0XFF00) >> 8;
  21.  return((lo == 0) ? hi + 256 : lo);
  22. } /* getkey */
  23.  
  24. int editstring(char *s, char *legal, int maxlength)
  25. /* Allows the user to edit a string with only certain characters allowed -
  26.    Returns TRUE if ESC was not pressed, FALSE is ESC was pressed.
  27. */
  28. {
  29.  int c, len = strlen(s), pos = len, insert = TRUE;
  30.  
  31.  changecursor(insert);
  32.  do
  33.  {
  34.   writef(1, 25, WHITE, 79, "%s", s);
  35.   gotoxy(pos + 1, 25);
  36.   switch(c = getkey())
  37.   {
  38.    case HOMEKEY :
  39.     pos = 0;
  40.     break;
  41.    case ENDKEY :
  42.     pos = len;
  43.     break;
  44.    case INSKEY :
  45.     insert = !insert;
  46.     changecursor(insert);
  47.     break;
  48.    case LEFTKEY :
  49.     if (pos > 0)
  50.      pos--;
  51.     break;
  52.    case RIGHTKEY :
  53.     if (pos < len)
  54.      pos++;
  55.     break;
  56.    case BS :
  57.     if (pos > 0)
  58.     {
  59.      movmem(&s[pos], &s[pos - 1], len - pos + 1);
  60.      pos--;
  61.      len--;
  62.     }
  63.     break;
  64.    case DELKEY :
  65.     if (pos < len)
  66.     {
  67.      movmem(&s[pos + 1], &s[pos], len - pos);
  68.      len--;
  69.     }
  70.     break;
  71.    case CR :
  72.     break;
  73.    case UPKEY :
  74.     c = CR;
  75.     break;
  76.    case DOWNKEY :
  77.     c = CR;
  78.     break;
  79.    case ESC :
  80.     len = 0;
  81.     break;
  82.    default :
  83.     if (((legal[0] == 0) || (strchr(legal, c) != NULL)) &&
  84.         ((c >= ' ') && (c <= '~')) &&
  85.         (len < maxlength))
  86.     {
  87.      if (insert)
  88.      {
  89.       memmove(&s[pos + 1], &s[pos], len - pos + 1);
  90.       len++;
  91.      }
  92.      else if (pos >= len)
  93.       len++;
  94.      s[pos++] = c;
  95.     }
  96.     break;
  97.   } /* switch */
  98.   s[len] = 0;
  99.  }
  100.  while ((c != CR) && (c != ESC));
  101.  clearinput();
  102.  changecursor(FALSE);
  103.  setcursor(nocursor);
  104.  return(c != ESC);
  105. } /* editstring */
  106.  
  107. void getinput(int c)
  108. /* Reads and acts on an input string from the keyboard that started with c. */
  109. {
  110.  char s[MAXINPUT + 1];
  111.  
  112.  s[0] = c;
  113.  s[1] = 0;
  114.  if (!editstring(s, "", MAXINPUT) || (s[0] == 0))
  115.   return;
  116.  act(s);
  117.  changed = TRUE;
  118. } /* getinput */
  119.  
  120. int getint(int *number, int low, int high)
  121. /* Reads in a positive integer from low to high */
  122. {
  123.  int i, good = FALSE;
  124.  char s[5], message[81];
  125.  
  126.  s[0] = 0;
  127.  sprintf(message, MSGBADNUMBER, low, high);
  128.  do
  129.  {
  130.   if (!editstring(s, "1234567890", 4))
  131.    return(FALSE);
  132.   i = atoi(s);
  133.   if ((good = ((i >= low) && (i <= high))) == 0)
  134.    errormsg(message);
  135.  }
  136.  while (!good);
  137.  *number = i;
  138.  return(TRUE);
  139. } /* getint */
  140.  
  141. int getcell(int *col, int *row)
  142. /* Reads in a cell name that was typed in - Returns FALSE if ESC was pressed */
  143. {
  144.  int first = TRUE, good = FALSE, len, numlen = rowwidth(MAXROWS),
  145.      oldcol = *col, oldrow = *row;
  146.  char data[10], *input, *start, numstring[6];
  147.  
  148.  data[0] = 0;
  149.  do
  150.  {
  151.   if (!first)
  152.    errormsg(MSGBADCELL);
  153.   first = FALSE;
  154.   input = data;
  155.   if (!editstring(data, "", numlen + 2))
  156.   {
  157.    *col = oldcol;
  158.    *row = oldrow;
  159.    return(FALSE);
  160.   }
  161.   *col = toupper(*(input++)) - 'A';
  162.   if (isalpha(*input))
  163.   {
  164.    *col *= 26;
  165.    *col += toupper(*(input++)) - 'A' + 26;
  166.   }
  167.   if (*col >= MAXCOLS)
  168.    continue;
  169.   start = input;
  170.   for (len = 0; len < numlen; len++)
  171.   {
  172.    if (!isdigit(*(input++)))
  173.    {
  174.     input--;
  175.     break;
  176.    }
  177.   }
  178.   if (len == 0)
  179.    continue;
  180.   strncpy(numstring, start, len);
  181.   numstring[len] = 0;
  182.   *row = atoi(numstring) - 1;
  183.   if ((*row >= MAXROWS) || (*row == -1) || (*input != 0))
  184.    continue;
  185.   good = TRUE;
  186.  }
  187.  while (!good);
  188.  return(TRUE);
  189. } /* getcell */
  190. int getyesno(int *yesno, char *prompt)
  191. /* Prints a prompt and gets a yes or no answer - returns TRUE if ESC was
  192.    pressed, FALSE if not.
  193. */
  194. {
  195.  writeprompt(prompt);
  196.  setcursor(shortcursor);
  197.  do
  198.  {
  199.   *yesno = toupper(getkey());
  200.   if (*yesno == ESC)
  201.   {
  202.    setcursor(nocursor);
  203.    return(FALSE);
  204.   }
  205.  }
  206.  while (strchr("YN", *yesno) == NULL);
  207.  setcursor(nocursor);
  208.  return(TRUE);
  209. } /* getyesno */
  210.  
  211.