Annotation of mstools/samples/sdktools/porttool/ptfind.c, revision 1.1.1.1

1.1       root        1: 
                      2: /******************************************************************************\
                      3: *       This is a part of the Microsoft Source Code Samples. 
                      4: *       Copyright (C) 1993 Microsoft Corporation.
                      5: *       All rights reserved. 
                      6: *       This source code is only intended as a supplement to 
                      7: *       Microsoft Development Tools and/or WinHelp documentation.
                      8: *       See these sources for detailed information regarding the 
                      9: *       Microsoft samples programs.
                     10: \******************************************************************************/
                     11: 
                     12: #include "PortTool.h"
                     13: 
                     14: /* global search string */
                     15: char   lpszSearch[MAXSEARCHSTRING+1] = "";
                     16: HWND   hDlgSearch;
                     17: 
                     18: FINDREPLACE    frSearch;
                     19: 
                     20: 
                     21: /* compare two substrings */
                     22: BOOL WINAPI RealSlowCompare (WORD, char *, char *);
                     23: BOOL WINAPI RealSlowCompare (
                     24:     WORD    wCase,
                     25:     char    *lpszSubject,
                     26:     char    *lpszTarget)
                     27: {
                     28:     if (wCase)
                     29:        {
                     30:        while (*lpszTarget)
                     31:            if (*lpszTarget++ != *lpszSubject++)
                     32:                return FALSE;
                     33:        }
                     34:     else
                     35:        {
                     36:        /* convert to lower case before comparing */
                     37:        CharLower ((char *)lpszTarget);
                     38:        while (*lpszTarget)
                     39:            if (*lpszTarget++ != (CHAR)(DWORD)CharLower ((char *)(DWORD)(BYTE)*lpszSubject++))
                     40:                return FALSE;
                     41:        }
                     42:     return TRUE;
                     43: }
                     44: 
                     45: 
                     46: 
                     47: 
                     48: /* invoke the common search/replace dialog */
                     49: BOOL WINAPI FindDialog (
                     50:     HWND    hWnd,
                     51:     WORD    wCase,
                     52:     WORD    wDir,
                     53:     char    *lpszInit)
                     54: {
                     55:     frSearch.lStructSize      = sizeof (FINDREPLACE);
                     56:     frSearch.hwndOwner       = hWnd;
                     57:     frSearch.hInstance       = (HANDLE)GetWindowLong (hWnd, GWL_HINSTANCE);
                     58:     frSearch.Flags           = 0;
                     59: 
                     60:     /* if wCase, case sensitive */
                     61:     if (wCase)
                     62:        frSearch.Flags        |= FR_MATCHCASE;
                     63:     /* if wDir, search forward */
                     64:     if (wDir)
                     65:        frSearch.Flags        |= FR_DOWN;
                     66: 
                     67:     frSearch.lpstrFindWhat    = lpszInit;
                     68:     frSearch.lpstrReplaceWith = NULL;
                     69:     frSearch.wFindWhatLen     = MAXSEARCHSTRING+1;
                     70:     frSearch.wReplaceWithLen  = 0;
                     71:     frSearch.lCustData       = 0;
                     72:     frSearch.lpfnHook        = NULL;
                     73:     frSearch.lpTemplateName   = NULL;
                     74: 
                     75:     /* call common search dialog */
                     76:     if (hDlgSearch = FindText (&frSearch))
                     77:        return TRUE;
                     78:     else
                     79:        return FALSE;
                     80: }
                     81: 
                     82: 
                     83: 
                     84: /* perform the actual text searching in the edit control data */
                     85: BOOL WINAPI LocateText (
                     86:     HWND       hWnd,
                     87:     WORD       wCase,
                     88:     WORD       wDir,
                     89:     char       *lpszStr)
                     90: {
                     91:     UINT    uBegSel, uEndSel, uOrgBegSel, uOrgEndSel;
                     92:     HANDLE  hEditData;
                     93:     HWND    hWndEdit = (HANDLE)GetWindowLong (hWnd, WL_HWNDEDIT);
                     94:     char    *lpEditData;
                     95:     UINT    uLen;
                     96:     int     nStrLen = strlen (lpszStr);
                     97:     int     nChars;
                     98: 
                     99:     /* test for valid string */
                    100:     if (!*lpszStr)
                    101:        return FALSE;
                    102: 
                    103:     /* locate beginning of selected text */
                    104:     SendMessage (hWndEdit, EM_GETSEL, (UINT)&uBegSel, (UINT)&uEndSel);
                    105:     uOrgBegSel = uBegSel;
                    106:     uOrgEndSel = uEndSel;
                    107: 
                    108:     /* get length of the text */
                    109:     uLen = (UINT)SendMessage (hWndEdit, WM_GETTEXTLENGTH, 0, 0);
                    110: 
                    111:     /* Get handle to edit text data and lock it */
                    112:     hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0);
                    113:     lpEditData = LocalLock (hEditData);
                    114: 
                    115:     /* advance starting point past selection one char */
                    116:     lpEditData += (wDir ? ++uBegSel : --uBegSel);
                    117: 
                    118:     /* count characters to search (either forward to end of file or back to beginning) */
                    119:     if (wDir)
                    120:        nChars = (int)(uLen - uBegSel + 1 - nStrLen);
                    121:     else
                    122:        nChars = (int)uBegSel;
                    123: 
                    124:     /* compare character by character for a substring match */
                    125:     while (nChars >= nStrLen)
                    126:        {
                    127:        /* compare this substring for a match */
                    128:        if (RealSlowCompare (wCase, lpEditData, lpszStr))
                    129:            {
                    130:            /* string found, cleanup and go away */
                    131:            LocalUnlock(hEditData);
                    132: 
                    133:            /* scroll parent edit control and select offending text */
                    134:            SendMessage (hWndEdit, EM_LINESCROLL, 0,
                    135:                SendMessage (hWndEdit, EM_LINEFROMCHAR, uBegSel, 0) -
                    136:                SendMessage (hWndEdit, EM_GETFIRSTVISIBLELINE, 0, 0));
                    137: 
                    138:            /* Select the located string */
                    139:            uEndSel = uBegSel + nStrLen;
                    140:            SendMessage(hWndEdit, EM_SETSEL, uBegSel, uEndSel);
                    141: 
                    142:            /* return success */
                    143:            return TRUE;
                    144:            }
                    145: 
                    146:        nChars--;
                    147: 
                    148:        /* increment/decrement start position by 1 */
                    149:        lpEditData += (wDir ? 1 : -1);
                    150:        wDir ? uBegSel++ : uBegSel--;
                    151:        }
                    152: 
                    153:     LocalUnlock (hEditData);
                    154:     SendMessage (hWndEdit, EM_SETSEL, uOrgBegSel, uOrgEndSel);
                    155: 
                    156:     /* return failed search  */
                    157:     return FALSE;
                    158: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.