 
#import "RegExp.h"
#import <appkit/Application.h>

#import "regex.h"
#import "RegExpConstants.h"

/*
 * Define the list of commands and functions this XModule implements
 */

const char *handlerList[] = {
	"CHECKEXPRESSION",
	"RE_VERSION",
	NULL
};
 
static struct mach_header	*loadHeader = NULL;

@implementation RegExp : XModule

- init
{
	[super init];
	
	// Do your stuff here...
	
	return ( self );
}


- free
{
	// Do your stuff here...

	return ( [super free] );
}



// ---------------------------------------------------------------
//   Standard XModule methods
// ---------------------------------------------------------------

+ (const char *)moduleName
{
	return ( "RegularExpression" );
}


+ (const char **)xCmdsAndFcns
{
	return ( handlerList );
}


- prepareToExecuteHandlers
{
	// [self loadInterfaceData];
	return ( self );
}


- executeHandler:(NXAtom)handlerName
{
	if (!strcmp(handlerName, "CHECKEXPRESSION")) {
		return ( [self checkExpression] );
	} else if (!strcmp(handlerName, "RE_VERSION")) {
		return ( [self re_version] );
	} else return ( nil ); // handlerName not recognized!
}




// ---------------------------------------------------------------
//   Useful methods for loading interface data
// ---------------------------------------------------------------


+ finishLoading:(struct mach_header *)header
{
	loadHeader = header; // Save this for later use
	return ( self );
}


- loadInterfaceData
{
	//[NXApp loadNibSection:"MyXModule.nib" owner:self
	//    withNames:NO fromHeader:loadHeader] )
		
	return ( self );
}


// XModule Methods
- checkExpression
{
	int				pCount = [self paramCount];
	char				*regExp;
	char				*aString;
	const char			*errStrPtr;
	struct re_pattern_buffer 	exp;
	struct re_registers 		regs;
	int				sResult;
	char				fastmap[256];

	// If we don't have a regexp and string, then return with message
	if ( pCount != 2 ) {
		return ( [[self getEmptyContainer] setString:
			"Usage: checkExpression(<aRegExp>, <string>)"] );
	}
	
	// Now we get the two string...
	regExp = NXCopyStringBuffer([[self getParam:1] stringVal]);
	aString = NXCopyStringBuffer([[self getParam:2] stringVal]);
	
	// Initialize the expression.
	exp.allocated = 0;
	exp.buffer = 0;
	exp.translate = NULL;
	exp.fastmap = fastmap;

	// Now set up the expression to be compiled, return if error.
	errStrPtr = re_compile_pattern(regExp, strlen(regExp), &exp);
	if ( errStrPtr ) {
		return ( [[self getEmptyContainer] setString:errStrPtr] );
	}
	
	// No error, so go and check and see if string fits regExp
	sResult = re_search(&exp, aString,
			strlen(aString), 0, strlen(aString), &regs);

	return ( [[self getEmptyContainer] setInt:sResult] ); 
}

- re_version
{
	id		version;

	version = [[ self getEmptyContainer ] setString:"r" ];
	[ version appendString:RE_RELEASE ];
	[ version appendString:" v" ];
	[ version appendString:RE_VERSION ];
	[ version appendString:"    " ];
	[ version appendString:RE_RELTIME ];
	[ version appendString:"\n" ];
	[ version appendString:RE_COPYRIGHT ];

	return ( version );
}

@end

