home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1992 / 06 / dflt12 / search.c < prev    next >
Text File  |  1992-03-26  |  5KB  |  159 lines

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