Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.73 KB | None | 0 0
  1. #include "common.h"
  2. #include "search.h"
  3.  
  4. void SaveSearchString(void *n)
  5. /* Saving current search string */
  6. {
  7.     Char *stmp, *newtmp, *tmp = NULL;
  8.     State *s;
  9.     s = (State *)n;
  10.    
  11.     /* Make sure again and again for no memory leaks */
  12.     if(s -> childhead == NULL)
  13.         return;
  14.     FREEMEMChar(s -> searchstring);
  15.     stmp = s -> childhead -> ch;
  16.     newtmp = ALLOC(sizeof(Char));
  17.     s -> searchstring = newtmp;
  18.     while(stmp != NULL && stmp -> ch != 0)
  19.     {
  20.         newtmp -> ch = stmp -> ch;
  21.         if(tmp != NULL)
  22.             tmp -> next = newtmp;
  23.         tmp = newtmp;
  24.         newtmp = ALLOC(sizeof(Char));
  25.         CheckAlloc((void *)newtmp);
  26.         stmp = stmp -> next;
  27.     }
  28.     if(tmp != NULL)
  29.         tmp -> next = NULL;
  30.     if(newtmp != NULL)
  31.         FREEMEM(newtmp);
  32.    
  33.     /* FIXME: Dirty solution */
  34.     s -> menu -> next -> menu -> next -> next -> next -> next -> next -> next -> next -> next -> enabled = 1;
  35. }
  36.  
  37. void CmdSearch(void *n)
  38. /* Search button function */
  39. {
  40.     State *s;
  41.     s = (State *)n;
  42.    
  43.     if(s -> ischildwindow == 4)
  44.     {
  45.         SaveSearchString(n);
  46.         CloseChildWindow(n);
  47.         DoSearch(n);
  48.     }
  49. }
  50.                    
  51. void DoSearch(void *n)
  52. /* Do a search to find the search string */
  53. {
  54.     Char *ch, *chtmp, *stmp;
  55.     Row *row;
  56.     int i, l = 0;
  57.     int M, N;
  58.     State *s;
  59.     s = (State *)n;
  60.    
  61.     row = s -> filehead;
  62.     if(row == NULL)
  63.         return;
  64.        
  65.     M = s -> currentcolumn;
  66.     N = s -> currentrow;
  67.     /* Start from the current position */
  68.    
  69.     if(M != 1 || N != 1)
  70.     /* In fact, right of it */
  71.         M ++;
  72.    
  73.     for(i = 1; i < N && row != NULL; i ++)
  74.         row = row -> next;
  75.     if(row == NULL)
  76.         return;
  77.     ch = row -> ch;
  78.     for(i = 1; i < M && ch != NULL; i ++)
  79.         ch = ch -> next;
  80.     if(ch == NULL)
  81.     {
  82.         M = 1;
  83.         N ++;
  84.         row = row -> next;
  85.         if(row == NULL)
  86.             return;
  87.         ch = row -> ch;
  88.         if(ch == NULL)
  89.             return;
  90.     }
  91.     if(s -> searchstring == NULL)
  92.         return;
  93.     stmp = s -> searchstring;
  94.     while(stmp != NULL && stmp -> ch != 0)
  95.         stmp = stmp -> next, l ++;
  96.     //if(l == 0)
  97.     //    return;
  98.        
  99.     while(row != NULL)
  100.     {
  101.         chtmp = ch;
  102.         stmp = s -> searchstring;
  103.         for(i = 0; i < l && chtmp != NULL; i ++)
  104.             if(chtmp -> ch != stmp -> ch)
  105.                 break;
  106.             else
  107.                 chtmp = chtmp -> next, stmp = stmp -> next;
  108.         if(i == l)
  109.         /* Now get it! */
  110.         {
  111.             s -> startchar = M;
  112.             s -> startrow = N;
  113.             s -> endchar = M + l;
  114.             s -> endrow = N;
  115.             s -> currentcolumn = M + l;
  116.             s -> currentrow = N;
  117.             CleanUp(n);
  118.             if(!s -> dotted)
  119.             {
  120.                 setactivepage(!s -> page);
  121.                 DrawMainWindow(n);
  122.                 DrawStatusBox(n);
  123.                 DoEvent(s -> redraw, n);
  124.                 CleanUp(n);
  125.                 setvisualpage(!s -> page);
  126.                 s -> page = !s -> page;
  127.             }
  128.             else
  129.             {
  130.                 OutPutXY(n);
  131.                 CleanUp(n);
  132.             }
  133.             return;
  134.         }
  135.         ch = ch -> next;
  136.         M ++;
  137.         if(ch == NULL || ch -> ch == 0)
  138.         {
  139.             row = row -> next;
  140.             if(row != NULL)
  141.                 ch = row -> ch;
  142.             N ++;
  143.             M = 1;
  144.         }
  145.     }
  146.     if(!s -> isreplace)
  147.         MsgBox(n, "错误", "找不到指定字符串!");
  148.     else
  149.         s -> isreplace = 0;
  150. }
  151.  
  152. void CmdReplace(void *n)
  153. /* Replace button function */
  154. {
  155.     Row *tmp;
  156.     Char *ch;
  157.     State *s;
  158.     char replacestring[20] = {0};
  159.     int i;
  160.     s = (State *)n;
  161.    
  162.     if(s -> ischildwindow == 5)
  163.     /* Make sure in replace window */
  164.     {
  165.         if(s -> edity1 != s -> childy1 + 27)
  166.         /* Switch the two textfield if needed */
  167.         {
  168.             tmp = s -> childhead;
  169.             s -> childhead = s -> childhead2;
  170.             s -> childhead2 = tmp;
  171.         }
  172.         SaveSearchString(n);
  173.         if(s -> childhead2 == NULL)
  174.             return;
  175.         ch = s -> childhead2 -> ch;
  176.         for(i = 0; i < 19 && ch != NULL && ch -> ch != 0; i ++)
  177.         {
  178.             replacestring[i] = ch -> ch;
  179.             ch = ch -> next;
  180.         }
  181.         replacestring[i] = 0;
  182.         CloseChildWindow(n);
  183.         s -> isreplace = 1;
  184.         while(1)
  185.         /* Keep searching and replacing all occurrences */
  186.         {
  187.             DoSearch(n);    
  188.             if(!s -> isreplace)
  189.                 return;
  190.             //BackSpace(n);
  191.             Insert(n, replacestring);
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement