/*
 *
 */
 
#ifndef NO_INCLUDE_LOCL
#include "error.h"
#endif

#include <stdio.h>
#include <string.h>

int 
ERROR_count(const char *err)
{
	int n = 0;
	
	do
	{
		if (*err == '\n')
			n += 1;
	}
	while (*err++);
	
	return (n);
}

int 
ERROR_push(char *err, int size, const char *file, const char *func, 
	const char *line, const char *msg)
{
	int len = strlen(err);
	
	if ( size < len + 32 + strlen(file) + strlen(func) + strlen(line) + strlen(msg) )
	{
		if ( (4 < len) && (strcmp(err + len - 5, "\n...\n") != 0) )
			sprintf(err + len, "...\n");
		
		return (-1);
	}
	
	sprintf(err + len, "%s(%s):%s:%s\n", file, line, func, msg);
	
	return (0);
}

const char* 
ERROR_pop(char *err)
{
	char *head = err;
	
	do
	{
		if ( (*err == '\n') && (*(err + 1) != '\0') )
			head = ++err;
	}
	while (*err++);
	
	err--;
	
	if (head != err)
	{
		while (head != (--err))
			*err = *(err - 1);
		
		*head++ = '\0';
	}
	else
	{
		head = 0;
	}
	
	return (head);
}

