/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/core/regex/sgxwrapper.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/core/regex/regex.hpp"

using namespace std;

// The regex wrapper requires a one-line change to the
// (usually generic) sgxRegex class definition. That change
// being a reference to the underlying handling code.
// This does require extra work when/if the regex code is
// replaced, but due to the complexity of regex libraries,
// PCRE probably won't be replaced often.
sgxRegex::sgxRegex()
{
	m_bReady = FALSE;
}

sgxRegex::sgxRegex(const char *pRegexStr)
{
	re.compile(pRegexStr);
	m_bReady = TRUE;
}

sgxRegex::sgxRegex(const sgxString &regex)
{
	re.compile(regex);
	re.options(PCRE_NO_AUTO_CAPTURE);
	m_bReady = TRUE;
}

/**
Return the number of matches found within @pString. The regex must
have already been specified, usually in the constructor.
The specific results are discarded.
*/
tUINT32
sgxRegex::match(const char *pString)
{
	if (!m_bReady) {
		return 0;
	}

	sgxVector<Regexp::markers> mrk = re.match(pString);
	return mrk.size();
}

/**
Return the number of matches found within @pString, and place the results
in @results.
*/
tUINT32
sgxRegex::match(const char *pString, sgxVector<sgxString> &results)
{
	if (!m_bReady) {
		return 0;
	}

	sgxVector<Regexp::markers> mrk = re.match(pString);
	sgxVector<Regexp::markers>::iterator it = mrk.begin();

	for(;it!=mrk.end();++it) {
		results.push_back(sgxString(&pString[(*it).first], (*it).second));
	}

	return mrk.size();
}

/**
Return the number of matches found within @string. The regex must
have already been specified, usually in the constructor.
The specific results are discarded.
*/
tUINT32
sgxRegex::match(const sgxString &string)
{
	return match(string.c_str());
}

