#include "wordfinder.h"
#include "locale.h"

extern struct Catalog *catalog;

extern APTR app,
    wi_main,st_word,cy_dic,tx_sense,st_path,
    wi_aboutmui;

extern char *dicname[20],*dictype[20];


void QuitFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    if (MUI_Request(app,wi_main,0,Gcs(16,MSG_16),Gcs(17,MSG_17),Gcs(18,MSG_18),NULL))
        DoMethod(app, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
}

struct Hook QuitHook = {{NULL,NULL},(void *)QuitFunc,NULL,NULL};

void AboutmuiFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{

    if (!wi_aboutmui)
    {
        wi_aboutmui = AboutmuiObject,
            MUIA_Window_RefWindow,wi_main,
            MUIA_Aboutmui_Application, app,
        End;
    }

    if (wi_aboutmui)
        set(wi_aboutmui, MUIA_Window_Open, TRUE);

}

struct Hook AboutmuiHook = {{NULL,NULL},(void *)AboutmuiFunc,NULL,NULL};

void FindFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{

    char ipath[256];
    char dpath[256];
    char dfile[128];
    char ifile[128];
    char buf[1024];
    char *word,*sense,*path;
    int num,aa,bb,cc,len,slen,fres;
    FILE *fhi,*fhd;
    BOOL searching,found;

    get(cy_dic,MUIA_Cycle_Active,&num);
    get(st_word,MUIA_String_Contents,&word);
    get(st_path,MUIA_String_Contents,&path);

    strcpy(dpath,path);
    strcpy(ipath,path);
    sprintf(dfile,"%s/%s_%c.ltdic",dictype[num],dictype[num],word[0]);
    sprintf(ifile,"%s/%s_%c.ltidx",dictype[num],dictype[num],word[0]);

    AddPart(dpath,dfile,250);
    AddPart(ipath,ifile,250);

    if ((fhd = fopen(dpath,"r"))!=NULL)
    {
        if ((fhi = fopen(ipath,"r"))!=NULL)
        {
            aa=0;
            bb=0;
            fseek(fhi,0,SEEK_END);
            cc = ftell(fhi)/4;
            fseek(fhi,0,SEEK_SET);
            searching = TRUE;
            found = FALSE;

            while (searching)
            {
                fseek(fhi,(bb*4),SEEK_SET);
                fread(&num,4,1,fhi);
                fread(&len,4,1,fhi);
                fseek(fhd,num,SEEK_SET);
                fgets(buf,1000,fhd);
                buf[strlen(buf)-1]='\0';
                fres = strcmp(word,buf);

                if(fres==0)
                {
                    found = TRUE;

                    slen = len-num-2;

                    if (sense = AllocVec(slen+16,MEMF_PUBLIC|MEMF_CLEAR))
                    {
                        fseek(fhd,num,SEEK_SET);
                        fread(sense,1,slen,fhd);
                        set(st_word, MUIA_String_Contents, word);
                        set(tx_sense, MUIA_TextEditor_Contents, sense);
                    FreeVec(sense);
                    }

                    aa=0;
                    bb=0;
                    cc=0;
                }
                else if (fres<0)
                {
                    cc=bb;
                }
                else if (fres>0)
                {
                    aa=bb;
                }
                bb=(aa+cc)/2;
                if ((aa==bb) || (cc==bb))
                    searching = FALSE;

            }
            if (!found)
                set(tx_sense,MUIA_TextEditor_Contents,Gcs(19,MSG_19));
        fclose(fhi);
        }
        else
            MUI_Request(app,wi_main,0,Gcs(12,MSG_12),Gcs(13,MSG_13),Gcs(20,MSG_20),FilePart(ipath));
    fclose(fhd);
    }
    else
        MUI_Request(app,wi_main,0,Gcs(12,MSG_12),Gcs(13,MSG_13),Gcs(20,MSG_20),FilePart(dpath));

    set(wi_main,MUIA_Window_ActiveObject,st_word);

}

struct Hook FindHook ={{NULL,NULL},(void *)FindFunc,NULL,NULL};
