2000-03-05 19:58:08 +00:00
|
|
|
/* ---------------- search.c ------------- */
|
|
|
|
#include "dflat.h"
|
|
|
|
|
2003-06-19 02:48:13 +00:00
|
|
|
extern DF_DBOX SearchTextDB;
|
|
|
|
extern DF_DBOX ReplaceTextDB;
|
2000-03-05 19:58:08 +00:00
|
|
|
static int CheckCase = TRUE;
|
|
|
|
static int Replacing = FALSE;
|
|
|
|
|
|
|
|
/* - case-insensitive, white-space-normalized char compare - */
|
|
|
|
static BOOL SearchCmp(int a, int b)
|
|
|
|
{
|
|
|
|
if (b == '\n')
|
|
|
|
b = ' ';
|
|
|
|
if (CheckCase)
|
|
|
|
return a != b;
|
|
|
|
return tolower(a) != tolower(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----- replace a matching block of text ----- */
|
2003-06-19 02:48:13 +00:00
|
|
|
static void replacetext(DFWINDOW wnd, char *cp1, DF_DBOX *db)
|
2000-03-05 19:58:08 +00:00
|
|
|
{
|
2003-06-19 02:48:13 +00:00
|
|
|
char *cr = DfGetEditBoxText(db, DF_ID_REPLACEWITH);
|
|
|
|
char *cp = DfGetEditBoxText(db, DF_ID_SEARCHFOR);
|
2000-03-05 19:58:08 +00:00
|
|
|
int oldlen = strlen(cp); /* length of text being replaced */
|
|
|
|
int newlen = strlen(cr); /* length of replacing text */
|
|
|
|
int dif;
|
|
|
|
if (oldlen < newlen) {
|
|
|
|
/* ---- new text expands text size ---- */
|
|
|
|
dif = newlen-oldlen;
|
|
|
|
if (wnd->textlen < strlen(wnd->text)+dif) {
|
|
|
|
/* ---- need to reallocate the text buffer ---- */
|
|
|
|
int offset = (int)((int)cp1-(int)wnd->text);
|
|
|
|
wnd->textlen += dif;
|
2003-06-19 02:48:13 +00:00
|
|
|
wnd->text = DfRealloc(wnd->text, wnd->textlen+2);
|
2000-03-05 19:58:08 +00:00
|
|
|
cp1 = wnd->text + offset;
|
|
|
|
}
|
|
|
|
memmove(cp1+dif, cp1, strlen(cp1)+1);
|
|
|
|
}
|
|
|
|
else if (oldlen > newlen) {
|
|
|
|
/* ---- new text collapses text size ---- */
|
|
|
|
dif = oldlen-newlen;
|
|
|
|
memmove(cp1, cp1+dif, strlen(cp1)+1);
|
|
|
|
}
|
|
|
|
strncpy(cp1, cr, newlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------- search for the occurrance of a string ------- */
|
|
|
|
static void SearchTextBox(DFWINDOW wnd, int incr)
|
|
|
|
{
|
2005-09-16 18:00:49 +00:00
|
|
|
char *s1 , *s2, *cp1;
|
|
|
|
s1 = s2 = cp1 = NULL;
|
2003-06-19 02:48:13 +00:00
|
|
|
DF_DBOX *db = Replacing ? &ReplaceTextDB : &SearchTextDB;
|
|
|
|
char *cp = DfGetEditBoxText(db, DF_ID_SEARCHFOR);
|
2000-03-05 19:58:08 +00:00
|
|
|
BOOL rpl = TRUE, FoundOne = FALSE;
|
|
|
|
|
|
|
|
while (rpl == TRUE && cp != NULL && *cp) {
|
|
|
|
rpl = Replacing ?
|
2003-06-19 02:48:13 +00:00
|
|
|
DfCheckBoxSetting(&ReplaceTextDB, DF_ID_REPLACEALL) :
|
2000-03-05 19:58:08 +00:00
|
|
|
FALSE;
|
2003-06-19 02:48:13 +00:00
|
|
|
if (DfTextBlockMarked(wnd)) {
|
|
|
|
DfClearTextBlock(wnd);
|
|
|
|
DfSendMessage(wnd, DFM_PAINT, 0, 0);
|
2000-03-05 19:58:08 +00:00
|
|
|
}
|
2003-06-19 02:48:13 +00:00
|
|
|
/* search for a match starting at DfCursor position */
|
|
|
|
cp1 = DfCurrChar;
|
2000-03-05 19:58:08 +00:00
|
|
|
if (incr)
|
|
|
|
cp1++; /* start past the last hit */
|
|
|
|
/* --- compare at each character position --- */
|
|
|
|
while (*cp1) {
|
|
|
|
s1 = cp;
|
|
|
|
s2 = cp1;
|
|
|
|
while (*s1 && *s1 != '\n') {
|
|
|
|
if (SearchCmp(*s1, *s2))
|
|
|
|
break;
|
|
|
|
s1++, s2++;
|
|
|
|
}
|
|
|
|
if (*s1 == '\0' || *s1 == '\n')
|
|
|
|
break;
|
|
|
|
cp1++;
|
|
|
|
}
|
|
|
|
if (s1 != NULL && (*s1 == 0 || *s1 == '\n')) {
|
|
|
|
/* ----- match at *cp1 ------- */
|
|
|
|
FoundOne = TRUE;
|
|
|
|
|
|
|
|
/* mark a block at beginning of matching text */
|
2003-06-19 02:48:13 +00:00
|
|
|
wnd->BlkEndLine = DfTextLineNumber(wnd, s2);
|
|
|
|
wnd->BlkBegLine = DfTextLineNumber(wnd, cp1);
|
2000-03-05 19:58:08 +00:00
|
|
|
if (wnd->BlkEndLine < wnd->BlkBegLine)
|
|
|
|
wnd->BlkEndLine = wnd->BlkBegLine;
|
|
|
|
wnd->BlkEndCol =
|
2003-06-19 02:48:13 +00:00
|
|
|
(int)((int)s2 - (int)DfTextLine(wnd, wnd->BlkEndLine));
|
2000-03-05 19:58:08 +00:00
|
|
|
wnd->BlkBegCol =
|
2003-06-19 02:48:13 +00:00
|
|
|
(int)((int)cp1 - (int)DfTextLine(wnd, wnd->BlkBegLine));
|
2000-03-05 19:58:08 +00:00
|
|
|
|
2003-06-19 02:48:13 +00:00
|
|
|
/* position the DfCursor at the matching text */
|
2000-03-05 19:58:08 +00:00
|
|
|
wnd->CurrCol = wnd->BlkBegCol;
|
|
|
|
wnd->CurrLine = wnd->BlkBegLine;
|
|
|
|
wnd->WndRow = wnd->CurrLine - wnd->wtop;
|
|
|
|
|
|
|
|
/* align the window scroll to matching text */
|
2003-06-19 02:48:13 +00:00
|
|
|
if (DfWndCol > DfClientWidth(wnd)-1)
|
2000-03-05 19:58:08 +00:00
|
|
|
wnd->wleft = wnd->CurrCol;
|
2003-06-19 02:48:13 +00:00
|
|
|
if (wnd->WndRow > DfClientHeight(wnd)-1) {
|
2000-03-05 19:58:08 +00:00
|
|
|
wnd->wtop = wnd->CurrLine;
|
|
|
|
wnd->WndRow = 0;
|
|
|
|
}
|
|
|
|
|
2003-06-19 02:48:13 +00:00
|
|
|
DfSendMessage(wnd, DFM_PAINT, 0, 0);
|
|
|
|
DfSendMessage(wnd, DFM_KEYBOARD_CURSOR,
|
|
|
|
DfWndCol, wnd->WndRow);
|
2000-03-05 19:58:08 +00:00
|
|
|
|
|
|
|
if (Replacing) {
|
|
|
|
if (rpl || DfYesNoBox("Replace the text?")) {
|
|
|
|
replacetext(wnd, cp1, db);
|
|
|
|
wnd->TextChanged = TRUE;
|
2003-06-19 02:48:13 +00:00
|
|
|
DfBuildTextPointers(wnd);
|
2000-03-05 19:58:08 +00:00
|
|
|
}
|
|
|
|
if (rpl) {
|
|
|
|
incr = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
2003-06-19 02:48:13 +00:00
|
|
|
DfClearTextBlock(wnd);
|
|
|
|
DfSendMessage(wnd, DFM_PAINT, 0, 0);
|
2000-03-05 19:58:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!FoundOne)
|
|
|
|
DfMessageBox("Search/Replace Text", "No match found");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------- search for the occurrance of a string,
|
|
|
|
replace it with a specified string ------- */
|
|
|
|
void DfReplaceText(DFWINDOW wnd)
|
|
|
|
{
|
|
|
|
Replacing = TRUE;
|
|
|
|
if (CheckCase)
|
2003-06-19 02:48:13 +00:00
|
|
|
DfSetCheckBox(&ReplaceTextDB, DF_ID_MATCHCASE);
|
2000-03-05 19:58:08 +00:00
|
|
|
if (DfDialogBox(NULL, &ReplaceTextDB, TRUE, NULL)) {
|
2003-06-19 02:48:13 +00:00
|
|
|
CheckCase=DfCheckBoxSetting(&ReplaceTextDB,DF_ID_MATCHCASE);
|
2000-03-05 19:58:08 +00:00
|
|
|
SearchTextBox(wnd, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------- search for the first occurrance of a string ------ */
|
|
|
|
void DfSearchText(DFWINDOW wnd)
|
|
|
|
{
|
|
|
|
Replacing = FALSE;
|
|
|
|
if (CheckCase)
|
2003-06-19 02:48:13 +00:00
|
|
|
DfSetCheckBox(&SearchTextDB, DF_ID_MATCHCASE);
|
2000-03-05 19:58:08 +00:00
|
|
|
if (DfDialogBox(NULL, &SearchTextDB, TRUE, NULL)) {
|
2003-06-19 02:48:13 +00:00
|
|
|
CheckCase=DfCheckBoxSetting(&SearchTextDB,DF_ID_MATCHCASE);
|
2000-03-05 19:58:08 +00:00
|
|
|
SearchTextBox(wnd, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------- search for the next occurrance of a string ------- */
|
|
|
|
void DfSearchNext(DFWINDOW wnd)
|
|
|
|
{
|
|
|
|
SearchTextBox(wnd, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|