Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- struct litera
- {
- char lit;
- struct litera *next;
- struct litera *prev;
- };
- typedef struct litera LIT;
- //------------variabile globale---------------------
- LIT *cap_lista;
- LIT *cap_lista_undo;
- LIT *cap_lista_redo;
- LIT *lastCursorPosition;
- LIT *lastCursorPosition_redo;
- LIT *currentPos;
- LIT* delete2(LIT*asd );
- char v[2000];
- int curPos;
- int deleteCount;
- int copyCount;
- int n=1;
- int n2=1;
- int n2_redo=1;
- int lastCurPos;
- int lastCurPos_redo;
- //---------------functie pentru nod----------------
- LIT *createNode(char c)
- {
- LIT *q;
- q=(LIT*)malloc(sizeof(LIT));
- q->lit=c;
- return q;
- }
- //---------functie citire si facere lista----------
- void dataRead(char *fisier)
- {
- FILE *input;
- LIT *p, *q;
- char c;
- input=fopen(fisier,"rt");
- fscanf(input,"%c",&c);
- p=createNode(c);
- //p->next=NULL;
- cap_lista=p;
- currentPos=cap_lista;
- curPos=0;
- while(!feof(input) && !ferror(input))
- {
- fscanf(input,"%c",&c);
- if(!feof(input) && !ferror(input))
- {
- q=createNode(c);
- //q->next=NULL;
- q->prev=p;
- p->next=q;
- p=q;
- n++;
- }
- }
- p->next=cap_lista;
- cap_lista->prev=p;
- currentPos->prev=p;
- currentPos->next=cap_lista->next;
- fclose(input);
- }
- //--------functie scriere lista in fisier output------
- void dataWrite(char *fisier)
- {
- FILE *output;
- LIT *p;
- int i;
- output=fopen(fisier,"w+t");
- for(p=cap_lista, i=0; i<n; p=p->next, i++)
- fprintf(output, "%c", p->lit);
- fprintf(stdout, "\n");
- fclose(output);
- }
- //-------------eliberare memorie-------------------
- void freeMem()
- {
- LIT *p, *q;
- for(p=cap_lista; p!=cap_lista; p=q)
- {
- q=p->next;
- free(p);
- }
- }
- //------------------MOVE-------------------------
- void move(int posToMove)
- {
- LIT *x;
- int i;
- if( curPos + posToMove > n )
- {
- for(x=currentPos, i=0; x->next->next!=cap_lista; x=x->next, i++)
- {
- ;
- }
- currentPos=x->next;
- currentPos->next=cap_lista;
- currentPos->prev=x->next->prev;
- curPos=n;
- return;
- }
- else if( curPos + posToMove < 0)
- {
- currentPos=cap_lista;
- currentPos->next=cap_lista->next;
- currentPos->prev=cap_lista->prev;
- printf("\na trecut prin move <0\n" );
- curPos=0;
- return;
- }
- if(posToMove > 0)
- {
- for(x=currentPos, i=1; i<=posToMove; x=x->next, i++)
- ;
- currentPos=x;
- curPos+=posToMove;
- }
- else if(posToMove < 0)
- {
- for(x=currentPos, i=1; i<=(-posToMove); x=x->prev, i++)
- ;
- currentPos=x;
- curPos+=posToMove;
- }
- }
- //-----------------INSERT----------------------
- void insert(char *stringText)
- {
- LIT *p, *q;
- LIT *toInsertStart;
- int i, textLength=1;
- p=createNode(stringText[0]);
- n++;
- p->next=NULL;
- toInsertStart=p;
- for(i=1; i<strlen(stringText); i++)
- {
- q=createNode(stringText[i]);
- q->next=NULL;
- q->prev=p;
- p->next=q;
- p=q;
- n++;
- textLength++;
- }
- p->next=toInsertStart;
- toInsertStart->prev=p;
- q=currentPos->prev;
- if( curPos == 0 )
- {
- p->next=cap_lista;
- cap_lista->prev->next=toInsertStart;
- cap_lista->prev=p;
- toInsertStart->prev=cap_lista->prev;
- currentPos=cap_lista;
- cap_lista=toInsertStart;
- }
- else if( curPos == (n - textLength) )
- {
- q=q->next;
- p->next=cap_lista;
- cap_lista->prev=p;
- q->next=toInsertStart;
- toInsertStart->prev=q;
- currentPos=p;
- }
- else
- {
- p->next=q->next;
- q->next->prev=p;
- q->next=toInsertStart;
- toInsertStart->prev=q;
- currentPos=p->next;
- }
- curPos+=textLength;
- //n+=textLength;
- }
- //-----------------DELETE---------------------
- void delete()
- {
- LIT *p;
- int i;
- if( deleteCount >= n - curPos )
- deleteCount = n - curPos;
- if( currentPos == cap_lista )
- {
- p = currentPos;
- for(i=0; i<deleteCount; p=p->next, i++)
- {
- p->prev->next = p->next;
- p->next->prev=p->prev;
- free(p);
- n--;
- cap_lista=p->next;
- }
- currentPos=cap_lista;
- }
- else if( curPos == n-1 )
- {
- return;
- }
- else
- {
- p=currentPos;
- currentPos=currentPos->prev;
- for( i=0; i<deleteCount ; p=p->next, i++ )
- {
- p->prev->next=p->next;
- p->next->prev=p->prev;
- free(p);
- n--;
- }
- LIT* itr = cap_lista;
- for (i = 0; i < curPos ;i++)
- {
- printf("as\n");
- itr = itr->next;
- if( itr == cap_lista) break;
- }
- currentPos = itr->prev;
- //currentPos=currentPos->next;
- printf("\nPOZ CUR eEE %c\n", currentPos->lit);
- }
- }
- //----------BACKSPACE-------------------------------
- void backspace()
- {
- LIT *p, *q;
- if( currentPos==cap_lista || curPos == 0)
- return;
- else if( curPos == n )
- {
- p=currentPos->prev;
- q=currentPos;
- printf("BACKSPACE == n\n" );
- p->next=cap_lista;
- cap_lista->prev=p;
- n--;
- curPos=n;
- currentPos=p;
- currentPos->next=cap_lista;
- cap_lista->prev=currentPos;
- free(q);
- return;
- }
- p=currentPos->prev->prev;
- q=currentPos->prev;
- p->next=currentPos;
- p->prev=currentPos->prev->prev->prev;
- currentPos->prev=p;
- n--;
- curPos--;
- free(q);
- }
- //-------------COPY------------------------
- void copy()
- {
- LIT *p;
- int i;
- printf("Initial copyCount este: %d\n",copyCount );
- if( copyCount >= n - curPos )
- copyCount = n - curPos;
- printf("Dupa copyCount este: %d\n",copyCount );
- for(p=currentPos, i=0; i<copyCount; p=p->next, i++)
- v[i]=p->lit;
- v[i]='\0';
- }
- //-----------------UNDO-----------------
- void replace(char *toReplace, char *replacedBy)
- {
- LIT *cap_replace, *p, *q, *curr_aux;
- int i, j, k=0;
- char c1,c2;
- for(p=cap_lista, i=0; i<=n-strlen(toReplace); p=p->next, i++)
- {
- k=1;
- for(q=p, j=0; j<strlen(toReplace);q=q->next, j++)
- {
- c1=q->lit;
- c2=toReplace[j];
- if(c1 != c2)
- k=0;
- }
- if(k == 1)
- {
- //if(i==0)
- //cap_lista=
- //q=p;
- curr_aux=currentPos/*->prev*/;
- curPos=i-1;
- currentPos=p;
- insert(replacedBy);
- LIT * itr = currentPos;
- for(i = 0; i < strlen(toReplace) ;i++){
- itr = delete2(itr);
- if(itr == NULL) break;
- }
- //deleteCount=strlen(toReplace);
- //delete();
- currentPos=curr_aux;
- }
- }
- }
- //--------------------------------
- void changeList(LIT* cap2)//cap1 este capul noii liste
- //cap2 este cap_lista
- {
- LIT *p, *q, *l;
- int i;
- lastCursorPosition=currentPos;
- lastCurPos=curPos;
- for(p=cap_lista_undo; p!=cap_lista_undo; p=q)
- {
- q=p->next;
- free(p);
- }
- //n2=1;
- n2=n;
- p=createNode(cap2->lit);
- p->next=NULL;
- cap_lista_undo=p;
- for(l=cap2->next, i=1; i<n; l=l->next, i++)
- {
- q=createNode(l->lit);
- q->next=NULL;
- q->prev=p;
- p->next=q;
- p=q;
- //n2++;
- }
- p->next=cap_lista_undo;
- cap_lista_undo->prev=p;
- for(p=cap_lista_undo, i=0; i<n2; p=p->next, i++)
- printf("%c",p->lit);
- printf("\n");
- }
- void changeListRedo(LIT* cap2)//cap1 este capul noii liste
- //cap2 este cap_lista
- {
- LIT *p, *q, *l;
- int i;
- lastCursorPosition_redo=currentPos;
- lastCurPos_redo=curPos;
- for(p=cap_lista_redo; p!=cap_lista_redo; p=q)
- {
- q=p->next;
- free(p);
- }
- //n2=1;
- n2_redo=n;
- p=createNode(cap2->lit);
- p->next=NULL;
- cap_lista_redo=p;
- for(l=cap2->next, i=1; i<n; l=l->next, i++)
- {
- q=createNode(l->lit);
- q->next=NULL;
- q->prev=p;
- p->next=q;
- p=q;
- //n2++;
- }
- p->next=cap_lista_redo;
- cap_lista_redo->prev=p;
- for(p=cap_lista_redo, i=0; i<n2_redo; p=p->next, i++)
- printf("%c",p->lit);
- printf("\n");
- }
- //-----------------------------
- void undo()
- {
- n=n2;
- cap_lista=cap_lista_undo;
- currentPos=lastCursorPosition;
- curPos=lastCurPos;
- }
- //---------------------------
- void instructionRead(char *fisier)
- {
- FILE *instructions;
- instructions=fopen(fisier,"rt");
- int dltCount;
- int toMove;
- char instr[11];
- int valoare;
- puts("Noul continut al fisierului este:\n");
- fflush(stdin);
- fseek(instructions,0,SEEK_SET);
- int nr_instr = 0;
- fscanf(instructions, "%d", &nr_instr);
- while(nr_instr){
- fscanf(instructions,"%s",instr);
- if(strcmp(instr,"move")==0)
- {
- changeList(cap_lista);
- fscanf(instructions,"%d",&toMove);
- move(toMove);
- printf("%d\n",toMove );
- }
- else if(strcmp(instr,"insert")==0)
- {
- char stringText[20];
- changeList(cap_lista);
- fscanf(instructions,"%s",stringText);
- insert(stringText);
- printf("%s\n",stringText );
- }
- else if(strcmp(instr,"del")==0)
- {
- changeList(cap_lista);
- fscanf(instructions,"%d",&dltCount);
- deleteCount=dltCount;
- int i = 0;
- LIT * itr = currentPos;
- for(i = 0; i < dltCount ;i++){
- itr = delete2(itr);
- if(itr == NULL) break;
- }
- currentPos = itr;
- }
- else if(strcmp(instr,"backspace")==0)
- {
- changeList(cap_lista);
- //if(curPos != n)
- //move(-1);
- //currentPos =delete2(currentPos);
- printf("BACKSPACE si poz cursor este %c si numarul %d\n", currentPos->lit,curPos);
- backspace();
- }
- else if(strcmp(instr,"copy")==0)
- {
- changeList(cap_lista);
- fscanf(instructions,"%d",©Count);
- copy();
- }
- else if(strcmp(instr,"paste")==0)
- {
- changeList(cap_lista);
- if(copyCount>0)
- insert(v);
- }
- else if(strcmp(instr,"undo")==0)
- {
- changeListRedo(cap_lista);
- undo();
- int i;
- LIT * itr = cap_lista;
- for(i = 0; i < curPos ;i++)
- {
- itr = itr->next;
- if(itr == NULL) break;
- }
- currentPos = itr;
- }
- else if(strcmp(instr,"repzlace") == 0 )
- {
- changeListRedo(cap_lista);
- char toReplace[20];
- char replacedBy[20];
- fscanf(instructions,"%s",toReplace);
- fscanf(instructions,"%s",replacedBy);
- replace(toReplace, replacedBy);
- }
- nr_instr--;
- }
- fclose(instructions);
- }
- //--------------main-------------
- LIT * delete2(LIT *node)
- {
- printf("\n%d\n", curPos);
- if(curPos == 0)
- {
- if(n == 1)
- {
- free(node);
- cap_lista->next = NULL;
- cap_lista->prev = NULL;
- n = 0;
- return NULL;
- }
- else
- {
- LIT * anterior = node->prev;
- LIT * urmator = node->next;
- n--;
- anterior->next = urmator;
- urmator->prev = anterior;
- cap_lista = urmator;
- free(node);
- return urmator;
- }
- }else{
- LIT * anterior = node->prev;
- LIT * urmator = node->next;
- n--;
- anterior->next = urmator;
- urmator->prev = anterior;
- free(node);
- return urmator;
- }
- }
- int main(int argc, char **argv)
- {
- LIT *p;
- dataRead(argv[1]);
- instructionRead(argv[2]);
- dataWrite(argv[3]);
- freeMem();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement