WONKY



LOG | FILES | OVERVIEW


#ifndef GSTRING_C
#define GSTRING_C GSTRING_C
#include <gcc_string.h>


size_t gstrlen(const char *str)
{
	size_t i;
	for(i=0;str[i]!='\0';++i);
	return i;
}
size_t gstrnlen(const char *str,size_t limit)
{
	size_t i;
	for(i=0;i<limit && str[i]!='\0';++i);
	return i;
}

char* gstr_append(const char *lead,const char *follower)
{
	char *ret,*hold;
	hold=ret=wonky_malloc(gstrlen(lead) + gstrlen(follower)+1);
	while(*(hold++)=*(lead++));
	hold--;
	while(*(hold++)=*(follower++));
	return ret;
}

char* gstrn_append(char *lead,char *follower,size_t overall_limit)
{
	char *ret;
	size_t lead_size;
	size_t follower_size;

	lead_size=gstrnlen(lead,overall_limit);
	follower_size=gstrnlen(follower,overall_limit);

	if(lead_size+follower_size>overall_limit)
		return gstrncpy("",1);

	ret=wonky_malloc(lead_size+follower_size);
	gmemmove(ret,lead,lead_size);
	gmemmove(ret+lead_size,follower,follower_size);
	return ret;
}

void strmv(char *target,char *source)
{
	while(*(target++)=*(source++));
}
char* gstrcpy(const char *str)
{
	char *temp=wonky_malloc(gstrlen(str)+1);
	for(size_t i=0;(temp[i]=str[i])!='\0';++i);
	return temp;
}
char* gstrncpy(const char *str,size_t size)
{
	size_t i;
	char *temp=wonky_malloc(size+1);
	for(i=0;i<size;++i)
		temp[i]=str[i];
	return temp;
}
char gstr_cmp(const char *a,const char *b)
{
	while(*a==*b && *a)
		++a,++b;
	if(*a==*b)
		return 1;
	else
		return 0;
}
char gstrn_cmp(const char *a,const char *b,size_t size)
{
	size_t i;
	for(i=0;i<size;++i)
		if(a[i]!=b[i])
			return 0;
	return 1;
}
char *gstr_append_and_consume(const char *lead,const char *follower)
{
	char *ret;

	ret=gstr_append(lead,follower);

	wonky_free((void*)lead);
	wonky_free((void*)follower);

	return ret;
}
char* gstr_dup(const char *first,const char *last,size_t limit)
{
	char *ret;
	ptrdiff_t diff;
	size_t i;

	wonky_assert(first!=NULL && last!=NULL);

	diff=last-first;

	if((ssize_t)diff<0 || (size_t)diff>=limit)
	{
		ret=wonky_malloc(1);
		ret[0]='\0';
		return ret;
	}else
	{
		ret=wonky_malloc((size_t)diff+1);
		ret[(size_t)diff]='\0';

		for(i=0;first+i!=last;++i)
			ret[i]=first[i];

		return ret;
	}

	wonky_assert(SHOULD_NOT_REACH_HERE);
}
char* gstr_to_heap(const char *literal)
{
	size_t i;
	size_t literal_length;
	char *ret;

	literal_length=gstrlen(literal);

	ret=wonky_malloc(literal_length+1);

	for(i=0;i<=literal_length;++i)
		ret[i]=literal[i];

	return ret;

}
/*where and from must not overlap*/
#warning rename to gmemcpy
void gmemmove(void *where_to,const void *from_where,size_t how_many_bytes)
{
	size_t i;
	for(i=0;i<how_many_bytes;++i)
		((unsigned char*)where_to)[i]=((const unsigned char *)from_where)[i];
}
int gmemcmp(void *a,void *b,size_t num_bytes)
{
	size_t i;
	for(i=0;i<num_bytes;++i)
		if(((unsigned char*)a)[i]!=((unsigned char*)b)[i])
			/*holy cast!*/
			return ((int)((unsigned char*)a)[i])-((int)((unsigned char*)b)[i]);
	return 1;
} 
#endif