/***************************************************************************
                            QRegExp.m -  description
                             -------------------
    begin                : Wed Nov 21 12:08:37 2001
    copyright            : (C) 2000-2001 Lost Highway Ltd. All rights reserved.
    email                : Lost_Highway@tipitina.demon.co.uk
    generated by         : duke@tipitina on Wed Nov 21 12:08:37 2001, using kalyptus 0.9.
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 ***************************************************************************/

#include <qtc/qt_QString.h>
#include <qtc/qt_QCString.h>
#include <qtc/qt_QRegExp.h>
#include <qtobjc/QRegExp.h>

@implementation  QRegExp

- (void *) qt
{
	return _qt;
}

- setQt: (void *) qt
{
	_qt = qt;
	return self;
}

- (BOOL) allocatedInObjcWorld
{
	return _allocatedInObjcWorld;
}

- setAllocatedInObjcWorld: (BOOL) yn
{
	_allocatedInObjcWorld = yn;
	return self;
}


- init
{
	if ([self qt] == 0L) {
		[self setQt: qt_new_QRegExp()];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

- initWithText: (NSString *)pattern caseSensitive: (BOOL)caseSensitive wildcard: (BOOL)wildcard
{
static qt_QString * _qstring_pattern = 0L;
	if ([self qt] == 0L) {
		[self setQt: qt_new_QRegExp1((qt_QString *) [pattern toQString: &_qstring_pattern], (int)caseSensitive, (int)wildcard)];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

- initWithText: (NSString *)pattern 
{
static qt_QString * _qstring_pattern = 0L;
	if ([self qt] == 0L) {
		[self setQt: qt_new_QRegExp1((qt_QString *) [pattern toQString: &_qstring_pattern], 1, 0)];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

- copyWithZone: (NSZone *) zone
{
	QRegExp * aCopy;
	aCopy = [[(QRegExp *)[[self class] allocWithZone: zone] setQt: qt_new_QRegExp2([self qt])] init];
	[aCopy setAllocatedInObjcWorld: YES];
	[QtSupport setObject: aCopy forQtKey: [aCopy qt]];
	return aCopy;
}

- (void) dealloc
{
	if ([self qt] != 0L) {
		if ([self allocatedInObjcWorld]) {
			qt_del_QRegExp((qt_QRegExp *)[self qt]);
		}
		[QtSupport removeQtKey: [self qt]];
		[self setQt: 0L];
	}
	[super dealloc];
}

- (BOOL) isEmpty
{
	return (BOOL) qt_QRegExp_isEmpty((qt_QRegExp *)[self qt]);
}

- (BOOL) isValid
{
	return (BOOL) qt_QRegExp_isValid((qt_QRegExp *)[self qt]);
}

- (NSString *) pattern
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QRegExp_pattern((qt_QRegExp *)[self qt]);
	return [NSString fromQString: _qstring];
}

- setPattern: (NSString *)pattern
{
static qt_QString * _qstring_pattern = 0L;
	qt_QRegExp_setPattern((qt_QRegExp *)[self qt], (qt_QString *) [pattern toQString: &_qstring_pattern]);
	return self;
}

- (BOOL) caseSensitive
{
	return (BOOL) qt_QRegExp_caseSensitive((qt_QRegExp *)[self qt]);
}

- setCaseSensitive: (BOOL)sensitive
{
	qt_QRegExp_setCaseSensitive((qt_QRegExp *)[self qt], (int)sensitive);
	return self;
}

- (BOOL) wildcard
{
	return (BOOL) qt_QRegExp_wildcard((qt_QRegExp *)[self qt]);
}

- setWildcard: (BOOL)wildcard
{
	qt_QRegExp_setWildcard((qt_QRegExp *)[self qt], (int)wildcard);
	return self;
}

- (BOOL) minimal
{
	return (BOOL) qt_QRegExp_minimal((qt_QRegExp *)[self qt]);
}

- setMinimal: (BOOL)minimal
{
	qt_QRegExp_setMinimal((qt_QRegExp *)[self qt], (int)minimal);
	return self;
}

- (BOOL) exactMatch: (NSString *)str
{
static qt_QString * _qstring_str = 0L;
	return (BOOL) qt_QRegExp_exactMatch((qt_QRegExp *)[self qt], (qt_QString *) [str toQString: &_qstring_str]);
}

- (int) match: (NSString *)str index: (int)index len: (int*)len indexIsStart: (BOOL)indexIsStart
{
static qt_QString * _qstring_str = 0L;
	return (int) qt_QRegExp_match((qt_QRegExp *)[self qt], (qt_QString *) [str toQString: &_qstring_str], (int)index, (int*)len, (int)indexIsStart);
}

- (int) match: (NSString *)str 
{
static qt_QString * _qstring_str = 0L;
	return (int) qt_QRegExp_match((qt_QRegExp *)[self qt], (qt_QString *) [str toQString: &_qstring_str], 0, 0, 1);
}

- (int) search: (NSString *)str start: (int)start
{
static qt_QString * _qstring_str = 0L;
	return (int) qt_QRegExp_search((qt_QRegExp *)[self qt], (qt_QString *) [str toQString: &_qstring_str], (int)start);
}

- (int) search: (NSString *)str 
{
static qt_QString * _qstring_str = 0L;
	return (int) qt_QRegExp_search((qt_QRegExp *)[self qt], (qt_QString *) [str toQString: &_qstring_str], 0);
}

- (int) searchRev: (NSString *)str start: (int)start
{
static qt_QString * _qstring_str = 0L;
	return (int) qt_QRegExp_searchRev((qt_QRegExp *)[self qt], (qt_QString *) [str toQString: &_qstring_str], (int)start);
}

- (int) searchRev: (NSString *)str 
{
static qt_QString * _qstring_str = 0L;
	return (int) qt_QRegExp_searchRev((qt_QRegExp *)[self qt], (qt_QString *) [str toQString: &_qstring_str], -1);
}

- (int) matchedLength
{
	return (int) qt_QRegExp_matchedLength((qt_QRegExp *)[self qt]);
}

- (NSArray *) capturedTexts
{
static qt_QStringList * _qlist = 0L;
	_qlist = qt_QRegExp_capturedTexts((qt_QRegExp *)[self qt]);
	return (NSArray *) [NSMutableArray arrayWithQStringList: _qlist];
}

- (NSString *) cap: (int)nth
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QRegExp_cap((qt_QRegExp *)[self qt], (int)nth);
	return [NSString fromQString: _qstring];
}

- (int) pos: (int)nth
{
	return (int) qt_QRegExp_pos((qt_QRegExp *)[self qt], (int)nth);
}

- (int) pos
{
	return (int) qt_QRegExp_pos((qt_QRegExp *)[self qt], 0);
}

@end
