Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.55 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. struct litera
  7. {
  8.   char lit;
  9.  
  10.   struct litera *next;
  11.   struct litera *prev;
  12. };
  13. typedef struct litera LIT;
  14.  
  15. //------------variabile globale---------------------
  16. LIT *cap_lista;
  17. LIT *cap_lista_undo;
  18. LIT *cap_lista_redo;
  19. LIT *lastCursorPosition;
  20. LIT *lastCursorPosition_redo;
  21. LIT *currentPos;
  22. LIT* delete2(LIT*asd    );
  23. char v[2000];
  24. int curPos;
  25. int deleteCount;
  26. int copyCount;
  27. int n=1;
  28. int n2=1;
  29. int n2_redo=1;
  30. int lastCurPos;
  31. int lastCurPos_redo;
  32. //---------------functie pentru nod----------------
  33. LIT *createNode(char c)
  34. {
  35.     LIT *q;
  36.     q=(LIT*)malloc(sizeof(LIT));
  37.     q->lit=c;
  38.  
  39.     return q;
  40. }
  41. //---------functie citire si facere lista----------
  42. void dataRead(char *fisier)
  43. {
  44.     FILE *input;
  45.     LIT *p, *q;
  46.     char c;
  47.  
  48.     input=fopen(fisier,"rt");
  49.     fscanf(input,"%c",&c);
  50.  
  51.     p=createNode(c);
  52.  
  53.     //p->next=NULL;
  54.     cap_lista=p;
  55.     currentPos=cap_lista;
  56.     curPos=0;
  57.  
  58.     while(!feof(input) && !ferror(input))
  59.     {
  60.         fscanf(input,"%c",&c);
  61.  
  62.         if(!feof(input) && !ferror(input))
  63.         {
  64.             q=createNode(c);
  65.  
  66.             //q->next=NULL;
  67.             q->prev=p;
  68.             p->next=q;
  69.             p=q;
  70.             n++;
  71.         }
  72.     }
  73.  
  74.     p->next=cap_lista;
  75.     cap_lista->prev=p;
  76.  
  77.     currentPos->prev=p;
  78.     currentPos->next=cap_lista->next;
  79.  
  80.     fclose(input);
  81. }
  82. //--------functie scriere lista in fisier output------
  83. void dataWrite(char *fisier)
  84. {
  85.   FILE *output;
  86.   LIT *p;
  87.   int i;
  88.  
  89.   output=fopen(fisier,"w+t");
  90.  
  91.   for(p=cap_lista, i=0; i<n; p=p->next, i++)
  92.       fprintf(output, "%c", p->lit);
  93.  
  94.   fprintf(stdout, "\n");
  95.  
  96.   fclose(output);
  97. }
  98. //-------------eliberare memorie-------------------
  99. void freeMem()
  100. {
  101.   LIT *p, *q;
  102.  
  103.   for(p=cap_lista; p!=cap_lista; p=q)
  104.   {
  105.       q=p->next;
  106.       free(p);
  107.   }
  108. }
  109. //------------------MOVE-------------------------
  110. void move(int posToMove)
  111. {
  112.   LIT *x;
  113.   int i;
  114.  
  115.     if( curPos + posToMove > n )
  116.         {
  117.           for(x=currentPos, i=0; x->next->next!=cap_lista; x=x->next, i++)
  118.             {
  119.               ;
  120.             }
  121.  
  122.           currentPos=x->next;
  123.           currentPos->next=cap_lista;
  124.           currentPos->prev=x->next->prev;
  125.  
  126.           curPos=n;
  127.           return;
  128.         }
  129.     else if( curPos + posToMove < 0)
  130.           {
  131.             currentPos=cap_lista;
  132.             currentPos->next=cap_lista->next;
  133.             currentPos->prev=cap_lista->prev;
  134.             printf("\na trecut prin move <0\n" );
  135.             curPos=0;
  136.             return;
  137.           }
  138.  
  139.       if(posToMove > 0)
  140.       {
  141.         for(x=currentPos, i=1; i<=posToMove; x=x->next, i++)
  142.         ;
  143.  
  144.         currentPos=x;
  145.         curPos+=posToMove;
  146.       }
  147.       else if(posToMove < 0)
  148.       {
  149.         for(x=currentPos, i=1; i<=(-posToMove); x=x->prev, i++)
  150.         ;
  151.  
  152.         currentPos=x;
  153.         curPos+=posToMove;
  154.       }
  155.  
  156. }
  157. //-----------------INSERT----------------------
  158. void insert(char *stringText)
  159. {
  160.   LIT *p, *q;
  161.   LIT *toInsertStart;
  162.   int i, textLength=1;
  163.  
  164.   p=createNode(stringText[0]);
  165.   n++;
  166.   p->next=NULL;
  167.   toInsertStart=p;
  168.  
  169.   for(i=1; i<strlen(stringText); i++)
  170.   {
  171.     q=createNode(stringText[i]);
  172.  
  173.     q->next=NULL;
  174.     q->prev=p;
  175.     p->next=q;
  176.     p=q;
  177.     n++;
  178.     textLength++;
  179.   }
  180.   p->next=toInsertStart;
  181.   toInsertStart->prev=p;
  182.  
  183.   q=currentPos->prev;
  184.  
  185.   if( curPos == 0 )
  186.   {
  187.     p->next=cap_lista;
  188.     cap_lista->prev->next=toInsertStart;
  189.     cap_lista->prev=p;
  190.     toInsertStart->prev=cap_lista->prev;
  191.     currentPos=cap_lista;
  192.     cap_lista=toInsertStart;
  193.   }
  194.   else if( curPos == (n - textLength) )
  195.   {
  196.     q=q->next;
  197.     p->next=cap_lista;
  198.     cap_lista->prev=p;
  199.     q->next=toInsertStart;
  200.     toInsertStart->prev=q;
  201.     currentPos=p;
  202.   }
  203.   else
  204.   {
  205.     p->next=q->next;
  206.     q->next->prev=p;
  207.     q->next=toInsertStart;
  208.     toInsertStart->prev=q;
  209.       currentPos=p->next;
  210.   }
  211.   curPos+=textLength;
  212.   //n+=textLength;
  213. }
  214. //-----------------DELETE---------------------
  215. void delete()
  216. {
  217.   LIT *p;
  218.   int i;
  219.  
  220.   if( deleteCount >= n - curPos )
  221.       deleteCount = n - curPos;
  222.  
  223.   if( currentPos == cap_lista )
  224.   {
  225.     p = currentPos;
  226.  
  227.     for(i=0; i<deleteCount; p=p->next, i++)
  228.     {
  229.       p->prev->next = p->next;
  230.       p->next->prev=p->prev;
  231.       free(p);
  232.       n--;
  233.       cap_lista=p->next;
  234.     }
  235.  
  236.     currentPos=cap_lista;
  237.   }
  238.   else if( curPos == n-1 )
  239.   {
  240.   return;
  241.   }
  242.   else
  243.   {
  244.     p=currentPos;
  245.     currentPos=currentPos->prev;
  246.  
  247.     for( i=0; i<deleteCount ; p=p->next, i++ )
  248.     {
  249.         p->prev->next=p->next;
  250.         p->next->prev=p->prev;
  251.         free(p);
  252.         n--;
  253.     }
  254.     LIT* itr = cap_lista;
  255.  
  256.     for (i = 0; i < curPos ;i++)
  257.     {
  258.       printf("as\n");
  259.        itr = itr->next;
  260.        if( itr == cap_lista) break;
  261.     }
  262.     currentPos = itr->prev;
  263.     //currentPos=currentPos->next;
  264.     printf("\nPOZ CUR eEE %c\n", currentPos->lit);
  265.   }
  266. }
  267. //----------BACKSPACE-------------------------------
  268. void backspace()
  269. {
  270.   LIT *p, *q;
  271.  
  272.   if( currentPos==cap_lista || curPos == 0)
  273.     return;
  274.   else if( curPos == n )
  275.   {
  276.     p=currentPos->prev;
  277.  
  278.     q=currentPos;
  279.     printf("BACKSPACE == n\n" );
  280.     p->next=cap_lista;
  281.     cap_lista->prev=p;
  282.     n--;
  283.     curPos=n;
  284.     currentPos=p;
  285.     currentPos->next=cap_lista;
  286.     cap_lista->prev=currentPos;
  287.     free(q);
  288.     return;
  289.   }
  290.  
  291.   p=currentPos->prev->prev;
  292.  
  293.   q=currentPos->prev;
  294.  
  295.   p->next=currentPos;
  296.   p->prev=currentPos->prev->prev->prev;
  297.   currentPos->prev=p;
  298.  
  299.   n--;
  300.   curPos--;
  301.  
  302.   free(q);
  303. }
  304. //-------------COPY------------------------
  305. void copy()
  306. {
  307.   LIT *p;
  308.   int i;
  309.   printf("Initial copyCount este: %d\n",copyCount );
  310.   if( copyCount >= n - curPos )
  311.       copyCount = n - curPos;
  312.  
  313.     printf("Dupa copyCount este: %d\n",copyCount );
  314.  
  315.  
  316.   for(p=currentPos, i=0; i<copyCount; p=p->next, i++)
  317.     v[i]=p->lit;
  318.  
  319.     v[i]='\0';
  320. }
  321. //-----------------UNDO-----------------
  322. void replace(char *toReplace, char *replacedBy)
  323. {
  324.   LIT *cap_replace, *p, *q, *curr_aux;
  325.   int i, j, k=0;
  326.   char c1,c2;
  327.  
  328.  
  329.  
  330.   for(p=cap_lista, i=0; i<=n-strlen(toReplace); p=p->next, i++)
  331.   {
  332.     k=1;
  333.  
  334.     for(q=p, j=0; j<strlen(toReplace);q=q->next, j++)
  335.     {
  336.       c1=q->lit;
  337.       c2=toReplace[j];
  338.  
  339.       if(c1 != c2)
  340.         k=0;
  341.  
  342.     }
  343.  
  344.     if(k == 1)
  345.     {
  346.       //if(i==0)
  347.         //cap_lista=
  348.       //q=p;
  349.       curr_aux=currentPos/*->prev*/;
  350.       curPos=i-1;
  351.       currentPos=p;
  352.  
  353.       insert(replacedBy);
  354.       LIT * itr = currentPos;
  355.       for(i = 0; i < strlen(toReplace) ;i++){
  356.         itr = delete2(itr);
  357.         if(itr == NULL) break;
  358.       }
  359.  
  360.       //deleteCount=strlen(toReplace);
  361.       //delete();
  362.  
  363.       currentPos=curr_aux;
  364.     }
  365.   }
  366.  
  367. }
  368. //--------------------------------
  369. void changeList(LIT* cap2)//cap1 este capul noii liste
  370.                                     //cap2 este cap_lista
  371. {
  372.   LIT *p, *q, *l;
  373.   int i;
  374.  
  375.   lastCursorPosition=currentPos;
  376.   lastCurPos=curPos;
  377.  
  378.   for(p=cap_lista_undo; p!=cap_lista_undo; p=q)
  379.   {
  380.       q=p->next;
  381.       free(p);
  382.   }
  383.  
  384.   //n2=1;
  385.   n2=n;
  386.   p=createNode(cap2->lit);
  387.  
  388.   p->next=NULL;
  389.   cap_lista_undo=p;
  390.  
  391.   for(l=cap2->next, i=1; i<n; l=l->next, i++)
  392.   {
  393.     q=createNode(l->lit);
  394.     q->next=NULL;
  395.     q->prev=p;
  396.     p->next=q;
  397.     p=q;
  398.     //n2++;
  399.   }
  400.  
  401.   p->next=cap_lista_undo;
  402.   cap_lista_undo->prev=p;
  403.  
  404.   for(p=cap_lista_undo, i=0; i<n2; p=p->next, i++)
  405.     printf("%c",p->lit);
  406.     printf("\n");
  407.  
  408.  
  409. }
  410. void changeListRedo(LIT* cap2)//cap1 este capul noii liste
  411.                                     //cap2 este cap_lista
  412. {
  413.   LIT *p, *q, *l;
  414.   int i;
  415.  
  416.   lastCursorPosition_redo=currentPos;
  417.   lastCurPos_redo=curPos;
  418.  
  419.   for(p=cap_lista_redo; p!=cap_lista_redo; p=q)
  420.   {
  421.       q=p->next;
  422.       free(p);
  423.   }
  424.  
  425.   //n2=1;
  426.   n2_redo=n;
  427.   p=createNode(cap2->lit);
  428.  
  429.   p->next=NULL;
  430.   cap_lista_redo=p;
  431.  
  432.   for(l=cap2->next, i=1; i<n; l=l->next, i++)
  433.   {
  434.     q=createNode(l->lit);
  435.     q->next=NULL;
  436.     q->prev=p;
  437.     p->next=q;
  438.     p=q;
  439.     //n2++;
  440.   }
  441.  
  442.   p->next=cap_lista_redo;
  443.   cap_lista_redo->prev=p;
  444.  
  445.   for(p=cap_lista_redo, i=0; i<n2_redo; p=p->next, i++)
  446.     printf("%c",p->lit);
  447.     printf("\n");
  448.  
  449.  
  450. }
  451. //-----------------------------
  452.   void undo()
  453.   {
  454.     n=n2;
  455.     cap_lista=cap_lista_undo;
  456.     currentPos=lastCursorPosition;
  457.     curPos=lastCurPos;
  458.   }
  459. //---------------------------
  460. void instructionRead(char *fisier)
  461. {
  462.   FILE *instructions;
  463.   instructions=fopen(fisier,"rt");
  464.   int dltCount;
  465.   int toMove;
  466.  
  467.   char instr[11];
  468.   int valoare;
  469.  
  470.  
  471.   puts("Noul continut al fisierului este:\n");
  472.   fflush(stdin);
  473.  
  474.   fseek(instructions,0,SEEK_SET);
  475.   int nr_instr = 0;
  476.   fscanf(instructions, "%d", &nr_instr);
  477.   while(nr_instr){
  478.     fscanf(instructions,"%s",instr);
  479.  
  480.     if(strcmp(instr,"move")==0)
  481.     {
  482.       changeList(cap_lista);
  483.       fscanf(instructions,"%d",&toMove);
  484.       move(toMove);
  485.       printf("%d\n",toMove );
  486.     }
  487.     else if(strcmp(instr,"insert")==0)
  488.     {
  489.       char stringText[20];
  490.       changeList(cap_lista);
  491.       fscanf(instructions,"%s",stringText);
  492.       insert(stringText);
  493.       printf("%s\n",stringText );
  494.     }
  495.     else if(strcmp(instr,"del")==0)
  496.     {
  497.       changeList(cap_lista);
  498.       fscanf(instructions,"%d",&dltCount);
  499.       deleteCount=dltCount;
  500.  
  501.       int i = 0;
  502.         LIT * itr = currentPos;
  503.         for(i = 0; i < dltCount ;i++){
  504.           itr = delete2(itr);
  505.           if(itr == NULL) break;
  506.         }
  507.         currentPos = itr;
  508.     }
  509.     else if(strcmp(instr,"backspace")==0)
  510.     {
  511.  
  512.  
  513.       changeList(cap_lista);
  514.       //if(curPos != n)
  515.         //move(-1);
  516.       //currentPos =delete2(currentPos);
  517.  
  518.       printf("BACKSPACE si poz cursor este %c si numarul %d\n", currentPos->lit,curPos);
  519.  
  520.       backspace();
  521.     }
  522.     else if(strcmp(instr,"copy")==0)
  523.     {
  524.       changeList(cap_lista);
  525.       fscanf(instructions,"%d",&copyCount);
  526.       copy();
  527.     }
  528.     else if(strcmp(instr,"paste")==0)
  529.     {
  530.       changeList(cap_lista);
  531.       if(copyCount>0)
  532.         insert(v);
  533.  
  534.     }
  535.     else if(strcmp(instr,"undo")==0)
  536.     {
  537.       changeListRedo(cap_lista);
  538.       undo();
  539.       int i;
  540.       LIT * itr = cap_lista;
  541.       for(i = 0; i < curPos ;i++)
  542.       {
  543.         itr = itr->next;
  544.         if(itr == NULL) break;
  545.       }
  546.  
  547.       currentPos = itr;
  548.     }
  549.     else if(strcmp(instr,"repzlace") == 0 )
  550.     {
  551.  
  552.       changeListRedo(cap_lista);
  553.       char toReplace[20];
  554.       char replacedBy[20];
  555.  
  556.       fscanf(instructions,"%s",toReplace);
  557.       fscanf(instructions,"%s",replacedBy);
  558.  
  559.       replace(toReplace, replacedBy);
  560.     }
  561.  
  562.     nr_instr--;
  563.   }
  564.   fclose(instructions);
  565. }
  566. //--------------main-------------
  567. LIT * delete2(LIT *node)
  568. {
  569.  
  570.   printf("\n%d\n", curPos);
  571.   if(curPos == 0)
  572.   {
  573.       if(n  == 1)
  574.       {
  575.         free(node);
  576.         cap_lista->next = NULL;
  577.         cap_lista->prev = NULL;
  578.         n = 0;
  579.         return NULL;
  580.       }
  581.       else
  582.       {
  583.         LIT * anterior = node->prev;
  584.         LIT * urmator = node->next;
  585.         n--;
  586.         anterior->next = urmator;
  587.         urmator->prev = anterior;
  588.         cap_lista = urmator;
  589.         free(node);
  590.         return urmator;
  591.       }
  592.   }else{
  593.     LIT * anterior = node->prev;
  594.     LIT * urmator = node->next;
  595.     n--;
  596.     anterior->next = urmator;
  597.     urmator->prev = anterior;
  598.     free(node);
  599.     return urmator;
  600.   }
  601. }
  602. int main(int argc, char **argv)
  603. {
  604.   LIT *p;
  605.  
  606.     dataRead(argv[1]);
  607.  
  608.     instructionRead(argv[2]);
  609.  
  610.     dataWrite(argv[3]);
  611.  
  612.     freeMem();
  613.  
  614.     return 0;
  615. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement