Friday, October 21, 2011

SYSTEM SOFTWARE LAB-SIMULATION OF A TEXT EDITOR


                                        SIMULATION OF A TEXT EDITOR
PROGRAM CODE:-


Requirements:-
                  1.Run in Linux terminal after installing <curses.h>
                  2.Install <curses.h> by typing
                                     RHEL / Fedora / CentOS Linux
                                               # yum install ncurses-devel ncurses
                                    Debian / Ubuntu Linux
                                              $ sudo apt-get install libncurses5-dev libncursesw5-dev
                  3.compile like:-
                                   gcc -Wall -W -pedantic editorlogictest.c -o test -lncurses 


#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


struct contentmap
{
int maxx;
int maxy;
char **data;
int *lineendposition;
};


void removecharacter(struct contentmap *cmap,int y,int x);
void printscreen(struct contentmap* cmap);
void printline(struct contentmap* cmap,int y);


WINDOW* initializemodes()
{
WINDOW *win = initscr();
cbreak();
noecho();
keypad(stdscr,true);
return win;
}


struct contentmap* getcontentmap(WINDOW *win)
{
struct contentmap *c =  (struct contentmap *)malloc(sizeof(struct contentmap) * (1));
int i;
getmaxyx(win,c->maxy,c->maxx);
c->data =  (char **)malloc(sizeof(char *) * (c->maxy));
c->lineendposition =  (int *)malloc(sizeof(int) * (c->maxy));
for(i=0;i<c->maxy;i++)
{
c->data[i] =  (char *)malloc(sizeof(char) * (c->maxx));
c->data[i][0] = '\n';
c->lineendposition[i] = 0;
}
return c;
}


void insertcharacter(struct contentmap *cmap,char ch,int y,int x)
{
int i;
for(i=cmap->lineendposition[y];i>x;i--)
cmap->data[y][i] = cmap->data[y][i-1];
cmap->data[y][x] = ch;
cmap->lineendposition[y]++;
}


void removecharacter(struct contentmap *cmap,int y,int x)
{
int i;
for(i=x;i<cmap->lineendposition[y];i++)
cmap->data[y][i] = cmap->data[y][i+1];
cmap->lineendposition[y]--;
}


void insertnewline(struct contentmap *cmap,int y,int x)
{
int i;
int count;
int limit;
char *buffer=  (char *)malloc(sizeof(char) * (cmap->maxx));
if(y+1 < cmap->maxy)
{
limit = cmap->lineendposition[y] - x;
for(count=0;count<limit;count++)
{
buffer[count] = cmap->data[y][x];
removecharacter(cmap,y,x);
}
for(i=cmap->maxy-1;i>y+1;i--)
cmap->data[i][cmap->lineendposition[i]] = '\0';
for(i=cmap->maxy-1;i>y+1;i--)
strcpy(cmap->data[i],cmap->data[i-1]);
for(i=cmap->maxy-1;i>y+1;i--)
cmap->lineendposition[i] = cmap->lineendposition[i-1];
cmap->lineendposition[y+1]  = 0;
for(i=0;i<count;i++)
insertcharacter(cmap,buffer[i],y+1,i);
printscreen(cmap);
}
free(buffer);
}


void deleteline(struct contentmap *cmap,int y)
{
char *buffer =  (char *)malloc(sizeof(char) * (cmap->maxx));
int i,count;
if(y != 0)
{
if(cmap->lineendposition[y]!=0)
{
count = cmap->lineendposition[y];
for(i=0;i<count;i++)
{
buffer[i] = cmap->data[y][0];
removecharacter(cmap,y,0);
}
for(i=0;i<count;i++)
insertcharacter(cmap,buffer[i],y-1,cmap->lineendposition[y-1]);
}
for(i=y;i<cmap->maxy;i++)
cmap->data[i][cmap->lineendposition[i]] = '\0';
for(i=y;i<cmap->maxy-1;i++)
strcpy(cmap->data[i],cmap->data[i+1]);
for(i=y;i<cmap->maxy-1;i++)
cmap->lineendposition[i] = cmap->lineendposition[i+1];
printscreen(cmap);
}
free(buffer);
}


void printscreen(struct contentmap* cmap)
{
int i;
for(i=0;i<cmap->maxy-1;i++)
{
printline(cmap,i);
refresh();
}
}


void printline(struct contentmap* cmap,int y)
{
int x=0;
if(cmap->lineendposition[y] != 0)
for(x=0;x<cmap->lineendposition[y];x++)
mvaddch(y,x,cmap->data[y][x]);
for(;x<cmap->maxx;x++)
mvaddch(y,x,' ');
}


int main (int argc, char *argv[])
{
WINDOW *win = initializemodes();
struct contentmap *cmap = getcontentmap(win);
int ch=0;
int x = 0,y = 0;
int exit_flag = 0;
int temp;
move(y,x);
while(exit_flag == 0)
{
ch = getch();
getyx(stdscr,y,x);
switch(ch)
{
case KEY_LEFT:
if(x!=0)
move(y,x-1);
break;
case KEY_RIGHT:
if(x!=cmap->lineendposition[y])
move(y,x+1);
break;
case KEY_DOWN:
if(y!= cmap->maxy)
move(y+1,x);
break;
case KEY_UP:
if(y != 0)
move(y-1,x);
break;
case KEY_BACKSPACE:
if(x != 0)
{
removecharacter(cmap,y,x-1);
printline(cmap,y);
move(y,x-1);
}
else
{
temp = cmap->lineendposition[y-1];
deleteline(cmap,y);
move(y-1,temp);
}
break;
case KEY_DC:
if(x != cmap->lineendposition[y])
{
removecharacter(cmap,y,x);
printline(cmap,y);
}
else
deleteline(cmap,y+1);
move(y,x);
break;
case KEY_END:
move(y,cmap->lineendposition[y]);
break;
case KEY_HOME:
move(y,0);
break;
case 10: // ENTER KEY
insertnewline(cmap,y,x);
move(y+1,0);
break;
case 27: // ESCAPE KEY
move(cmap->maxy-5,0); echo();
                                printw("enter the file name :");
                                char file[10],c; int fn=0;
                                while((c=getch())!='\n')
                                {   file[fn]=c; fn++; }
                                file[fn]='\0';
                                FILE *f;
                                f=fopen(file,"w");
                                if(f!=NULL)
                                    for(y=0;y<cmap->maxy;y++)
                                     { for(x=0;x<cmap->lineendposition[y];x++)
                                         fprintf(f,"%c",cmap->data[y][x]);
                                       fprintf(f,"\n");
                                     }
                                fclose(f); noecho();
                                exit_flag=1;
break;
default:
if(ch >=0 && ch<=255)
insertcharacter(cmap,ch,y,x);
printline(cmap,y);
move(y,x+1);
break;
}
refresh();
}
endwin();
return 0;
}

No comments:

Post a Comment