/*********************************************************
 * Gotcha library example code.
 * (c)1995 Thomas Bickel. All rights reserved.
 *
 * Shows usage of GL_GetListsDiffs
 *********************************************************/
#include <stdio.h>

#include <exec/lists.h>
#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>

#include <clib/gotchalib_protos.h>
#include <pragmas/gotchalib_pragmas.h>
#include <libraries/gotchalib.h>

/***********************************************************************/
void showlist(struct List *l, UBYTE *list)
{
	struct Node *nn;

	nn = l->lh_Head;
	if (NULL == nn->ln_Succ) printf("No entries %s\n",list);
	else {
		printf("Strings %s:\n",list);
		while (nn->ln_Succ) {
			puts(nn->ln_Name);
			nn = nn->ln_Succ;
		}
	}
}

int main(long argc, char *argv[])
{
	struct Library *GotchaLibBase;
	struct List *old, *new, *onlyinnew=NULL, *notinnew=NULL, *inboth=NULL;

	if (GotchaLibBase = OpenLibrary("gotcha.library",1)) {

		old = GL_NewList();
		new = GL_NewList();

		if (old && new) {

            GL_AddNodeString("A",old,AN_TAIL,0);
            GL_AddNodeString("B",old,AN_TAIL,0);
            GL_AddNodeString("c",old,AN_TAIL,0);
            GL_AddNodeString("D",old,AN_TAIL,0);
            GL_AddNodeString("E",old,AN_TAIL,0);

            GL_AddNodeString("C",new,AN_TAIL,0);
            GL_AddNodeString("d",new,AN_TAIL,0);
            GL_AddNodeString("E",new,AN_TAIL,0);
            GL_AddNodeString("f",new,AN_TAIL,0);
            GL_AddNodeString("G",new,AN_TAIL,0);

			if (GL_GetListsDiffs(old,new,&onlyinnew,&notinnew,&inboth,FN_I)) {

				puts("\nNot case sensitive gives:");
				showlist(onlyinnew,"only in 'new'");
				showlist(notinnew,"that are not in 'new'");
				showlist(inboth,"in both lists");

				/* we can call GL_GetListsDiffs again without freeing
				 * the lists that were allocated in the previous call
                 * because gotcha.library will do this for us.
				 * This is nice but we MUST take care not to pass crap!
				 * So don't forget to 'NULL' the variables else!
				 */
				if (GL_GetListsDiffs(old,new,&onlyinnew,&notinnew,&inboth,FN_S)) {

					puts("\nCase sensitive gives:");
					showlist(onlyinnew,"only in 'new'");
					showlist(notinnew,"that are not in 'new'");
					showlist(inboth,"in both lists");

					GL_FreeList(onlyinnew);		/* free lists we asked for! */
					GL_FreeList(notinnew);
					GL_FreeList(inboth);
				}
			}

		} else
			puts("Out of memory!");

		if (new) GL_FreeList(new);
		if (old) GL_FreeList(old);

		CloseLibrary(GotchaLibBase);
	} else
		puts("Could not open 'gotcha.library'!");
}
