home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
CSSRC
/
TEDSRCH.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-12-10
|
3KB
|
151 lines
/*
tedsrch.c
% ted_Search
text editing search functions
C-scape 3.2
Copyright (c) 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
8/10/88 jdc created
2/08/89 jmd added args
3/24/89 jmd added sed_ macros
5/23/89 jdc made tb_setcursor menu_setcursor
12/19/89 jmdc added ted insensitive search
3/28/90 jmd ansi-fied
12/10/90 jdc added check for ted_GotoPosition's return value
*/
#include "ted.h"
#include <ctype.h>
typedef struct _scomp {
char *s;
char *pattern;
int cmp_len;
int match_len;
int more;
int mode;
} scomp_struct;
OSTATIC int search_compare(scomp_struct *scs);
boolean ted_Search(sed_type sed, char *pattern, int mode)
{
tb_type tb;
long cursor;
scomp_struct scs;
bbpeek_struct bp;
int srch, i, done, dlen, row, col;
tb = sed_GetTextbuf(sed);
bp.b = tb->bbc->b;
bp.off = bp.b->off + tb_GetCursor(tb);
bp.len = tb->size;
cursor = tb->cursor;
scs.pattern = pattern;
scs.more = 0;
scs.mode = mode;
for (done = FALSE; !done;) {
for (i = 0, dlen = bbpeek(&bp); i < dlen;) {
scs.s = bp.p + i;
scs.cmp_len = dlen - i;
if ((srch = search_compare(&scs)) == TED_MATCH) {
done = TRUE;
break;
}
else if (srch == TED_NOMATCH) {
if (mode & TED_FORWARD) {
i++;
bp.off++;
if (scs.more > 0) {
bp.off -= scs.more;
scs.more = 0;
break;
}
if (i >= dlen) {
break;
}
}
else {
i--;
bp.off--;
if (scs.more > 0) {
bp.off -= scs.more;
scs.more = 0;
break;
}
if (bp.off < 0) {
break;
}
}
}
else { /* TED_MORE */
bp.off += dlen - i;
break;
}
}
if (dlen <= 0) {
return(FALSE);
}
cursor += i;
}
if (mode & TED_AFTER) {
cursor += scs.match_len;
}
else if (mode & TED_BEFORE) {
cursor--;
}
row = tb_GetRow(tb);
col = tb_GetCol(tb);
tb->cursor = cursor;
menu_setcursor(sed_GetMenu(sed));
if (!ted_GotoPosition(sed, tb_GetRow(tb), tb_GetCol(tb))) {
tb_FindPosition(tb, row, col);
}
return(TRUE);
}
static int search_compare(scomp_struct *scs)
{
char *p, *s;
int len, more;
for (p = scs->pattern + scs->more, s = scs->s, len = scs->cmp_len,
more = 0;
;
p++, s++, more++, len--) {
if (*p == '\0') {
scs->match_len = scs->more + more;
return(TED_MATCH);
}
else if (len == 0) {
scs->more += more;
return(TED_MORE);
}
else if ((*p == *s) ||
((scs->mode & TED_INSENSITIVE) && otolower(*p) == otolower(*s))) {
continue;
}
else {
return(TED_NOMATCH);
}
}
}