/*
 *   LSD2DITO © Dirk Holtwick, 1996
 *
 *   Converts LSD files to DITO databases
 */

/// INCLUDES & DEFS
#include <stdio.h>
#include <stdlib.h>
#include <string.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);
}
///
/// GetStr
char *GetStr(char *str, FILE *f)
{
	fgets(str, 255, f);
	str[strlen(str)-1] = 0;
	return(str);
}
///

/// Main()
// Main program
main(int argc, char *argv[]){
	struct DITO_UserInfo    ui;
	struct DITO_Database    *data;
	struct DITO_EntryInfo   ei;
	static char   word[256], str[256], trans[2000];
	FILE   *f;
	int    i;

	if(argc==3)
	{
		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())
			{

				// Open inputfile

				if(f = fopen(argv[1],"r"))
				{
					// First 6 Lines are not of  interest
					for(i=0;i<6;i++) GetStr(str,f);

					while(!feof(f))
					{
						// Add entry to database

						DITO_InitEntryInfo(&ei);

						ei.word  = GetStr(word,  f);

						trans[0] = 0;
						str[0] = 0;

						do
						{
							if(trans[0]) strcat(trans, " ");
							strcat (trans, str);
							GetStr(str,f);
						}
						while(!feof(f) && (str[0] != '&'));

						ei.trans = trans;

						DITO_InsertEntry(data, &ei);
					}

					fclose(f);

					// Save complete database
					DITO_SaveDatabase (data, argv[2], DITO_Save_All);
				}
				else puts("Couldn't open inputfile");

				// 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: LSD2DITO [lsd-file.dat] [output.voc]");
}
///
