home *** CD-ROM | disk | FTP | other *** search
- // RegExdlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "RegExpTest.h"
- #include "RegExdlg.h"
- #include "oolregex.h"
- #include "ctrlext.h"
- #include <string.h>
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- #define E_TESTREGCOMPFAIL 0x800600BL
- #define E_TESTREGCOMPSUCCESS 0x800600CL
- #define E_TESTREGEXECFAIL 0x800600DL
- #define E_TESTREGEXECSUCCESS 0x800600EL
- #define E_TESTREGEXECERROR 0x800600FL
- #define E_TESTREGSUBERROR 0x8006010L
- #define E_TESTREGSUBWRONG 0x8006011L
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
-
- // Implementation
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //{{AFX_MSG(CAboutDlg)
- virtual BOOL OnInitDialog();
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg message handlers
-
- BOOL CAboutDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- CenterWindow();
-
- // TODO: Add extra about dlg initialization here
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CRegExpTestDlg dialog
-
- CRegExpTestDlg::CRegExpTestDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CRegExpTestDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CRegExpTestDlg)
- m_string = _T("abc");
- m_regexp = _T("(ab|a)(b*)c");
- m_res1 = _T("");
- m_res2 = _T("");
- m_res3 = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CRegExpTestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CRegExpTestDlg)
- DDX_Control(pDX, IDC_LIST_TESTINFO, m_listTestInfo);
- DDX_Text(pDX, IDC_EDIT1, m_string);
- DDX_Text(pDX, IDC_EDIT2, m_regexp);
- DDX_Text(pDX, IDC_EDIT3, m_res1);
- DDX_Text(pDX, IDC_EDIT4, m_res2);
- DDX_Text(pDX, IDC_EDIT5, m_res3);
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CRegExpTestDlg, CDialog)
- //{{AFX_MSG_MAP(CRegExpTestDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_DESTROY()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(ID_BUTTON_MATCH, OnButtonMatch)
- ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CRegExpTestDlg message handlers
-
- BOOL CRegExpTestDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- CenterWindow();
-
- // Add "About..." menu item to system menu.
-
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
-
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
-
- // TODO: Add extra initialization here
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- void CRegExpTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
-
- void CRegExpTestDlg::OnDestroy()
- {
- WinHelp(0L, HELP_QUIT);
- CDialog::OnDestroy();
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CRegExpTestDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CRegExpTestDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- HRESULT tryRegExp(char **fields)
- {
- oolregexp *r = NULL;
- char dbuf[BUFSIZ];
-
- HRESULT result = S_OK;
- result = OolRegComp(fields[0], r);
- if (FAILED(result)) {
- if (*fields[2] != 'c')
- return E_TESTREGCOMPFAIL;
- return S_OK;
- }
- if (*fields[2] == 'c') {
- result = E_TESTREGCOMPSUCCESS;
- free((char *)r);
- return result;
- }
- result = OolRegExec(r, fields[1], fields[1]);
- if (FAILED(result)) {
- result = E_TESTREGEXECERROR;
- free((char *)r);
- return result;
- }
- if (result == S_FAIL) {
- if (*fields[2] != 'n') {
- return E_TESTREGEXECFAIL;
- }
- free((char *)r);
- return S_OK;
- }
- if (*fields[2] == 'n') {
- result = E_TESTREGEXECSUCCESS;
- free((char *)r);
- return result;
- }
- result = S_OK;
- result = OolRegSub(r, fields[3], dbuf);
- if (result != S_OK) {
- result = E_TESTREGSUBERROR;
- free((char *)r);
- return result;
- }
- if (strcmp(dbuf, fields[4]) != 0)
- result = E_TESTREGSUBWRONG;
- free((char *)r);
- return result;
- }
-
- HRESULT tryRegExpression(char **fields)
- {
- oolregexp *r = NULL;
-
- HRESULT result = OolRegCompCache(fields[0], r);
- if (FAILED(result)) {
- if (*fields[2] != 'c')
- return E_TESTREGCOMPFAIL;
- return S_OK;
- }
- if (*fields[2] == 'c') {
- result = E_TESTREGCOMPSUCCESS;
- return result;
- }
- result = OolRegExec(r, fields[1], fields[1]);
- if (FAILED(result)) {
- result = E_TESTREGEXECERROR;
- return result;
- }
- if (result == S_FAIL) {
- if (*fields[2] != 'n') {
- return E_TESTREGEXECFAIL;
- }
- return S_OK;
- }
- if (*fields[2] == 'n') {
- result = E_TESTREGEXECSUCCESS;
- return result;
- }
- result = S_OK;
- return result;
- }
-
- void CRegExpTestDlg::DisplayList()
- {
- // Erase the list
- CListCtrlEx& ctlList = (CListCtrlEx&) m_listTestInfo;
- ctlList.DeleteAllItems();
- while(ctlList.DeleteColumn(0));
- UpdateWindow();
-
- int nCount = 0;
- ctlList.AddColumn(_T("Regular Expression"),0);
- ctlList.AddColumn(_T("Input String"),1);
- ctlList.AddColumn(_T("Match"),2);
- ctlList.AddColumn(_T("Return"),3);
- ctlList.AddColumn(_T("Result"),4);
- ctlList.AddColumn(_T("Test Success"),5);
- ctlList.AddColumn(_T("Cache Success"),6);
-
- char rbuf[100];
- FILE* stream;
- char *field[5];
- char *scan;
- int i;
-
- // errreport = 1;
- int lineno = 0;
-
- if( (stream = fopen( "testdata.txt", "r" )) == NULL )
- {
- AfxMessageBox("testdata.txt file not found.");
- return;
- }
-
- int row = 0;
- lineno = 0;
- int errorFound = 0;
- while (fgets(rbuf, sizeof(rbuf), stream) != NULL) {
- rbuf[strlen(rbuf)-1] = '\0'; /* Dispense with \n. */
- lineno++;
- scan = rbuf;
- for (i = 0; i < 5; i++) {
- field[i] = scan;
- if (field[i] == NULL) {
- sprintf(rbuf,"Bad TestFile at line: %d",lineno);
- AfxMessageBox(rbuf);
- exit(1);
- }
- int temp = '\t';
- scan = strchr(scan, temp);
- if (scan != NULL)
- *scan++ = '\0';
- }
- for (i = 0; i < 5; i++) {
- ctlList.AddItem(row,i,field[i]);
- }
-
- HRESULT result = tryRegExp(field);
- switch (result)
- {
- case S_OK:
- ctlList.AddItem(row,5,"OK");
- break;
- case E_TESTREGCOMPFAIL:
- ctlList.AddItem(row,5,"RegComp Fail");
- errorFound = 1;
- break;
- case E_TESTREGCOMPSUCCESS:
- ctlList.AddItem(row,5,"RegComp Succ");
- errorFound = 1;
- break;
- case E_TESTREGEXECFAIL:
- ctlList.AddItem(row,5,"RegExec Fail");
- errorFound = 1;
- break;
- case E_TESTREGEXECSUCCESS:
- ctlList.AddItem(row,5,"RegExec Succ");
- errorFound = 1;
- break;
- case E_TESTREGEXECERROR:
- ctlList.AddItem(row,5,"RegExec Err");
- errorFound = 1;
- break;
- case E_TESTREGSUBWRONG:
- ctlList.AddItem(row,5,"RegSub Fail");
- errorFound = 1;
- break;
- case E_TESTREGSUBERROR:
- ctlList.AddItem(row,5,"RegSub Err");
- errorFound = 1;
- break;
- default:
- AfxMessageBox("Bad return value from tryRegExp");
- }
-
- result = tryRegExpression(field);
- switch (result)
- {
- case S_OK:
- ctlList.AddItem(row,6,"OK");
- break;
- case E_TESTREGCOMPFAIL:
- ctlList.AddItem(row,6,"RegComp Fail");
- errorFound = 1;
- break;
- case E_TESTREGCOMPSUCCESS:
- ctlList.AddItem(row,6,"RegComp Succ");
- errorFound = 1;
- break;
- case E_TESTREGEXECFAIL:
- ctlList.AddItem(row,6,"RegExec Fail");
- errorFound = 1;
- break;
- case E_TESTREGEXECSUCCESS:
- ctlList.AddItem(row,6,"RegExec Succ");
- errorFound = 1;
- break;
- case E_TESTREGEXECERROR:
- ctlList.AddItem(row,6,"RegExec Err");
- errorFound = 1;
- break;
- default:
- char temp[100];
- sprintf(temp,"Bad return value from tryRegExpression: %d",result);
- AfxMessageBox(temp);
- }
- row++;
- }
- if (errorFound == 1) {
- AfxMessageBox("The Test Failed");
- } else {
- AfxMessageBox("The Test Succeeded");
- }
- }
-
- void CRegExpTestDlg::OnButtonMatch()
- {
- UpdateData(TRUE);
- HRESULT match;
- char* res1;
- char* res2;
- char* res3;
- match = OolRegExpr(m_string, m_regexp, &res1, &res2, &res3, NULL);
- m_res1 = res1;
- m_res2 = res2;
- m_res3 = res3;
- UpdateData(FALSE);
- if (match == S_OK)
- {
- AfxMessageBox("Pattern Matches!");
- } else if (match == S_FALSE) {
- AfxMessageBox("No Match");
- } else {
- char temp[50];
- sprintf(temp, "Error: %X", match);
- AfxMessageBox(temp);
- }
- }
-
- void CRegExpTestDlg::OnButtonTest()
- {
- DisplayList();
- }
-
-