/*

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

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.


Copyright 2011 Nick Warne
E-mail: nick@ukfsn.org

=========================
The Qxw program:
Copyright 2011 Mark Owen
http://www.quinapalus.com
=========================

This Qxw plugin enables users to remove (letter latent style)
consecutive letters from an answer before entry into the grid.

 To build:
 gcc -Wall -fPIC qxw-remove-letters.c -o qxw-remove-letters.so -shared

 To use in Qxw:
	'Message 1' contains consectutive letters to be excluded
	from grid lights wherever they occur when the Qxw engine proposes them.
	'answer' remains the same.

	If 'Message 2' contains 'all', only 'answer' proposed by the Qxw engine
	that have 'Message 1' consecutive letters within them are processed and
	forwarded for grid entries. 'answer' remains the same
*/

#include <qxw/qxwplugin.h>

int treat(const char*answer) {

int ll=strlen(answer);
int ltm=strlen(treatmessageAZ[0]);
char tmplight[ll];
char *found;
int i;
int j;
int match=0;

 if (ltm==0) // no Message 1!
	return 0;

 strcpy(tmplight,answer);

 found=strstr(tmplight, treatmessageAZ[0]); // do we have a match?

 if (found==NULL) { // process this first, as non-matches will be prevalent (I guess?)
 	if ((ll!=lightlength) || strcmp(treatmessageAZ[1],"ALL")==0) { // reject unsuitable answers 
		return 0;
 	} else {
		return treatedanswer(tmplight); // nothing to do, so return the answer untouched
 	}
 
 } else {

	if (ll!=(lightlength+ltm)) // only need to process words that
		return 0;          // fit the light after adjustment
 }

 for (i=0; i<=(ll-ltm); ++i) {
	for (j=0; j<ltm; ++j) {
		if (treatmessageAZ[0][j]==tmplight[i+j]) {
			match=1; // walk through the string[s] when matching;
				 // can occur several times in the same word
		} else {
			match=0;
			j=(ltm);
		}
	}

	if (match==1) { // found an answer that contains 'Message 1' string
		for (j=0; j<ltm; ++j) {
		tmplight[i+j]='*'; // replace matching chars with '*' to condense later
		}
	match=0;
	}
 } // do it again until substring of 'answer' doesn't match 'Message 1'
 
 j=0;
 for (i=0; i<(ll); ++i) {
	if (tmplight[i] != '*') { 
		light[j]=tmplight[i]; // condense the string
		++j;
	}
 }

 light[j]='\0';
 return treatedanswer(light);

 }

