/*
**      $VER: TextDSLib.c 37.0 (30.01.97)
**
**      Demo program for ds.library
**
**      (C) Copyright 1997 Markus Hillenbrand
**      All Rights Reserved.
*/

#include <stdio.h>
#include <string.h>

#include <exec/types.h>
#include <libraries/ds.h>

#include <clib/ds_protos.h>
#include <clib/exec_protos.h>

#include <pragma/ds_lib.h>

struct Library *DSBase = NULL;

/// Test for DS_BTree

#define KEYSIZE 7

typedef struct
	{
	char key[KEYSIZE];
	char name[40];
	} customer;

int mycompare(void *s1, void *s2)
{
	return strcmp((char *)s1,(char *)s2);
}

void DS_BTree_Test()
{
	printf("B-Tree on Disk\n--------------\n\n");

	BTREE t;

	if (DS_BTreeOpen(&t,"RAM:1.tree",KEYSIZE, sizeof(customer), mycompare))
		{
		customer k1 	= { "003001", "Markus Hillenbrand" };
		customer k2  	= { "000005", "Arnold Schwarzenegger" };
		customer k3 	= { "000201", "Bruce Willis" };
		customer k4 	= { "000003", "Claudia Schiffer" };
		customer k5 	= { "000401", "Naomi Cambell" };
		customer k6 	= { "020002", "Sylvester Stallone" };
		customer k7 	= { "000001", "Bill Clinton" };
		customer k8 	= { "103001", "Helmut Kohl" };
		customer k9 	= { "200005", "Boris Jelzin" };
		customer kA		= { "300201", "Vincent van Gogh" };
		customer kB  	= { "500401", "Johann Sebastian Bach" };
		customer kC 	= { "500402", "Johann Wolfgang von Goethe" };
		customer kD 	= { "620002", "Donald Duck" };
		customer kE 	= { "700001", "Veronica Ferres" };

		if (!DS_BTreeInsert(t, k1.key, &k1)) printf("1 already in tree\n");
		if (!DS_BTreeInsert(t, k2.key, &k2)) printf("2 already in tree\n");
		if (!DS_BTreeInsert(t, k3.key, &k3)) printf("3 already in tree\n");
		if (!DS_BTreeInsert(t, k4.key, &k4)) printf("4 already in tree\n");
		if (!DS_BTreeInsert(t, k5.key, &k5)) printf("5 already in tree\n");
		if (!DS_BTreeInsert(t, k6.key, &k6)) printf("6 already in tree\n");
		if (!DS_BTreeInsert(t, k7.key, &k7)) printf("7 already in tree\n");
		if (!DS_BTreeInsert(t, k8.key, &k8)) printf("8 already in tree\n");
		if (!DS_BTreeInsert(t, k9.key, &k9)) printf("9 already in tree\n");
		if (!DS_BTreeInsert(t, kA.key, &kA)) printf("A already in tree\n");
		if (!DS_BTreeInsert(t, kB.key, &kB)) printf("B already in tree\n");
		if (!DS_BTreeInsert(t, kC.key, &kC)) printf("C already in tree\n");
		if (!DS_BTreeInsert(t, kD.key, &kD)) printf("D already in tree\n");
		if (!DS_BTreeInsert(t, kE.key, &kE)) printf("E already in tree\n");

		if (!DS_BTreeDelete(t, k2.key )) printf("2 not deleted\n");
		if (!DS_BTreeDelete(t, k7.key )) printf("7 not deleted\n");
		if (!DS_BTreeDelete(t, k7.key )) printf("7 not deleted\n");

		if (DS_BTreeGetEntry(t,kC.key,&k1)) printf("Found \"%s\" for key \"%s\"\n", k1.name, kC.key);
		if (DS_BTreeGetEntry(t,k3.key,&k1)) printf("Found \"%s\" for key \"%s\"\n", k1.name, k3.key);
		if (DS_BTreeGetEntry(t,k6.key,&k1)) printf("Found \"%s\" for key \"%s\"\n", k1.name, k6.key);
		if (DS_BTreeGetEntry(t,k2.key,&k1)) printf("Found \"%s\" for key \"%s\"\n", k1.name, k7.key);
		if (DS_BTreeGetEntry(t,k7.key,&k1)) printf("Found \"%s\" for key \"%s\" (wrong !)\n", k1.name, k7.key); else printf("Found nothing for key %s, and that is correct.\n", k7.key);

		if (DS_BTreeGetEntry(t,"Hello ",&k1)) printf("Such a shit\n");

		DS_BTreeClose(t);
		}
	else printf("Could not open/create BTree on disk.\n");
}

void main (int argc, char **argv)
{
	struct Task *task = FindTask(0);
	
	if (((long)task->tc_SPUpper-(long)task->tc_SPLower)>=20000)
		{
		DSBase = OpenLibrary("ds.library", 37);
		if (DSBase)
			{
			DS_BTree_Test();

			CloseLibrary(DSBase);
			}
		else printf("Library not opened.\n");
		}
	else printf("Stack must be at least 20.000\n");
}
