/*                      QBCC Project

This source file is licensed under the GNU Lesser Public Licence which should be 
included with the distribution of this source.

*/

/* QB String Functions */
#include "../include/stdlibh.h"
#include "../include/stringh.h"


string *strVcpy(char *fmt, ...)
{ 
	va_list p;
	string *ret = (string *)GetPointer(sizeof(string));
	string *sTmp;
	char *cTmp;	

 ret->ptr = NULL;
 ret->length = 0;

 va_start(p, fmt);

 do {
    if (*fmt == 'c') { /* String Constant (or ASCIIZ string) */
        cTmp = (char *)va_arg(p, char *);
        ret->ptr = realloc(ret->ptr, ret->length + strlen(cTmp) + 1);
		strcpy(ret->ptr + ret->length, cTmp);
		ret->length += strlen(cTmp);

    } else if (*fmt == 's') { // BASIC style string
		sTmp = (string *)va_arg(p, string *);
		if(sTmp->ptr)
		{
			ret->ptr = realloc(ret->ptr, ret->length + sTmp->length + 1);
			strncpy(ret->ptr + ret->length, sTmp->ptr, sTmp->length + 1);
			ret->length += sTmp->length;
		}
    }
 } while (*(++fmt) != '\0');

 va_end(p);

 return ret;
}


/* Simple enough to understand this one should be */
int strQcmp(string *s1, string *s2)
{
 if (s1->length > s2->length)
	return strncmp(s1->ptr, s2->ptr, s2->length);
 else
	return strncmp(s1->ptr, s2->ptr, s1->length);
}

/* Initialize string */
string NewString(void)
{
	string empty;

	empty.ptr = NULL;
	empty.length = 0;
	return empty;
}

/* Initialize string */
string *NewPTRString(void)
{
	string *empty = (string *)GetPointer(sizeof(string));

	empty->ptr = NULL;
	empty->length = 0;
	return empty;
}