home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tcpp / examples / tcalc / tcinput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-10  |  4.2 KB  |  215 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. /* Liest mit Hilfe vom BIOS das nächste Tastaturzeichen */
  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. /* Der Benutzer kann einen String nur mit bestimmten Zeichen editieren -
  26.    wenn ESC nicht gedrückt wird, wird TRUE geliefert, andernfalls FALSE.
  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. /* Liest und bearbeitet einen Eingabestring von der Tastatur,
  109.    der mit c beginnt.
  110. */
  111. {
  112.  char s[MAXINPUT + 1];
  113.  
  114.  s[0] = c;
  115.  s[1] = 0;
  116.  if (!editstring(s, "", MAXINPUT) || (s[0] == 0))
  117.   return;
  118.  act(s);
  119.  changed = TRUE;
  120. } /* getinput */
  121.  
  122. int getint(int *number, int low, int high)
  123. /* Liest eine positive Integerzahl ein */
  124. {
  125.  int i, good = FALSE;
  126.  char s[5], message[81];
  127.  
  128.  s[0] = 0;
  129.  sprintf(message, MSGBADNUMBER, low, high);
  130.  do
  131.  {
  132.   if (!editstring(s, "1234567890", 4))
  133.    return(FALSE);
  134.   i = atoi(s);
  135.   if ((good = ((i >= low) && (i <= high))) == 0)
  136.    errormsg(message);
  137.  }
  138.  while (!good);
  139.  *number = i;
  140.  return(TRUE);
  141. } /* getint */
  142.  
  143. int getcell(int *col, int *row)
  144. /* Liest einen eingegebenen Feldnamen ein - wenn ESC gedrückt wird, wird */
  145. /* FALSE geliefert */
  146. {
  147.  int first = TRUE, good = FALSE, len, numlen = rowwidth(MAXROWS),
  148.      oldcol = *col, oldrow = *row;
  149.  char data[10], *input, *start, numstring[6];
  150.  
  151.  data[0] = 0;
  152.  do
  153.  {
  154.   if (!first)
  155.    errormsg(MSGBADCELL);
  156.   first = FALSE;
  157.   input = data;
  158.   if (!editstring(data, "", numlen + 2))
  159.   {
  160.    *col = oldcol;
  161.    *row = oldrow;
  162.    return(FALSE);
  163.   }
  164.   *col = toupper(*(input++)) - 'A';
  165.   if (isalpha(*input))
  166.   {
  167.    *col *= 26;
  168.    *col += toupper(*(input++)) - 'A' + 26;
  169.   }
  170.   if (*col >= MAXCOLS)
  171.    continue;
  172.   start = input;
  173.   for (len = 0; len < numlen; len++)
  174.   {
  175.    if (!isdigit(*(input++)))
  176.    {
  177.     input--;
  178.     break;
  179.    }
  180.   }
  181.   if (len == 0)
  182.    continue;
  183.   strncpy(numstring, start, len);
  184.   numstring[len] = 0;
  185.   *row = atoi(numstring) - 1;
  186.   if ((*row >= MAXROWS) || (*row == -1) || (*input != 0))
  187.    continue;
  188.   good = TRUE;
  189.  }
  190.  while (!good);
  191.  return(TRUE);
  192. } /* getcell */
  193.  
  194. int getyesno(int *yesno, char *prompt)
  195. /* Gibt einen Prompt für eine Ja/Nein-Abfrage aus - liefert TRUE, wenn ESC
  196.    gedrückt wurde, anderenfalls FALSE.
  197. */
  198. {
  199.  writeprompt(prompt);
  200.  setcursor(shortcursor);
  201.  do
  202.  {
  203.   *yesno = toupper(getkey());
  204.   if (*yesno == ESC)
  205.   {
  206.    setcursor(nocursor);
  207.    return(FALSE);
  208.   }
  209.  }
  210.  while (strchr("YN", *yesno) == NULL);
  211.  setcursor(nocursor);
  212.  return(TRUE);
  213. } /* getyesno */
  214.  
  215.