numutils.c 8.62 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/*-------------------------------------------------------------------------
 *
 * numutils.c
 *	  utility functions for I/O of built-in numeric types.
 *
 *		integer:				itoa, ltoa
 *		floating point:			ftoa, atof1
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/numutils.c,v 1.32 1999/07/09 03:27:20 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>				/* for sprintf() */
#include <errno.h>
#include <math.h>
#ifdef HAVE_LIMITS
#include <limits.h>
#endif
#include "postgres.h"
#include "utils/builtins.h"		/* where the declarations go */
#ifndef HAVE_MEMMOVE
#include <regex/utils.h>
#else
#include <string.h>
#endif

#ifndef INT_MAX
#define INT_MAX (0x7FFFFFFFL)
#endif
#ifndef INT_MIN
#define INT_MIN (-0x80000000L)
#endif
#ifndef SHRT_MAX
#define SHRT_MAX (0x7FFF)
#endif
#ifndef SHRT_MIN
#define SHRT_MIN (-0x8000)
#endif
#ifndef SCHAR_MAX
#define SCHAR_MAX (0x7F)
#endif
#ifndef SCHAR_MIN
#define SCHAR_MIN (-0x80)
#endif

int32
pg_atoi(char *s, int size, int c)
{
	long		l = 0;
	char	   *badp = (char *) NULL;

	Assert(s);

	errno = 0;

	/*
	 * Some versions of strtol treat the empty string as an error.	This
	 * code will explicitly return 0 for an empty string.
	 */

	if (s == (char *) NULL)
		elog(ERROR, "pg_atoi: NULL pointer!");
	else if (*s == 0)
		l = (long) 0;
	else
		l = strtol(s, &badp, 10);
	if (errno)					/* strtol must set ERANGE */
		elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
	if (badp && *badp && (*badp != c))
		elog(ERROR, "pg_atoi: error in \"%s\": can\'t parse \"%s\"", s, badp);

	switch (size)
	{
		case sizeof(int32):
#ifdef HAS_LONG_LONG
			/* won't get ERANGE on these with 64-bit longs... */
			if (l < INT_MIN)
			{
				errno = ERANGE;
				elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
			}
			if (l > INT_MAX)
			{
				errno = ERANGE;
				elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
			}
#endif	 /* HAS_LONG_LONG */
			break;
		case sizeof(int16):
			if (l < SHRT_MIN)
			{
				errno = ERANGE;
				elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
			}
			if (l > SHRT_MAX)
			{
				errno = ERANGE;
				elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
			}
			break;
		case sizeof(int8):
			if (l < SCHAR_MIN)
			{
				errno = ERANGE;
				elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
			}
			if (l > SCHAR_MAX)
			{
				errno = ERANGE;
				elog(ERROR, "pg_atoi: error reading \"%s\": %m", s);
			}
			break;
		default:
			elog(ERROR, "pg_atoi: invalid result size: %d", size);
	}
	return (int32) l;
}

/*
 *		itoa			- converts a short int to its string represention
 *
 *		Note:
 *				previously based on ~ingres/source/gutil/atoi.c
 *				now uses vendor's sprintf conversion
 */
void
itoa(int i, char *a)
{
	sprintf(a, "%hd", (short) i);
}

/*
 *		ltoa			- converts a long int to its string represention
 *
 *		Note:
 *				previously based on ~ingres/source/gutil/atoi.c
 *				now uses vendor's sprintf conversion
 */
void
ltoa(int32 l, char *a)
{
	sprintf(a, "%d", l);
}

/*
 **  ftoa		- FLOATING POINT TO ASCII CONVERSION
 **
 **		CODE derived from ingres, ~ingres/source/gutil/ftoa.c
 **
 **		'Value' is converted to an ascii character string and stored
 **		into 'ascii'.  Ascii should have room for at least 'width' + 1
 **		characters.  'Width' is the width of the output field (max).
 **		'Prec' is the number of characters to put after the decimal
 **		point.	The format of the output string is controlled by
 **		'format'.
 **
 **		'Format' can be:
 **				e or E: "E" format output
 **				f or F:  "F" format output
 **				g or G:  "F" format output if it will fit, otherwise
 **						use "E" format.
 **				n or N:  same as G, but decimal points will not always
 **						be aligned.
 **
 **		If 'format' is upper case, the "E" comes out in upper case;
 **		otherwise it comes out in lower case.
 **
 **		When the field width is not big enough, it fills the field with
 **		stars ("*****") and returns zero.  Normal return is the width
 **		of the output field (sometimes shorter than 'width').
 */
#ifdef NOT_USED
int
ftoa(double value, char *ascii, int width, int prec1, char format)
{
#ifndef HAVE_FCVT
	char		out[256];
	char		fmt[256];
	int			ret;

	sprintf(fmt, "%%%d.%d%c", width, prec1, format);
	sprintf(out, fmt, value);
	if ((ret = strlen(out)) > width)
	{
		MemSet(ascii, '*', width - 2);
		ascii[width] = 0;
		return 0;
	}
	strcpy(ascii, out);
	return ret;
#else
	auto int	expon;
	auto int	sign;
	int			avail = 0;
	char	   *a = NULL;
	char	   *p = NULL;
	char		mode;
	int			lowercase;
	int			prec;

/*	  extern char		*ecvt(), *fcvt();*/

	prec = prec1;
	mode = format;
	lowercase = 'a' - 'A';
	if (mode >= 'a')
		mode -= 'a' - 'A';
	else
		lowercase = 0;

	if (mode != 'E')
	{
		/* try 'F' style output */
		p = fcvt(value, prec, &expon, &sign);
		avail = width;
		a = ascii;

		/* output sign */
		if (sign)
		{
			avail--;
			*a++ = '-';
		}

		/* output '0' before the decimal point */
		if (expon <= 0)
		{
			*a++ = '0';
			avail--;
		}

		/* compute space length left after dec pt and fraction */
		avail -= prec + 1;
		if (mode == 'G')
			avail -= 4;

		if (avail >= expon)
		{

			/* it fits.  output */
			while (expon > 0)
			{
				/* output left of dp */
				expon--;
				if (*p)
					*a++ = *p++;
				else
					*a++ = '0';
			}

			/* output fraction (right of dec pt) */
			avail = expon;
			goto frac_out;
		}
		/* won't fit; let's hope for G format */
	}

	if (mode != 'F')
	{
		/* try to do E style output */
		p = ecvt(value, prec + 1, &expon, &sign);
		avail = width - 5;
		a = ascii;

		/* output the sign */
		if (sign)
		{
			*a++ = '-';
			avail--;
		}
	}

	/* check for field too small */
	if (mode == 'F' || avail < prec)
	{
		/* sorry joker, you lose */
		a = ascii;
		for (avail = width; avail > 0; avail--)
			*a++ = '*';
		*a = 0;
		return 0;
	}

	/* it fits; output the number */
	mode = 'E';

	/* output the LHS single digit */
	*a++ = *p++;
	expon--;

	/* output the rhs */
	avail = 1;

frac_out:
	*a++ = '.';
	while (prec > 0)
	{
		prec--;
		if (avail < 0)
		{
			avail++;
			*a++ = '0';
		}
		else
		{
			if (*p)
				*a++ = *p++;
			else
				*a++ = '0';
		}
	}

	/* output the exponent */
	if (mode == 'E')
	{
		*a++ = 'E' + lowercase;
		if (expon < 0)
		{
			*a++ = '-';
			expon = -expon;
		}
		else
			*a++ = '+';
		*a++ = (expon / 10) % 10 + '0';
		*a++ = expon % 10 + '0';
	}

	/* output spaces on the end in G format */
	if (mode == 'G')
	{
		*a++ = ' ';
		*a++ = ' ';
		*a++ = ' ';
		*a++ = ' ';
	}

	/* finally, we can return */
	*a = 0;
	avail = a - ascii;
	return avail;
#endif
}

#endif

/*
 **   atof1		- ASCII TO FLOATING CONVERSION
 **
 **		CODE derived from ~ingres/source/gutil/atof.c
 **
 **		Converts the string 'str' to floating point and stores the
 **		result into the cell pointed to by 'val'.
 **
 **		The syntax which it accepts is pretty much what you would
 **		expect.  Basically, it is:
 **				{<sp>} [+|-] {<sp>} {<digit>} [.{digit}] {<sp>} [<exp>]
 **		where <exp> is "e" or "E" followed by an integer, <sp> is a
 **		space character, <digit> is zero through nine, [] is zero or
 **		one, and {} is zero or more.
 **
 **		Parameters:
 **				str -- string to convert.
 **				val -- pointer to place to put the result (which
 **						must be type double).
 **
 **		Returns:
 **				zero -- ok.
 **				-1 -- syntax error.
 **				+1 -- overflow (not implemented).
 **
 **		Side Effects:
 **				clobbers *val.
 */
#ifdef NOT_USED
int
atof1(char *str, double *val)
{
	char	   *p;
	double		v;
	double		fact;
	int			minus;
	char		c;
	int			expon;
	int			gotmant;

	v = 0.0;
	p = str;
	minus = 0;

	/* skip leading blanks */
	while ((c = *p) != '\0')
	{
		if (c != ' ')
			break;
		p++;
	}

	/* handle possible sign */
	switch (c)
	{
		case '-':
			minus++;

		case '+':
			p++;
	}

	/* skip blanks after sign */
	while ((c = *p) != '\0')
	{
		if (c != ' ')
			break;
		p++;
	}

	/* start collecting the number to the decimal point */
	gotmant = 0;
	for (;;)
	{
		c = *p;
		if (c < '0' || c > '9')
			break;
		v = v * 10.0 + (c - '0');
		gotmant++;
		p++;
	}

	/* check for fractional part */
	if (c == '.')
	{
		fact = 1.0;
		for (;;)
		{
			c = *++p;
			if (c < '0' || c > '9')
				break;
			fact *= 0.1;
			v += (c - '0') * fact;
			gotmant++;
		}
	}

	/* skip blanks before possible exponent */
	while ((c = *p) != '\0')
	{
		if (c != ' ')
			break;
		p++;
	}

	/* test for exponent */
	if (c == 'e' || c == 'E')
	{
		p++;
		expon = pg_atoi(p, sizeof(expon), '\0');
		if (!gotmant)
			v = 1.0;
		fact = expon;
		v *= pow(10.0, fact);
	}
	else
	{
		/* if no exponent, then nothing */
		if (c != 0)
			return -1;
	}

	/* store the result and exit */
	if (minus)
		v = -v;
	*val = v;
	return 0;
}

#endif