Skip to content
Snippets Groups Projects
int.c 9.83 KiB
Newer Older
/*-------------------------------------------------------------------------
 *
 * int.c--
 *	  Functions for the built-in integer types.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.11 1998/01/05 03:34:09 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
/*
 * OLD COMMENTS
 *		I/O routines:
 *		 int2in, int2out, int28in, int28out, int4in, int4out
 *		Conversion routines:
 *		 itoi, int2_text, int4_text
 *		Boolean operators:
 *		 inteq, intne, intlt, intle, intgt, intge
 *		Arithmetic operators:
 *		 intpl, intmi, int4mul, intdiv
 *		Arithmetic operators:
 *		 intmod, int4fac
 *
 * XXX makes massive and possibly unwarranted type promotion assumptions.
 * fix me when we figure out what we want to do about ANSIfication...
 */
#ifdef HAVE_LIMITS
#include <limits.h>
#endif

#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"		/* where the declarations go */
#ifndef SHRT_MAX
#define SHRT_MAX (0x7FFF)
#endif
#ifndef SHRT_MIN
#define SHRT_MIN (-0x8000)
#endif

/*****************************************************************************
 *	 USER I/O ROUTINES														 *
 *****************************************************************************/

/*
 *		int2in			- converts "num" to short
	return ((int32) pg_atoi(num, sizeof(int16), '\0'));
 *		int2out			- converts short to "num"

	result = (char *) palloc(7);/* assumes sign, 5 digits, '\0' */
	itoa((int) sh, result);
	return (result);
 *		int28in			- converts "num num ..." to internal form
 *		Note:
 *				Fills any nonexistent digits with NULLs.

	if (shs == NULL)
		return (NULL);
	result = (int16 (*)[]) palloc(sizeof(int16[8]));
	if ((nums = sscanf(shs, "%hd%hd%hd%hd%hd%hd%hd%hd",
					   *result,
					   *result + 1,
					   *result + 2,
					   *result + 3,
					   *result + 4,
					   *result + 5,
					   *result + 6,
					   *result + 7)) != 8)
	{
		do
			(*result)[nums++] = 0;
		while (nums < 8);
	}
	return ((int16 *) result);
 *		int28out		- converts internal form to "num num ..."

	if (shs == NULL)
	{
		result = (char *) palloc(2);
		result[0] = '-';
		result[1] = '\0';
		return (result);
	}
	rp = result = (char *) palloc(8 * 7);		/* assumes sign, 5 digits,
												 * ' ' */
	sp = *shs;
	for (num = 8; num != 0; num--)
	{
		itoa(*sp++, rp);
		while (*++rp != '\0')
			;
		*rp++ = ' ';
	}
	*--rp = '\0';
	return (result);
 *		int28in			- converts "num num ..." to internal form
 *		Note:
 *				Fills any nonexistent digits with NULLs.
int44in(char *input_string)
{
	int32	   *foo = (int32 *) palloc(4 * sizeof(int32));
	register int i = 0;

	i = sscanf(input_string,
			   "%d, %d, %d, %d",
			   &foo[0],
			   &foo[1],
			   &foo[2],
			   &foo[3]);
	while (i < 4)
		foo[i++] = 0;

	return (foo);
 *		int28out		- converts internal form to "num num ..."
int44out(int32 an_array[])
{

		output_string = (char *) palloc(16 * temp);		/* assume 15 digits +
														 * sign */
		walk = output_string;
		for (i = 0; i < temp; i++)
		{
			itoa(an_array[i], walk);
			while (*++walk != '\0')
				;
			*walk++ = ' ';
		}
		*--walk = '\0';
/*****************************************************************************
 *	 PUBLIC ROUTINES														 *
 *****************************************************************************/

/*
 *		int4in			- converts "num" to int4
	return (pg_atoi(num, sizeof(int32), '\0'));
 *		int4out			- converts int4 to "num"

	result = (char *) palloc(12);		/* assumes sign, 10 digits, '\0' */
	ltoa(l, result);
	return (result);
 *		===================
 *		CONVERSION ROUTINES
 *		===================
		elog(ABORT, "i4toi2: '%d' causes int2 underflow", arg1);
		elog(ABORT, "i4toi2: '%d' causes int2 overflow", arg1);
text *
int2_text(int16 arg1)
{
	text *result;

	int len;
	char *str;

	str = int2out(arg1);
	len = (strlen(str) + VARHDRSZ);

	result = PALLOC(len);

	VARSIZE(result) = len;
	memmove(VARDATA(result), str, (len - VARHDRSZ));

	PFREE(str);

	return(result);
} /* int2_text() */

int16
text_int2(text *string)
{
	int16 result;

	int len;
	char *str;

	len = (VARSIZE(string) - VARHDRSZ);

	str = PALLOC(len+1);
	memmove(str, VARDATA(string), len);
	*(str+len) = '\0';

	result = int2in(str);
	PFREE(str);
 
	return(result);
} /* text_int2() */

text *
int4_text(int32 arg1)
{
	text *result;

	int len;
	char *str;

	str = int4out(arg1);
	len = (strlen(str) + VARHDRSZ);

	result = PALLOC(len);

	VARSIZE(result) = len;
	memmove(VARDATA(result), str, (len - VARHDRSZ));

	PFREE(str);

	return(result);
} /* int4_text() */

int32
text_int4(text *string)
{
	int32 result;

	int len;
	char *str;

	len = (VARSIZE(string) - VARHDRSZ);

	str = PALLOC(len+1);
	memmove(str, VARDATA(string), len);
	*(str+len) = '\0';

	result = int4in(str);
	PFREE(str);
 
	return(result);
} /* text_int4() */

 *		=========================
 *		BOOLEAN OPERATOR ROUTINES
 *		=========================
 *		inteq			- returns 1 iff arg1 == arg2
 *		intne			- returns 1 iff arg1 != arg2
 *		intlt			- returns 1 iff arg1 < arg2
 *		intle			- returns 1 iff arg1 <= arg2
 *		intgt			- returns 1 iff arg1 > arg2
 *		intge			- returns 1 iff arg1 >= arg2
keyfirsteq(int16 *arg1, int16 arg2)
 *		int[24]pl		- returns arg1 + arg2
 *		int[24]mi		- returns arg1 - arg2
 *		int[24]mul		- returns arg1 * arg2
 *		int[24]div		- returns arg1 / arg2
 *		int[24]mod		- returns arg1 mod arg2
 *		int[24]fac		- returns arg1!

	if (arg1 < 1)
		result = 0;
	else
		for (result = 1; arg1 > 0; --arg1)
			result *= arg1;
	return (result);

	if (arg1 < 1)
		result = 0;
	else
		for (result = 1; arg1 > 0; --arg1)
			result *= arg1;
	return (result);
int16
int2larger(int16 arg1, int16 arg2)
	return ((arg1 > arg2) ? arg1 : arg2);
int16
int2smaller(int16 arg1, int16 arg2)
	return ((arg1 < arg2) ? arg1 : arg2);
int32
int4larger(int32 arg1, int32 arg2)
	return ((arg1 > arg2) ? arg1 : arg2);
int32
int4smaller(int32 arg1, int32 arg2)
	return ((arg1 < arg2) ? arg1 : arg2);