Monday, December 28, 2009

Line editor

Simple line editor print characters and manipulate using arrows also home and end


#include
#include
#include
#include
#define size 20
#define ENTER 13
#define ESC 27
#define RIGHT 77
#define LEFT 75
#define HOME 71
#define END 79

void main (void)
{
int flag=1;
char *pcur;
char *plast;
char ch;
int first=1,last=1,curr=1; //screen

char line[size];
textattr(0x79);
pcur=line;
plast=line;
do
{
gotoxy(curr,1);
ch=getch();
switch(ch)
{
case NULL:
ch=getch();
switch(ch)
{
case RIGHT: //right ..check for last then increment
if(curr!=last)
{
curr++;
pcur++;
}
break;

case LEFT: // left check for first then decrement
if(curr!=first)
{
curr--;
pcur--;
}
break;

case HOME: //home cur = first
curr=first;
pcur=line;
break;

case END: // end cur = last
curr=last;
pcur=plast;
break;
}

break;

case ENTER:
*plast='\0';
flag=0;
break;

case ESC:
*line='\0';
flag=0;
break;

default:
if(curr == last)
{
if(isprint(ch)&&last {
*pcur=ch;
plast++;
pcur++;
cprintf("%c",ch);
curr++;
last++;

}
}
else
{
*pcur=ch;
pcur++;
cprintf("%c",ch);
curr++;
}
//check if current == last
// check is printable
// if printable ( put char in array ,
// increment plast pcurrent
//cprintf for char
// increment current last

// current != last
// *cur = ch
// pcur ++; // memory
// cprintf("%c",ch) // screen
//cur ++ //for screen
break;

}

}while(flag!=0);
textattr(0x3f);
clrscr();
puts(line);
getch();
}

No comments:

Post a Comment