home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tc20 / mcinput.c < prev    next >
Text File  |  1988-10-13  |  4KB  |  219 lines

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