/*
 *   ShellDict © Dirk Holtwick, 1996
 *
 *   Small example to demonstrate how to write a program
 *   wich uses the DICT files.
 */

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

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

struct Library *DitoBase;
///

/// HandleDITOError()

// Easy routine for error handling
int 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;
	}
	return(error);
}
///
/// Main()

// Main program
main(int argc, char *argv[])
{
	struct DITO_UserInfo    ui;
	struct DITO_Dict        *dt;
	struct DITO_Database    *data;
	struct DITO_Entry       *entry;
	ULONG  n;
	char   pat[255];

	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!

			DITO_GetUserInfo(&ui);

			if(ui.nr) printf("You are the registred user: %s, #%d\n\n",ui.name,ui.nr);
			else puts("You are not registred. Please do it!\n");

			if(data = DITO_CreateDatabase())
			{
				// You may now add entries to the database, e.g.
				// with DITO_LoadDatabase()

				printf("     Loading Dictionary ...\n\n");

				if(dt = DITO_OpenDict (argv[1]))
				{

					printf("     To quit just press RETURN.\n");

					do
					{
						// Delete all entries

						DITO_InitDatabase(data);

						// Ask pattern

						printf ("\n     \033[1mSEARCHPATTERN: \033[0m");
						gets(pat);

						if(pat[0])
						{
							// Load entries that fit the pattern

							DITO_LoadDictData(data, dt, pat, DITO_Cmp_First);

							// Show all found entries

							printf("\n");

							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)
								);
							}

						}
					}
					while (pat[0]);

					// Don't forget to close the DICT

					DITO_CloseDict(dt);
				}

				// 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: ShellDict [file.dict]");
}
///
