home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1990 / 12 / tricks / instr.c < prev    next >
C/C++ Source or Header  |  1990-09-05  |  4KB  |  164 lines

  1. /************** INTSTR.C **********************************
  2. *  Autor: (c) 1990 by Vincenso Iuorno & TOOLBOX           *
  3. *  Compiler: Turbo C ab Version 2.0              *
  4. *  EINGABE: Spalte, Zeile, Pointer auf String, Anz. Zeich.*
  5. *  AUSGABE: keine                                         *
  6. *  Funktion: INSTR erlaubt eine komfortable Stringeingabe.*
  7. *            Cursor sowie DEL, INS, HOME, BS, END und     *
  8. *            ESCAPE-Taste werden interpretiert.           *
  9. **********************************************************/
  10. #include <stdio.h>
  11. #include <bios.h>
  12. #include <string.h>
  13. #include <conio.h>
  14. #include <dos.h>
  15.  
  16. #define BLANK 32
  17.  
  18. char findlc(char *str, char lc);
  19.  
  20. void intstr(char pos, char line, char *str, char l)
  21. {
  22.   union {      /* Feld für Eingabezeichen */
  23.     unsigned ic;
  24.     char c[2];
  25.   } key;
  26.   union REGS regs;  /* Register für Interrupt */
  27.   char lc=0;        /* Position des letzten Zeichens */
  28.   char i=0, po;     /* i = Position des Cursors im String */
  29.   char ins=0;       /* Flag für Test auf Insert */
  30.   char lpos;        /* Position des letzten Zeichens */
  31.  
  32.   po=pos;
  33.   l--;              /* Länge Eingabestring -1 */
  34.   lpos=pos + l;
  35.   *str=NULL;
  36.  
  37.   do {      /*** main loop ***/
  38.     gotoxy(po, line); key.ic=bioskey(0);
  39.     if(!key.c[0]) {
  40.       switch(key.c[1]) {
  41.     case 83:                 /* delete */
  42.       if(lc > i) {
  43.         memmove(str+i, str+i+1, lc-i);
  44.         *(str+lc)=NULL;
  45.         lc--;
  46.         printf("%s ",str+i);
  47.       }
  48.       else if(lc==i) {
  49.         *(str+i)=NULL;
  50.         printf(" ");
  51.         if(lc) lc--;
  52.       }
  53.       break;
  54.     case 71:                 /* home */
  55.       po=pos; i=0;
  56.       break;
  57.     case 82:                 /* insert */
  58.       regs.h.cl = 7;
  59.       regs.h.ch = (int)(ins = !ins) ? 0 : 6;
  60.       regs.h.ah = 0x01;
  61.       int86(0x10, ®s, ®s); /* Cursor verändern */
  62.       break;
  63.     case 77:                 /* Cursor rechts */
  64.       if(po < lpos) {
  65.         if(lc && i <= lc) {
  66.           i++; po++;
  67.         }
  68.         else if(i > lc) {
  69.           *(str+i)=BLANK;
  70.           i++;
  71.           *(str+i)=NULL;
  72.           po++; lc++;
  73.         }
  74.         else if(!*str) {
  75.           *str=BLANK;
  76.           *(str+1)=NULL;
  77.           i=1; po++;
  78.         }
  79.       }
  80.       break;
  81.     case 75:                 /* Cursor links */
  82.       if(po > pos) {
  83.         po--; i--;
  84.       }
  85.       break;
  86.     case 79:                 /* end */
  87.       if(i < l) {
  88.         lc=findlc(str, lc);
  89.         if(lc==l) {
  90.           i=lc; po=lpos;
  91.         }
  92.         else if(*str) {
  93.           i=lc+1;
  94.           po=pos+i;
  95.         }
  96.       }
  97.       break;
  98.       }
  99.     }
  100.     else {
  101.       switch(key.c[0]) {
  102.     case 13:               /* return */
  103.       lc=findlc(str, lc);
  104.       *(str+lc+1)=NULL;
  105.       break;
  106.     case 27:               /* escape */
  107.       *str=27;
  108.       break;
  109.     case 8:                /* backspace */
  110.       if(po > pos) {
  111.         if(i > lc) {
  112.           *(str+lc)=NULL;
  113.           printf("\b ");
  114.         }
  115.         else {
  116.           memmove(str+i-1, str+i, lc-i+1);
  117.           *(str+lc)=NULL;
  118.           printf("\b%s ", str+i-1);
  119.         }
  120.         if(lc) lc--;
  121.         po--; i--;
  122.       }
  123.       break;
  124.     default:
  125.       if(!ins) {      /* Ueberschreib-Modus */
  126.         *(str+i)=key.c[0];
  127.         printf("%c", key.c[0]);
  128.         if(po < lpos) po++;  /* Neue Pos. für Eingabe */
  129.         if(lc < i) lc++; /* Pos. des letzten Zeichens */
  130.         if(i < l) i++;  /* Index für nächstes Zeichen */
  131.         *(str+lc+1)=NULL;
  132.       }
  133.       else {            /* Insert-Modus */
  134.         lc=findlc(str, lc);
  135.         if(lc < i && i < l) lc=i-1;
  136.         if(lc < l && i < l) {
  137.           memmove(str+i+1, str+i, lc-i+1);
  138.           if (*str) lc++;
  139.           *(str+lc+1)=NULL;
  140.           *(str+i)=key.c[0];
  141.           printf("%s", str+i);
  142.           if(po < lpos) {
  143.         i++; po++;
  144.           }
  145.         }
  146.       }
  147.     }
  148.       }
  149.     } while(key.c[0] != 13 && key.c[0] != 27);
  150.            /* Eingabe beenden bei RETURN oder ESCAPE */
  151.     if(ins) {  /* Cursor verändern */
  152.       regs.h.cl = 7; regs.h.ch = 6; regs.h.ah = 0x01;
  153.       int86(0x10, ®s, ®s);
  154.     }
  155. }
  156.  
  157. char findlc(char *str, char lc)
  158.             /* Letztes eingebenes Zeichen finden */
  159. {
  160.    for(; lc && *(str+lc) == BLANK; lc--);
  161.    if(!lc && *str == BLANK) *str=NULL;
  162.    return lc;
  163. }
  164. /************** INTSTR.C Ende ****************************/