diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2000-08-03 23:07:51 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2000-08-03 23:07:51 +0000 |
commit | ed9ca687582caa88f31c4b273b9fd4eb5743cf41 (patch) | |
tree | 5f291877d0a74c7c3f34da74c4e20aca6bd9a65f /src/backend/utils/adt/mac.c | |
parent | 61aca818c486dbe000ce94c77cb1dd1f379baf67 (diff) | |
download | postgresql-ed9ca687582caa88f31c4b273b9fd4eb5743cf41.tar.gz postgresql-ed9ca687582caa88f31c4b273b9fd4eb5743cf41.zip |
Convert inet-related functions to new fmgr style. I have also taken it
on myself to do something about the non-self-consistency of the inet
comparison functions. The results are probably still semantically wrong
(inet and cidr should have different comparison semantics, I think)
but at least the boolean operators now agree with each other and with
the sort order of indexes on inet/cidr.
Diffstat (limited to 'src/backend/utils/adt/mac.c')
-rw-r--r-- | src/backend/utils/adt/mac.c | 146 |
1 files changed, 76 insertions, 70 deletions
diff --git a/src/backend/utils/adt/mac.c b/src/backend/utils/adt/mac.c index 5b542504643..e8d502b87d2 100644 --- a/src/backend/utils/adt/mac.c +++ b/src/backend/utils/adt/mac.c @@ -1,20 +1,18 @@ /* * PostgreSQL type definitions for MAC addresses. * - * $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.16 2000/07/06 05:48:11 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.17 2000/08/03 23:07:46 tgl Exp $ */ #include "postgres.h" #include "utils/builtins.h" - +#include "utils/inet.h" /* - * macaddr is a pass-by-reference datatype. + * XXX this table of manufacturers is long out of date, and should never + * have been wired into the code in the first place. */ -#define PG_GETARG_MACADDR_P(n) ((macaddr *) PG_GETARG_POINTER(n)) -#define PG_RETURN_MACADDR_P(x) return PointerGetDatum(x) - typedef struct manufacturer { @@ -145,18 +143,18 @@ static manufacturer manufacturers[] = { */ #define hibits(addr) \ - ((unsigned long)((addr->a<<16)|(addr->b<<8)|(addr->c))) + ((unsigned long)(((addr)->a<<16)|((addr)->b<<8)|((addr)->c))) #define lobits(addr) \ - ((unsigned long)((addr->d<<16)|(addr->e<<8)|(addr->f))) + ((unsigned long)(((addr)->d<<16)|((addr)->e<<8)|((addr)->f))) /* * MAC address reader. Accepts several common notations. */ - -macaddr * -macaddr_in(char *str) +Datum +macaddr_in(PG_FUNCTION_ARGS) { + char *str = PG_GETARG_CSTRING(0); int a, b, c, @@ -201,21 +199,18 @@ macaddr_in(char *str) result->e = e; result->f = f; - return (result); + PG_RETURN_MACADDR_P(result); } /* * MAC address output function. Fixed format. */ - -char * -macaddr_out(macaddr *addr) +Datum +macaddr_out(PG_FUNCTION_ARGS) { + macaddr *addr = PG_GETARG_MACADDR_P(0); char *result; - if (addr == NULL) - return (NULL); - result = (char *) palloc(32); if ((hibits(addr) > 0) || (lobits(addr) > 0)) @@ -225,84 +220,95 @@ macaddr_out(macaddr *addr) } else { - result[0] = 0; /* special case for missing address */ + result[0] = '\0'; /* special case for missing address */ } - return (result); + + PG_RETURN_CSTRING(result); } /* - * Boolean tests. + * Comparison function for sorting: */ -bool -macaddr_lt(macaddr *a1, macaddr *a2) +static int32 +macaddr_cmp_internal(macaddr *a1, macaddr *a2) { - if (!PointerIsValid(a1) || !PointerIsValid(a2)) - return FALSE; - return ((hibits(a1) < hibits(a2)) || - ((hibits(a1) == hibits(a2)) && lobits(a1) < lobits(a2))); + if (hibits(a1) < hibits(a2)) + return -1; + else if (hibits(a1) > hibits(a2)) + return 1; + else if (lobits(a1) < lobits(a2)) + return -1; + else if (lobits(a1) > lobits(a2)) + return 1; + else + return 0; } -bool -macaddr_le(macaddr *a1, macaddr *a2) +Datum +macaddr_cmp(PG_FUNCTION_ARGS) { - if (!PointerIsValid(a1) || !PointerIsValid(a2)) - return FALSE; - return ((hibits(a1) < hibits(a2)) || - ((hibits(a1) == hibits(a2)) && lobits(a1) <= lobits(a2))); + macaddr *a1 = PG_GETARG_MACADDR_P(0); + macaddr *a2 = PG_GETARG_MACADDR_P(1); + + PG_RETURN_INT32(macaddr_cmp_internal(a1, a2)); } -bool -macaddr_eq(macaddr *a1, macaddr *a2) +/* + * Boolean comparisons. + */ +Datum +macaddr_lt(PG_FUNCTION_ARGS) { - if (!PointerIsValid(a1) || !PointerIsValid(a2)) - return FALSE; - return ((hibits(a1) == hibits(a2)) && (lobits(a1) == lobits(a2))); + macaddr *a1 = PG_GETARG_MACADDR_P(0); + macaddr *a2 = PG_GETARG_MACADDR_P(1); + + PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) < 0); } -bool -macaddr_ge(macaddr *a1, macaddr *a2) +Datum +macaddr_le(PG_FUNCTION_ARGS) { - if (!PointerIsValid(a1) || !PointerIsValid(a2)) - return FALSE; - return ((hibits(a1) > hibits(a2)) || - ((hibits(a1) == hibits(a2)) && lobits(a1) >= lobits(a2))); + macaddr *a1 = PG_GETARG_MACADDR_P(0); + macaddr *a2 = PG_GETARG_MACADDR_P(1); + + PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) <= 0); } -bool -macaddr_gt(macaddr *a1, macaddr *a2) +Datum +macaddr_eq(PG_FUNCTION_ARGS) { - if (!PointerIsValid(a1) || !PointerIsValid(a2)) - return FALSE; - return ((hibits(a1) > hibits(a2)) || - ((hibits(a1) == hibits(a2)) && lobits(a1) > lobits(a2))); + macaddr *a1 = PG_GETARG_MACADDR_P(0); + macaddr *a2 = PG_GETARG_MACADDR_P(1); + + PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) == 0); } -bool -macaddr_ne(macaddr *a1, macaddr *a2) +Datum +macaddr_ge(PG_FUNCTION_ARGS) { - if (!PointerIsValid(a1) || !PointerIsValid(a2)) - return FALSE; - return ((hibits(a1) != hibits(a2)) || (lobits(a1) != lobits(a2))); + macaddr *a1 = PG_GETARG_MACADDR_P(0); + macaddr *a2 = PG_GETARG_MACADDR_P(1); + + PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) >= 0); } -/* - * Comparison function for sorting: - */ +Datum +macaddr_gt(PG_FUNCTION_ARGS) +{ + macaddr *a1 = PG_GETARG_MACADDR_P(0); + macaddr *a2 = PG_GETARG_MACADDR_P(1); + + PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) > 0); +} -int4 -macaddr_cmp(macaddr *a1, macaddr *a2) +Datum +macaddr_ne(PG_FUNCTION_ARGS) { - if (hibits(a1) < hibits(a2)) - return -1; - else if (hibits(a1) > hibits(a2)) - return 1; - else if (lobits(a1) < lobits(a2)) - return -1; - else if (lobits(a1) > lobits(a2)) - return 1; - else - return 0; + macaddr *a1 = PG_GETARG_MACADDR_P(0); + macaddr *a2 = PG_GETARG_MACADDR_P(1); + + PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) != 0); } /* |