1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* numutils.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* utility functions for I/O of built-in numeric types.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-02-18 01:01:34 +01:00
|
|
|
* integer: pg_atoi, pg_itoa, pg_ltoa
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-08-29 06:13:13 +02:00
|
|
|
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2004-10-04 16:42:48 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/numutils.c,v 1.66 2004/10/04 14:42:46 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-07-13 00:59:15 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <math.h>
|
1997-11-17 17:26:27 +01:00
|
|
|
#include <limits.h>
|
2004-03-11 03:11:14 +01:00
|
|
|
#include <ctype.h>
|
2000-07-13 00:59:15 +02:00
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/builtins.h"
|
1997-12-19 03:09:10 +01:00
|
|
|
|
1997-11-17 17:26:27 +01:00
|
|
|
#ifndef INT_MAX
|
|
|
|
#define INT_MAX (0x7FFFFFFFL)
|
|
|
|
#endif
|
|
|
|
#ifndef INT_MIN
|
1999-07-09 19:40:31 +02:00
|
|
|
#define INT_MIN (-INT_MAX-1)
|
1997-11-17 17:26:27 +01:00
|
|
|
#endif
|
|
|
|
#ifndef SHRT_MAX
|
|
|
|
#define SHRT_MAX (0x7FFF)
|
|
|
|
#endif
|
|
|
|
#ifndef SHRT_MIN
|
1999-07-09 19:40:31 +02:00
|
|
|
#define SHRT_MIN (-SHRT_MAX-1)
|
1997-11-17 17:26:27 +01:00
|
|
|
#endif
|
|
|
|
#ifndef SCHAR_MAX
|
|
|
|
#define SCHAR_MAX (0x7F)
|
|
|
|
#endif
|
|
|
|
#ifndef SCHAR_MIN
|
1999-07-09 19:40:31 +02:00
|
|
|
#define SCHAR_MIN (-SCHAR_MAX-1)
|
1997-11-17 17:26:27 +01:00
|
|
|
#endif
|
|
|
|
|
2002-08-27 22:54:47 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pg_atoi: convert string to integer
|
|
|
|
*
|
2004-03-11 03:11:14 +01:00
|
|
|
* 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes).
|
2002-08-27 22:54:47 +02:00
|
|
|
*
|
2004-03-11 03:11:14 +01:00
|
|
|
* allows any number of leading or trailing whitespace characters.
|
|
|
|
*
|
|
|
|
* 'c' is the character that terminates the input string (after any
|
|
|
|
* number of whitespace characters).
|
2002-08-27 22:54:47 +02:00
|
|
|
*
|
2003-07-27 06:53:12 +02:00
|
|
|
* Unlike plain atoi(), this will throw ereport() upon bad input format or
|
2002-08-27 22:54:47 +02:00
|
|
|
* overflow.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
int32
|
|
|
|
pg_atoi(char *s, int size, int c)
|
|
|
|
{
|
2003-07-27 06:53:12 +02:00
|
|
|
long l;
|
2004-03-11 03:11:14 +01:00
|
|
|
char *badp;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-12 18:04:35 +02:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Some versions of strtol treat the empty string as an error, but
|
|
|
|
* some seem not to. Make an explicit test to be sure we catch it.
|
1998-09-12 18:04:35 +02:00
|
|
|
*/
|
2004-01-07 19:56:30 +01:00
|
|
|
if (s == NULL)
|
2003-07-27 06:53:12 +02:00
|
|
|
elog(ERROR, "NULL pointer");
|
|
|
|
if (*s == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("invalid input syntax for integer: \"%s\"",
|
|
|
|
s)));
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
l = strtol(s, &badp, 10);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2004-03-11 03:11:14 +01:00
|
|
|
/* We made no progress parsing the string, so bail out */
|
|
|
|
if (s == badp)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("invalid input syntax for integer: \"%s\"",
|
|
|
|
s)));
|
2004-03-11 03:11:14 +01:00
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Skip any trailing whitespace; if anything but whitespace remains
|
|
|
|
* before the terminating character, bail out
|
2004-03-11 03:11:14 +01:00
|
|
|
*/
|
2004-04-02 00:51:31 +02:00
|
|
|
while (*badp != c && isspace((unsigned char) *badp))
|
2004-03-11 03:11:14 +01:00
|
|
|
badp++;
|
|
|
|
|
|
|
|
if (*badp != c)
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("invalid input syntax for integer: \"%s\"",
|
|
|
|
s)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (size)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case sizeof(int32):
|
2003-07-27 06:53:12 +02:00
|
|
|
if (errno == ERANGE
|
1999-07-10 19:03:34 +02:00
|
|
|
#if defined(HAVE_LONG_INT_64)
|
2003-08-04 02:43:34 +02:00
|
|
|
/* won't get ERANGE on these with 64-bit longs... */
|
2003-07-27 06:53:12 +02:00
|
|
|
|| l < INT_MIN || l > INT_MAX
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("value \"%s\" is out of range for type integer", s)));
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case sizeof(int16):
|
2003-07-27 06:53:12 +02:00
|
|
|
if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
2004-10-04 16:42:48 +02:00
|
|
|
errmsg("value \"%s\" is out of range for type smallint", s)));
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case sizeof(int8):
|
2003-07-27 06:53:12 +02:00
|
|
|
if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("value \"%s\" is out of range for 8-bit integer", s)));
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-27 06:53:12 +02:00
|
|
|
elog(ERROR, "unsupported result size: %d", size);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return (int32) l;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-01 20:29:35 +02:00
|
|
|
* pg_itoa - converts a short int to its string represention
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Note:
|
|
|
|
* previously based on ~ingres/source/gutil/atoi.c
|
|
|
|
* now uses vendor's sprintf conversion
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2000-08-01 20:29:35 +02:00
|
|
|
pg_itoa(int16 i, char *a)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
sprintf(a, "%hd", (short) i);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-01 20:29:35 +02:00
|
|
|
* pg_ltoa - converts a long int to its string represention
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Note:
|
|
|
|
* previously based on ~ingres/source/gutil/atoi.c
|
|
|
|
* now uses vendor's sprintf conversion
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2000-08-01 20:29:35 +02:00
|
|
|
pg_ltoa(int32 l, char *a)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
sprintf(a, "%d", l);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|