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.18 1997/12/19 02:08:01 scrappy Exp $
15 *-------------------------------------------------------------------------
17 #include <stdio.h> /* for sprintf() */
24 #include "utils/builtins.h" /* where the declarations go */
26 #include <regex/utils.h>
32 # include <port-protos.h> /* ecvt(), fcvt() */
36 #define INT_MAX (0x7FFFFFFFL)
39 #define INT_MIN (-0x80000000L)
42 #define SHRT_MAX (0x7FFF)
45 #define SHRT_MIN (-0x8000)
48 #define SCHAR_MAX (0x7F)
51 #define SCHAR_MIN (-0x80)
55 pg_atoi(char *s, int size, int c)
58 char *badp = (char *) NULL;
63 l = strtol(s, &badp, 10);
64 if (errno) /* strtol must set ERANGE */
65 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
66 if (badp && *badp && (*badp != c))
67 elog(WARN, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);
73 /* won't get ERANGE on these with 64-bit longs... */
77 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
82 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
84 #endif /* HAS_LONG_LONG */
90 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
95 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
102 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
107 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
111 elog(WARN, "pg_atoi: invalid result size: %d", size);
117 * itoa - converts a short int to its string represention
120 * previously based on ~ingres/source/gutil/atoi.c
121 * now uses vendor's sprintf conversion
126 sprintf(a, "%hd", (short) i);
130 * ltoa - converts a long int to its string represention
133 * previously based on ~ingres/source/gutil/atoi.c
134 * now uses vendor's sprintf conversion
137 ltoa(int32 l, char *a)
143 ** ftoa - FLOATING POINT TO ASCII CONVERSION
145 ** CODE derived from ingres, ~ingres/source/gutil/ftoa.c
147 ** 'Value' is converted to an ascii character string and stored
148 ** into 'ascii'. Ascii should have room for at least 'width' + 1
149 ** characters. 'Width' is the width of the output field (max).
150 ** 'Prec' is the number of characters to put after the decimal
151 ** point. The format of the output string is controlled by
155 ** e or E: "E" format output
156 ** f or F: "F" format output
157 ** g or G: "F" format output if it will fit, otherwise
159 ** n or N: same as G, but decimal points will not always
162 ** If 'format' is upper case, the "E" comes out in upper case;
163 ** otherwise it comes out in lower case.
165 ** When the field width is not big enough, it fills the field with
166 ** stars ("*****") and returns zero. Normal return is the width
167 ** of the output field (sometimes shorter than 'width').
171 ftoa(double value, char *ascii, int width, int prec1, char format)
178 sprintf(fmt, "%%%d.%d%c", width, prec1, format);
179 sprintf(out, fmt, value);
180 if ((ret = strlen(out)) > width)
182 MemSet(ascii, '*', width - 2);
191 register int avail = 0;
192 register char *a = NULL;
193 register char *p = NULL;
198 /* extern char *ecvt(), *fcvt();*/
202 lowercase = 'a' - 'A';
210 /* try 'F' style output */
211 p = fcvt(value, prec, &expon, &sign);
222 /* output '0' before the decimal point */
229 /* compute space length left after dec pt and fraction */
237 /* it fits. output */
240 /* output left of dp */
250 /* output fraction (right of dec pt) */
254 /* won't fit; let's hope for G format */
259 /* try to do E style output */
260 p = ecvt(value, prec + 1, &expon, &sign);
264 /* output the sign */
272 /* check for field too small */
273 if (mode == 'F' || avail < prec)
275 /* sorry joker, you lose */
277 for (avail = width; avail > 0; avail--)
283 /* it fits; output the number */
286 /* output the LHS single digit */
312 /* output the exponent */
315 *a++ = 'E' + lowercase;
323 *a++ = (expon / 10) % 10 + '0';
324 *a++ = expon % 10 + '0';
327 /* output spaces on the end in G format */
336 /* finally, we can return */
346 ** atof1 - ASCII TO FLOATING CONVERSION
348 ** CODE derived from ~ingres/source/gutil/atof.c
350 ** Converts the string 'str' to floating point and stores the
351 ** result into the cell pointed to by 'val'.
353 ** The syntax which it accepts is pretty much what you would
354 ** expect. Basically, it is:
355 ** {<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
356 ** where <exp> is "e" or "E" followed by an integer, <sp> is a
357 ** space character, <digit> is zero through nine, [] is zero or
358 ** one, and {} is zero or more.
361 ** str -- string to convert.
362 ** val -- pointer to place to put the result (which
363 ** must be type double).
367 ** -1 -- syntax error.
368 ** +1 -- overflow (not implemented).
375 atof1(char *str, double *val)
383 register int gotmant;
389 /* skip leading blanks */
390 while ((c = *p) != '\0')
397 /* handle possible sign */
407 /* skip blanks after sign */
408 while ((c = *p) != '\0')
415 /* start collecting the number to the decimal point */
420 if (c < '0' || c > '9')
422 v = v * 10.0 + (c - '0');
427 /* check for fractional part */
434 if (c < '0' || c > '9')
437 v += (c - '0') * fact;
442 /* skip blanks before possible exponent */
443 while ((c = *p) != '\0')
450 /* test for exponent */
451 if (c == 'e' || c == 'E')
454 expon = pg_atoi(p, sizeof(expon), '\0');
458 v *= pow(10.0, fact);
462 /* if no exponent, then nothing */
467 /* store the result and exit */