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.10 1997/04/22 17:47:14 scrappy Exp $
15 *-------------------------------------------------------------------------
17 #include <stdio.h> /* for sprintf() */
21 #include "utils/builtins.h" /* where the declarations go */
23 # include <regex/utils.h>
27 #include <port-protos.h> /* ecvt(), fcvt() */
30 pg_atoi(char *s, int size, int c)
33 char *badp = (char *) NULL;
38 l = strtol(s, &badp, 10);
39 if (errno) /* strtol must set ERANGE */
40 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
41 if (badp && *badp && (*badp != c))
42 elog(WARN, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);
47 /* won't get ERANGE on these with 64-bit longs... */
48 if (l < -0x80000000L) {
50 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
52 if (l > 0x7fffffffL) {
54 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
56 #endif /* HAS_LONG_LONG */
61 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
65 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
71 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
75 elog(WARN, "pg_atoi: error reading \"%s\": %m", s);
79 elog(WARN, "pg_atoi: invalid result size: %d", size);
85 * itoa - converts a short int to its string represention
88 * previously based on ~ingres/source/gutil/atoi.c
89 * now uses vendor's sprintf conversion
94 sprintf(a, "%hd", (short)i);
98 * ltoa - converts a long int to its string represention
101 * previously based on ~ingres/source/gutil/atoi.c
102 * now uses vendor's sprintf conversion
105 ltoa(int32 l, char *a)
111 ** ftoa - FLOATING POINT TO ASCII CONVERSION
113 ** CODE derived from ingres, ~ingres/source/gutil/ftoa.c
115 ** 'Value' is converted to an ascii character string and stored
116 ** into 'ascii'. Ascii should have room for at least 'width' + 1
117 ** characters. 'Width' is the width of the output field (max).
118 ** 'Prec' is the number of characters to put after the decimal
119 ** point. The format of the output string is controlled by
123 ** e or E: "E" format output
124 ** f or F: "F" format output
125 ** g or G: "F" format output if it will fit, otherwise
127 ** n or N: same as G, but decimal points will not always
130 ** If 'format' is upper case, the "E" comes out in upper case;
131 ** otherwise it comes out in lower case.
133 ** When the field width is not big enough, it fills the field with
134 ** stars ("*****") and returns zero. Normal return is the width
135 ** of the output field (sometimes shorter than 'width').
138 ftoa(double value, char *ascii, int width, int prec1, char format)
145 (void) sprintf(fmt, "%%%d.%d%c", width, prec1, format);
146 (void) sprintf(out, fmt, value);
147 if ((ret = strlen(out)) > width) {
148 memset(ascii, '*', width - 2);
152 (void) strcpy(ascii, out);
157 register int avail = 0;
158 register char *a = NULL;
159 register char *p = NULL;
163 /* extern char *ecvt(), *fcvt();*/
167 lowercase = 'a' - 'A';
174 /* try 'F' style output */
175 p = fcvt(value, prec, &expon, &sign);
185 /* output '0' before the decimal point */
191 /* compute space length left after dec pt and fraction */
196 if (avail >= expon) {
198 /* it fits. output */
200 /* output left of dp */
208 /* output fraction (right of dec pt) */
212 /* won't fit; let's hope for G format */
216 /* try to do E style output */
217 p = ecvt(value, prec + 1, &expon, &sign);
221 /* output the sign */
228 /* check for field too small */
229 if (mode == 'F' || avail < prec) {
230 /* sorry joker, you lose */
232 for (avail = width; avail > 0; avail--)
238 /* it fits; output the number */
241 /* output the LHS single digit */
263 /* output the exponent */
265 *a++ = 'E' + lowercase;
271 *a++ = (expon / 10) % 10 + '0';
272 *a++ = expon % 10 + '0';
275 /* output spaces on the end in G format */
283 /* finally, we can return */
287 #endif /* !BSD44_derived */
291 ** atof1 - ASCII TO FLOATING CONVERSION
293 ** CODE derived from ~ingres/source/gutil/atof.c
295 ** Converts the string 'str' to floating point and stores the
296 ** result into the cell pointed to by 'val'.
298 ** The syntax which it accepts is pretty much what you would
299 ** expect. Basically, it is:
300 ** {<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
301 ** where <exp> is "e" or "E" followed by an integer, <sp> is a
302 ** space character, <digit> is zero through nine, [] is zero or
303 ** one, and {} is zero or more.
306 ** str -- string to convert.
307 ** val -- pointer to place to put the result (which
308 ** must be type double).
312 ** -1 -- syntax error.
313 ** +1 -- overflow (not implemented).
319 atof1(char *str, double *val)
327 register int gotmant;
333 /* skip leading blanks */
334 while ((c = *p) != '\0') {
340 /* handle possible sign */
349 /* skip blanks after sign */
350 while ((c = *p) != '\0') {
356 /* start collecting the number to the decimal point */
360 if (c < '0' || c > '9')
362 v = v * 10.0 + (c - '0');
367 /* check for fractional part */
372 if (c < '0' || c > '9')
375 v += (c - '0') * fact;
380 /* skip blanks before possible exponent */
381 while ((c = *p) != '\0') {
387 /* test for exponent */
388 if (c == 'e' || c == 'E') {
390 expon = pg_atoi(p, sizeof(expon), '\0');
394 v *= pow(10.0, fact);
396 /* if no exponent, then nothing */
401 /* store the result and exit */