/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program is distributed in the hope that it will be 
	useful, but WITHOUT ANY WARRANTY; without even the 
	implied warranty of MERCHANTABILITY or FITNESS FOR A 
	PARTICULAR PURPOSE.

**********************************************************************/


#include	"memory_routine.h"
#include	"mlong_char.h"
#include	"xl.h"

void gc_gb_sym_field();


XL_SYM_FIELD *
set_sym_field(XL_SYM_FIELD * sf,L_CHAR * field,L_CHAR * data)
{
XL_SYM_FIELD * sf1, * sf2, * sf3;
XL_SYM_FIELD * ret,** retp;
	for ( sf1 = sf ; sf1 ; sf1 = sf1->next )
		if ( l_strcmp(sf1->name,field) == 0 )
			goto ok;
	if ( data == 0 )
		return sf;
	sf1 = mmalloc(sizeof(*sf1),gc_gb_sym_field);
	sf1->next = sf;
	sf1->name = ll_copy_mstr(field);
	sf1->data = ll_copy_mstr(data);
	return sf1;
ok:
	ret = 0;
	retp = &ret;
	for ( sf2 = sf ; sf2 != sf1 ; sf2 = sf2->next ) {
		sf3 = mmalloc(sizeof(*sf3),gc_gb_sym_field);
		sf3->next = 0;
		sf3->name = sf2->name;
		sf3->data = sf2->data;
		*retp = sf3;
		retp = &sf3->next;
	}
	if ( data ) {
		sf3 = mmalloc(sizeof(*sf3),gc_gb_sym_field);
		sf3->next = sf;
		sf3->name = ll_copy_mstr(field);
		sf3->data = ll_copy_mstr(data);
		*retp = sf3;
		return ret;
	}
	else {
		*retp = sf1->next;
		return ret;
	}
}
