STREDIT.C  ·  C  ·  2.7 KB  ·  1990-04-18  ·  from PCToday_Vol-1_June-1990
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <mem.h>


#define PADCHAR '_'

/* Ascii codes */

#define AC_NUL '\0'
#define AC_DEL 8
#define AC_RET '\r'

/* Scan codes */

#define SC_LCUR 75
#define SC_RCUR 77
#define SC_HOME 71
#define SC_END  79
#define SC_INS  82

/* Cursor shapes */

#define INVISIBLE 0
#define UNDERLINE 1
#define BLOCK     2


int get_video_status(void)
{
union REGS regs;

	regs.x.ax=0x0F00;
	int86(0x10, &regs, &regs);
	return regs.h.al;
}

void curshape(int shape)
/* shape=  0=0ff, 1=underline, 2=full cell */
{
static int start_end[2][3]={
								0x2000,0x0607,0x0007,
								0x2000,0x0c0d,0x010d
								};
/* Has to be 010d and not 000d
   because of weird bug on
   Amstrad PC1640 */

union REGS regs;
int ad;

	if( (shape>2) || (shape<0) )
		return;
	ad=(get_video_status() == 7) ? 1 : 0;
	regs.x.ax=0x0100;
	regs.x.cx=start_end[ad][shape];
	int86(0x10, &regs, &regs);
}


void stredit(int x, int y, char *str, int len)
{
int c, ip, cl, insmode;

	insmode=1;
	curshape(INVISIBLE);
	ip=cl=strlen(str);
	gotoxy(x, y);
	printf(str);
	for( c=cl; c<len; c++)
		putchar(PADCHAR);
	gotoxy(x+ip, y);
	curshape(BLOCK);

	while( (c=getch()) != AC_RET )
		{
		curshape(INVISIBLE);
		switch(c)
			{
			case AC_DEL:
				if( ! ip )
					break;
				gotoxy(x+cl-1, y);
				putchar(PADCHAR);
				memmove(str+ip-1, str+ip, cl-ip+1);
				ip--;
				cl--;
				break;

			case AC_NUL:
				c=getch(); /* Get scan code */
				switch(c)
					{
					case SC_HOME:
						ip=0;
						break;

					case SC_END:
						ip=cl;
						break;

					case SC_LCUR:
						if( ip )
							ip--;
						break;

					case SC_RCUR:
						if( ip < cl )
							ip++;
						break;

					case SC_INS:
						insmode ^= 1;
					} /* Bottom of inner SWITCH */
				break;

			default:
				if( cl == len )
					break;
				if( insmode )
					memmove(str+ip+1, str+ip, cl-ip+1);
				str[ip]=c;
				ip++;
				if( insmode || ip>cl )
					cl++;
				str[cl]=AC_NUL;
			} /* Bottom of outer SWITCH */

		gotoxy(x, y);
		printf(str);
		gotoxy(x+ip, y);
		curshape(insmode+1);
		} /* Bottom of WHILE */
}


main() /* Check it out */
{
char *teststring[11]; /* room for 10 chars + '\0' */

	teststring[0]=AC_NUL; /* Must be a legal string */
	clrscr();
	gotoxy(10, 8);
	printf("Type a 10-character string :");
	stredit(38, 8, teststring, 10);
	gotoxy(10, 9);
	printf("You entered %s", teststring);
	gotoxy(10, 10);
	printf("Edit it again :");
	stredit(25, 10, teststring, 10);
	gotoxy(10, 11);
	printf("Now it was %s", teststring);
	puts("\nPress any key");
	getchar();
}