Line Editor




MASTER PROGRAM FOR LINE EDITOR

          Write a command line program for line editor. The file to be edited is   taken as command line argument.  It should display '$' prompt to accept the
          line editing commands. Implement the following commands.
i.             a                           - to append
          ii.       d n                        - to delete nth line
          iii.      d m n                    - to delete range of lines
          iv.      f <pat>              - to search pattern
          v.       p                           - to print all lines
          vi.      p n                        - to print nth line
          vii.     p m n                    - to print m to nth line
          viii.    s                           - to save file
          ix.      q                           - to quit
          x.       m m n                             - to move mth line at nth position
          xi.      m m n j                 - to move mth to nth line at jth position
          xii.     c m n                    - to copy m line at nth position
          xiii.    c m n j                  - to copy mth to nth line at jth position

========================================================================
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
          char data[80];
          struct node *next;
}NODE;

NODE *head,*newnode,*t;
int changed=0;
char fname[100];

void display(int s,int e)
{
          int i;
          for(i=1,t=head;i<s;i++,t=t->next);
          for(;i<=e;i++,t=t->next)
          {
                   //puts(t->data);
                   printf("%d : %s",i,t->data);       
          }

}
NODE * getnode(char *s)
{
          NODE *temp;
          temp=(NODE *)malloc(sizeof(NODE));
          strcpy(temp->data,s);
          temp->next=NULL;
          return(temp);
}
void append()
{
          char line[100];
          printf("Enter Text(. to Quit)\n");
          fflush(stdin);
          gets(line);
          for(t=head;t->next!=NULL;t=t->next);
          while(strcmp(line,".")!=0)
          {
                   newnode=getnode(strcat(line,"\n"));
                   t->next=newnode;
                   t=newnode;
                   fflush(stdin);
                   gets(line);
                   changed=1;

          }
          printf("Lines Added\n");
}
void save()
{
          FILE *fp1;
          fp1=fopen(fname,"w");
          if(!fp1)
          {
                   printf("File not saved");
                   exit(0);
          }
          else
          {
                   for(t=head;t!=NULL;t=t->next)
                   {
                             fputs(t->data,fp1);
                   }

          }
          printf("File Saved Successfully\n");
          exit(0);
}
void delete(int s,int e)
{
          int i;
          NODE *del;
          if(s==1 && e==1)
          {                                                                
                   del=head;
                   head=head->next;
                   free(del);
          }
          else if(s==1 && e>1)
          {
                   for(i=s;i<=e;i++)
                   {
                             del=head;
                             head=head->next;
                             free(del);
                   }
          }
          else if(s==e && s!=1)
          {
                   for(i=1,t=head;i<s-1;i++,t=t->next);
                   del=t->next;
                   t->next=del->next;
                   free(del);
          }
          else
          {
                   for(i=1,t=head;i<s-1;i++,t=t->next);
                   for(;i<e;i++)
                   {
                             del=t->next;
                             t->next=del->next;
                             free(del);

                   }
          }
}
void insert(int s, int e)
{
          char line[100];
          int cnt=0,i,j;
          for(t=head;t!=NULL;t=t->next,cnt++);
          /*      printf("Enter Text\n");
                   fflush(stdin);
                   gets(line);
           */
          if(s==1 && s==e)
          {

                   printf("Enter Text\n");
                   fflush(stdin);
                   gets(line);
                   newnode=getnode(strcat(line,"\n"));
                   newnode->next=head;
                   head=newnode;
          }
          else if(s==cnt && s==e)
          {
                   printf("Enter Text\n");
                   fflush(stdin);
                   gets(line);
                   newnode=getnode(strcat(line,"\n"));
                   for(t=head;t->next!=NULL;t=t->next);
                   t->next=newnode;
          }
          else if(s!=1 && s!=cnt && s==e)
          {
                   printf("Enter Text\n");
                   fflush(stdin);
                   gets(line);
                   for(t=head,i=1 ;i<s-1;t=t->next,i++);
                   newnode=getnode(strcat(line,"\n"));
                   newnode->next=t->next;
                   t->next=newnode;
          }
          else if(s!=e)
          {
                   for(t=head,j=1;j<e-1;t=t->next,j++);
                   printf("In Multiline");

                   for(i=1;i<=s;i++)
                   {
                             printf("Enter Text\n");
                             fflush(stdin);
                             gets(line);
                             newnode=getnode(strcat(line,"\n"));
                             newnode->next=t->next;  
                             t->next=newnode;
                             t=newnode;
                             //t=t->next;
                             changed=1;

                   }
          }
}
void move(int s,int e,int dest)
{

          int i,cnt=0;
          NODE *temp1,*temp2;

          for(t=head;t!=NULL;t=t->next,cnt++);
          if(dest==-1 && s>0 && s<=cnt && e>0 && e<=cnt)
          {
                   if(s==e)
                   {
                   }
                   else if(s==1 && e!=s)
                   {
                             temp1=head;
                             head=head->next;
                             for(i=1,t=head;i<e-1;t=t->next,i++);
                             temp1->next=t->next;
                             t->next=temp1;
                   }
                   else if(s!=1 && s!=e)
                   {
                             if(e==1)
                             {
                                      for(i=1,t=head;i<s-1;i++,t=t->next);
                                      temp1=t->next;
                                      t->next=temp1->next;
                                      temp1->next=head;
                                      head=temp1;
                             }
                             else if(e!=1)
                             {
                                      for(i=1,t=head;i<s-1;i++,t=t->next);
                                      temp1=t->next;
                                      t->next=temp1->next;

                                      for(i=1,t=head;i<e-1;t=t->next,i++);
                                      temp1->next=t->next;
                                      t->next=temp1;
                             }
                   }
          }
          else if(dest!=-1 && s>0 && s<=cnt && e>0 && e<=cnt)
          {
                   if(s==e && s==dest)
                   {
                   }
                   else if(s==e && s!=dest && s==1)
                   {
                             temp1=head;
                             head=head->next;
                             for(i=1,t=head;i<dest-1;t=t->next,i++);
                             temp1->next=t->next;
                             t->next=temp1;
                   }
                   else if(s==e && s!=dest && s!=1)
                   {
                             for(i=1,t=head;i<s-1;i++,t=t->next);
                             temp1=t->next;
                             t->next=temp1->next;

                             for(i=1,t=head;i<dest-1;t=t->next,i++);
                             temp1->next=t->next;
                             t->next=temp1;
                   }
                   else if(s==1 && s!=e)
                   {
                             temp1=head;
                             head=head->next;
                             for(i=1;i<e-1;i++,head=head->next);
                             temp2=head;
                             //temp2->next=NULL;
                             head=head->next;

                             for(i=1,t=head;i<dest-1;i++,t=t->next);
                             temp2->next=t->next;
                             t->next=temp1;

                   }
                   else if(s!=1 && s!=e  && dest==1)
                   {        
                             for(i=1,t=head;i<s-1;i++,t=t->next);
                             temp1=t->next;
                             for(i=1,temp2=head;i<e;i++,temp2=temp2->next);
                             t->next=temp2->next;
                             temp2->next=t;
                             head=temp1;
                   }
                   else if(s!=1 && s!=e && dest!=1)
                   {
                             for(i=1,t=head;i<s-1;i++,t=t->next);
                             temp1=t->next;
                             for(i=1,temp2=head;i<e;i++,temp2=temp2->next);
                             t->next=temp2->next;
                             for(i=1,t=head;i<dest-1;t=t->next,i++);
                             temp2->next=t->next;
                             t->next=temp1;
                   }        

          }
}
void copy(int s,int e,int dest)
{
          NODE *temp,*temp1;
          int i,cnt=0;
          for(t=head;t!=NULL;t=t->next,cnt++);
          if(dest==-1 && s>0 && s<=cnt && e>0 && e<=cnt+1)
          {
                   for(i=1,t=head;i<s;i++,t=t->next);
                   temp=getnode(t->data);
                   if(e==1)
                   {
                             temp->next=head;
                             head=temp;
                   }
                   else if(e>=cnt+1)
                   {
                             for(t=head;t->next!=NULL;t=t->next);
                             t->next=temp;
                   }
                   else
                   {
                             for(i=1,t=head;i<e-1;i++,t=t->next);
                             temp->next=t->next;
                             t->next=temp;
                   }
          }
          else if(dest!=-1 && s>0 && s<=cnt && e>0 && e<=cnt)
          {
                   for(i=1,t=head;i<s;i++,t=t->next);
                   temp=getnode(t->data);
                   temp1=temp;
                   t=t->next;
                   for(i=1;i<=e-s;i++)
                   {
                             temp1->next=getnode(t->data);
                             temp1=temp1->next;
                             t=t->next;
                   }
                   for(i=1,t=head;i<dest-1;i++,t=t->next);
                   temp1->next=t->next;
                   t->next=temp;
          }        
}

void dispPattern(char *pat)
{
          int i;
          t = head;
          i = 1;
          while(t)
          {
                   if(strstr(t->data,pat))
                   {
                             printf("%d: %s",i,t->data);
                   }
                   t=t->next;;
                   i++;
          }
}

main(int ac,char *av[])
{
          FILE *fp;
          int i,tokens,len=0;
          char ch,line[100],command[10],t1[2],t2[2],t3[2],t4[2];
          if(ac!=2)
          {
                   printf("Enter File Name:\t");
                   gets(fname);
          }
          else
          {
                   strcpy(fname,av[1]);
          }
          fp=fopen(fname,"r");
          head=NULL;
          while(fgets(line,80,fp))
          {
                   newnode=(NODE *)malloc(sizeof(NODE));
                   strcpy(newnode->data,line);
                   newnode->next=NULL;
                   if(head==NULL)
                             head=t=newnode;
                   else
                   {
                             t->next=newnode;
                             t=t->next;
                   }
          }
          for(t=head,i=1;t!=NULL;t=t->next,i++)
                   printf("%d : %s",i,t->data);       

          while(1)
          {
                   printf("LE$");
                   fflush(stdin);
                   gets(command);
                   tokens=sscanf(command,"%s%s%s%s",t1,t2,t3,t4);
                   printf("Tokens =%d\n",tokens);
                   switch(tokens)
                   {
                             case 1:
                                      if(strcmp(t1,"p")==0)
                                      {
                                                len=0;
                                                for(t=head;t!=NULL;t=t->next)
                                                          len++;

                                                display(1,len);
                                      }
                                      else if(strcmp("a",t1)==0)
                                                append();
                                      else if(strcmp("q",t1)==0)
                                      {
                                                if(changed==1)
                                                {
                                                          printf("Do u want to save file?(y/n)");
                                                          ch=getchar();
                                                          if(ch=='y' || ch=='Y')
                                                                   save();
                                                          else
                                                                   exit(0);
                                                }
                                                exit(0);
                                      }
                                      else if(strcmp("h",t1)==0)
                                                printf("a : append\np: Print\ns: Save \nq: Quit\n");

                                      else if(strcmp("s",t1)==0)
                                                save();                            
                                      else   
                                                printf("Invalid Command");

                                      break;
                             case 2:
                                      if(strcmp("p",t1)==0)
                                      {
                                                display(atoi(t2),atoi(t2));
                                      }
                                      else if(strcmp("d",t1)==0)
                                      {
                                                delete(atoi(t2),atoi(t2));
                                      }
                                      else if(strcmp("i",t1)==0)
                                                insert(atoi(t2),atoi(t2));
                                      else if(strcmp(t1,"f")==0)
                                                dispPattern(t2);
                                      else
                                                printf("Invalid Command\n");
                                      break;
                             case 3:
                                      if(strcmp("p",t1)==0)
                                      {
                                                if(atoi(t2)>atoi(t3))
                                                {
                                                          display(atoi(t3),atoi(t2));
                                                }
                                                else
                                                          display(atoi(t2),atoi(t3));
                                      }
                                      else if(strcmp("d",t1)==0)
                                      {
                                                delete(atoi(t2),atoi(t3));
                                      }
                                       else if(strcmp("i",t1)==0)
                                                insert(atoi(t2),atoi(t3));
                                      else if(strcmp("m",t1)==0)
                                                move(atoi(t2),atoi(t3),-1);
                                      else if(strcmp("c",t1)==0)
                                                copy(atoi(t2),atoi(t3),-1);
                                      else
                                                printf("Invalid command\n");


                                      break;
                             case 4:
                                      if(strcmp("m",t1)==0)
                                                move(atoi(t2),atoi(t3),atoi(t4));
                                      if(strcmp("c",t1)==0)
                                                copy(atoi(t2),atoi(t3),atoi(t4));
                                      else
                                                printf("Invalid command");
                                      break;
                             default: printf("Invalid");
                   }        

          }
}


No comments:

Post a Comment

Note: only a member of this blog may post a comment.