
#include "quiz.h"


struct thai_phrase *		chosen_sentence;
struct thai_phrase *		chosen_word;
int							file_changed = FALSE;
struct thai_phrase			sentence_head;
struct thai_phrase			word_head;

char scan_buf[ 3 ][ MAX_STRING ];
struct thai_phrase scan = {
	&scan_buf[0][0] , &scan_buf[1][0] , &scan_buf[2][0] , 0 , 0
};


main ()
{
	if ( load_phrases () ) {
		rm_dup ( &sentence_head , "sentence" );
		rm_dup ( &word_head , "word" );
	}
	else
		puts ( "Failed to load phrases" );
	unload_phrases ();
}


rm_dup ( head , desc )
struct thai_phrase *head;
char *desc;
{
	struct thai_phrase *p , *q , *next;
	int num_duplicates , num_deletions;

	num_duplicates = 0;
	num_deletions = 0;
	for ( p = head->next; p != NULL  &&  p->next != NULL; p = p->next ) {
		for ( q = p->next; q != NULL; q = next ) {
			next = q->next;
			switch ( same ( p , q ) ) {

			case 0 :		/* different */
				break;

			case 1 :
				num_duplicates++;
				break;

			case 2 :
				del_phrase ( head , q );
				file_changed = TRUE;
				num_deletions++;
			}
		}
	}
	printf ( "%d duplicate %ss removed\n" , num_deletions , desc );
	printf ( "%d duplicate %ss not removed\n" , num_duplicates , desc );
}


same ( p , q )
struct thai_phrase *p , *q;
{
	if ( strcmp ( p->thai , q->thai ) == 0 ) {
		if ( strcmp ( p->phonetic , q->phonetic ) == 0
		&&   strcmp ( p->english , q->english ) == 0 )
			return ( 2 );		/* identical */
		return ( 1 );			/* same word, different description */
	}
	return ( 0 );				/* different word */
}
