#include <v_lib.h>
#include "util.h"
#include "messages.h"

#ifdef	LEAK_DEBUG

void_p mem_alloc_z(u32_t bs)
{
	if(bs == LEAK)
	{
		puts("LEAK: hit");
	}
	return (memAllocZ)(bs);
}

#endif	/* LEAK_DEBUG */

u32_t random_seed = 1;
vhandle_t random_gen = LIBV_INVALID_HANDLE;
char dotchar = IPL_DOT_CHAR;

void
filePrintFloatStr(vhandle_t out,char dotchar,vec_t v,char *str)
{
	char buf[64], *ptr;
	sprintf(buf,"%g",v);
	for(ptr=buf;*ptr;ptr++)
		if(*ptr == '.')
			*ptr = dotchar;
	filePrintf(out,"%s%s",buf,str);
}

vboolean
lexSkip_r(vhandle_t lexer,char *keyword)
{
	CALLER_NAME(ipl_skip_r)
	DEBUG_MESSAGE(unbalanced,"unbalanced braces")
	static char *cbrace = "}";
	char *ptr;
	while((ptr = lexNext(lexer)) != NULL)
	{
		if(ptr[0] == '{' && !ptr[1])
		{
			if(!lexSkip_r(lexer,cbrace))
			{
				PARSE_ERROR(lexer,unbalanced);
				return false;
			}
			continue;
		}
		if(STR_EQ(ptr,keyword))
			return true;
	}
	return false;
}

vboolean
scriptSearchPair(vhandle_t script,char *key,char *name)
{
	CALLER_NAME(scriptSearchPair)
	char *token;
	lexSetInteger(script,LEX_CURRENT_LINE,1);
	while(lexSkip_r(script,key))
	{
		token = lexNext(script);
		if(!token)
		{
			SYN_UNEXPECTED_EOF(script);
			return false;
		}
		if(STR_EQ(token,name))
			return true;
	}
	ERROR_GENERIC_2ARG(ipl_not_found,name);
	return false;
}

