]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/numutils.c
Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[postgresql] / src / backend / utils / adt / numutils.c
1 /*-------------------------------------------------------------------------
2  *
3  * numutils.c
4  *        utility functions for I/O of built-in numeric types.
5  *
6  *              integer:                                pg_itoa, pg_ltoa
7  *              floating point:                 ftoa, atof1
8  *
9  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  *
13  * IDENTIFICATION
14  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.44 2001/01/24 19:43:14 momjian Exp $
15  *
16  *-------------------------------------------------------------------------
17  */
18 #include "postgres.h"
19
20 #include <errno.h>
21 #include <math.h>
22 #include <limits.h>
23
24 #include "utils/builtins.h"
25
26 #ifndef INT_MAX
27 #define INT_MAX (0x7FFFFFFFL)
28 #endif
29 #ifndef INT_MIN
30 #define INT_MIN (-INT_MAX-1)
31 #endif
32 #ifndef SHRT_MAX
33 #define SHRT_MAX (0x7FFF)
34 #endif
35 #ifndef SHRT_MIN
36 #define SHRT_MIN (-SHRT_MAX-1)
37 #endif
38 #ifndef SCHAR_MAX
39 #define SCHAR_MAX (0x7F)
40 #endif
41 #ifndef SCHAR_MIN
42 #define SCHAR_MIN (-SCHAR_MAX-1)
43 #endif
44
45 int32
46 pg_atoi(char *s, int size, int c)
47 {
48         long            l = 0;
49         char       *badp = (char *) NULL;
50
51         Assert(s);
52
53         errno = 0;
54
55         /*
56          * Some versions of strtol treat the empty string as an error.  This
57          * code will explicitly return 0 for an empty string.
58          */
59
60         if (s == (char *) NULL)
61                 elog(ERROR, "pg_atoi: NULL pointer!");
62         else if (*s == 0)
63                 l = (long) 0;
64         else
65                 l = strtol(s, &badp, 10);
66         /*
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
70          * across platforms.
71          */
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);
76
77         switch (size)
78         {
79                 case sizeof(int32):
80 #if defined(HAVE_LONG_INT_64)
81                         /* won't get ERANGE on these with 64-bit longs... */
82                         if (l < INT_MIN)
83                         {
84                                 errno = ERANGE;
85                                 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
86                         }
87                         if (l > INT_MAX)
88                         {
89                                 errno = ERANGE;
90                                 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
91                         }
92 #endif   /* HAVE_LONG_INT_64 */
93                         break;
94                 case sizeof(int16):
95                         if (l < SHRT_MIN)
96                         {
97                                 errno = ERANGE;
98                                 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
99                         }
100                         if (l > SHRT_MAX)
101                         {
102                                 errno = ERANGE;
103                                 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
104                         }
105                         break;
106                 case sizeof(int8):
107                         if (l < SCHAR_MIN)
108                         {
109                                 errno = ERANGE;
110                                 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
111                         }
112                         if (l > SCHAR_MAX)
113                         {
114                                 errno = ERANGE;
115                                 elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
116                         }
117                         break;
118                 default:
119                         elog(ERROR, "pg_atoi: invalid result size: %d", size);
120         }
121         return (int32) l;
122 }
123
124 /*
125  *              pg_itoa                 - converts a short int to its string represention
126  *
127  *              Note:
128  *                              previously based on ~ingres/source/gutil/atoi.c
129  *                              now uses vendor's sprintf conversion
130  */
131 void
132 pg_itoa(int16 i, char *a)
133 {
134         sprintf(a, "%hd", (short) i);
135 }
136
137 /*
138  *              pg_ltoa                 - converts a long int to its string represention
139  *
140  *              Note:
141  *                              previously based on ~ingres/source/gutil/atoi.c
142  *                              now uses vendor's sprintf conversion
143  */
144 void
145 pg_ltoa(int32 l, char *a)
146 {
147         sprintf(a, "%d", l);
148 }
149
150 /*
151  **  ftoa               - FLOATING POINT TO ASCII CONVERSION
152  **
153  **             CODE derived from ingres, ~ingres/source/gutil/ftoa.c
154  **
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
160  **             'format'.
161  **
162  **             'Format' can be:
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
166  **                                             use "E" format.
167  **                             n or N:  same as G, but decimal points will not always
168  **                                             be aligned.
169  **
170  **             If 'format' is upper case, the "E" comes out in upper case;
171  **             otherwise it comes out in lower case.
172  **
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').
176  */
177 #ifdef NOT_USED
178 int
179 ftoa(double value, char *ascii, int width, int prec1, char format)
180 {
181 #ifndef HAVE_FCVT
182         char            out[256];
183         char            fmt[256];
184         int                     ret;
185
186         sprintf(fmt, "%%%d.%d%c", width, prec1, format);
187         sprintf(out, fmt, value);
188         if ((ret = strlen(out)) > width)
189         {
190                 MemSet(ascii, '*', width - 2);
191                 ascii[width] = 0;
192                 return 0;
193         }
194         strcpy(ascii, out);
195         return ret;
196 #else
197         auto int        expon;
198         auto int        sign;
199         int                     avail = 0;
200         char       *a = NULL;
201         char       *p = NULL;
202         char            mode;
203         int                     lowercase;
204         int                     prec;
205
206 /*        extern char           *ecvt(), *fcvt();*/
207
208         prec = prec1;
209         mode = format;
210         lowercase = 'a' - 'A';
211         if (mode >= 'a')
212                 mode -= 'a' - 'A';
213         else
214                 lowercase = 0;
215
216         if (mode != 'E')
217         {
218                 /* try 'F' style output */
219                 p = fcvt(value, prec, &expon, &sign);
220                 avail = width;
221                 a = ascii;
222
223                 /* output sign */
224                 if (sign)
225                 {
226                         avail--;
227                         *a++ = '-';
228                 }
229
230                 /* output '0' before the decimal point */
231                 if (expon <= 0)
232                 {
233                         *a++ = '0';
234                         avail--;
235                 }
236
237                 /* compute space length left after dec pt and fraction */
238                 avail -= prec + 1;
239                 if (mode == 'G')
240                         avail -= 4;
241
242                 if (avail >= expon)
243                 {
244
245                         /* it fits.  output */
246                         while (expon > 0)
247                         {
248                                 /* output left of dp */
249                                 expon--;
250                                 if (*p)
251                                         *a++ = *p++;
252                                 else
253                                         *a++ = '0';
254                         }
255
256                         /* output fraction (right of dec pt) */
257                         avail = expon;
258                         goto frac_out;
259                 }
260                 /* won't fit; let's hope for G format */
261         }
262
263         if (mode != 'F')
264         {
265                 /* try to do E style output */
266                 p = ecvt(value, prec + 1, &expon, &sign);
267                 avail = width - 5;
268                 a = ascii;
269
270                 /* output the sign */
271                 if (sign)
272                 {
273                         *a++ = '-';
274                         avail--;
275                 }
276         }
277
278         /* check for field too small */
279         if (mode == 'F' || avail < prec)
280         {
281                 /* sorry joker, you lose */
282                 a = ascii;
283                 for (avail = width; avail > 0; avail--)
284                         *a++ = '*';
285                 *a = 0;
286                 return 0;
287         }
288
289         /* it fits; output the number */
290         mode = 'E';
291
292         /* output the LHS single digit */
293         *a++ = *p++;
294         expon--;
295
296         /* output the rhs */
297         avail = 1;
298
299 frac_out:
300         *a++ = '.';
301         while (prec > 0)
302         {
303                 prec--;
304                 if (avail < 0)
305                 {
306                         avail++;
307                         *a++ = '0';
308                 }
309                 else
310                 {
311                         if (*p)
312                                 *a++ = *p++;
313                         else
314                                 *a++ = '0';
315                 }
316         }
317
318         /* output the exponent */
319         if (mode == 'E')
320         {
321                 *a++ = 'E' + lowercase;
322                 if (expon < 0)
323                 {
324                         *a++ = '-';
325                         expon = -expon;
326                 }
327                 else
328                         *a++ = '+';
329                 *a++ = (expon / 10) % 10 + '0';
330                 *a++ = expon % 10 + '0';
331         }
332
333         /* output spaces on the end in G format */
334         if (mode == 'G')
335         {
336                 *a++ = ' ';
337                 *a++ = ' ';
338                 *a++ = ' ';
339                 *a++ = ' ';
340         }
341
342         /* finally, we can return */
343         *a = 0;
344         avail = a - ascii;
345         return avail;
346 #endif
347 }
348
349 #endif
350
351 /*
352  **   atof1             - ASCII TO FLOATING CONVERSION
353  **
354  **             CODE derived from ~ingres/source/gutil/atof.c
355  **
356  **             Converts the string 'str' to floating point and stores the
357  **             result into the cell pointed to by 'val'.
358  **
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.
365  **
366  **             Parameters:
367  **                             str -- string to convert.
368  **                             val -- pointer to place to put the result (which
369  **                                             must be type double).
370  **
371  **             Returns:
372  **                             zero -- ok.
373  **                             -1 -- syntax error.
374  **                             +1 -- overflow (not implemented).
375  **
376  **             Side Effects:
377  **                             clobbers *val.
378  */
379 #ifdef NOT_USED
380 int
381 atof1(char *str, double *val)
382 {
383         char       *p;
384         double          v;
385         double          fact;
386         int                     minus;
387         char            c;
388         int                     expon;
389         int                     gotmant;
390
391         v = 0.0;
392         p = str;
393         minus = 0;
394
395         /* skip leading blanks */
396         while ((c = *p) != '\0')
397         {
398                 if (c != ' ')
399                         break;
400                 p++;
401         }
402
403         /* handle possible sign */
404         switch (c)
405         {
406                 case '-':
407                         minus++;
408
409                 case '+':
410                         p++;
411         }
412
413         /* skip blanks after sign */
414         while ((c = *p) != '\0')
415         {
416                 if (c != ' ')
417                         break;
418                 p++;
419         }
420
421         /* start collecting the number to the decimal point */
422         gotmant = 0;
423         for (;;)
424         {
425                 c = *p;
426                 if (c < '0' || c > '9')
427                         break;
428                 v = v * 10.0 + (c - '0');
429                 gotmant++;
430                 p++;
431         }
432
433         /* check for fractional part */
434         if (c == '.')
435         {
436                 fact = 1.0;
437                 for (;;)
438                 {
439                         c = *++p;
440                         if (c < '0' || c > '9')
441                                 break;
442                         fact *= 0.1;
443                         v += (c - '0') * fact;
444                         gotmant++;
445                 }
446         }
447
448         /* skip blanks before possible exponent */
449         while ((c = *p) != '\0')
450         {
451                 if (c != ' ')
452                         break;
453                 p++;
454         }
455
456         /* test for exponent */
457         if (c == 'e' || c == 'E')
458         {
459                 p++;
460                 expon = pg_atoi(p, sizeof(expon), '\0');
461                 if (!gotmant)
462                         v = 1.0;
463                 fact = expon;
464                 v *= pow(10.0, fact);
465         }
466         else
467         {
468                 /* if no exponent, then nothing */
469                 if (c != 0)
470                         return -1;
471         }
472
473         /* store the result and exit */
474         if (minus)
475                 v = -v;
476         *val = v;
477         return 0;
478 }
479
480 #endif