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-2001, 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.44 2001/01/24 19:43:14 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)
46 pg_atoi(char *s, int size, int c)
49 char *badp = (char *) NULL;
56 * Some versions of strtol treat the empty string as an error. This
57 * code will explicitly return 0 for an empty string.
60 if (s == (char *) NULL)
61 elog(ERROR, "pg_atoi: NULL pointer!");
65 l = strtol(s, &badp, 10);
67 * strtol() normally only sets ERANGE. On some systems it also
68 * may set EINVAL, which simply means it couldn't parse the
69 * input string. This is handled by the second "if" consistent
72 if (errno && errno != EINVAL)
73 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
74 if (badp && *badp && (*badp != c))
75 elog(ERROR, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);
80 #if defined(HAVE_LONG_INT_64)
81 /* won't get ERANGE on these with 64-bit longs... */
85 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
90 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
92 #endif /* HAVE_LONG_INT_64 */
98 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
103 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
110 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
115 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
119 elog(ERROR, "pg_atoi: invalid result size: %d", size);
125 * pg_itoa - converts a short int to its string represention
128 * previously based on ~ingres/source/gutil/atoi.c
129 * now uses vendor's sprintf conversion
132 pg_itoa(int16 i, char *a)
134 sprintf(a, "%hd", (short) i);
138 * pg_ltoa - converts a long int to its string represention
141 * previously based on ~ingres/source/gutil/atoi.c
142 * now uses vendor's sprintf conversion
145 pg_ltoa(int32 l, char *a)
151 ** ftoa - FLOATING POINT TO ASCII CONVERSION
153 ** CODE derived from ingres, ~ingres/source/gutil/ftoa.c
155 ** 'Value' is converted to an ascii character string and stored
156 ** into 'ascii'. Ascii should have room for at least 'width' + 1
157 ** characters. 'Width' is the width of the output field (max).
158 ** 'Prec' is the number of characters to put after the decimal
159 ** point. The format of the output string is controlled by
163 ** e or E: "E" format output
164 ** f or F: "F" format output
165 ** g or G: "F" format output if it will fit, otherwise
167 ** n or N: same as G, but decimal points will not always
170 ** If 'format' is upper case, the "E" comes out in upper case;
171 ** otherwise it comes out in lower case.
173 ** When the field width is not big enough, it fills the field with
174 ** stars ("*****") and returns zero. Normal return is the width
175 ** of the output field (sometimes shorter than 'width').
179 ftoa(double value, char *ascii, int width, int prec1, char format)
186 sprintf(fmt, "%%%d.%d%c", width, prec1, format);
187 sprintf(out, fmt, value);
188 if ((ret = strlen(out)) > width)
190 MemSet(ascii, '*', width - 2);
206 /* extern char *ecvt(), *fcvt();*/
210 lowercase = 'a' - 'A';
218 /* try 'F' style output */
219 p = fcvt(value, prec, &expon, &sign);
230 /* output '0' before the decimal point */
237 /* compute space length left after dec pt and fraction */
245 /* it fits. output */
248 /* output left of dp */
256 /* output fraction (right of dec pt) */
260 /* won't fit; let's hope for G format */
265 /* try to do E style output */
266 p = ecvt(value, prec + 1, &expon, &sign);
270 /* output the sign */
278 /* check for field too small */
279 if (mode == 'F' || avail < prec)
281 /* sorry joker, you lose */
283 for (avail = width; avail > 0; avail--)
289 /* it fits; output the number */
292 /* output the LHS single digit */
318 /* output the exponent */
321 *a++ = 'E' + lowercase;
329 *a++ = (expon / 10) % 10 + '0';
330 *a++ = expon % 10 + '0';
333 /* output spaces on the end in G format */
342 /* finally, we can return */
352 ** atof1 - ASCII TO FLOATING CONVERSION
354 ** CODE derived from ~ingres/source/gutil/atof.c
356 ** Converts the string 'str' to floating point and stores the
357 ** result into the cell pointed to by 'val'.
359 ** The syntax which it accepts is pretty much what you would
360 ** expect. Basically, it is:
361 ** {<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
362 ** where <exp> is "e" or "E" followed by an integer, <sp> is a
363 ** space character, <digit> is zero through nine, [] is zero or
364 ** one, and {} is zero or more.
367 ** str -- string to convert.
368 ** val -- pointer to place to put the result (which
369 ** must be type double).
373 ** -1 -- syntax error.
374 ** +1 -- overflow (not implemented).
381 atof1(char *str, double *val)
395 /* skip leading blanks */
396 while ((c = *p) != '\0')
403 /* handle possible sign */
413 /* skip blanks after sign */
414 while ((c = *p) != '\0')
421 /* start collecting the number to the decimal point */
426 if (c < '0' || c > '9')
428 v = v * 10.0 + (c - '0');
433 /* check for fractional part */
440 if (c < '0' || c > '9')
443 v += (c - '0') * fact;
448 /* skip blanks before possible exponent */
449 while ((c = *p) != '\0')
456 /* test for exponent */
457 if (c == 'e' || c == 'E')
460 expon = pg_atoi(p, sizeof(expon), '\0');
464 v *= pow(10.0, fact);
468 /* if no exponent, then nothing */
473 /* store the result and exit */