Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "common.h"
- #include "search.h"
- void SaveSearchString(void *n)
- /* Saving current search string */
- {
- Char *stmp, *newtmp, *tmp = NULL;
- State *s;
- s = (State *)n;
- /* Make sure again and again for no memory leaks */
- if(s -> childhead == NULL)
- return;
- FREEMEMChar(s -> searchstring);
- stmp = s -> childhead -> ch;
- newtmp = ALLOC(sizeof(Char));
- s -> searchstring = newtmp;
- while(stmp != NULL && stmp -> ch != 0)
- {
- newtmp -> ch = stmp -> ch;
- if(tmp != NULL)
- tmp -> next = newtmp;
- tmp = newtmp;
- newtmp = ALLOC(sizeof(Char));
- CheckAlloc((void *)newtmp);
- stmp = stmp -> next;
- }
- if(tmp != NULL)
- tmp -> next = NULL;
- if(newtmp != NULL)
- FREEMEM(newtmp);
- /* FIXME: Dirty solution */
- s -> menu -> next -> menu -> next -> next -> next -> next -> next -> next -> next -> next -> enabled = 1;
- }
- void CmdSearch(void *n)
- /* Search button function */
- {
- State *s;
- s = (State *)n;
- if(s -> ischildwindow == 4)
- {
- SaveSearchString(n);
- CloseChildWindow(n);
- DoSearch(n);
- }
- }
- void DoSearch(void *n)
- /* Do a search to find the search string */
- {
- Char *ch, *chtmp, *stmp;
- Row *row;
- int i, l = 0;
- int M, N;
- State *s;
- s = (State *)n;
- row = s -> filehead;
- if(row == NULL)
- return;
- M = s -> currentcolumn;
- N = s -> currentrow;
- /* Start from the current position */
- if(M != 1 || N != 1)
- /* In fact, right of it */
- M ++;
- for(i = 1; i < N && row != NULL; i ++)
- row = row -> next;
- if(row == NULL)
- return;
- ch = row -> ch;
- for(i = 1; i < M && ch != NULL; i ++)
- ch = ch -> next;
- if(ch == NULL)
- {
- M = 1;
- N ++;
- row = row -> next;
- if(row == NULL)
- return;
- ch = row -> ch;
- if(ch == NULL)
- return;
- }
- if(s -> searchstring == NULL)
- return;
- stmp = s -> searchstring;
- while(stmp != NULL && stmp -> ch != 0)
- stmp = stmp -> next, l ++;
- //if(l == 0)
- // return;
- while(row != NULL)
- {
- chtmp = ch;
- stmp = s -> searchstring;
- for(i = 0; i < l && chtmp != NULL; i ++)
- if(chtmp -> ch != stmp -> ch)
- break;
- else
- chtmp = chtmp -> next, stmp = stmp -> next;
- if(i == l)
- /* Now get it! */
- {
- s -> startchar = M;
- s -> startrow = N;
- s -> endchar = M + l;
- s -> endrow = N;
- s -> currentcolumn = M + l;
- s -> currentrow = N;
- CleanUp(n);
- if(!s -> dotted)
- {
- setactivepage(!s -> page);
- DrawMainWindow(n);
- DrawStatusBox(n);
- DoEvent(s -> redraw, n);
- CleanUp(n);
- setvisualpage(!s -> page);
- s -> page = !s -> page;
- }
- else
- {
- OutPutXY(n);
- CleanUp(n);
- }
- return;
- }
- ch = ch -> next;
- M ++;
- if(ch == NULL || ch -> ch == 0)
- {
- row = row -> next;
- if(row != NULL)
- ch = row -> ch;
- N ++;
- M = 1;
- }
- }
- if(!s -> isreplace)
- MsgBox(n, "错误", "找不到指定字符串!");
- else
- s -> isreplace = 0;
- }
- void CmdReplace(void *n)
- /* Replace button function */
- {
- Row *tmp;
- Char *ch;
- State *s;
- char replacestring[20] = {0};
- int i;
- s = (State *)n;
- if(s -> ischildwindow == 5)
- /* Make sure in replace window */
- {
- if(s -> edity1 != s -> childy1 + 27)
- /* Switch the two textfield if needed */
- {
- tmp = s -> childhead;
- s -> childhead = s -> childhead2;
- s -> childhead2 = tmp;
- }
- SaveSearchString(n);
- if(s -> childhead2 == NULL)
- return;
- ch = s -> childhead2 -> ch;
- for(i = 0; i < 19 && ch != NULL && ch -> ch != 0; i ++)
- {
- replacestring[i] = ch -> ch;
- ch = ch -> next;
- }
- replacestring[i] = 0;
- CloseChildWindow(n);
- s -> isreplace = 1;
- while(1)
- /* Keep searching and replacing all occurrences */
- {
- DoSearch(n);
- if(!s -> isreplace)
- return;
- //BackSpace(n);
- Insert(n, replacestring);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement