Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.0 KiB

  1. /*
  2. +-------------------------------------------------------------------------+
  3. | MDI Text File Viewer - Text Search Routines |
  4. +-------------------------------------------------------------------------+
  5. | (c) Copyright 1994 |
  6. | Microsoft Corp. |
  7. | All rights reserved |
  8. | |
  9. | Program : [FVFind.c] |
  10. | Programmer : Arthur Hanson |
  11. | Original Program Date : [Jul 27, 1993 |
  12. | Last Update : [Jul 30, 1993] Time : 18:30 |
  13. | |
  14. | Version: 0.10 |
  15. | |
  16. | Description: |
  17. | |
  18. | History: |
  19. | arth Jul 27, 1993 0.10 Original Version. |
  20. | |
  21. +-------------------------------------------------------------------------+
  22. */
  23. #include "LogView.h"
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #undef HIWORD
  27. #undef LOWORD
  28. #define HIWORD(l) (((WORD*)&(l))[1])
  29. #define LOWORD(l) (((WORD*)&(l))[0])
  30. BOOL fCase = FALSE; // Turn case sensitivity off
  31. CHAR szSearch[160] = ""; // Initialize search string
  32. extern HWND hDlgFind; /* handle to modeless FindText window */
  33. LPTSTR ReverseScan(
  34. LPTSTR lpSource,
  35. LPTSTR lpLast,
  36. LPTSTR lpSearch,
  37. BOOL fCaseSensitive ) {
  38. int iLen = lstrlen(lpSearch);
  39. if (!lpLast)
  40. lpLast = lpSource + lstrlen(lpSource);
  41. do {
  42. if (lpLast == lpSource)
  43. return NULL;
  44. --lpLast;
  45. if (fCaseSensitive) {
  46. if (*lpLast != *lpSearch)
  47. continue;
  48. } else {
  49. if (CharUpper ((LPTSTR)MAKELONG((WORD)*lpLast, 0)) != CharUpper ((LPTSTR)MAKELONG((WORD)*lpSearch, 0)))
  50. continue;
  51. }
  52. if (fCaseSensitive) {
  53. if (!strncmp( lpLast, lpSearch, iLen))
  54. break;
  55. } else {
  56. if (!_strnicmp (lpLast, lpSearch, iLen))
  57. break;
  58. }
  59. } while (TRUE);
  60. return lpLast;
  61. } // ReverseScan
  62. LPTSTR ForwardScan(LPTSTR lpSource, LPTSTR lpSearch, BOOL fCaseSensitive ) {
  63. int iLen = lstrlen(lpSearch);
  64. while (*lpSource) {
  65. if (fCaseSensitive) {
  66. if (*lpSource != *lpSearch) {
  67. lpSource++;
  68. continue;
  69. }
  70. } else {
  71. if (CharUpper ((LPTSTR)MAKELONG((WORD)*lpSource, 0)) != CharUpper ((LPTSTR)MAKELONG((WORD)*lpSearch, 0))) {
  72. lpSource++;
  73. continue;
  74. }
  75. }
  76. if (fCaseSensitive) {
  77. if (!strncmp( lpSource, lpSearch, iLen))
  78. break;
  79. } else {
  80. if (!_strnicmp( lpSource, lpSearch, iLen))
  81. break;
  82. }
  83. lpSource++;
  84. }
  85. return *lpSource ? lpSource : NULL;
  86. } // ForwardScan
  87. // search forward or backward in the edit control text for the given pattern
  88. void FAR Search (TCHAR * szKey) {
  89. HANDLE hText;
  90. TCHAR * pStart, *pMatch;
  91. DWORD_PTR StartIndex, EndIndex;
  92. DWORD SelStart, SelEnd, i;
  93. LRESULT dwSel;
  94. INT_PTR cbText, LineNum;
  95. if (!*szKey)
  96. return;
  97. SetCursor(hWaitCursor);
  98. dwSel= SendMessage(hwndActiveEdit, EM_GETSEL, (WPARAM)&SelStart, (LPARAM)&SelEnd);
  99. /*
  100. * Allocate hText and read edit control text into it.
  101. * Lock hText and fall through to existing code.
  102. */
  103. cbText= SendMessage(hwndActiveEdit, WM_GETTEXTLENGTH, 0, 0L) + 1;
  104. hText= LocalAlloc( LPTR, cbText );
  105. if( !hText ) // quiet exit if not enough memory
  106. return;
  107. if( !(pStart= LocalLock(hText)) ) {
  108. LocalFree(hText);
  109. return;
  110. }
  111. SendMessage(hwndActiveEdit, WM_GETTEXT, cbText, (LPARAM)pStart);
  112. if (fReverse) {
  113. // Get current line number
  114. LineNum= SendMessage(hwndActiveEdit, EM_LINEFROMCHAR, SelStart, 0);
  115. // Get index to start of the line
  116. StartIndex= SendMessage(hwndActiveEdit, EM_LINEINDEX, LineNum, 0);
  117. // Set upper limit for search text
  118. EndIndex= SelStart;
  119. pMatch= NULL;
  120. // Search line by line, from LineNum to 0
  121. i = (DWORD) LineNum;
  122. while (TRUE) {
  123. pMatch= ReverseScan(pStart+StartIndex,pStart+EndIndex,szKey,fCase);
  124. if (pMatch)
  125. break;
  126. // current StartIndex is the upper limit for the next search
  127. EndIndex= StartIndex;
  128. if (i) {
  129. // Get start of the next line
  130. i-- ;
  131. StartIndex= SendMessage(hwndActiveEdit, EM_LINEINDEX, i, 0);
  132. } else
  133. break ;
  134. }
  135. } else {
  136. pMatch= ForwardScan(pStart + SelEnd, szKey, fCase);
  137. }
  138. LocalUnlock(hText);
  139. LocalFree( hText );
  140. SetCursor(hStdCursor);
  141. if (pMatch == NULL) {
  142. TCHAR Message[256], AppName[256] ;
  143. if (!LoadString(hInst, IDS_CANTFINDSTR, Message,
  144. sizeof(Message)/sizeof(Message[0]))) {
  145. Message[0] = 0 ;
  146. }
  147. if (!LoadString(hInst, IDS_APPNAME, AppName,
  148. sizeof(AppName)/sizeof(AppName[0]))) {
  149. AppName[0] = 0 ;
  150. }
  151. MessageBox(hwndFrame, Message, AppName,
  152. MB_APPLMODAL | MB_OK | MB_ICONASTERISK);
  153. } else {
  154. SelStart = (DWORD) (pMatch - pStart);
  155. SendMessage(hwndActiveEdit, EM_SETSEL, SelStart, SelStart+lstrlen(szKey));
  156. SendMessage(hwndActiveEdit, EM_SCROLLCARET, 0, 0);
  157. }
  158. } // Search