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.26 1998/09/01 04:32:43 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;
59 l = strtol(s, &badp, 10);
60 if (errno) /* strtol must set ERANGE */
61 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
62 if (badp && *badp && (*badp != c))
63 elog(ERROR, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);
69 /* won't get ERANGE on these with 64-bit longs... */
73 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
78 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
80 #endif /* HAS_LONG_LONG */
86 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
91 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
98 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
103 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
107 elog(ERROR, "pg_atoi: invalid result size: %d", size);
113 * itoa - converts a short int to its string represention
116 * previously based on ~ingres/source/gutil/atoi.c
117 * now uses vendor's sprintf conversion
122 sprintf(a, "%hd", (short) i);
126 * ltoa - converts a long int to its string represention
129 * previously based on ~ingres/source/gutil/atoi.c
130 * now uses vendor's sprintf conversion
133 ltoa(int32 l, char *a)
139 ** ftoa - FLOATING POINT TO ASCII CONVERSION
141 ** CODE derived from ingres, ~ingres/source/gutil/ftoa.c
143 ** 'Value' is converted to an ascii character string and stored
144 ** into 'ascii'. Ascii should have room for at least 'width' + 1
145 ** characters. 'Width' is the width of the output field (max).
146 ** 'Prec' is the number of characters to put after the decimal
147 ** point. The format of the output string is controlled by
151 ** e or E: "E" format output
152 ** f or F: "F" format output
153 ** g or G: "F" format output if it will fit, otherwise
155 ** n or N: same as G, but decimal points will not always
158 ** If 'format' is upper case, the "E" comes out in upper case;
159 ** otherwise it comes out in lower case.
161 ** When the field width is not big enough, it fills the field with
162 ** stars ("*****") and returns zero. Normal return is the width
163 ** of the output field (sometimes shorter than 'width').
167 ftoa(double value, char *ascii, int width, int prec1, char format)
174 sprintf(fmt, "%%%d.%d%c", width, prec1, format);
175 sprintf(out, fmt, value);
176 if ((ret = strlen(out)) > width)
178 MemSet(ascii, '*', width - 2);
194 /* extern char *ecvt(), *fcvt();*/
198 lowercase = 'a' - 'A';
206 /* try 'F' style output */
207 p = fcvt(value, prec, &expon, &sign);
218 /* output '0' before the decimal point */
225 /* compute space length left after dec pt and fraction */
233 /* it fits. output */
236 /* output left of dp */
244 /* output fraction (right of dec pt) */
248 /* won't fit; let's hope for G format */
253 /* try to do E style output */
254 p = ecvt(value, prec + 1, &expon, &sign);
258 /* output the sign */
266 /* check for field too small */
267 if (mode == 'F' || avail < prec)
269 /* sorry joker, you lose */
271 for (avail = width; avail > 0; avail--)
277 /* it fits; output the number */
280 /* output the LHS single digit */
306 /* output the exponent */
309 *a++ = 'E' + lowercase;
317 *a++ = (expon / 10) % 10 + '0';
318 *a++ = expon % 10 + '0';
321 /* output spaces on the end in G format */
330 /* finally, we can return */
340 ** atof1 - ASCII TO FLOATING CONVERSION
342 ** CODE derived from ~ingres/source/gutil/atof.c
344 ** Converts the string 'str' to floating point and stores the
345 ** result into the cell pointed to by 'val'.
347 ** The syntax which it accepts is pretty much what you would
348 ** expect. Basically, it is:
349 ** {<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
350 ** where <exp> is "e" or "E" followed by an integer, <sp> is a
351 ** space character, <digit> is zero through nine, [] is zero or
352 ** one, and {} is zero or more.
355 ** str -- string to convert.
356 ** val -- pointer to place to put the result (which
357 ** must be type double).
361 ** -1 -- syntax error.
362 ** +1 -- overflow (not implemented).
369 atof1(char *str, double *val)
383 /* skip leading blanks */
384 while ((c = *p) != '\0')
391 /* handle possible sign */
401 /* skip blanks after sign */
402 while ((c = *p) != '\0')
409 /* start collecting the number to the decimal point */
414 if (c < '0' || c > '9')
416 v = v * 10.0 + (c - '0');
421 /* check for fractional part */
428 if (c < '0' || c > '9')
431 v += (c - '0') * fact;
436 /* skip blanks before possible exponent */
437 while ((c = *p) != '\0')
444 /* test for exponent */
445 if (c == 'e' || c == 'E')
448 expon = pg_atoi(p, sizeof(expon), '\0');
452 v *= pow(10.0, fact);
456 /* if no exponent, then nothing */
461 /* store the result and exit */