#include "c4d_string.h"
#include "c4d_memory.h"

CHAR* String::GetCStringCopy(STRINGENCODING type) const
{
	LONG len = StCall(GetCStringLen, type);
	if (!len) return 0;

	CHAR *txt = GeAllocType(CHAR,len+2);
	if (!txt) return NULL;

	StCall(GetCString, txt,len+2,type);
	txt[len] = 0;

	return txt;
}

#if (defined _DEBUG) && (defined __PC)
static CHAR array[1000];

CHAR *cstr(String &str)
{
	str.GetCString(array,1000);
	return array;
}

CHAR *cstr(const String &str)
{
	str.GetCString(array,1000);
	return array;
}

#include "c4d_baselist.h"

CHAR *cstr(GeListNode *node)
{
	if (node->IsInstanceOf(Tbaselist2d))
		((BaseList2D*)node)->GetName().GetCString(array,1000);
	else
		strcpy(array,"no baselist");
	return array;
}

#include "c4d_file.h"

CHAR *cstr(Filename &fn)
{
	fn.GetString().GetCString(array,1000);
	return array;
}

CHAR *cstr(const Filename &fn)
{
	fn.GetString().GetCString(array,1000);
	return array;
}

#endif


String PtrToString(const void *t_hex)
{
	VLONG hex = (VLONG)t_hex;
	CHAR hexstring[sizeof(VULONG)*2+4];
	LONG t,i;

	t = 0;
	hexstring[t++] = '0';
	hexstring[t++] = 'x';

	for (i=sizeof(VULONG)*8-4; i>=0; i-=4)
	{
		CHAR x = CHAR((hex>>i)&0xF);
		if (x<10) hexstring[t++] = x+'0';
		else hexstring[t++] = x-10+'A';
	}
	hexstring[t]=0;
	return String(hexstring);
}

String MemoryToString(LLONG mem)
{
	if (mem>1024*1024*1024)
		return RealToString(Real(mem >> 20)/1024,0,2)+String(" GB");
	else if (mem>1024*1024)
		return RealToString(Real(mem >> 10)/1024,0,2)+String(" MB");
	else if (mem>1024)
		return RealToString(Real(mem)/(1024),0,2)+String(" KB");

	return LongToString((LONG)mem)+String(" B");
}
