Newer
Older
/*-------------------------------------------------------------------------
*
* int.c
* Functions for the built-in integer types (except int8).
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
*
*-------------------------------------------------------------------------
*/
/*
* OLD COMMENTS
* int2in, int2out, int2recv, int2send
* int4in, int4out, int4recv, int4send
* int2vectorin, int2vectorout, int2vectorrecv, int2vectorsend
* Boolean operators:
* inteq, intne, intlt, intle, intgt, intge
* Arithmetic operators:
* intpl, intmi, int4mul, intdiv
* Arithmetic operators:
* intmod
#include <ctype.h>
#include <limits.h>
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
#define Int2VectorSize(n) (offsetof(int2vector, values) + (n) * sizeof(int2))
typedef struct
{
int32 current;
int32 finish;
int32 step;
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
/*
* int2in - converts "num" to short
Datum
int2in(PG_FUNCTION_ARGS)
char *num = PG_GETARG_CSTRING(0);
PG_RETURN_INT16(pg_atoi(num, sizeof(int16), '\0'));
* int2out - converts short to "num"
Datum
int2out(PG_FUNCTION_ARGS)
int16 arg1 = PG_GETARG_INT16(0);
char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */
pg_itoa(arg1, result);
PG_RETURN_CSTRING(result);
/*
* int2recv - converts external binary format to int2
*/
Datum
int2recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_INT16((int16) pq_getmsgint(buf, sizeof(int16)));
}
/*
* int2send - converts int2 to binary format
*/
Datum
int2send(PG_FUNCTION_ARGS)
{
int16 arg1 = PG_GETARG_INT16(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, arg1, sizeof(int16));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
* construct int2vector given a raw array of int2s
* If int2s is NULL then caller must fill values[] afterward
*/
int2vector *
buildint2vector(const int2 *int2s, int n)
{
result = (int2vector *) palloc0(Int2VectorSize(n));
if (n > 0 && int2s)
memcpy(result->values, int2s, n * sizeof(int2));
/*
* Attach standard array header. For historical reasons, we set the index
* lower bound to 0 not 1.
SET_VARSIZE(result, Int2VectorSize(n));
result->ndim = 1;
result->dataoffset = 0; /* never any nulls */
result->elemtype = INT2OID;
result->dim1 = n;
result->lbound1 = 0;
return result;
}
/*
* int2vectorin - converts "num num ..." to internal form
Datum
int2vectorin(PG_FUNCTION_ARGS)
char *intString = PG_GETARG_CSTRING(0);
int2vector *result;
int n;
result = (int2vector *) palloc0(Int2VectorSize(FUNC_MAX_ARGS));
for (n = 0; *intString && n < FUNC_MAX_ARGS; n++)
while (*intString && isspace((unsigned char) *intString))
intString++;
result->values[n] = pg_atoi(intString, sizeof(int16), ' ');
while (*intString && !isspace((unsigned char) *intString))
while (*intString && isspace((unsigned char) *intString))
intString++;
if (*intString)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("int2vector has too many elements")));
SET_VARSIZE(result, Int2VectorSize(n));
result->ndim = 1;
result->dataoffset = 0; /* never any nulls */
result->elemtype = INT2OID;
result->dim1 = n;
result->lbound1 = 0;
PG_RETURN_POINTER(result);
Bruce Momjian
committed
* int2vectorout - converts internal form to "num num ..."
Datum
int2vectorout(PG_FUNCTION_ARGS)
int2vector *int2Array = (int2vector *) PG_GETARG_POINTER(0);
nnums = int2Array->dim1;
char *rp;
Bruce Momjian
committed
char *result;
/* assumes sign, 5 digits, ' ' */
rp = result = (char *) palloc(nnums * 7 + 1);
for (num = 0; num < nnums; num++)
if (num != 0)
*rp++ = ' ';
pg_itoa(int2Array->values[num], rp);
while (*++rp != '\0')
;
}
*rp = '\0';
PG_RETURN_CSTRING(result);
/*
* int2vectorrecv - converts external binary format to int2vector
Loading
Loading full blame...