Copy rpoolmgr.h from trunk
[reactos.git] / rosapps / dflat32 / search.c
1 /* ---------------- search.c ------------- */
2 #include "dflat.h"
3
4 extern DF_DBOX SearchTextDB;
5 extern DF_DBOX ReplaceTextDB;
6 static int CheckCase = TRUE;
7 static int Replacing = FALSE;
8
9 /* - case-insensitive, white-space-normalized char compare - */
10 static BOOL SearchCmp(int a, int b)
11 {
12 if (b == '\n')
13 b = ' ';
14 if (CheckCase)
15 return a != b;
16 return tolower(a) != tolower(b);
17 }
18
19 /* ----- replace a matching block of text ----- */
20 static void replacetext(DFWINDOW wnd, char *cp1, DF_DBOX *db)
21 {
22 char *cr = DfGetEditBoxText(db, DF_ID_REPLACEWITH);
23 char *cp = DfGetEditBoxText(db, DF_ID_SEARCHFOR);
24 int oldlen = strlen(cp); /* length of text being replaced */
25 int newlen = strlen(cr); /* length of replacing text */
26 int dif;
27 if (oldlen < newlen) {
28 /* ---- new text expands text size ---- */
29 dif = newlen-oldlen;
30 if (wnd->textlen < strlen(wnd->text)+dif) {
31 /* ---- need to reallocate the text buffer ---- */
32 int offset = (int)((int)cp1-(int)wnd->text);
33 wnd->textlen += dif;
34 wnd->text = DfRealloc(wnd->text, wnd->textlen+2);
35 cp1 = wnd->text + offset;
36 }
37 memmove(cp1+dif, cp1, strlen(cp1)+1);
38 }
39 else if (oldlen > newlen) {
40 /* ---- new text collapses text size ---- */
41 dif = oldlen-newlen;
42 memmove(cp1, cp1+dif, strlen(cp1)+1);
43 }
44 strncpy(cp1, cr, newlen);
45 }
46
47 /* ------- search for the occurrance of a string ------- */
48 static void SearchTextBox(DFWINDOW wnd, int incr)
49 {
50 char *s1 = NULL, *s2, *cp1;
51 DF_DBOX *db = Replacing ? &ReplaceTextDB : &SearchTextDB;
52 char *cp = DfGetEditBoxText(db, DF_ID_SEARCHFOR);
53 BOOL rpl = TRUE, FoundOne = FALSE;
54
55 while (rpl == TRUE && cp != NULL && *cp) {
56 rpl = Replacing ?
57 DfCheckBoxSetting(&ReplaceTextDB, DF_ID_REPLACEALL) :
58 FALSE;
59 if (DfTextBlockMarked(wnd)) {
60 DfClearTextBlock(wnd);
61 DfSendMessage(wnd, DFM_PAINT, 0, 0);
62 }
63 /* search for a match starting at DfCursor position */
64 cp1 = DfCurrChar;
65 if (incr)
66 cp1++; /* start past the last hit */
67 /* --- compare at each character position --- */
68 while (*cp1) {
69 s1 = cp;
70 s2 = cp1;
71 while (*s1 && *s1 != '\n') {
72 if (SearchCmp(*s1, *s2))
73 break;
74 s1++, s2++;
75 }
76 if (*s1 == '\0' || *s1 == '\n')
77 break;
78 cp1++;
79 }
80 if (s1 != NULL && (*s1 == 0 || *s1 == '\n')) {
81 /* ----- match at *cp1 ------- */
82 FoundOne = TRUE;
83
84 /* mark a block at beginning of matching text */
85 wnd->BlkEndLine = DfTextLineNumber(wnd, s2);
86 wnd->BlkBegLine = DfTextLineNumber(wnd, cp1);
87 if (wnd->BlkEndLine < wnd->BlkBegLine)
88 wnd->BlkEndLine = wnd->BlkBegLine;
89 wnd->BlkEndCol =
90 (int)((int)s2 - (int)DfTextLine(wnd, wnd->BlkEndLine));
91 wnd->BlkBegCol =
92 (int)((int)cp1 - (int)DfTextLine(wnd, wnd->BlkBegLine));
93
94 /* position the DfCursor at the matching text */
95 wnd->CurrCol = wnd->BlkBegCol;
96 wnd->CurrLine = wnd->BlkBegLine;
97 wnd->WndRow = wnd->CurrLine - wnd->wtop;
98
99 /* align the window scroll to matching text */
100 if (DfWndCol > DfClientWidth(wnd)-1)
101 wnd->wleft = wnd->CurrCol;
102 if (wnd->WndRow > DfClientHeight(wnd)-1) {
103 wnd->wtop = wnd->CurrLine;
104 wnd->WndRow = 0;
105 }
106
107 DfSendMessage(wnd, DFM_PAINT, 0, 0);
108 DfSendMessage(wnd, DFM_KEYBOARD_CURSOR,
109 DfWndCol, wnd->WndRow);
110
111 if (Replacing) {
112 if (rpl || DfYesNoBox("Replace the text?")) {
113 replacetext(wnd, cp1, db);
114 wnd->TextChanged = TRUE;
115 DfBuildTextPointers(wnd);
116 }
117 if (rpl) {
118 incr = TRUE;
119 continue;
120 }
121 DfClearTextBlock(wnd);
122 DfSendMessage(wnd, DFM_PAINT, 0, 0);
123 }
124 return;
125 }
126 break;
127 }
128 if (!FoundOne)
129 DfMessageBox("Search/Replace Text", "No match found");
130 }
131
132 /* ------- search for the occurrance of a string,
133 replace it with a specified string ------- */
134 void DfReplaceText(DFWINDOW wnd)
135 {
136 Replacing = TRUE;
137 if (CheckCase)
138 DfSetCheckBox(&ReplaceTextDB, DF_ID_MATCHCASE);
139 if (DfDialogBox(NULL, &ReplaceTextDB, TRUE, NULL)) {
140 CheckCase=DfCheckBoxSetting(&ReplaceTextDB,DF_ID_MATCHCASE);
141 SearchTextBox(wnd, FALSE);
142 }
143 }
144
145 /* ------- search for the first occurrance of a string ------ */
146 void DfSearchText(DFWINDOW wnd)
147 {
148 Replacing = FALSE;
149 if (CheckCase)
150 DfSetCheckBox(&SearchTextDB, DF_ID_MATCHCASE);
151 if (DfDialogBox(NULL, &SearchTextDB, TRUE, NULL)) {
152 CheckCase=DfCheckBoxSetting(&SearchTextDB,DF_ID_MATCHCASE);
153 SearchTextBox(wnd, FALSE);
154 }
155 }
156
157 /* ------- search for the next occurrance of a string ------- */
158 void DfSearchNext(DFWINDOW wnd)
159 {
160 SearchTextBox(wnd, TRUE);
161 }
162
163 /* EOF */