summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/cash.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-06-05 07:29:25 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-06-05 07:29:25 +0000
commit48165ec2262b73c5b81a6caabab66d883d013a83 (patch)
tree08e878a2a1e7f76981406ac2b34729a510aecac6 /src/backend/utils/adt/cash.c
parentc61db5ba2decf2e620f6ce3699d4b702957ed72a (diff)
Latest round of fmgr updates. All functions with bool,char, or int2
inputs have been converted to newstyle. This should go a long way towards fixing our portability problems with platforms where char and short parameters are passed differently from int-width parameters. Still more to do for the Alpha port however.
Diffstat (limited to 'src/backend/utils/adt/cash.c')
-rw-r--r--src/backend/utils/adt/cash.c76
1 files changed, 43 insertions, 33 deletions
diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c
index 9a06bc38954..9bfc46bda6c 100644
--- a/src/backend/utils/adt/cash.c
+++ b/src/backend/utils/adt/cash.c
@@ -9,7 +9,7 @@
* 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.36 2000/05/16 20:48:49 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.37 2000/06/05 07:28:51 tgl Exp $
*/
#include <limits.h>
@@ -35,6 +35,24 @@ static struct lconv *lconvert = NULL;
#endif
+
+/*
+ * 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 *) DatumGetPointer(fcinfo->arg[n])))
+#define PG_RETURN_CASH(x) return CashGetDatum(x)
+
+static Datum
+CashGetDatum(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 [$]###[,]###[.##]
@@ -573,32 +591,30 @@ cash_div_int4(Cash *c, int4 i)
/* cash_mul_int2()
* Multiply cash by int2.
*/
-Cash *
-cash_mul_int2(Cash *c, int2 s)
+Datum
+cash_mul_int2(PG_FUNCTION_ARGS)
{
- Cash *result;
-
- if (!PointerIsValid(c))
- return NULL;
-
- if (!PointerIsValid(result = palloc(sizeof(Cash))))
- elog(ERROR, "Memory allocation failed, can't multiply cash");
-
- *result = ((s) * (*c));
-
- return result;
-} /* cash_mul_int2() */
+ Cash c = PG_GETARG_CASH(0);
+ int16 s = PG_GETARG_INT16(1);
+ Cash result;
+ result = c * s;
+ PG_RETURN_CASH(result);
+}
/* int2_mul_cash()
* Multiply int2 by cash.
*/
-Cash *
-int2_mul_cash(int2 s, Cash *c)
+Datum
+int2_mul_cash(PG_FUNCTION_ARGS)
{
- return cash_mul_int2(c, s);
-} /* int2_mul_cash() */
+ int16 s = PG_GETARG_INT16(0);
+ Cash c = PG_GETARG_CASH(1);
+ Cash result;
+ result = s * c;
+ PG_RETURN_CASH(result);
+}
/* cash_div_int2()
* Divide cash by int2.
@@ -606,25 +622,19 @@ int2_mul_cash(int2 s, Cash *c)
* XXX Don't know if rounding or truncating is correct behavior.
* Round for now. - tgl 97/04/15
*/
-Cash *
-cash_div_int2(Cash *c, int2 s)
+Datum
+cash_div_int2(PG_FUNCTION_ARGS)
{
- Cash *result;
-
- if (!PointerIsValid(c))
- return NULL;
-
- if (!PointerIsValid(result = palloc(sizeof(Cash))))
- elog(ERROR, "Memory allocation failed, can't divide cash");
+ Cash c = PG_GETARG_CASH(0);
+ int16 s = PG_GETARG_INT16(1);
+ Cash result;
if (s == 0)
elog(ERROR, "cash_div: divide by 0 error");
- *result = rint(*c / s);
-
- return result;
-} /* cash_div_int2() */
-
+ result = rint(c / s);
+ PG_RETURN_CASH(result);
+}
/* cashlarger()
* Return larger of two cash values.