home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / oolregex / regexdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-08  |  10.0 KB  |  445 lines

  1. // RegExdlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "RegExpTest.h"
  6. #include "RegExdlg.h"
  7. #include "oolregex.h"
  8. #include "ctrlext.h"
  9. #include <string.h>
  10.  
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char BASED_CODE THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. #define E_TESTREGCOMPFAIL        0x800600BL        
  17. #define E_TESTREGCOMPSUCCESS    0x800600CL        
  18. #define E_TESTREGEXECFAIL        0x800600DL        
  19. #define E_TESTREGEXECSUCCESS    0x800600EL        
  20. #define E_TESTREGEXECERROR        0x800600FL        
  21. #define E_TESTREGSUBERROR        0x8006010L        
  22. #define E_TESTREGSUBWRONG        0x8006011L        
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAboutDlg dialog used for App About
  26.  
  27. class CAboutDlg : public CDialog
  28. {
  29. public:
  30.     CAboutDlg();
  31.  
  32. // Dialog Data
  33.     //{{AFX_DATA(CAboutDlg)
  34.     enum { IDD = IDD_ABOUTBOX };
  35.     //}}AFX_DATA
  36.  
  37. // Implementation
  38. protected:
  39.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  40.     //{{AFX_MSG(CAboutDlg)
  41.     virtual BOOL OnInitDialog();
  42.     //}}AFX_MSG
  43.     DECLARE_MESSAGE_MAP()
  44. };
  45.  
  46. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  47. {
  48.     //{{AFX_DATA_INIT(CAboutDlg)
  49.     //}}AFX_DATA_INIT
  50. }
  51.  
  52. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  53. {
  54.     CDialog::DoDataExchange(pDX);
  55.     //{{AFX_DATA_MAP(CAboutDlg)
  56.     //}}AFX_DATA_MAP
  57. }
  58.  
  59. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  60.     //{{AFX_MSG_MAP(CAboutDlg)
  61.         // No message handlers
  62.     //}}AFX_MSG_MAP
  63. END_MESSAGE_MAP()
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CAboutDlg message handlers
  67.  
  68. BOOL CAboutDlg::OnInitDialog()
  69. {
  70.     CDialog::OnInitDialog();
  71.     CenterWindow();
  72.     
  73.     // TODO: Add extra about dlg initialization here
  74.     
  75.     return TRUE;  // return TRUE  unless you set the focus to a control
  76. }
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CRegExpTestDlg dialog
  80.  
  81. CRegExpTestDlg::CRegExpTestDlg(CWnd* pParent /*=NULL*/)
  82.     : CDialog(CRegExpTestDlg::IDD, pParent)
  83. {
  84.     //{{AFX_DATA_INIT(CRegExpTestDlg)
  85.     m_string = _T("abc");
  86.     m_regexp = _T("(ab|a)(b*)c");
  87.     m_res1 = _T("");
  88.     m_res2 = _T("");
  89.     m_res3 = _T("");
  90.     //}}AFX_DATA_INIT
  91.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  92.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  93. }
  94.  
  95. void CRegExpTestDlg::DoDataExchange(CDataExchange* pDX)
  96. {
  97.     CDialog::DoDataExchange(pDX);
  98.     //{{AFX_DATA_MAP(CRegExpTestDlg)
  99.     DDX_Control(pDX, IDC_LIST_TESTINFO, m_listTestInfo);
  100.     DDX_Text(pDX, IDC_EDIT1, m_string);
  101.     DDX_Text(pDX, IDC_EDIT2, m_regexp);
  102.     DDX_Text(pDX, IDC_EDIT3, m_res1);
  103.     DDX_Text(pDX, IDC_EDIT4, m_res2);
  104.     DDX_Text(pDX, IDC_EDIT5, m_res3);
  105.     //}}AFX_DATA_MAP
  106. }
  107.  
  108. BEGIN_MESSAGE_MAP(CRegExpTestDlg, CDialog)
  109.     //{{AFX_MSG_MAP(CRegExpTestDlg)
  110.     ON_WM_SYSCOMMAND()
  111.     ON_WM_DESTROY()
  112.     ON_WM_PAINT()
  113.     ON_WM_QUERYDRAGICON()
  114.     ON_BN_CLICKED(ID_BUTTON_MATCH, OnButtonMatch)
  115.     ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
  116.     //}}AFX_MSG_MAP
  117. END_MESSAGE_MAP()
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CRegExpTestDlg message handlers
  121.  
  122. BOOL CRegExpTestDlg::OnInitDialog()
  123. {
  124.     CDialog::OnInitDialog();
  125.     CenterWindow();
  126.  
  127.     // Add "About..." menu item to system menu.
  128.  
  129.     // IDM_ABOUTBOX must be in the system command range.
  130.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  131.     ASSERT(IDM_ABOUTBOX < 0xF000);
  132.  
  133.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  134.     CString strAboutMenu;
  135.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  136.     if (!strAboutMenu.IsEmpty())
  137.     {
  138.         pSysMenu->AppendMenu(MF_SEPARATOR);
  139.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  140.     }
  141.     
  142.     // TODO: Add extra initialization here
  143.     
  144.     return TRUE;  // return TRUE  unless you set the focus to a control
  145. }
  146.  
  147. void CRegExpTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  148. {
  149.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  150.     {
  151.         CAboutDlg dlgAbout;
  152.         dlgAbout.DoModal();
  153.     }
  154.     else
  155.     {
  156.         CDialog::OnSysCommand(nID, lParam);
  157.     }
  158. }
  159.  
  160. void CRegExpTestDlg::OnDestroy()
  161. {
  162.     WinHelp(0L, HELP_QUIT);
  163.     CDialog::OnDestroy();
  164. }
  165.  
  166. // If you add a minimize button to your dialog, you will need the code below
  167. //  to draw the icon.  For MFC applications using the document/view model,
  168. //  this is automatically done for you by the framework.
  169.  
  170. void CRegExpTestDlg::OnPaint() 
  171. {
  172.     if (IsIconic())
  173.     {
  174.         CPaintDC dc(this); // device context for painting
  175.  
  176.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  177.  
  178.         // Center icon in client rectangle
  179.         int cxIcon = GetSystemMetrics(SM_CXICON);
  180.         int cyIcon = GetSystemMetrics(SM_CYICON);
  181.         CRect rect;
  182.         GetClientRect(&rect);
  183.         int x = (rect.Width() - cxIcon + 1) / 2;
  184.         int y = (rect.Height() - cyIcon + 1) / 2;
  185.  
  186.         // Draw the icon
  187.         dc.DrawIcon(x, y, m_hIcon);
  188.     }
  189.     else
  190.     {
  191.         CDialog::OnPaint();
  192.     }
  193. }
  194.  
  195. // The system calls this to obtain the cursor to display while the user drags
  196. //  the minimized window.
  197. HCURSOR CRegExpTestDlg::OnQueryDragIcon()
  198. {
  199.     return (HCURSOR) m_hIcon;
  200. }
  201.  
  202. HRESULT tryRegExp(char **fields)
  203. {
  204.     oolregexp *r = NULL;
  205.     char dbuf[BUFSIZ];
  206.  
  207.     HRESULT result = S_OK;
  208.     result = OolRegComp(fields[0], r);
  209.     if (FAILED(result)) {
  210.         if (*fields[2] != 'c')
  211.             return E_TESTREGCOMPFAIL;
  212.         return S_OK;
  213.     }
  214.     if (*fields[2] == 'c') {
  215.         result = E_TESTREGCOMPSUCCESS;
  216.         free((char *)r);
  217.         return result;
  218.     }
  219.     result = OolRegExec(r, fields[1], fields[1]);
  220.     if (FAILED(result)) {
  221.         result = E_TESTREGEXECERROR;
  222.         free((char *)r);
  223.         return result;
  224.     }
  225.     if (result == S_FAIL) {
  226.         if (*fields[2] != 'n') {
  227.             return E_TESTREGEXECFAIL;
  228.         }
  229.         free((char *)r);
  230.         return S_OK;
  231.     }
  232.     if (*fields[2] == 'n') {
  233.         result = E_TESTREGEXECSUCCESS;
  234.         free((char *)r);
  235.         return result;
  236.     }
  237.     result = S_OK;
  238.     result = OolRegSub(r, fields[3], dbuf);
  239.     if (result != S_OK) {
  240.         result = E_TESTREGSUBERROR;
  241.         free((char *)r);
  242.         return result;
  243.     }
  244.     if (strcmp(dbuf, fields[4]) != 0)
  245.         result = E_TESTREGSUBWRONG;
  246.     free((char *)r);
  247.     return result;
  248. }
  249.  
  250. HRESULT tryRegExpression(char **fields)
  251. {
  252.     oolregexp *r = NULL;
  253.  
  254.     HRESULT result = OolRegCompCache(fields[0], r);
  255.     if (FAILED(result)) {
  256.         if (*fields[2] != 'c')
  257.             return E_TESTREGCOMPFAIL;
  258.         return S_OK;
  259.     }
  260.     if (*fields[2] == 'c') {
  261.         result = E_TESTREGCOMPSUCCESS;
  262.         return result;
  263.     }
  264.     result = OolRegExec(r, fields[1], fields[1]);
  265.     if (FAILED(result)) {
  266.         result = E_TESTREGEXECERROR;
  267.         return result;
  268.     }
  269.     if (result == S_FAIL) {
  270.         if (*fields[2] != 'n') {
  271.             return E_TESTREGEXECFAIL;
  272.         }
  273.         return S_OK;
  274.     }
  275.     if (*fields[2] == 'n') {
  276.         result = E_TESTREGEXECSUCCESS;
  277.         return result;
  278.     }
  279.     result = S_OK;
  280.     return result;
  281. }
  282.  
  283. void CRegExpTestDlg::DisplayList()
  284. {
  285.     // Erase the list
  286.     CListCtrlEx& ctlList = (CListCtrlEx&) m_listTestInfo;
  287.     ctlList.DeleteAllItems();
  288.     while(ctlList.DeleteColumn(0));
  289.     UpdateWindow();
  290.  
  291.     int nCount = 0;
  292.     ctlList.AddColumn(_T("Regular Expression"),0);
  293.     ctlList.AddColumn(_T("Input String"),1);
  294.     ctlList.AddColumn(_T("Match"),2);
  295.     ctlList.AddColumn(_T("Return"),3);
  296.     ctlList.AddColumn(_T("Result"),4);
  297.     ctlList.AddColumn(_T("Test Success"),5);
  298.     ctlList.AddColumn(_T("Cache Success"),6);
  299.  
  300.     char rbuf[100];
  301.     FILE* stream;
  302.     char *field[5];
  303.     char *scan;
  304.     int i;
  305.  
  306.     //    errreport = 1;
  307.     int lineno = 0;
  308.  
  309.    if( (stream = fopen( "testdata.txt", "r" )) == NULL )
  310.    {
  311.         AfxMessageBox("testdata.txt file not found.");
  312.        return;
  313.    }
  314.  
  315.     int row = 0;
  316.     lineno = 0;
  317.     int errorFound = 0;
  318.     while (fgets(rbuf, sizeof(rbuf), stream) != NULL) {
  319.         rbuf[strlen(rbuf)-1] = '\0';    /* Dispense with \n. */
  320.         lineno++;
  321.         scan = rbuf;
  322.         for (i = 0; i < 5; i++) {
  323.             field[i] = scan;
  324.             if (field[i] == NULL) {
  325.                 sprintf(rbuf,"Bad TestFile at line: %d",lineno);
  326.                 AfxMessageBox(rbuf);
  327.                 exit(1);
  328.             }
  329.             int temp = '\t';
  330.             scan = strchr(scan, temp);
  331.             if (scan != NULL)
  332.                 *scan++ = '\0';
  333.         }
  334.         for (i = 0; i < 5; i++) {
  335.             ctlList.AddItem(row,i,field[i]);
  336.         }
  337.  
  338.         HRESULT result = tryRegExp(field);
  339.         switch (result) 
  340.         {
  341.         case S_OK:
  342.             ctlList.AddItem(row,5,"OK");
  343.             break;
  344.         case E_TESTREGCOMPFAIL:
  345.             ctlList.AddItem(row,5,"RegComp Fail");
  346.             errorFound = 1;
  347.             break;
  348.         case E_TESTREGCOMPSUCCESS:
  349.             ctlList.AddItem(row,5,"RegComp Succ");
  350.             errorFound = 1;
  351.             break;
  352.         case E_TESTREGEXECFAIL:
  353.             ctlList.AddItem(row,5,"RegExec Fail");
  354.             errorFound = 1;
  355.             break;
  356.         case E_TESTREGEXECSUCCESS:
  357.             ctlList.AddItem(row,5,"RegExec Succ");
  358.             errorFound = 1;
  359.             break;
  360.         case E_TESTREGEXECERROR:
  361.             ctlList.AddItem(row,5,"RegExec Err");
  362.             errorFound = 1;
  363.             break;
  364.         case E_TESTREGSUBWRONG:
  365.             ctlList.AddItem(row,5,"RegSub Fail");
  366.             errorFound = 1;
  367.             break;
  368.         case E_TESTREGSUBERROR:
  369.             ctlList.AddItem(row,5,"RegSub Err");
  370.             errorFound = 1;
  371.             break;
  372.         default:
  373.             AfxMessageBox("Bad return value from tryRegExp");
  374.         }
  375.  
  376.         result = tryRegExpression(field);
  377.         switch (result) 
  378.         {
  379.         case S_OK:
  380.             ctlList.AddItem(row,6,"OK");
  381.             break;
  382.         case E_TESTREGCOMPFAIL:
  383.             ctlList.AddItem(row,6,"RegComp Fail");
  384.             errorFound = 1;
  385.             break;
  386.         case E_TESTREGCOMPSUCCESS:
  387.             ctlList.AddItem(row,6,"RegComp Succ");
  388.             errorFound = 1;
  389.             break;
  390.         case E_TESTREGEXECFAIL:
  391.             ctlList.AddItem(row,6,"RegExec Fail");
  392.             errorFound = 1;
  393.             break;
  394.         case E_TESTREGEXECSUCCESS:
  395.             ctlList.AddItem(row,6,"RegExec Succ");
  396.             errorFound = 1;
  397.             break;
  398.         case E_TESTREGEXECERROR:
  399.             ctlList.AddItem(row,6,"RegExec Err");
  400.             errorFound = 1;
  401.             break;
  402.         default:
  403.             char temp[100];
  404.             sprintf(temp,"Bad return value from tryRegExpression: %d",result);
  405.             AfxMessageBox(temp);
  406.         }
  407.         row++;
  408.     }
  409.     if (errorFound == 1) {
  410.         AfxMessageBox("The Test Failed");
  411.     } else {
  412.         AfxMessageBox("The Test Succeeded");
  413.     }
  414. }
  415.  
  416. void CRegExpTestDlg::OnButtonMatch() 
  417. {
  418.     UpdateData(TRUE);
  419.     HRESULT match;
  420.     char* res1; 
  421.     char* res2;
  422.     char* res3;
  423.     match = OolRegExpr(m_string, m_regexp, &res1, &res2, &res3, NULL);
  424.     m_res1 = res1;
  425.     m_res2 = res2;
  426.     m_res3 = res3;
  427.     UpdateData(FALSE);
  428.     if (match == S_OK)
  429.     {
  430.         AfxMessageBox("Pattern Matches!");
  431.     } else if (match == S_FALSE) {
  432.         AfxMessageBox("No Match");
  433.     } else {
  434.         char temp[50];
  435.         sprintf(temp, "Error: %X", match);
  436.         AfxMessageBox(temp);
  437.     }
  438. }
  439.  
  440. void CRegExpTestDlg::OnButtonTest() 
  441. {
  442.     DisplayList();    
  443. }
  444.  
  445.