#include "include/glbtypes.h"
#include "include/glbproto.h"
#include "include/globvars.h"

cmp_str str_cmp(char * s1,char * s2)
{
	while(!!*s1&!!*s2&(*s1==*s2)&(*s1!=32)) {s1++;s2++;};
	if (*s1==*s2) return eq;
	if (!*s1) return rlong;
	if (!*s2) return llong;
	if (*s1>*s2) return left;
	return right;
};

char * next_token1(char * str)
{
	int level=0;
	while(*str==32) str++;
	while(!(((!*str)||(*str==32))&!level))
	{
		if (*str=='(') level++;
		if (*str==')') level--;
		str++;
	}
	/* while(*str==32) str++; */
	return str;
}

char * next_token(char * str)
{
	while(*str==32) str++;
	while(!((!*str)||(*str==32)))	str++;
	/* while(*str==32) str++; */
	return str;
}

/*********************  Gets variable  ***********************************/

char * get_variable(char * str)
{
	while(*str==32) str++;
	while(isidch(*str)) str++;
	return str;
}


/********************* End get_variable **********************************/

char * get_variable_name(char * str)
{
	while(*str==32) str++;
	if (!isalpha(*str)&(*str!='_')&(*str!=4))
		return str; /* not an identifier */
	else
		str++;
	while(isidch(*str)) str++;
	return str;
}

int insert_spaces(char * src,char *dst)
/* Separates special chars by spaces */
{
	while (*src)	{
	if (isalnum(*src)!=isalnum(*(src-1)))
	{	*dst=32;
		dst++;
	 };
	 *dst++=*src++;
	}
*dst=0;  return 0;
}

/****************************************************************/
/*
char * get_var_cp(char * str,listnode l)
{
	int ll=0;
	while(*str==32) str++;
	while(isalnum(*str))
		{
		*((l->data)+ll)=*str;
		ll++;
		str++;
		};
	while(*str==32) str++;
	return str;
}
*/
/**** Gets string BEGINING with Nth token. String DOESN'T ENDS AFTER TOKEN *****/

char * get_nth_token(int n,char * str)
{
	int i;
	while(*str==32) str++;
	for(i=1;i<n;i++)
		{
		while(!(!*str)&*str!=32) str++;
		while(*str==32) str++;
		};
	return str;
}

/*************************** End get_nth_token ************************************/

int refresh_buffer(char * buffer,int howmuch,int bufsize)
{
if ((!howmuch)||(!bufsize)) return 0;
if(global_buffers_lst!=NULL)
	{
	if(global_buffers_lst->data!=current_global_buffer)
		{
		int i;
		i=strlen(global_buffers_lst->data);
		if(i>=howmuch)
			{
			memmove(buffer,buffer+howmuch,bufsize-howmuch);
			memmove(buffer+bufsize-howmuch,global_buffers_lst->data,howmuch);
			if(i==howmuch)
				global_buffers_lst=rm_cdr_next(global_buffers_lst);
			else
				memmove(global_buffers_lst->data,global_buffers_lst->data+howmuch,i-howmuch);
			}
		else
			{
			memmove(buffer,buffer+i,bufsize-i);
			memmove(buffer+bufsize-i,global_buffers_lst->data,i);
			global_buffers_lst=rm_cdr_next(global_buffers_lst);
			/* recursively shift buffers */
			refresh_buffer(buffer,howmuch-i,bufsize);
			};
		}
	else   /* if global_buffers_lst->data==current_global_buffer ...*/
		{
		memmove(buffer,buffer+howmuch,bufsize-howmuch);
/*
		memmove(buffer+bufsize-howmuch,global_buffers_lst->data,howmuch);
		memmove(global_buffers_lst->data,global_buffers_lst->data+howmuch,bufsize-howmuch);
*/
		memmove(buffer+bufsize-howmuch,global_bufptr,howmuch);
		global_bufptr+=howmuch;
		};
	}
else
	{
	memmove(buffer,buffer+howmuch,bufsize-howmuch);
	memmove(buffer+bufsize-howmuch,global_bufptr,howmuch);
	global_bufptr+=howmuch;
	};
return howmuch;
};

int normalize_buffer(char * bufptr,int bufsize)
{
int howmuch;
if((global_buffers_lst==NULL)||(current_global_buffer==NULL))
	return 0;

howmuch=bufsize-(bufptr-current_global_buffer);

if(global_buffers_lst->data!=current_global_buffer)
	{
	int i;
	i=strlen(global_buffers_lst->data);
	if(i>=howmuch)
		{
		memmove(bufptr,global_buffers_lst->data,howmuch);
		if(i==howmuch)
			global_buffers_lst=rm_cdr_next(global_buffers_lst);
		else
			memmove(global_buffers_lst->data,global_buffers_lst->data+howmuch,i-howmuch);
		}
	else
		{
		memmove(bufptr,global_buffers_lst->data,i);
		global_buffers_lst=rm_cdr_next(global_buffers_lst);
		/* recursively shift buffers */
		normalize_buffer(bufptr+i,bufsize);
		};
	}
else 
	{
	memmove(bufptr,global_bufptr,howmuch);
	global_bufptr+=howmuch;
	};
return howmuch;
};

char * create_new_buffer(char * bufptr,int bufsize)
{
char * newbuf;
if (!bufsize) return NULL;
if((newbuf=my_malloc(bufsize))==NULL)
	return NULL;
if(bufptr!=NULL)
	{
	memmove(newbuf,bufptr,bufsize-(bufptr-current_global_buffer));
	memmove(newbuf+bufsize-(bufptr-current_global_buffer),global_bufptr,bufptr-current_global_buffer);
	global_bufptr+=bufptr-current_global_buffer;
	}
else
	{
	memmove(newbuf,global_bufptr,bufsize);
	global_bufptr+=bufsize;
	};
global_buffers_lst=mk_head_lnd(global_buffers_lst,newbuf,PARTOFOP);
return current_global_buffer=newbuf;
};
