/*
        askdic.c (AVE-Front Version 2 ASK Dictionary ROM Interface)
        Copyright 1995,96,97 ACCESS Co.,Ltd.
        All rights reserved.

        All rights are reserved by ACCESS CO.,LTD., 
        whether the whole or part of the source code 
        including any modifications is concerned. 
*/

/* $Header: /NetFront/AskDic/askdic.c 2     97/09/26 20:41 Katoh $ */

/**** Version String : 1999.01.25 N.Wakabayashi. ****/
const char * const ntgDicBuild =   "\nntDic Ver 1.00 Build:" __DATE__ " "  __TIME__ "\n";

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

#define zmemcpy         (memcpy)

/* ASK辞書のROMデータ */
static const unsigned char gROM_ASK[] = {
#ifdef ASK_DIC_SMALL
        #include "../res/askdic_s.rom"
#endif
#ifdef ASK_DIC_NORMAL
        #include "../res/askdic_n.rom"
#endif
};

typedef struct {
        long    length;
        char    *offset;
} FAskRom;

static FAskRom gAskRom;


/* ASK辞書ROM 制御関数 */
int
FAskRom_Open(char *in_filename, int in_mode)
{
        gAskRom.length = sizeof(gROM_ASK);
        gAskRom.offset = (char*)gROM_ASK;

        /* ファイルハンドルのダミー */
        return 1;
}

int
FAskRom_Close(int in_stream)
{
        /* 常に成功する */
        return 0;
}

int
FAskRom_Read(int in_stream, unsigned char *out_buf, int in_buflen)
{
        int s;

        s = gAskRom.length;
        if (s >= in_buflen)
                s = in_buflen;

        zmemcpy(out_buf, gAskRom.offset, s);

        gAskRom.length -= s;
        gAskRom.offset += s;

        return s;
}

int
FAskRom_Write(int in_stream, unsigned char *in_buf, int in_buflen)
{
        /* 常に失敗する */
        return -1;
}

long
FAskRom_Seek(int in_stream, long in_posx, int in_mdx)
{
        long l;

        switch (in_mdx) {
                case SEEK_SET:
                        if (in_posx < 0)
                                return -1;
                        gAskRom.length = sizeof(gROM_ASK) - in_posx;
                        gAskRom.offset = (char*)gROM_ASK + in_posx;
                        break;
                case SEEK_CUR:
                        l = gAskRom.length - in_posx;
                        if (l < 0 || l > sizeof(gROM_ASK))
                                return -1;
                        gAskRom.length = l;
                        gAskRom.offset += in_posx;
                        break;
                case SEEK_END:
                        if (in_posx > 0)
                                return -1;
                        gAskRom.length = -in_posx;
                        gAskRom.offset = ((char*)gROM_ASK + sizeof(gROM_ASK) - 1) + in_posx;
                        break;
        }

        return (gAskRom.offset - (char*)gROM_ASK);
}
