1 /*-------------------------------------------------------------------------
4 * utility functions for I/O of built-in numeric types.
6 * integer: pg_itoa, pg_ltoa
7 * floating point: ftoa, atof1
9 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 * $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.54 2002/09/04 20:31:28 momjian Exp $
16 *-------------------------------------------------------------------------
24 #include "utils/builtins.h"
27 #define INT_MAX (0x7FFFFFFFL)
30 #define INT_MIN (-INT_MAX-1)
33 #define SHRT_MAX (0x7FFF)
36 #define SHRT_MIN (-SHRT_MAX-1)
39 #define SCHAR_MAX (0x7F)
42 #define SCHAR_MIN (-SCHAR_MAX-1)
47 * pg_atoi: convert string to integer
49 * size is the sizeof() the desired integral result (1, 2, or 4 bytes).
51 * c, if not 0, is the terminator character that may appear after the
52 * integer. If 0, the string must end after the integer.
54 * Unlike plain atoi(), this will throw elog() upon bad input format or
58 pg_atoi(char *s, int size, int c)
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.
70 if (s == (char *) NULL)
71 elog(ERROR, "pg_atoi: NULL pointer");
73 elog(ERROR, "pg_atoi: zero-length string");
75 l = strtol(s, &badp, 10);
78 * strtol() normally only sets ERANGE. On some systems it also may
79 * set EINVAL, which simply means it couldn't parse the input string.
80 * This is handled by the second "if" consistent across platforms.
82 if (errno && errno != EINVAL)
83 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
84 if (badp && *badp && *badp != c)
85 elog(ERROR, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);
90 #if defined(HAVE_LONG_INT_64)
91 /* won't get ERANGE on these with 64-bit longs... */
95 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
100 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
102 #endif /* HAVE_LONG_INT_64 */
108 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
113 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
120 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
125 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
129 elog(ERROR, "pg_atoi: invalid result size: %d", size);
135 * pg_itoa - converts a short int to its string represention
138 * previously based on ~ingres/source/gutil/atoi.c
139 * now uses vendor's sprintf conversion
142 pg_itoa(int16 i, char *a)
144 sprintf(a, "%hd", (short) i);
148 * pg_ltoa - converts a long int to its string represention
151 * previously based on ~ingres/source/gutil/atoi.c
152 * now uses vendor's sprintf conversion
155 pg_ltoa(int32 l, char *a)
161 ** ftoa - FLOATING POINT TO ASCII CONVERSION
163 ** CODE derived from ingres, ~ingres/source/gutil/ftoa.c
165 ** 'Value' is converted to an ascii character string and stored
166 ** into 'ascii'. Ascii should have room for at least 'width' + 1
167 ** characters. 'Width' is the width of the output field (max).
168 ** 'Prec' is the number of characters to put after the decimal
169 ** point. The format of the output string is controlled by
173 ** e or E: "E" format output
174 ** f or F: "F" format output
175 ** g or G: "F" format output if it will fit, otherwise
177 ** n or N: same as G, but decimal points will not always
180 ** If 'format' is upper case, the "E" comes out in upper case;
181 ** otherwise it comes out in lower case.
183 ** When the field width is not big enough, it fills the field with
184 ** stars ("*****") and returns zero. Normal return is the width
185 ** of the output field (sometimes shorter than 'width').
189 ftoa(double value, char *ascii, int width, int prec1, char format)
196 sprintf(fmt, "%%%d.%d%c", width, prec1, format);
197 sprintf(out, fmt, value);
198 if ((ret = strlen(out)) > width)
200 MemSet(ascii, '*', width - 2);
216 /* extern char *ecvt(), *fcvt();*/
220 lowercase = 'a' - 'A';
228 /* try 'F' style output */
229 p = fcvt(value, prec, &expon, &sign);
240 /* output '0' before the decimal point */
247 /* compute space length left after dec pt and fraction */
255 /* it fits. output */
258 /* output left of dp */
266 /* output fraction (right of dec pt) */
270 /* won't fit; let's hope for G format */
275 /* try to do E style output */
276 p = ecvt(value, prec + 1, &expon, &sign);
280 /* output the sign */
288 /* check for field too small */
289 if (mode == 'F' || avail < prec)
291 /* sorry joker, you lose */
293 for (avail = width; avail > 0; avail--)
299 /* it fits; output the number */
302 /* output the LHS single digit */
328 /* output the exponent */
331 *a++ = 'E' + lowercase;
339 *a++ = (expon / 10) % 10 + '0';
340 *a++ = expon % 10 + '0';
343 /* output spaces on the end in G format */
352 /* finally, we can return */
361 ** atof1 - ASCII TO FLOATING CONVERSION
363 ** CODE derived from ~ingres/source/gutil/atof.c
365 ** Converts the string 'str' to floating point and stores the
366 ** result into the cell pointed to by 'val'.
368 ** The syntax which it accepts is pretty much what you would
369 ** expect. Basically, it is:
370 ** {<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
371 ** where <exp> is "e" or "E" followed by an integer, <sp> is a
372 ** space character, <digit> is zero through nine, [] is zero or
373 ** one, and {} is zero or more.
376 ** str -- string to convert.
377 ** val -- pointer to place to put the result (which
378 ** must be type double).
382 ** -1 -- syntax error.
383 ** +1 -- overflow (not implemented).
390 atof1(char *str, double *val)
404 /* skip leading blanks */
405 while ((c = *p) != '\0')
412 /* handle possible sign */
422 /* skip blanks after sign */
423 while ((c = *p) != '\0')
430 /* start collecting the number to the decimal point */
435 if (c < '0' || c > '9')
437 v = v * 10.0 + (c - '0');
442 /* check for fractional part */
449 if (c < '0' || c > '9')
452 v += (c - '0') * fact;
457 /* skip blanks before possible exponent */
458 while ((c = *p) != '\0')
465 /* test for exponent */
466 if (c == 'e' || c == 'E')
469 expon = pg_atoi(p, sizeof(expon), '\0');
473 v *= pow(10.0, fact);
477 /* if no exponent, then nothing */
482 /* store the result and exit */