1 /*-------------------------------------------------------------------------
4 * utility functions for I/O of built-in numeric types.
7 * floating point: ftoa, atof1
9 * Copyright (c) 1994, Regents of the University of California
13 * $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.27 1998/09/12 16:04:35 momjian Exp $
15 *-------------------------------------------------------------------------
17 #include <stdio.h> /* for sprintf() */
24 #include "utils/builtins.h" /* where the declarations go */
26 #include <regex/utils.h>
32 #define INT_MAX (0x7FFFFFFFL)
35 #define INT_MIN (-0x80000000L)
38 #define SHRT_MAX (0x7FFF)
41 #define SHRT_MIN (-0x8000)
44 #define SCHAR_MAX (0x7F)
47 #define SCHAR_MIN (-0x80)
51 pg_atoi(char *s, int size, int c)
54 char *badp = (char *) NULL;
61 * Some versions of strtol treat the empty string as an error. This
62 * code will explicitly return 0 for an empty string.
65 if (s == (char *)NULL)
66 elog(ERROR, "pg_atoi: NULL pointer!");
70 l = strtol(s, &badp, 10);
71 if (errno) /* strtol must set ERANGE */
72 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
73 if (badp && *badp && (*badp != c))
74 elog(ERROR, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);
80 /* won't get ERANGE on these with 64-bit longs... */
84 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
89 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
91 #endif /* HAS_LONG_LONG */
97 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
102 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
109 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
114 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
118 elog(ERROR, "pg_atoi: invalid result size: %d", size);
124 * itoa - converts a short int to its string represention
127 * previously based on ~ingres/source/gutil/atoi.c
128 * now uses vendor's sprintf conversion
133 sprintf(a, "%hd", (short) i);
137 * ltoa - converts a long int to its string represention
140 * previously based on ~ingres/source/gutil/atoi.c
141 * now uses vendor's sprintf conversion
144 ltoa(int32 l, char *a)
150 ** ftoa - FLOATING POINT TO ASCII CONVERSION
152 ** CODE derived from ingres, ~ingres/source/gutil/ftoa.c
154 ** 'Value' is converted to an ascii character string and stored
155 ** into 'ascii'. Ascii should have room for at least 'width' + 1
156 ** characters. 'Width' is the width of the output field (max).
157 ** 'Prec' is the number of characters to put after the decimal
158 ** point. The format of the output string is controlled by
162 ** e or E: "E" format output
163 ** f or F: "F" format output
164 ** g or G: "F" format output if it will fit, otherwise
166 ** n or N: same as G, but decimal points will not always
169 ** If 'format' is upper case, the "E" comes out in upper case;
170 ** otherwise it comes out in lower case.
172 ** When the field width is not big enough, it fills the field with
173 ** stars ("*****") and returns zero. Normal return is the width
174 ** of the output field (sometimes shorter than 'width').
178 ftoa(double value, char *ascii, int width, int prec1, char format)
185 sprintf(fmt, "%%%d.%d%c", width, prec1, format);
186 sprintf(out, fmt, value);
187 if ((ret = strlen(out)) > width)
189 MemSet(ascii, '*', width - 2);
205 /* extern char *ecvt(), *fcvt();*/
209 lowercase = 'a' - 'A';
217 /* try 'F' style output */
218 p = fcvt(value, prec, &expon, &sign);
229 /* output '0' before the decimal point */
236 /* compute space length left after dec pt and fraction */
244 /* it fits. output */
247 /* output left of dp */
255 /* output fraction (right of dec pt) */
259 /* won't fit; let's hope for G format */
264 /* try to do E style output */
265 p = ecvt(value, prec + 1, &expon, &sign);
269 /* output the sign */
277 /* check for field too small */
278 if (mode == 'F' || avail < prec)
280 /* sorry joker, you lose */
282 for (avail = width; avail > 0; avail--)
288 /* it fits; output the number */
291 /* output the LHS single digit */
317 /* output the exponent */
320 *a++ = 'E' + lowercase;
328 *a++ = (expon / 10) % 10 + '0';
329 *a++ = expon % 10 + '0';
332 /* output spaces on the end in G format */
341 /* finally, we can return */
351 ** atof1 - ASCII TO FLOATING CONVERSION
353 ** CODE derived from ~ingres/source/gutil/atof.c
355 ** Converts the string 'str' to floating point and stores the
356 ** result into the cell pointed to by 'val'.
358 ** The syntax which it accepts is pretty much what you would
359 ** expect. Basically, it is:
360 ** {<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
361 ** where <exp> is "e" or "E" followed by an integer, <sp> is a
362 ** space character, <digit> is zero through nine, [] is zero or
363 ** one, and {} is zero or more.
366 ** str -- string to convert.
367 ** val -- pointer to place to put the result (which
368 ** must be type double).
372 ** -1 -- syntax error.
373 ** +1 -- overflow (not implemented).
380 atof1(char *str, double *val)
394 /* skip leading blanks */
395 while ((c = *p) != '\0')
402 /* handle possible sign */
412 /* skip blanks after sign */
413 while ((c = *p) != '\0')
420 /* start collecting the number to the decimal point */
425 if (c < '0' || c > '9')
427 v = v * 10.0 + (c - '0');
432 /* check for fractional part */
439 if (c < '0' || c > '9')
442 v += (c - '0') * fact;
447 /* skip blanks before possible exponent */
448 while ((c = *p) != '\0')
455 /* test for exponent */
456 if (c == 'e' || c == 'E')
459 expon = pg_atoi(p, sizeof(expon), '\0');
463 v *= pow(10.0, fact);
467 /* if no exponent, then nothing */
472 /* store the result and exit */