home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turbo Toolbox
/
Turbo_Toolbox.iso
/
1990
/
12
/
tricks
/
instr.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-05
|
4KB
|
164 lines
/************** INTSTR.C **********************************
* Autor: (c) 1990 by Vincenso Iuorno & TOOLBOX *
* Compiler: Turbo C ab Version 2.0 *
* EINGABE: Spalte, Zeile, Pointer auf String, Anz. Zeich.*
* AUSGABE: keine *
* Funktion: INSTR erlaubt eine komfortable Stringeingabe.*
* Cursor sowie DEL, INS, HOME, BS, END und *
* ESCAPE-Taste werden interpretiert. *
**********************************************************/
#include <stdio.h>
#include <bios.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#define BLANK 32
char findlc(char *str, char lc);
void intstr(char pos, char line, char *str, char l)
{
union { /* Feld für Eingabezeichen */
unsigned ic;
char c[2];
} key;
union REGS regs; /* Register für Interrupt */
char lc=0; /* Position des letzten Zeichens */
char i=0, po; /* i = Position des Cursors im String */
char ins=0; /* Flag für Test auf Insert */
char lpos; /* Position des letzten Zeichens */
po=pos;
l--; /* Länge Eingabestring -1 */
lpos=pos + l;
*str=NULL;
do { /*** main loop ***/
gotoxy(po, line); key.ic=bioskey(0);
if(!key.c[0]) {
switch(key.c[1]) {
case 83: /* delete */
if(lc > i) {
memmove(str+i, str+i+1, lc-i);
*(str+lc)=NULL;
lc--;
printf("%s ",str+i);
}
else if(lc==i) {
*(str+i)=NULL;
printf(" ");
if(lc) lc--;
}
break;
case 71: /* home */
po=pos; i=0;
break;
case 82: /* insert */
regs.h.cl = 7;
regs.h.ch = (int)(ins = !ins) ? 0 : 6;
regs.h.ah = 0x01;
int86(0x10, ®s, ®s); /* Cursor verändern */
break;
case 77: /* Cursor rechts */
if(po < lpos) {
if(lc && i <= lc) {
i++; po++;
}
else if(i > lc) {
*(str+i)=BLANK;
i++;
*(str+i)=NULL;
po++; lc++;
}
else if(!*str) {
*str=BLANK;
*(str+1)=NULL;
i=1; po++;
}
}
break;
case 75: /* Cursor links */
if(po > pos) {
po--; i--;
}
break;
case 79: /* end */
if(i < l) {
lc=findlc(str, lc);
if(lc==l) {
i=lc; po=lpos;
}
else if(*str) {
i=lc+1;
po=pos+i;
}
}
break;
}
}
else {
switch(key.c[0]) {
case 13: /* return */
lc=findlc(str, lc);
*(str+lc+1)=NULL;
break;
case 27: /* escape */
*str=27;
break;
case 8: /* backspace */
if(po > pos) {
if(i > lc) {
*(str+lc)=NULL;
printf("\b ");
}
else {
memmove(str+i-1, str+i, lc-i+1);
*(str+lc)=NULL;
printf("\b%s ", str+i-1);
}
if(lc) lc--;
po--; i--;
}
break;
default:
if(!ins) { /* Ueberschreib-Modus */
*(str+i)=key.c[0];
printf("%c", key.c[0]);
if(po < lpos) po++; /* Neue Pos. für Eingabe */
if(lc < i) lc++; /* Pos. des letzten Zeichens */
if(i < l) i++; /* Index für nächstes Zeichen */
*(str+lc+1)=NULL;
}
else { /* Insert-Modus */
lc=findlc(str, lc);
if(lc < i && i < l) lc=i-1;
if(lc < l && i < l) {
memmove(str+i+1, str+i, lc-i+1);
if (*str) lc++;
*(str+lc+1)=NULL;
*(str+i)=key.c[0];
printf("%s", str+i);
if(po < lpos) {
i++; po++;
}
}
}
}
}
} while(key.c[0] != 13 && key.c[0] != 27);
/* Eingabe beenden bei RETURN oder ESCAPE */
if(ins) { /* Cursor verändern */
regs.h.cl = 7; regs.h.ch = 6; regs.h.ah = 0x01;
int86(0x10, ®s, ®s);
}
}
char findlc(char *str, char lc)
/* Letztes eingebenes Zeichen finden */
{
for(; lc && *(str+lc) == BLANK; lc--);
if(!lc && *str == BLANK) *str=NULL;
return lc;
}
/************** INTSTR.C Ende ****************************/