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

#include <version.h>

#define VS_FMT(comp) (comp).revision ? "%hhu.%hhu.%hhur%hhu" : "%hhu.%hhu.%hhu"

usize v_ctosn_sz(version_t ver)
{
	struct v_comp comp = ver.comp;
	return snprintf(NULL, 0, VS_FMT(comp),
			comp.major,
			comp.minor,
			comp.bugfix,
			comp.revision);

}
usize v_ctosn(version_t ver, usize max, char out[static pOUT max])
{
	struct v_comp comp = ver.comp;
	const char* const fmt = VS_FMT(comp);
	
	return snprintf(out, max, fmt, 
			comp.major,
			comp.minor,
			comp.bugfix,
			comp.revision);
}

const char* v_ctoss(version_t ver)
{
	_Thread_local static char buffer[VERSION_STRING_MAX+1];
	buffer[v_ctosn(ver, VERSION_STRING_MAX+1, buffer)] = 0;
	return buffer;
}

char* v_ctos(version_t ver)
{
	usize sz = v_ctosn_sz(ver);
	char* buf = malloc(sz+1);
	v_ctosn(ver, sz+1, buf);
	return buf;
}

int v_range(version_t version, version_t r1, version_t r2)
{
	IGNORE_ALL(version, r1, r2);
	TODO("if we need this, we'll try to implement it in a way that doesn't rely on bswap'd raws (see tests to see bswap'd raws do infact work (big endian comparisons))");
}
