#define DEBUG 0
/* 
	TOWNS囲碁棋譜記録プログラム
	                                      1992/07/22  久保田俊也

	92/07/22		kifuデ−タのセルを操作する関数の集まり 
				現在のバ−ジョンはMAX_TE_NUMBER以上のデ−タを要求すると
				NULLを返す
				

*/
#include <stdlib.h>
#include "igo.h"
#include "banx.h"
#include "kiffile.h"

/*
static TE *cell= NULL;
 */
static TE cell[MAX_TE_NUMBER];
static TE *free_p;
static int te_arg_no=0;

cell_init()
{
int i;
/*
int physicalMax;
int	logicalMax;
int	maxMemory;

	if(cell==NULL){
		physicalMax = TL_checkMemory(0);
		logicalMax = TL_checkMemory(2);
		maxMemory = (( physicalMax <logicalMax ) ? physicalMax : logicalMax );
		if((cell=my_alloc(maxMemory-200*1024)==NULL){
			return -1;
		}
		max_te_number=(maxMemory-200*1024)/sizeof(TE);
	}
 
 */
	for(i=0;i<MAX_TE_NUMBER-1;i++){
		cell[i].next = &cell[i+1];
		cell[i].iro  = FREE_CELL; /* free_cell の意味で使っている */
	}
	cell[MAX_TE_NUMBER-1].next = NULL;
	cell[MAX_TE_NUMBER-1].iro  = FREE_CELL; /* free_cell の意味で使っている */

	free_p = &cell[0];
	return 0;
	
}

TE *cell_get()
{
TE *wk_te;

	if(free_p == NULL){
		return NULL;
	}else{
		wk_te = free_p;
		free_p = free_p->next;
		return wk_te;
	}

}

cell_free(TE *p)
{

    p->next = free_p;
	p->iro  = FREE_CELL;
    free_p = p;
    return 0;
}

TE_ARG cell_read()
{
TE_ARG te_arg;

	while(cell[te_arg_no].iro  == FREE_CELL){
		if( te_arg_no > MAX_TE_NUMBER-1){
			te_arg_no = 0;
			te_arg.no = -1;
			return (te_arg);
		}
		te_arg_no++;
	}

	if( te_arg_no > MAX_TE_NUMBER-1){
		te_arg_no = 0;
		te_arg.no = -1;
		return (te_arg);
	}

	te_arg.no      = te_arg_no;
	if(cell[te_arg_no].prev == NULL){
		te_arg.prev = -1;
	}else{
		te_arg.prev    = cell[te_arg_no].prev - &cell[0];
	}
	if(cell[te_arg_no].next == NULL){
		te_arg.next = -1;
	}else{
		te_arg.next    = cell[te_arg_no].next - &cell[0];
	}
	if(cell[te_arg_no].brother == NULL){
		te_arg.brother = -1;
	}else{
		te_arg.brother    = cell[te_arg_no].brother - &cell[0];
	}
	te_arg.iro     = cell[te_arg_no].iro;
	te_arg.ichi    = cell[te_arg_no].ichi;
	te_arg.comment = cell[te_arg_no].comment;
	te_arg_no++;

	return (te_arg);
}

int cell_write(TE_ARG te_arg)
{
static int i;

	if (te_arg.no > MAX_TE_NUMBER-1){
		return (-1);
	}

	i               = te_arg.no;
	if(te_arg.prev == -1){
		cell[i].prev    = NULL;
	}else{
		cell[i].prev    = te_arg.prev + &cell[0];
	}
	if(te_arg.next == -1){
		cell[i].next    = NULL;
	}else{
		cell[i].next    = te_arg.next + &cell[0];
	}
	if(te_arg.brother == -1){
		cell[i].brother   = NULL;
	}else{
		cell[i].brother   = te_arg.brother + &cell[0];
	}
	cell[i].iro     = te_arg.iro;
	cell[i].ichi    = te_arg.ichi;
	cell[i].comment = te_arg.comment;

	return (0);
}

int cell_write_finish()
{
int i;
TE *wk_p;

	wk_p = NULL;
	for(i=0;i<MAX_TE_NUMBER;i++){
		if(cell[MAX_TE_NUMBER-i-1].iro == FREE_CELL){
			cell[MAX_TE_NUMBER-i-1].next = wk_p;
			wk_p         = &cell[MAX_TE_NUMBER-i-1];
		}
	}

	free_p = wk_p;

	return (0);
}
