Monday, September 14, 2020

Line Editor

 Assignment 1 : Line Editor 

Software Description – Editors are used to create digital copies of source program. The main functions supported by an editor is editing, viewing and navigating through the text. A line editor limits all operations to a line of text. The line is indicated positionally by giving line number i.e its serial number in the text or contextually by specifying a context which uniquely identifies the position.

 The file to be edited is taken as command line argument. An empty file is opened for editing if no argument is supplied. 

The editor has two modes In command mode it displays ‘?’ as prompt and accepts single-line commands. If ‘i’ for insert or ‘a’ for append command is given, it goes into input mode and accepts lines as text. When a line containing a single ‘.’ is given it goes back to command mode.

 The program at the start displays ‘lines :’ followed by number of lines(0 if file is empty or not specified) and goes into command mode.

 The Command format is a single character indicating the action followed by three optional integers separated by spaces. 

The character and intended actions are given in the table 1. The second parameter n1 and the third parameter n2 specify the range of lines and the command is valid if 1 <= n1 <= n2 <= total lines in the file being edited. The default value for n1 is 1 and the default value for n2 is n1. For example a command ‘ p 3 4 ‘ will print lines starting from line no 3 to line no 4, a command ‘ p 3’ will print line 3 just a ‘ p’ command will print the first line. If n1 or n2 is greater than the total lines in the file then n1 or n2 is set to total lines in the file so that command ‘p 1 1000’ will print the file till the end if total lines in file are < 1000.

p   ->  print or display 

i   -> Insert a Append 

d   -> Delete

m   -> Move

c   ->   Copy 

f   ->  Find 

s  ->  Save

 h  ->  Help 

q  ->  Quit 

The second parameter and fourth parameter depending on the command, indicate the position of action. For example the command ‘i 5’ indicates that the lines entered are to be inserted from the 5th position that is fifth line onwards while the command ‘m 2 4 5’ indicate that the lines ranging from 2 to 4 should be moved to the 5th position.


Slot 1

 i) Answer the following questions after carefully reading the description and program structure. 

a) How main function is declared in case of command line program? 

 Answer: -

       main( int argc, char *argv[])

        if(argc >1) { readbuff(head, argv[1]); 

        printf( “ Lines : %d”, lines(head)); 

 b) What are the two modes of the editor? 

 Answer: - 

1) Command Mode  2) Input Mode 

c) What data structure is appropriate for the edit buffer? Why? 

 Answer: - 

        Linked list of lines is the appropriate data structure for edit buffer that hold the lines to be edited, as lines are to be inserted, deleted, moved or copied. A singly linked list with a dummy header node can be used so that insertion deletion becomes easy

d) What command will print all lines in the file? 

 Answer: - 

    To print all lines the command is :- P or p 

e) What command will print nth line? What will print last line? 

 Answer: - 

    To print the nth line the command is :- P line no or p line no 

f) What will command ‘d 5’ will do? What effect ‘d’ command will have? 

 Answer: - 

        The command d 5  will delete the 5th line from the file . The command d will delete the lines from file.

ii) Partially implement a command line program for a line editor. Implement the following functionalities 

a) The program accepts the filename and prints the number of lines in the file and prompts for the command 

 b) Implement the print command

 c) Implement the insert command 

d) Implement the save command


Program :-

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

typedef struct node

{ char line[80];

struct node *next;

}NODE;

NODE *first,*last;

int len,changed;

NODE * get_node(char *s)

{ NODE *p;

p=(NODE*)malloc(sizeof(NODE));

strcpy(p->line,s);

p->next=NULL;

return p;

}

void create(char fname[])

{ NODE *p;

FILE *fp;

char buff[80];

fp = fopen(fname,"r");

if(fp==NULL)

{ printf("File %s not found.\n",fname);

exit(1);

}

while(fgets(buff,80,fp)!=NULL)

{ p = get_node(buff);

if(first==NULL)

first = p;

else

last->next = p;

last = p;

len++;

}

fclose(fp);

}

void append()

{ NODE *p;

char buff[80];

printf("Enter text (type END to stop):");

fflush(stdin);

fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)

{ p = get_node(buff);

if(first==NULL)

first = p;

else

last->next = p;

last=p;

len++;


fflush(stdin);

fgets(buff,80,stdin);

}

changed=1;

}

void print()

{ NODE *p;

int i=1;

p=first;

while(p!=NULL)

{ printf("%d:%s",i,p->line);

i++;

p=p->next;

}

}

void save(char fname[])

{ FILE *fp;

NODE *p;

fp = fopen(fname,"w");

p = first;

while(p!=NULL)

{ fputs(p->line,fp);

p=p->next;

}

fclose(fp);

changed=0;

}

int main(int argc, char *argv[])

{ char buff[80],ch,fname[30]="";

if(argc==2)

{      strcpy(fname,argv[1]);

create(fname);

}

while(1)

{ printf("$");

fflush(stdin);

fgets(buff,80,stdin);

sscanf(buff,"%c",&ch);

switch(ch)

{ case 'a':

append();

break;

case 'p':

print();

break;

case 's':

if(changed==1)

{ if(strlen(fname)==0)

{

printf("Enter source file name:");

fflush(stdin);

fgets(fname,30,stdin);

fname[strlen(fname)-1]='\0';

}

save(fname);

}

break;

case 'e':

if(changed==1)

{ printf("Quit without save (Y/N)?");

fflush(stdin);

fgets(buff,80,stdin);

if(buff[0]=='N')

{ if(strlen(fname)==0)

{ printf("Enter file name:");

fflush(stdin);

fgets(fname,30,stdin);

fname[strlen(fname)-1]='\0';

}

save(fname);

}

}

exit(0);

default:

printf("Invalid command.\n");

}

}

return 0;

}

                                                                                                                               

OUTPUT:- 

$a

Enter text (type END to stop):This is Line editor Program 

END

$p

1:This is Line editor Program 

$s

Enter source file name:abc.txt

$e

------------------------------------------------------------------------------------------------------

abc.txt

This is Line editor Program 

------------------------------------------------------------------------------------------------------

Slot 2 

i) Extend the line editor 

a) Implement the delete command 

b) Implement the move command 

c) Implement the copy command 

d) Implement the find command

Program :- 

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

typedef struct node

{ char line[80];

struct node *next;

}NODE;

NODE *first,*last;

int len,changed;

NODE * get_node(char *s)

{ NODE *p;

p=(NODE*)malloc(sizeof(NODE));

strcpy(p->line,s);

p->next=NULL;

return p;

}

void create(char fname[])

{ NODE *p;

FILE *fp;

char buff[80];

fp = fopen(fname,"r");

if(fp==NULL)

{ printf("File %s not found.\n",fname);

exit(1);

}

while(fgets(buff,80,fp)!=NULL)

{ p = get_node(buff);

    if(first==NULL)

first = p;

else

last->next = p;

last = p;

len++;

}

fclose(fp);

}

void append()

{ NODE *p;

char buff[80];

printf("Enter text (type END to stop):");

fflush(stdin);

fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)

{ p = get_node(buff);

if(first==NULL)

first = p;

else

last->next = p;

last=p;

len++;

fflush(stdin);

fgets(buff,80,stdin);

}

changed=1;

}

void copy(int n1, int n2)

{ NODE *p,*q,*t;

int i;

i=1;

p=first;

while(i<n1)

{

i++;

p=p->next;

}

t = get_node(p->line);

i=1;

p=q=first;

while(i<n2)

{ i++;

q=p;

p=p->next;

}

if(p==first)

{ t->next=first;

first=t;

}

else

{ q->next=t;

t->next=p;

}

len++;

changed=1;

}

void del(int pos)

{ NODE *p,*q;

int i=1;

p = q = first;

while(i<pos)

{ q = p;

p = p->next;

i++;

}

if(p==first)

{ first = p->next;

}

else if(p==last)

{ q->next=NULL;

last=q;

}

else

{ q->next=p->next;

}

free(p);

len--;

changed=1;

}

void del1(int pos)

{ NODE *p,*q;

int i=1;

p = q = first;

while(i<pos)

{ q = p;

p = p->next;

i++;

}

if(p==first)

{ first = p->next;

}

else if(p==last)

{ q->next=NULL;

last=q;

}

else

{ q->next=p->next;

}

free(p);

len--;

changed=1;

}

void delet(int n1, int n2)

{ int n,i;

n=n2-n1+1;

for(i=1;i<=n;i++)

del1(n1);

}

void print()

{ NODE *p;

int i=1;

p=first;

while(p!=NULL)

{ printf("%d:%s",i,p->line);

i++;

p=p->next;

}

}

void save(char fname[])

{ FILE *fp;

NODE *p;

fp = fopen(fname,"w");

p = first;

while(p!=NULL)

{ fputs(p->line,fp);

p=p->next;

}

fclose(fp);

changed=0;

}

int main(int argc, char *argv[])

{ char buff[80],ch,fname[30]="";

int n1,n2;

if(argc==2)

{ strcpy(fname,argv[1]);

create(fname);

}

while(1)

{ printf("$");

fflush(stdin);

fgets(buff,80,stdin);

sscanf(buff,"%c %d %d",&ch,&n1,&n2);

switch(ch)

{ case 'a':

append();

break;

case 'p':

print();

break;

case 'd':

if(n1>=1 && n2<=len && n1<=n2)

delet(n1,n2);

else

printf("Invalid parameter\n");

break;

case 'm':

if(n1>=1 && n1<=len && n2>=1 && n2<=len)

{ copy(n1,n2);

if(n1<=n2)

del1(n1);

else

del1(n1+1);

}

else

printf("Invalid parameter\n");

break;

case 's':

if(changed==1)

{ if(strlen(fname)==0)

{

printf("Enter source file name:");

fflush(stdin);

fgets(fname,30,stdin);

fname[strlen(fname)-1]='\0';

}

save(fname);

}

break;

case 'e':

if(changed==1)

{ printf("Quit without save (Y/N)?");

fflush(stdin);

fgets(buff,80,stdin);

if(buff[0]=='N')

{

if(strlen(fname)==0)

{

printf("Enter file name:");

fflush(stdin);

fgets(fname,30,stdin);

fname[strlen(fname)-1]='\0';

}

save(fname);

}

}

exit(0);

default:

printf("Invalid command.\n");

}

}

return 0;

}

----------------------------------------------------------------------------------------------

OUTPUT :- 

$a

Enter text (type END to stop):This is First line

This is second line

This is third Line

Thank You 

END

$p

1:This is First line

2:This is second line

3:This is third Line

4:Thank You 

$m

Invalid parameter

$m 1 2

$p

1:This is First line

2:This is second line

3:This is third Line

4:Thank You 

$m 1 3 

$p

1:This is second line

2:This is First line

3:This is third Line

4:Thank You 

$d 1

Invalid parameter

$d 1 2 

$p

1:This is third Line

2:Thank You 

$s

Enter source file name: xyz.txt

$e

 -----------------------------------------------------------------------------

XYZ.txt

This is third Line

Thank You 

--------------------------------------------------------------------------------





1 comment: