home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / dflt14.zip / SEARCH.C < prev    next >
Text File  |  1992-08-02  |  5KB  |  162 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.  
  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(WINDOW wnd, char *cp1, DBOX *db)
  21. {
  22.     char *cr = GetEditBoxText(db, ID_REPLACEWITH);
  23.     char *cp = GetEditBoxText(db, 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)(cp1-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(WINDOW wnd, int incr)
  49. {
  50.     char *s1 = NULL, *s2, *cp1;
  51.     DBOX *db = Replacing ? &ReplaceTextDB : &SearchTextDB;
  52.     char *cp = GetEditBoxText(db, ID_SEARCHFOR);
  53.     BOOL rpl = TRUE, FoundOne = FALSE;
  54.  
  55.     while (rpl == TRUE && cp != NULL && *cp)    {
  56.         rpl = Replacing ?
  57.                 CheckBoxSetting(&ReplaceTextDB, ID_REPLACEALL) :
  58.                 FALSE;
  59.         if (TextBlockMarked(wnd))    {
  60.             ClearTextBlock(wnd);
  61.             SendMessage(wnd, PAINT, 0, 0);
  62.         }
  63.         /* search for a match starting at cursor position */
  64.         cp1 = CurrChar;
  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 = TextLineNumber(wnd, s2);
  86.             wnd->BlkBegLine = TextLineNumber(wnd, cp1);
  87.             if (wnd->BlkEndLine < wnd->BlkBegLine)
  88.                 wnd->BlkEndLine = wnd->BlkBegLine;
  89.             wnd->BlkEndCol =
  90.                 (int)(s2 - TextLine(wnd, wnd->BlkEndLine));
  91.             wnd->BlkBegCol =
  92.                 (int)(cp1 - TextLine(wnd, wnd->BlkBegLine));
  93.  
  94.             /* position the cursor 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 (WndCol > ClientWidth(wnd)-1)
  101.                 wnd->wleft = wnd->CurrCol;
  102.             if (wnd->WndRow > ClientHeight(wnd)-1)    {
  103.                 wnd->wtop = wnd->CurrLine;
  104.                 wnd->WndRow = 0;
  105.             }
  106.  
  107.             SendMessage(wnd, PAINT, 0, 0);
  108.             SendMessage(wnd, KEYBOARD_CURSOR,
  109.                 WndCol, wnd->WndRow);
  110.  
  111.             if (Replacing)    {
  112.                 if (rpl || YesNoBox("Replace the text?"))  {
  113.                     replacetext(wnd, cp1, db);
  114.                     wnd->TextChanged = TRUE;
  115.                     BuildTextPointers(wnd);
  116.                 }
  117.                 if (rpl)    {
  118.                     incr = TRUE;
  119.                     continue;
  120.                 }
  121.                 ClearTextBlock(wnd);
  122.                 SendMessage(wnd, PAINT, 0, 0);
  123.             }
  124.             return;
  125.         }
  126.         break;
  127.     }
  128.     if (!FoundOne)
  129.         MessageBox("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 ReplaceText(WINDOW wnd)
  135. {
  136.     Replacing = TRUE;
  137.     if (CheckCase)
  138.         SetCheckBox(&ReplaceTextDB, ID_MATCHCASE);
  139.     if (DialogBox(NULL, &ReplaceTextDB, TRUE, NULL))    {
  140.         CheckCase=CheckBoxSetting(&ReplaceTextDB,ID_MATCHCASE);
  141.         SearchTextBox(wnd, FALSE);
  142.     }
  143. }
  144.  
  145. /* ------- search for the first occurrance of a string ------ */
  146. void SearchText(WINDOW wnd)
  147. {
  148.     Replacing = FALSE;
  149.     if (CheckCase)
  150.         SetCheckBox(&SearchTextDB, ID_MATCHCASE);
  151.     if (DialogBox(NULL, &SearchTextDB, TRUE, NULL))    {
  152.         CheckCase=CheckBoxSetting(&SearchTextDB,ID_MATCHCASE);
  153.         SearchTextBox(wnd, FALSE);
  154.     }
  155. }
  156.  
  157. /* ------- search for the next occurrance of a string ------- */
  158. void SearchNext(WINDOW wnd)
  159. {
  160.     SearchTextBox(wnd, TRUE);
  161. }
  162.