/********************************************

	CD Dic Init Func

*********************************************/
#include    "defs.h"

#ifdef	DEBUG
void	dump(char *ptr, int sz)
{
    int     n, i, ch;
    char    *p;

    for ( n = 0 ; n < sz ; n += 16 ) {
	printf("%04x ", n);
	p = ptr;
	for ( i = 0 ; i < 16 ; i++ )
	    printf("%02x ", *(p++) & 0xFF);

	printf(" ");
	p = ptr;
	for ( i = 0 ; i < 16 ; i++ ) {
	    ch = *(p++) & 0xFF;

	    if ( (ch & 0x80) == 0 && ch >= ' ' && ch < 0x7F )
		putchar(ch);
	    else 
		putchar('.');
	}
	putchar('\n');
	ptr += 16;
    }
}
void	debug_dump(ulong block)
{
    printf("Debug Dump %08lx %08lx %08lx\n",
		block, file_start_block, file_end_block);

    if ( IO_block_read(block) )
	return;

    dump(dic_buf, 2048);
}
#endif

void	index_tab_init(int bs, int mx)
{
    int     n;

    for ( n = 0 ; n < IDX_MAX ; n++ )
	index_tab[n].ip = NULL;

    index_tab_base = bs;
    index_tab_max  = mx;

    while ( bs < mx ) {
	for ( n = 0 ; n < IDX_MAX ; n++ ) {
	    if ( index_buf[bs].id == index_tab[n].id ) {
		index_tab[n].ip = &(index_buf[bs]);
		break;
	    }
	}
	bs++;
    }
}
static	int	multi_init(INDEX *ip)
{
    int     i, n;
    int     mx, ix;
    uchar   tmp[32];
    char    ttl[32];
    MULTI   *mul;
    MULPTR  *mp;

    if ( IO_seek(ip->start_block, 0) )
	return ERR;
    if ( IO_read(tmp, 16) != 16 )
	return ERR;

    if ( (mul = (MULTI *)malloc(sizeof(MULTI))) == NULL )
	return ERR;

    mul->mul_list = NULL;
    mx = tmp[0] * 256 + tmp[1];
    ix = index_total;

    for ( n = 0 ; n < mx ; n++ ) {
	if ( IO_read(tmp, 32) != 32 )
	    return ERR;

	i = tmp[0];
	if ( (ix + i) >= INDEX_MAX )
	    return ERR;

	str_cnv(ttl, tmp + 2, 30);
	if ( (mp = (MULPTR *)malloc(sizeof(MULPTR) + strlen(ttl))) == NULL )
	    return ERR;

	strcpy(mp->mul_ttl, ttl);
	mp->bs = ix;
	mp->mx = ix + i;

	while ( i-- > 0 ) {
	    if ( IO_read(tmp, 16) != 16 )
		return ERR;
	    index_buf[ix].id = tmp[0];
	    index_buf[ix].start_block = tolong(tmp + 2);
	    index_buf[ix].block_max   = tolong(tmp + 6);
	    index_buf[ix].type[0]     = tmp[11];
	    index_buf[ix].type[1]     = tmp[12];
	    index_buf[ix].type[2]     = tmp[13];
	    ix++;
	}

	mp->next = mul->mul_list;
	mul->mul_list = mp;
    }

    mul->next = multi_top;
    multi_top = mul;
    index_total = ix;

    return FALSE;
}
static	void	multi_clear()
{
    MULPTR  *mp;
    MULTI   *mul;

    while ( (mul = multi_top) != NULL ) {
	while ( (mp = mul->mul_list) != NULL ) {
	    mul->mul_list = mp->next;
	    free(mp);
	}
	multi_top = mul->next;
	free(mul);
    }
}
int	dic_open()
{
    int     n;
    uchar   *p;

    if ( IO_block_read(1) )
	return ERR;

    index_max = toshort(dic_buf + 0);
    p = &(dic_buf[16]);

    for ( n = 0 ; n < index_max && n < INDEX_MAX && !iszero(p, 16) ; n++ ) {
	index_buf[n].id = p[0];
	index_buf[n].start_block = tolong(p + 2);
	index_buf[n].block_max   = tolong(p + 6);
	index_buf[n].type[0]     = p[11];
	index_buf[n].type[1]     = p[12];
	index_buf[n].type[2]     = p[13];
	p += 16;
    }
    index_total = index_max = n;

    multi_clear();
    dicin_gaiji_flag = 0;

    for ( n = 0 ; n < index_max ; n++ ) {
	if ( index_buf[n].id == 0xFF )
	    multi_init(&(index_buf[n]));
	else if ( index_buf[n].id == 0xF1 ) {
	    zen_gaiji_start_block = file_start_block +
				    index_buf[n].start_block - 1;
	    zen_gaiji_end_block   = zen_gaiji_start_block +
				    index_buf[n].block_max - 1;
	    dicin_gaiji_flag |= 1;
	} else if ( index_buf[n].id == 0xF2 ) {
	    han_gaiji_start_block = file_start_block +
				    index_buf[n].start_block - 1;
	    han_gaiji_end_block   = han_gaiji_start_block +
				    index_buf[n].block_max - 1;
	    dicin_gaiji_flag |= 2;
	}
    }

    index_tab_init(0, index_max);
    return FALSE;
}
