pattern.c
上传用户:jlteech
上传日期:2007-01-06
资源大小:349k
文件大小:7k
源码类别:

压缩解压

开发平台:

Visual C++

  1. /* Author Mike White, 1996. Based on original WizUnZip code by
  2.  * Robert Heath.
  3.  */
  4. #include <stdio.h>
  5. #include <windows.h>
  6. #include <string.h>
  7. #include "pattern.h"
  8. #include "wiz.h"
  9. #include "unzipwindlldecs.h"
  10. #include "helpids.h"
  11. char __based(__segname("STRINGS_TEXT")) szNoMatch[] =
  12.             "No entry matches the pattern!";
  13. /****************************************************************************
  14.     FUNCTION: PatternSelectProc(HWND, unsigned, WPARAM, LPARAM)
  15.     PURPOSE:  Processes messages for "Select Files by Pattern" dialog box
  16.     MESSAGES:
  17.     WM_INITDIALOG - initialize dialog box
  18.     WM_COMMAND    - Input received
  19. ****************************************************************************/
  20. #ifdef __BORLANDC__
  21. #pragma warn -par
  22. #endif
  23. BOOL WINAPI
  24. PatternSelectProc(HWND hwndDlg, WORD wMessage, WPARAM wParam, LPARAM lParam)
  25. {
  26. static HWND hSelect, hDeselect, hPattern;
  27. PSTR psLBEntry = NULL;  /* list box entry */
  28. PSTR psPatternBuf = NULL;   /* pattern collection   */
  29. DWORD cListItems;   /* no. items in listbox   */
  30. UINT Fname_Inx;      /* index into LB entry   */
  31. BOOL fSelect;      /* we're selecting if TRUE */
  32. int nPatternLength;   /* length of data in pattern edit window */
  33. char *p;
  34.    switch (wMessage) {
  35.    case WM_INITDIALOG:
  36.       hSelect = GetDlgItem(hwndDlg, IDOK);
  37.       hDeselect = GetDlgItem(hwndDlg, IDM_PATTERN_DESELECT);
  38.       hPattern = GetDlgItem(hwndDlg, IDM_PATTERN_PATTERN);
  39.       CenterDialog(GetParent(hwndDlg), hwndDlg);
  40.       return TRUE;
  41.    case WM_COMMAND:
  42.       switch (LOWORD(wParam)) {
  43.         case IDM_PATTERN_PATTERN:
  44.             if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE)
  45.                {
  46.                if ((nPatternLength = GetWindowTextLength(hPattern))==1)
  47.                   {
  48.          /* Enable or disable buttons depending on fullness of
  49.           * "Suffix" box.
  50.           */
  51.                   BOOL fButtonState = TRUE ;
  52.                   WinAssert(hSelect);
  53.                   EnableWindow(hSelect, fButtonState);
  54.                   WinAssert(hDeselect);
  55.                   EnableWindow(hDeselect, fButtonState);
  56.                   }
  57.                if (nPatternLength == 0)
  58.                   {
  59.                   BOOL fButtonState = FALSE;
  60.                   WinAssert(hSelect);
  61.                   EnableWindow(hSelect, fButtonState);
  62.                   WinAssert(hDeselect);
  63.                   EnableWindow(hDeselect, fButtonState);
  64.                   }
  65.                }
  66.             break;
  67.       case IDOK: /* Select items using pattern */
  68.       case IDM_PATTERN_DESELECT:
  69.          fSelect = (BOOL)(LOWORD(wParam) == IDOK);
  70.          /* Be sure that listbox contains at least 1 item,
  71.           * that a pattern exists (is non-null), and
  72.           * that we can buffer them.
  73.           */
  74. #ifndef WIN32
  75.          if ((cListItems = SendMessage(hWndList, (UINT)LB_GETCOUNT, 0, 0L)) != (UINT)LB_ERR &&
  76.               cListItems >= 1U &&
  77. #else
  78.          if ((cListItems = ListView_GetItemCount(hWndList)) != 0 &&
  79. #endif
  80.             (nPatternLength = GetWindowTextLength(hPattern)) >= 1 &&
  81.             (psPatternBuf =   /* get a buffer   for the file name               */
  82.               (PSTR)GlobalAlloc(GMEM_FIXED, nPatternLength+1)) != NULL &&
  83.            (psLBEntry =   /* get a buffer   for the file name               */
  84.               (PSTR)GlobalAlloc(GMEM_FIXED, PATH_MAX+LONG_FORM_FNAME_INX)) != NULL &&
  85.             GetWindowText(hPattern, psPatternBuf, nPatternLength+1) > 0)
  86.          {
  87.          DWORD cItemsSelected = 0; /* no. "hits" during pattern search   */
  88.          UINT uLBInx;
  89.          LPSTR psPattern;   /* points to any one pattern in buffer      */
  90.          static char DELIMS[] = " t,"; /* delimiters, mostly whitespace */
  91. #ifndef __BORLANDC__
  92.             _strlwr(psPatternBuf);   /* convert pattern to lower case   */
  93. #else
  94.             strlwr(psPatternBuf);   /* convert pattern to lower case   */
  95. #endif
  96.             for (p = psPatternBuf; *p; p++)
  97.                 if (*p == '\')
  98.                     *p = '/';
  99. #ifndef WIN32
  100.             Fname_Inx = (UINT)(uf.fFormatLong ? LONG_FORM_FNAME_INX : SHORT_FORM_FNAME_INX);
  101. #else
  102.             Fname_Inx = 1;
  103. #endif
  104.             /* march through list of patterns in edit field            */
  105. #if (defined MSC) && (!defined WIN32)
  106.             for (psPattern = _fstrtok(psPatternBuf, DELIMS);
  107.                 psPattern != NULL; psPattern = _fstrtok(NULL, DELIMS))
  108. #else
  109.             for (psPattern = (PSTR)strtok(psPatternBuf, DELIMS);
  110.                 psPattern != NULL; psPattern = (PSTR)strtok(NULL, DELIMS))
  111. #endif
  112.             {
  113.                /* March thru listbox matching the complete path with every entry.
  114.                 * Note: unzip's match() function probably won't work for national
  115.                 * characters above the ASCII range.
  116.                 */
  117.                for (uLBInx = 0; uLBInx < cListItems; uLBInx++)
  118.                {
  119.                   /* Retrieve listbox entry                        */
  120. #ifndef WIN32
  121.                    if (SendMessage(hWndList, LB_GETTEXT, uLBInx, (LPARAM)((LPSTR)psLBEntry)) >
  122.                      (LRESULT)Fname_Inx)
  123. #else
  124.                    ListView_GetItemText(hWndList, uLBInx, 0, psLBEntry, PATH_MAX);
  125. #endif
  126.                   {
  127. #ifndef __BORLANDC__
  128.                      _strlwr(&psLBEntry[Fname_Inx]); /* convert filename to lower case */
  129. #else
  130.                      strlwr(&psLBEntry[Fname_Inx]); /* convert filename to lower case */
  131. #endif
  132.                      /* Use UnZip's match() function                  */
  133.                      if (UzpMatch(&psLBEntry[Fname_Inx], psPattern, TRUE))
  134.                      {
  135. #ifndef WIN32
  136.                         SendMessage(hWndList, LB_SETSEL, (WPARAM)fSelect,
  137.                                  MAKELPARAM(uLBInx,0));
  138. #else
  139.                         ListViewSetSel(uLBInx, fSelect);
  140. #endif
  141.                         cItemsSelected++;
  142.                      }
  143.                   }
  144.                }
  145.             }
  146.             if (!cItemsSelected)   /* If no pattern match               */
  147.             {
  148.                MessageBox(hwndDlg, szNoMatch, szAppName, MB_OK | MB_ICONASTERISK);
  149.             }
  150.             else /* one or more items were selected */
  151.             {
  152.                UpdateButtons(); /* turn main push buttons on or off */
  153.             }
  154.          }
  155.          if (psLBEntry)
  156.             GlobalFree((HGLOBAL)psLBEntry);
  157.          if (psPatternBuf)
  158.             GlobalFree((HGLOBAL)psPatternBuf);
  159.          break;
  160.       case IDCANCEL:
  161.          PostMessage(hwndDlg, WM_CLOSE, 0, 0L);
  162.          break;
  163.       case IDM_PATTERN_HELP:
  164.             WinHelp(hwndDlg,szHelpFileName,HELP_CONTEXT, (DWORD)(HELPID_SELECT_BY_PATTERN));
  165.          break;
  166.       }
  167.       return TRUE;
  168.    case WM_CLOSE:
  169.       DestroyWindow(hwndDlg);
  170.       hPatternSelectDlg = 0; /* flag dialog inactive   */
  171.       return TRUE;
  172.    }
  173.    return FALSE;
  174. }
  175. #ifdef __BORLANDC__
  176. #pragma warn .par
  177. #endif