#include "search.h"
#include <string.h>
int search(char *src, char *str)
{
/* info */
unsigned int src_len, str_len;
/* tool */
unsigned char match_cnt = 0;
unsigned int src_pos = 0;
unsigned char i = 0;
char x;
src_len = strlen(src);
str_len = strlen(str);
while(src_pos+str_len <= src_len)
{
// compare
for(i=0; i<str_len; i++)
{
if(src[src_pos+i] == str[i])
continue; else break;
}
// matched, count
if(i == str_len)
{
match_cnt++;
src_pos += str_len;
}
// move pos to continnue
else
{
x = src[src_pos+str_len];
if (0 == x)
break;
for(i=str_len-1; i>=0; i--)
{
if(x != str[i])
continue;
src_pos += (str_len-i);
break;
}
}
}
return match_cnt;
}
#ifndef search_h
#define search_h
enum SEARCH_METHOD_ALL
{
SIMPLE = 1,
FAST,
};
#define SEARCH_METHOD FAST
/* return matched str number */
int search(char *src, char *str);
#endif
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "mysearch.h"
#if 0
enum
{
ID_BUTTON = 1,
ID_EDIT,
ID_OUTPUT,
};
#endif
#define ID_BUTTON 1
#define ID_EDIT 2
#define ID_OUTPUT 3
#define FILE_CONTENT_SIZE 4*1024 // one page
char* pszFileContent;
HINSTANCE g_inst;
HWND g_show;
WNDPROC g_preProc;
static BOOL getFile(HWND hwnd, char* filename);
static BOOL readFile(HWND hwnd, char* filename);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static LRESULT CALLBACK EditProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR lpCmdLine, int nCmdShow )
{
MSG msg;
WNDCLASSW wc = {0};
wc.lpszClassName = L"TextSearch";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0,IDC_ARROW);
g_inst = hInstance;
RegisterClassW(&wc);
CreateWindowW(wc.lpszClassName, L"Text Search",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
200, 100, 380, 600, 0, 0, hInstance, 0);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit;
static HWND hwndButton;
static HWND hwndOutput;
//static wchar_t *str = L"Please select a file";
static char filename[40];
DWORD error;
char str[40];
wchar_t input[50];
static int i=0;
switch(msg)
{
case WM_CREATE:
pszFileContent = (char *)GlobalAlloc(GPTR, FILE_CONTENT_SIZE+1);
hwndButton = CreateWindowW(L"button", L"+", WS_VISIBLE|WS_CHILD,
10, 20, 30, 30, hwnd, (HMENU) ID_BUTTON, g_inst, NULL);
hwndEdit = CreateWindowW(L"Edit", L"Press F1 to Get Help", WS_CHILD|WS_VISIBLE|WS_BORDER,
45, 20, 315, 30, hwnd, (HMENU) ID_EDIT, g_inst, NULL);
hwndOutput = CreateWindowW(L"STATIC", L"", WS_CHILD|WS_VISIBLE|WS_BORDER|SS_LEFT,
10, 70, 350, 470, hwnd, (HMENU) ID_OUTPUT, g_inst,NULL);
g_show = hwndOutput;
g_preProc = (WNDPROC)GetWindowLong(hwndEdit, GWL_WNDPROC);
SetWindowLong(hwndEdit, GWL_WNDPROC, (long)EditProc);
break;
case WM_COMMAND:
if(ID_BUTTON == LOWORD(wParam))
{
if(getFile(hwnd, filename))
{
if(0 == readFile(hwndOutput, filename))
{
error = GetLastError();
sprintf(str, "\nError Code: %d\n", error);
SetWindowText(hwndOutput, str);
}
}
}
#if 0
else if(ID_EDIT == LOWORD(wParam))
{
sprintf(str, "wParam: %x\nlParam: %x", wParam, lParam);
SetWindowText(hwndOutput, str);
}
#endif
break;
case WM_DESTROY:
GlobalFree((HGLOBAL)pszFileContent);
PostQuitMessage(0);
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
LRESULT CALLBACK EditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static char str[40];
int match_cnt = 0;
switch(msg)
{
case WM_KEYDOWN:
#if 0
if(VK_RETURN == wParam)
{
GetWindowText(hwnd,str,40);
SetWindowText(g_show,str);
}
#endif
GetWindowText(hwnd,str,40);
if(0 != strlen(str))
match_cnt = search(pszFileContent, str);
sprintf(str, "get match cnt: %d\n", match_cnt);
SetWindowText(g_show, str);
break;
}
return CallWindowProc((WNDPROC)g_preProc, hwnd, msg, wParam, lParam);
//return DefWindowProcW(hwnd, msg, wParam, lParam);
}
static BOOL getFile(HWND hwnd, char* filename)
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "*.txt";
ofn.lpstrFile = filename;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn))
{
return TRUE;
}
return FALSE;
}
static BOOL readFile(HWND hwnd, char* filename)
{
HANDLE hFile;
DWORD dwFileSize;
DWORD dwRead;
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if(INVALID_HANDLE_VALUE == hFile)
return FALSE;
dwFileSize = GetFileSize(hFile, NULL);
if(0xFFFFFFFF == dwFileSize)
return FALSE;
if(dwFileSize > FILE_CONTENT_SIZE)
{
pszFileContent = (char *)GlobalReAlloc((char *)pszFileContent,
dwFileSize+1, GMEM_ZEROINIT|GMEM_MOVEABLE);
}
if(NULL == pszFileContent)
return FALSE;
// until at the last step to write pszFileContent, we clear it
ZeroMemory(pszFileContent, sizeof(pszFileContent));
if(0 == ReadFile(hFile, pszFileContent, dwFileSize, &dwRead, NULL))
return FALSE;
// check if dwRead < dwFileSize
pszFileContent[dwFileSize] = 0;
if(0 == SetWindowText(hwnd, pszFileContent))
return FALSE;
CloseHandle(hFile);
return TRUE;
}