/*
 *   ListAll © Dirk Holtwick, 1996
 *
 *   Small tool that demonstrates how to load a DITO database
 *   and how to use the single entries.
 *
 */

/// INCLUDES & DEFS
#include <stdio.h>
#include <stdlib.h>

#include <proto/exec.h>
#include <proto/dito.h>

struct Library *DitoBase;
///

/// HandleDITOError()

/*
** Easy routine for error handling
*/

void HandleDITOError(int error){
	switch(error){
		case DITO_Error_FileNotFound:
			puts("File not found!");
			break;
		case DITO_Error_NoDitoDatabase:
			puts("No DITO Database!");
			break;
		case DITO_Error_WrongLanguage:
			puts("Wrong Language!");
			break;
		case DITO_Error_Memory:
			puts("Not enough memory!");
			break;
	}
}
///
/// Main()

/*
** Main program
*/

main(int argc, char *argv[]){

	struct DITO_Database *data;
	struct DITO_Entry    *entry;
	ULONG  n;

	if(argc==2){
		if(DitoBase = OpenLibrary(DITO_NAME, DITO_VMIN)){

			/*
			** You have to create a database before adding or
			** loading entries to the structure. Do not try to
			** init the structure by yourself!
			*/

			if(data = DITO_CreateDatabase()){

				/*
				** You may now add entries to the database, e.g.
				** with DITO_LoadDatabase()
				*/

				HandleDITOError(DITO_LoadDatabase(data,argv[1]));

				/*
				** Now the contents will be listed
				*/

				for (n=0; n<data->sum; n++)
				{
					entry = DITO_GetPtr(data, n);

					printf(
						"%3d. \033[1m%s,\033[0m %s\n",
						n + 1,
						DITO_GetStr (entry, DITO_Str_Word),
						DITO_GetStr (entry, DITO_Str_Translation)
					);
				}

				/*
				** Do not forget to dispose the DITO_Database!
				*/

				DITO_DisposeDatabase(data);
			}

			CloseLibrary(DitoBase);

		}
		else puts("Couldn't open actual version of dito.library.");
	}
	else puts("USAGE: ListAll [file.voc]");
}
///
