
#include "postgres.h"
#include "catalog/pg_type.h" /* For xxxOID constants */
#include <stdlib.h>
#include <string.h>
#include "pg.h"
#include "libpq-int.h"

int PQgetchar(
	const PGresult *res,
	int tup_num,
	int field_num,
	unsigned char *charp)
{
	if(!charp || !res || PQftype(res, field_num) != CHAROID)
	{
		errno = EINVAL;
		return 0;
	}

	if(PQfformat(res, field_num) == 0)
	{
		unsigned int v;

		errno = 0;
		v = (unsigned int)strtol(PQgetvalue(res, tup_num, field_num), NULL, 10);
		if(v == 0 && errno)
			return 0;

		if(v > UCHAR_MAX)
		{
			errno = ERANGE;
			return 0;
		}

		*charp = (unsigned char)v;
		return 1;
	}

	/* binary format conversion */
	*charp= *(unsigned char *)PQgetvalue(res, tup_num, field_num);
	return 1;

}

int PQgetint2(
	const PGresult *res,
	int tup_num,
	int field_num,
	unsigned short *i2p)
{
	if(!i2p || !res || PQftype(res, field_num) != INT2OID)
	{
		errno = EINVAL;
		return 0;
	}

	if(PQfformat(res, field_num) == 0)
	{
		unsigned int v;

		errno = 0;
		v = (unsigned int)strtol(PQgetvalue(res, tup_num, field_num), NULL, 10);
		if(v == 0 && errno)
			return 0;

		if(v > USHRT_MAX)
		{
			errno = ERANGE;
			return 0;
		}

		*i2p = (unsigned short)v;
		return 1;
	}

	/* binary format conversion */
	*i2p = ntohs(*(unsigned short *)PQgetvalue(res, tup_num, field_num));
	return 1;
}

int PQgetint4(
	const PGresult *res,
	int tup_num,
	int field_num,
	unsigned int *i4p)
{
	Oid ftype = PQftype(res, field_num);
	char *val = PQgetvalue(res, tup_num, field_num);

	if(!i4p || !res || (ftype != INT4OID && ftype != FLOAT4OID))
	{
		errno = EINVAL;
		printf("failed getint4\n");
		return 0;
	}

	val = PQgetvalue(res, tup_num, field_num);
	if(PQfformat(res, field_num) == 0)
	{
		if(ftype == INT4OID)
		{
			unsigned int v;

			errno = 0;
			v = (unsigned int)strtol(val, NULL, 10);
			if(v == 0 && errno)
				return 0;

			*i4p = v;
		}
		else
		{
			float f;

			errno = 0;
			f = (float)strtod(val, NULL);
			if(f == 0 && errno)
				return 0;

			*(float *)i4p = f;
		}

		return 1;
	}

	/* binary format conversion */
	*i4p = ntohl(*(unsigned int *)val);
	return 1;
}

int PQgetint8(
	const PGresult *res,
	int tup_num,
	int field_num,
	void *i8p)
{
	unsigned int *v;
	unsigned int *i8;
	char *val;
	Oid ftype = PQftype(res, field_num);

	if(!i8p || !res || (ftype != INT8OID && ftype != FLOAT8OID))
	{
		errno = EINVAL;
		return 0;
	}

	val = PQgetvalue(res, tup_num, field_num);

	if(PQfformat(res, field_num) == 0)
	{
		if(ftype == INT8OID)
		{
#if defined(HAVE_LONG_LONG_INT_64) && defined(HAVE_STRTOLL)
			uint64 v;

			errno = 0;
			v = (uint64)strtoll(val, NULL, 10);
			if(v == 0 && errno)
				return 0;

			*(uint64 *)i8p = v;

#else
			unsigned long v;

			errno = 0;
			v = (unsigned long)strtol(val, NULL, 10);
			if(v == 0 && errno)
				return 0;

			*(unsigned long *)i8p = v;
#endif
		}
		else
		{
			double d;

			errno = 0;
			d = strtod(val, NULL);
			if(d == 0 && errno)
				return 0;

			*(double *)i8p = d;
		}

		return 1;
	}

	/* binary format conversion */
	i8 = (unsigned int *)i8p;
	v = (unsigned int *)val;

	i8 = (unsigned int *)i8p;
	if(1 != htonl(1))
	{
		i8[0] = ntohl(v[1]);
		i8[1] = ntohl(v[0]);
	}
	else
	{
		i8[0] = v[0];
		i8[1] = v[1];
	}

	return 1;
}



