Entry 771

part of regex lib for c#

   

Submitted by h0x91B on May 13, 2008 at 11:31 a.m.
Language: C++. Code size: 10.3 KB.

#define UNICODE 1
#define _UNICODE 1
// regex.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "regex.h"

#include <boost/regex.hpp>
#include <map>


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

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CRegexApp

BEGIN_MESSAGE_MAP(CRegexApp, CWinApp)
	//{{AFX_MSG_MAP(CRegexApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRegexApp construction

CRegexApp::CRegexApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CRegexApp object

CRegexApp theApp;

char * Utf2Ansi(TCHAR * unicodetext)
{
	int len=lstrlen(unicodetext)+1;
	char * ansistr = (char*)GlobalAlloc(GMEM_ZEROINIT|GMEM_FIXED,len);
	WideCharToMultiByte(CP_ACP, 
		0, 
		unicodetext, 
		-1, 
		ansistr, 
		len, 
		NULL, 
		NULL);
	return ansistr;
	//GlobalFree(ansistr);
}

TCHAR * Ansi2Utf(char * ansitext)
{
	int len=(strlen(ansitext)+1)*2;
	TCHAR * utfstr = (TCHAR*)GlobalAlloc(GMEM_ZEROINIT|GMEM_FIXED,len);
	MultiByteToWideChar(CP_ACP, 
		0, 
		ansitext, 
		-1, 
		utfstr, 
		len);
	//MessageBox(0,utfstr,L"UNICODE from ansi",0);
	return utfstr;

}


void testUnicode(TCHAR * msg)
{
	if(lstrlen(msg)==0)
	{
		MessageBox(0,L"text is null",L"error TestUnicode()",0);
		return;
	}
	MessageBoxW(0,msg,L"Unicode",0);

	char * ansistr=Utf2Ansi(msg);

	MessageBoxA(0,ansistr,"ANSI Func",0);

	GlobalFree(ansistr);
}


void test(TCHAR * msg)
{
	MessageBoxW(0,msg,0,0);

	std::string line="5 \r\n7\r\n 226 7 Closing data connection, file transfer successful\r\nnection, fi21le transfer succes";

    boost::smatch matches;
	//__asm int 3;
	int res=0;
	char *tregex=(char*)GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,512);
	strcpy(tregex,"([0-9])");
	try
	{
		//regex_search(0, 0, 0, 0, 0);
		//res=boost::regex_search(0,strlen(tregex),line, matches, boost::regex(tregex,boost::regex::extended));
	}
	catch(...)
	{
		MessageBox(0, L"catch",0,0);
	}
    if (res!=0)
	{
		std::string line222=matches[2];
		MessageBoxA(0,line222.c_str() ,0,0);
		matches[5].first;
		__asm int 3;
		int a=matches.size();
		matches.str(2);
	}
	else
	{
		MessageBox(0, L"nothing found",0,0);
		MessageBoxA(0,line.c_str() ,0,0);
		MessageBoxA(0,tregex ,0,0);
		sprintf(tregex,"matches is 0x%.8X , res=0x%.8X res=%i",matches,res,res);
		MessageBoxA(0,tregex ,0,0);
	}
	GlobalFree(tregex);
}

BOOL reg_match(TCHAR *text, TCHAR *patern)
{
	BOOL result=0;
	if( lstrlen(text)==0 || lstrlen(patern)==0 )
	{
		MessageBox(0,L"text or patern is null",0,0);
		return 0;
	}
	char * ansitext = Utf2Ansi(text);
	char * ansipatern = Utf2Ansi(patern);

	MessageBoxA(0,ansitext,"ansi text",0);
	MessageBoxA(0,ansipatern,"ansi pattern",0);

	boost::regex pat( ansipatern );
	boost::smatch matches;
	if (boost::regex_match(ansitext, matches, pat))
	{ 
		MessageBox(0,L"Match!!!",0,0);
		result=1;
	}
	else
	{
		MessageBox(0,L"Not Match",0,0);
		result=0;
	}

	//clean up
	GlobalFree(ansitext);
	GlobalFree(ansipatern);
	return result;
}




BOOL reg_search(TCHAR *text, TCHAR *patern)
{
	BOOL result=0;
	if( lstrlen(text)==0 || lstrlen(patern)==0 )
	{
		MessageBox(0,L"text or patern is null",0,0);
		return 0;
	}
	char * ansitext = Utf2Ansi(text);
	char * ansipatern = Utf2Ansi(patern);

	MessageBoxA(0,ansitext,"ansi text",0);
	MessageBoxA(0,ansipatern,"ansi pattern",0);

	boost::regex pat( ansipatern );
	boost::smatch matches;
	if (boost::regex_search(ansitext, matches, pat))
	{ 
		MessageBoxA(0,matches.str(1).c_str(),"Match!!!",0);
		const char * b0=matches.str(0).c_str();
		const char * b1=matches.str(1).c_str();
		const char * b2=matches.str(2).c_str();
		const char * b3=matches.str(3).c_str();
		
		int count=matches.size();
		__asm int 3;
		for(int n=0;n<count;n++)
			MessageBoxA(0,matches[n].str().c_str(),"Match!!!",0);
		result=1;
	}
	else
	{
		MessageBox(0,L"Not Match",0,0);
		result=0;
	}

	//clean up
	GlobalFree(ansitext);
	GlobalFree(ansipatern);
	return result;
}

struct reg_simple_result
{
	reg_simple_result * next;
	reg_simple_result * prev;
	char * textAnsi;
	TCHAR * textUtf;
	int text_len;
	char * patern;
};

reg_simple_result *first_reg_result=NULL;
reg_simple_result *result_Struct=NULL;

void *GetStruct()
{
	return first_reg_result;
}


bool grep_callback(const  boost::smatch& what) 
{ 
   // what[0] contains the whole string 
   // what[5] contains the class name. 
   // what[6] contains the template specialisation if any. 
   // add class name and position to map: 
	std::string abc=std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second);
	std::string t1=what[0];
	std::string t2=what[1];
	std::string t3=what[2];
	std::string t4=what[3];
	std::string t5=what[4];
	std::string t6=what[5];
	std::string t7=what[6];
	//MessageBoxA(0,t1.c_str(),"grep_callback",0);
	if(first_reg_result==NULL)
	{
		first_reg_result=(reg_simple_result*)GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,sizeof(reg_simple_result));
		first_reg_result->prev = NULL;
		char * buff = (char*)GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,strlen(t1.c_str())+1);
		strcpy(buff,t1.c_str());
		first_reg_result->textAnsi=buff;
		first_reg_result->textUtf=Ansi2Utf(buff);
		first_reg_result->text_len=strlen(t1.c_str());
		first_reg_result->patern=NULL;
	}
	else
	{
		reg_simple_result* tmpresult;
		reg_simple_result* tmpresult2=first_reg_result;
		tmpresult=(reg_simple_result*)GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,sizeof(reg_simple_result));
		if(first_reg_result->next==NULL)
		{
			first_reg_result->next = tmpresult;
			tmpresult->prev = first_reg_result;
		}
		else
		{
			while(tmpresult2->next!=NULL)
			{
				tmpresult2 = tmpresult2->next;
			}
			tmpresult2->next = tmpresult;
			tmpresult->prev = tmpresult2;
		}
		
		tmpresult->next = NULL;
		char * buff = (char*)GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,strlen(t1.c_str())+1);
		strcpy(buff,t1.c_str());
		tmpresult->textAnsi=buff;
		tmpresult->textUtf=Ansi2Utf(buff);
		tmpresult->text_len=strlen(t1.c_str());
		tmpresult->patern=NULL;
	}
//	__asm int 3;
   return true; 
}

BOOL reg_grep(TCHAR *text, TCHAR *patern)
{
	BOOL result=0;
	if( lstrlen(text)==0 || lstrlen(patern)==0 )
	{
		MessageBox(0,L"text or patern is null",0,0);
		return 0;
	}
	char * ansitext = Utf2Ansi(text);
	char * ansipatern = Utf2Ansi(patern);

	//MessageBoxA(0,ansitext,"ansi text",0);
	//MessageBoxA(0,ansipatern,"ansi pattern",0);

	boost::regex pat( ansipatern );
	boost::smatch matches;
	if (boost::regex_grep(grep_callback,ansitext,  pat))
	{ 
		/*MessageBoxA(0,matches.str(1).c_str(),"Match!!!",0);
		const char * b0=matches.str(0).c_str();
		const char * b1=matches.str(1).c_str();
		const char * b2=matches.str(2).c_str();
		const char * b3=matches.str(3).c_str();
		
		int count=matches.size();
		__asm int 3;
		for(int n=0;n<count;n++)
			MessageBoxA(0,matches[n].str().c_str(),"Match!!!",0);*/
		result=1;
		//MessageBoxA(0,0,"Match!!!",0);
	}
	else
	{
		//MessageBox(0,L"Not Match",0,0);
		result=0;
	}

	//clean up
	GlobalFree(ansitext);
	GlobalFree(ansipatern);
	return result;
}

//work
void testref(int &refint)
{
	refint=15;
}

void GetStructureParams(int &len,char *text)
{
	len=first_reg_result->text_len;
	text=first_reg_result->textAnsi;
}

/*
prefix result_
*/

const int ERR_NoError=1;
const int ERR_NoFirstResult=-1; //first_reg_result is NULL
const int ERR_NoNextResult=-2; //next or prev in struct is NULL
const int ERR_NoPrevResult=-3; //next or prev in struct is NULL

int result_First()
{
	if(first_reg_result==NULL)
		return ERR_NoFirstResult;
	result_Struct=first_reg_result;
	return ERR_NoError;
}

int result_Next()
{
	if(result_Struct!=NULL)
	{
		if(result_Struct->next==NULL)
			return ERR_NoNextResult;
		result_Struct=result_Struct->next;
	}
	else
		return ERR_NoFirstResult;
	return ERR_NoError;
}

int result_Prev()
{
	if(result_Struct!=NULL)
	{
		if(result_Struct->next==NULL)
			return ERR_NoPrevResult;
		result_Struct=result_Struct->prev;
	}
	else
		return ERR_NoFirstResult;
	return ERR_NoError;
}

void result_ClearResults()
{
	if(first_reg_result==NULL)
		return;
	reg_simple_result *last=first_reg_result;
	while(last->next!=NULL)
		last=last->next;
	last=last->prev;
	while(last!=NULL)
	{
		GlobalFree(last->next);
		last=last->prev;
	}
	first_reg_result=NULL;
	result_Struct=NULL;
}

char *result_GetAnsiText()
{
	if(result_Struct==NULL)
	{
		if(result_First()!=ERR_NoError)
			return "(NULL ANSI)";
	}
	return result_Struct->textAnsi;
}

TCHAR *result_GetUTFText()
{
	if(result_Struct==NULL)
	{
		int res=result_First();
		if(res!=ERR_NoError)
		{
			switch(res)
			{
			case ERR_NoFirstResult:
				return L"(ERR_NoFirstResult)";
				break;
			case ERR_NoNextResult:
				return L"(ERR_NoNextResult)";
				break;
			case ERR_NoPrevResult:
				return L"(ERR_NoPrevResult)";
				break;
			case ERR_NoError:
				return L"(ERR_NoError)";
				break;
			default:
				return L"(NULL UTF)";
			}
		}
	}
	return Ansi2Utf(result_Struct->textAnsi);
}

This snippet took 0.08 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).