?? cash.c
字號(hào):
/* * cash.c * Written by D'Arcy J.M. Cain * * Functions to allow input and output of money normally but store * and handle it as int4s * * A slightly modified version of this file and a discussion of the * workings can be found in the book "Software Solutions in C" by * Dale Schumacher, Academic Press, ISBN: 0-12-632360-7. * * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.61 2003/09/25 06:58:03 petere Exp $ */#include "postgres.h"#include <limits.h>#include <ctype.h>#include <math.h>#include <locale.h>#include "libpq/pqformat.h"#include "miscadmin.h"#include "utils/builtins.h"#include "utils/cash.h"#include "utils/pg_locale.h"static const char *num_word(Cash value);/* when we go to 64 bit values we will have to modify this */#define CASH_BUFSZ 24#define TERMINATOR (CASH_BUFSZ - 1)#define LAST_PAREN (TERMINATOR - 1)#define LAST_DIGIT (LAST_PAREN - 1)/* * Cash is a pass-by-ref SQL type, so we must pass and return pointers. * These macros and support routine hide the pass-by-refness. */#define PG_GETARG_CASH(n) (* ((Cash *) PG_GETARG_POINTER(n)))#define PG_RETURN_CASH(x) return CashGetDatum(x)static DatumCashGetDatum(Cash value){ Cash *result = (Cash *) palloc(sizeof(Cash)); *result = value; return PointerGetDatum(result);}/* cash_in() * Convert a string to a cash data type. * Format is [$]###[,]###[.##] * Examples: 123.45 $123.45 $123,456.78 * * This is currently implemented as a 32-bit integer. * XXX HACK It looks as though some of the symbols for * monetary values returned by localeconv() can be multiple * bytes/characters. This code assumes one byte only. - tgl 97/04/14 * XXX UNHACK Allow the currency symbol to be multibyte. * - thomas 1998-03-01 */Datumcash_in(PG_FUNCTION_ARGS){ char *str = PG_GETARG_CSTRING(0); Cash result; Cash value = 0; Cash dec = 0; Cash sgn = 1; int seen_dot = 0; const char *s = str; int fpoint; char *csymbol; char dsymbol, ssymbol, psymbol, *nsymbol; struct lconv *lconvert = PGLC_localeconv(); /* * frac_digits will be CHAR_MAX in some locales, notably C. However, * just testing for == CHAR_MAX is risky, because of compilers like * gcc that "helpfully" let you alter the platform-standard definition * of whether char is signed or not. If we are so unfortunate as to * get compiled with a nonstandard -fsigned-char or -funsigned-char * switch, then our idea of CHAR_MAX will not agree with libc's. The * safest course is not to test for CHAR_MAX at all, but to impose a * range check for plausible frac_digits values. */ fpoint = lconvert->frac_digits; if (fpoint < 0 || fpoint > 10) fpoint = 2; /* best guess in this case, I think */ dsymbol = ((*lconvert->mon_decimal_point != '\0') ? *lconvert->mon_decimal_point : '.'); ssymbol = ((*lconvert->mon_thousands_sep != '\0') ? *lconvert->mon_thousands_sep : ','); csymbol = ((*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$"); psymbol = ((*lconvert->positive_sign != '\0') ? *lconvert->positive_sign : '+'); nsymbol = ((*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-");#ifdef CASHDEBUG printf("cashin- precision '%d'; decimal '%c'; thousands '%c'; currency '%s'; positive '%c'; negative '%s'\n", fpoint, dsymbol, ssymbol, csymbol, psymbol, nsymbol);#endif /* we need to add all sorts of checking here. For now just */ /* strip all leading whitespace and any leading currency symbol */ while (isspace((unsigned char) *s)) s++; if (strncmp(s, csymbol, strlen(csymbol)) == 0) s += strlen(csymbol);#ifdef CASHDEBUG printf("cashin- string is '%s'\n", s);#endif /* a leading minus or paren signifies a negative number */ /* again, better heuristics needed */ if (strncmp(s, nsymbol, strlen(nsymbol)) == 0) { sgn = -1; s += strlen(nsymbol);#ifdef CASHDEBUG printf("cashin- negative symbol; string is '%s'\n", s);#endif } else if (*s == '(') { sgn = -1; s++; } else if (*s == psymbol) s++;#ifdef CASHDEBUG printf("cashin- string is '%s'\n", s);#endif while (isspace((unsigned char) *s)) s++; if (strncmp(s, csymbol, strlen(csymbol)) == 0) s += strlen(csymbol);#ifdef CASHDEBUG printf("cashin- string is '%s'\n", s);#endif for (;; s++) { /* we look for digits as int4 as we have less */ /* than the required number of decimal places */ if (isdigit((unsigned char) *s) && dec < fpoint) { value = (value * 10) + *s - '0'; if (seen_dot) dec++; /* decimal point? then start counting fractions... */ } else if (*s == dsymbol && !seen_dot) { seen_dot = 1; /* "thousands" separator? then skip... */ } else if (*s == ssymbol) { } else { /* round off */ if (isdigit((unsigned char) *s) && *s >= '5') value++; /* adjust for less than required decimal places */ for (; dec < fpoint; dec++) value *= 10; break; } } while (isspace((unsigned char) *s) || *s == '0' || *s == ')') s++; if (*s != '\0') ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type money: \"%s\"", str))); result = (value * sgn);#ifdef CASHDEBUG printf("cashin- result is %d\n", result);#endif PG_RETURN_CASH(result);}/* cash_out() * Function to convert cash to a dollars and cents representation. * XXX HACK This code appears to assume US conventions for * positive-valued amounts. - tgl 97/04/14 */Datumcash_out(PG_FUNCTION_ARGS){ Cash value = PG_GETARG_CASH(0); char *result; char buf[CASH_BUFSZ]; int minus = 0; int count = LAST_DIGIT; int point_pos; int comma_position = 0; int points, mon_group; char comma; char *csymbol, dsymbol, *nsymbol; char convention; struct lconv *lconvert = PGLC_localeconv(); /* see comments about frac_digits in cash_in() */ points = lconvert->frac_digits; if (points < 0 || points > 10) points = 2; /* best guess in this case, I think */ /* * As with frac_digits, must apply a range check to mon_grouping to * avoid being fooled by variant CHAR_MAX values. */ mon_group = *lconvert->mon_grouping; if (mon_group <= 0 || mon_group > 6) mon_group = 3; comma = ((*lconvert->mon_thousands_sep != '\0') ? *lconvert->mon_thousands_sep : ','); convention = lconvert->n_sign_posn; dsymbol = ((*lconvert->mon_decimal_point != '\0') ? *lconvert->mon_decimal_point : '.'); csymbol = ((*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$"); nsymbol = ((*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-"); point_pos = LAST_DIGIT - points; /* allow more than three decimal points and separate them */ if (comma) { point_pos -= (points - 1) / mon_group; comma_position = point_pos % (mon_group + 1); } /* we work with positive amounts and add the minus sign at the end */ if (value < 0) { minus = 1; value = -value; } /* allow for trailing negative strings */ MemSet(buf, ' ', CASH_BUFSZ); buf[TERMINATOR] = buf[LAST_PAREN] = '\0'; while (value || count > (point_pos - 2)) { if (points && count == point_pos) buf[count--] = dsymbol; else if (comma && count % (mon_group + 1) == comma_position) buf[count--] = comma; buf[count--] = ((unsigned int) value % 10) + '0'; value = ((unsigned int) value) / 10; } strncpy((buf + count - strlen(csymbol) + 1), csymbol, strlen(csymbol)); count -= strlen(csymbol) - 1; if (buf[LAST_DIGIT] == ',') buf[LAST_DIGIT] = buf[LAST_PAREN]; /* see if we need to signify negative amount */ if (minus) { if (!PointerIsValid(result = palloc(CASH_BUFSZ + 2 - count + strlen(nsymbol)))) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); /* Position code of 0 means use parens */ if (convention == 0) sprintf(result, "(%s)", buf + count); else if (convention == 2) sprintf(result, "%s%s", buf + count, nsymbol); else sprintf(result, "%s%s", nsymbol, buf + count); } else { if (!PointerIsValid(result = palloc(CASH_BUFSZ + 2 - count))) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); strcpy(result, buf + count); } PG_RETURN_CSTRING(result);}/* * cash_recv - converts external binary format to cash */Datumcash_recv(PG_FUNCTION_ARGS){ StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); PG_RETURN_CASH((Cash) pq_getmsgint(buf, sizeof(Cash)));}/* * cash_send - converts cash to binary format */Datumcash_send(PG_FUNCTION_ARGS){ Cash arg1 = PG_GETARG_CASH(0); StringInfoData buf; pq_begintypsend(&buf); pq_sendint(&buf, arg1, sizeof(Cash)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf));}/* * Comparison functions */Datumcash_eq(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); PG_RETURN_BOOL(c1 == c2);}Datumcash_ne(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); PG_RETURN_BOOL(c1 != c2);}Datumcash_lt(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); PG_RETURN_BOOL(c1 < c2);}Datumcash_le(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); PG_RETURN_BOOL(c1 <= c2);}Datumcash_gt(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); PG_RETURN_BOOL(c1 > c2);}Datumcash_ge(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); PG_RETURN_BOOL(c1 >= c2);}Datumcash_cmp(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -