Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / MFC

CPerlString - A Class to Utilise Perl String Functions

Rate me:
Please Sign up or sign in to vote.
4.11/5 (5 votes)
31 Aug 20023 min read 153.1K   1.4K   24  
A Class to Utilise Perl String Functions
// PerlStringTestDlg.cpp : implementation file
//

#include "stdafx.h"
#define MFC
#include <CPerlString.h>

#include "PerlStringTest.h"
#include "PerlStringTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CPerlStringTestDlg dialog

CPerlStringTestDlg::CPerlStringTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPerlStringTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPerlStringTestDlg)
	m_sPattern = _T("");
	m_sString = _T("");
	m_iTest = 0;
	m_sMatches = _T("");
	m_sResult = _T("");
	m_sStaticPattern = _T("Pattern - e.g. /abc/gmsi");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPerlStringTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPerlStringTestDlg)
	DDX_Control(pDX, IDC_PATTERN, m_cPattern);
	DDX_Control(pDX, IDC_MATCHES, m_cMatches);
	DDX_Control(pDX, IDC_TEST, m_cTest);
	DDX_Text(pDX, IDC_PATTERN, m_sPattern);
	DDX_Text(pDX, IDC_STRING, m_sString);
	DDX_Radio(pDX, ID_MATCH, m_iTest);
	DDX_Text(pDX, IDC_MATCHES, m_sMatches);
	DDX_Text(pDX, IDC_RESULT, m_sResult);
	DDX_Text(pDX, IDC_STATICPATTERN, m_sStaticPattern);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPerlStringTestDlg, CDialog)
	//{{AFX_MSG_MAP(CPerlStringTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_TEST, OnTest)
	ON_BN_CLICKED(ID_JOIN, OnJoin)
	ON_BN_CLICKED(ID_MATCH, OnMatch)
	ON_BN_CLICKED(ID_MATCHES, OnMatches)
	ON_BN_CLICKED(ID_SORT, OnSort)
	ON_BN_CLICKED(ID_SPLIT, OnSplit)
	ON_BN_CLICKED(ID_SUBSTITUTE, OnSubstitute)
	ON_BN_CLICKED(ID_REVERSESORT, OnReversesort)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPerlStringTestDlg message handlers

BOOL CPerlStringTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	SetWindowText("PerlStringTest - Match");
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPerlStringTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CPerlStringTestDlg::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 CPerlStringTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CPerlStringTestDlg::OnTest() 
{
	// TODO: Add your control notification handler code here
	CPerlString Perl;
	CStringArray List;
	int numOfMatches, i;

	UpdateData(TRUE);
	m_sResult = "";
	m_sMatches = "";

	switch (m_iTest)
	{
		case 0:	// Match
			if (Perl.Match(m_sString, m_sPattern))
			{
				m_sResult = "Pattern found";
			}
			else
			{
				m_sResult = "Pattern not found";
			}
			break;
		
		case 1:	// Matches
			numOfMatches = Perl.Matches(m_sString, m_sPattern, List);
			if (numOfMatches > 0)
			{
				m_sResult.Format("%d matches", numOfMatches);
				for (i = 0; i < List.GetSize(); i++)
				{
					m_sMatches += List.GetAt(i);
					m_sMatches += "\r\n";
				}
			}
			else
			{
				m_sResult = "No matches";
			}
			break;
		
		case 2:	// Substitute
			m_sResult = m_sString;
			Perl.Substitute(m_sResult, m_sPattern);
			break;
		
		case 3:	// Join
			Perl.Split(m_sString, "/\r\n/", List);
			Perl.Join(List, m_sPattern, m_sResult);
			break;
		
		case 4:	// Split
			Perl.Split(m_sString, m_sPattern, List);
			for (i = 0; i < List.GetSize(); i++)
			{
				m_sResult += List.GetAt(i);
				m_sResult += "\r\n";
			}
			break;	

		case 5:	// Sort
			Perl.Split(m_sString, "/\r\n/", List);
			Perl.Sort(List, List);
			for (i = 0; i < List.GetSize(); i++)
			{
				m_sResult += List.GetAt(i);
				m_sResult += "\r\n";
			}	
			break;

		case 6:	// Reverse Sort
			Perl.Split(m_sString, "/\r\n/", List);
			Perl.Sort(List, List, 1);
			for (i = 0; i < List.GetSize(); i++)
			{
				m_sResult += List.GetAt(i);
				m_sResult += "\r\n";
			}	
			break;
	}
	
	UpdateData(FALSE);
}

void CPerlStringTestDlg::OnMatch() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	SetWindowText("PerlStringTest - Match");
	m_cPattern.EnableWindow(TRUE);
	m_cMatches.EnableWindow(FALSE);
	m_sStaticPattern = "Pattern - e.g. /abc/gmsi";
	UpdateData(FALSE);
}

void CPerlStringTestDlg::OnMatches() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	SetWindowText("PerlStringTest - Matches");
	m_cPattern.EnableWindow(TRUE);
	m_cMatches.EnableWindow(TRUE);
	m_sStaticPattern = "Pattern - e.g /(abc)(d.)/gmsi";
	UpdateData(FALSE);
}

void CPerlStringTestDlg::OnSubstitute() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	SetWindowText("PerlStringTest - Substitute");
	m_cPattern.EnableWindow(TRUE);
	m_cMatches.EnableWindow(FALSE);
	m_sStaticPattern = "Pattern - e.g. s/abc/cde/gmsi";
	UpdateData(FALSE);
}

void CPerlStringTestDlg::OnJoin() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	SetWindowText("PerlStringTest - Join");
	m_cPattern.EnableWindow(TRUE);
	m_cMatches.EnableWindow(FALSE);
	m_sStaticPattern = "Pattern - e.g. ;";
	UpdateData(FALSE);
}

void CPerlStringTestDlg::OnSplit() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	SetWindowText("PerlStringTest - Split");
	m_cPattern.EnableWindow(TRUE);
	m_cMatches.EnableWindow(FALSE);
	m_sStaticPattern = "Pattern - e.g. /;/";
	UpdateData(FALSE);
}

void CPerlStringTestDlg::OnSort() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	SetWindowText("PerlStringTest - Sort");
	m_cPattern.EnableWindow(FALSE);
	m_cMatches.EnableWindow(FALSE);
	m_sStaticPattern = "Pattern";
	UpdateData(FALSE);
}

void CPerlStringTestDlg::OnReversesort() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	SetWindowText("PerlStringTest - Reverse Sort");
	m_cPattern.EnableWindow(FALSE);
	m_cMatches.EnableWindow(FALSE);
	m_sStaticPattern = "Pattern";
	UpdateData(FALSE);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Researcher
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions