]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/numutils.c
Pgindent run for 8.0.
[postgresql] / src / backend / utils / adt / numutils.c
1 /*-------------------------------------------------------------------------
2  *
3  * numutils.c
4  *        utility functions for I/O of built-in numeric types.
5  *
6  *              integer:                                pg_atoi, pg_itoa, pg_ltoa
7  *
8  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  *        $PostgreSQL: pgsql/src/backend/utils/adt/numutils.c,v 1.65 2004/08/29 05:06:49 momjian Exp $
14  *
15  *-------------------------------------------------------------------------
16  */
17 #include "postgres.h"
18
19 #include <errno.h>
20 #include <math.h>
21 #include <limits.h>
22 #include <ctype.h>
23
24 #include "utils/builtins.h"
25
26 #ifndef INT_MAX
27 #define INT_MAX (0x7FFFFFFFL)
28 #endif
29 #ifndef INT_MIN
30 #define INT_MIN (-INT_MAX-1)
31 #endif
32 #ifndef SHRT_MAX
33 #define SHRT_MAX (0x7FFF)
34 #endif
35 #ifndef SHRT_MIN
36 #define SHRT_MIN (-SHRT_MAX-1)
37 #endif
38 #ifndef SCHAR_MAX
39 #define SCHAR_MAX (0x7F)
40 #endif
41 #ifndef SCHAR_MIN
42 #define SCHAR_MIN (-SCHAR_MAX-1)
43 #endif
44
45
46 /*
47  * pg_atoi: convert string to integer
48  *
49  * 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes).
50  *
51  * allows any number of leading or trailing whitespace characters.
52  *
53  * 'c' is the character that terminates the input string (after any
54  * number of whitespace characters).
55  *
56  * Unlike plain atoi(), this will throw ereport() upon bad input format or
57  * overflow.
58  */
59 int32
60 pg_atoi(char *s, int size, int c)
61 {
62         long            l;
63         char       *badp;
64
65         /*
66          * Some versions of strtol treat the empty string as an error, but
67          * some seem not to.  Make an explicit test to be sure we catch it.
68          */
69         if (s == NULL)
70                 elog(ERROR, "NULL pointer");
71         if (*s == 0)
72                 ereport(ERROR,
73                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
74                                  errmsg("invalid input syntax for integer: \"%s\"",
75                                                 s)));
76
77         errno = 0;
78         l = strtol(s, &badp, 10);
79
80         /* We made no progress parsing the string, so bail out */
81         if (s == badp)
82                 ereport(ERROR,
83                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
84                                  errmsg("invalid input syntax for integer: \"%s\"",
85                                                 s)));
86
87         /*
88          * Skip any trailing whitespace; if anything but whitespace remains
89          * before the terminating character, bail out
90          */
91         while (*badp != c && isspace((unsigned char) *badp))
92                 badp++;
93
94         if (*badp != c)
95                 ereport(ERROR,
96                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
97                                  errmsg("invalid input syntax for integer: \"%s\"",
98                                                 s)));
99
100         switch (size)
101         {
102                 case sizeof(int32):
103                         if (errno == ERANGE
104 #if defined(HAVE_LONG_INT_64)
105                         /* won't get ERANGE on these with 64-bit longs... */
106                                 || l < INT_MIN || l > INT_MAX
107 #endif
108                                 )
109                                 ereport(ERROR,
110                                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
111                                                  errmsg("value \"%s\" is out of range for type integer", s)));
112                         break;
113                 case sizeof(int16):
114                         if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
115                                 ereport(ERROR,
116                                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
117                                                  errmsg("value \"%s\" is out of range for type shortint", s)));
118                         break;
119                 case sizeof(int8):
120                         if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
121                                 ereport(ERROR,
122                                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
123                                                  errmsg("value \"%s\" is out of range for 8-bit integer", s)));
124                         break;
125                 default:
126                         elog(ERROR, "unsupported result size: %d", size);
127         }
128         return (int32) l;
129 }
130
131 /*
132  *              pg_itoa                 - converts a short int to its string represention
133  *
134  *              Note:
135  *                              previously based on ~ingres/source/gutil/atoi.c
136  *                              now uses vendor's sprintf conversion
137  */
138 void
139 pg_itoa(int16 i, char *a)
140 {
141         sprintf(a, "%hd", (short) i);
142 }
143
144 /*
145  *              pg_ltoa                 - converts a long int to its string represention
146  *
147  *              Note:
148  *                              previously based on ~ingres/source/gutil/atoi.c
149  *                              now uses vendor's sprintf conversion
150  */
151 void
152 pg_ltoa(int32 l, char *a)
153 {
154         sprintf(a, "%d", l);
155 }