#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "swis.h"
#include "ka_mem.h"

/*
void* __mem_alloc(const char* f, int l, int x)
{
  void* p = malloc(x);
  ka_log(ka_log_error, "+MEM: %p (%d bytes) at line %d of file %s", p, x, l, f);
  return p;
}

void __mem_free(const char* f, int l, const void* p)
{
  free((void*) p);
  ka_log(ka_log_error, "-MEM: %p at line %d of file %s", p, l, f);
}
*/

char* ka_mem_allocstring(const char* pfrom)
{
	int len = pfrom ? strlen(pfrom) : 0;

	char* pstring = ka_mem_alloc(len + 1);

	if (pstring)
	{
		if (pfrom) memmove(pstring, pfrom, len);
		pstring[len] = '\0';
	}

	return pstring;
}

char* ka_mem_allocvprint(const char* pformat, va_list arg)
{
	va_list arg2;
	va_copy(arg2, arg);

	int size = vsnprintf(NULL, 0, pformat, arg) + 1;
	void* p = ka_mem_alloc(size);

	if (p) vsnprintf(p, size, pformat, arg2);
	va_end(arg2);

	return p;
}

char* ka_mem_allocprint(const char* pformat, ...)
{
	va_list arg;
	va_start(arg, pformat);
	void* p = ka_mem_allocvprint(pformat, arg);
	va_end(arg);

	return p;
}

int stricmp(const char* pa, const char* pb)
{
	int val = 0;

	if (_swix(Territory_Collate, _INR(0, 3) | _OUT(0)
			, -1, pa, pb, 1, &val))
		val = strcmp(pa, pb);

	return val;
}
