home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / dflt20.zip / SEARCH.C < prev    next >
C/C++ Source or Header  |  1994-03-05  |  6KB  |  170 lines

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