Datum spheretrans_type(PG_FUNCTION_ARGS) { SEuler * se = ( SEuler * ) PG_GETARG_POINTER ( 0 ) ; BpChar *result = ( BpChar * ) MALLOC ( 3 + VARHDRSZ ); char ret[4] ; int i ; unsigned char t = 0; for ( i=0; i<3; i++ ){ switch ( i ){ case 0: t = se->phi_a ; break; case 1: t = se->theta_a; break; case 2: t = se->psi_a ; break; } switch ( t ){ case EULER_AXIS_X : ret[i]='X'; break; case EULER_AXIS_Y : ret[i]='Y'; break; case EULER_AXIS_Z : ret[i]='Z'; break; } } ret[3] = '\0'; #if PG_VERSION_NUM < 80300 VARATT_SIZEP(result) = 3 + VARHDRSZ; #else SET_VARSIZE(result, 3 + VARHDRSZ); #endif memcpy((void*)VARDATA(result), (void*)&ret[0], 3 ); PG_RETURN_BPCHAR_P(result); }
/* char_bpchar() * Convert char to bpchar(1). */ Datum char_bpchar(PG_FUNCTION_ARGS) { char c = PG_GETARG_CHAR(0); BpChar *result; result = (BpChar *) palloc(VARHDRSZ + 1); VARATT_SIZEP(result) = VARHDRSZ + 1; *(VARDATA(result)) = c; PG_RETURN_BPCHAR_P(result); }
/* name_bpchar() * Converts a NameData type to a bpchar type. */ Datum name_bpchar(PG_FUNCTION_ARGS) { Name s = PG_GETARG_NAME(0); BpChar *result; int len; len = strlen(NameStr(*s)); result = (BpChar *) palloc(VARHDRSZ + len); memcpy(VARDATA(result), NameStr(*s), len); VARATT_SIZEP(result) = len + VARHDRSZ; PG_RETURN_BPCHAR_P(result); }
/* * Convert a C string to CHARACTER internal representation. atttypmod * is the declared length of the type plus VARHDRSZ. */ Datum bpcharin(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); #ifdef NOT_USED Oid typelem = PG_GETARG_OID(1); #endif int32 atttypmod = PG_GETARG_INT32(2); BpChar *result; result = bpchar_input(s, strlen(s), atttypmod); PG_RETURN_BPCHAR_P(result); }
Datum bpchar_smaller(PG_FUNCTION_ARGS) { BpChar *arg1 = PG_GETARG_BPCHAR_P(0); BpChar *arg2 = PG_GETARG_BPCHAR_P(1); int len1, len2; int cmp; len1 = bcTruelen(arg1); len2 = bcTruelen(arg2); cmp = varstr_cmp(VARDATA(arg1), len1, VARDATA(arg2), len2); PG_RETURN_BPCHAR_P((cmp <= 0) ? arg1 : arg2); }
/* * bpcharrecv - converts external binary format to bpchar */ Datum bpcharrecv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); #ifdef NOT_USED Oid typelem = PG_GETARG_OID(1); #endif int32 atttypmod = PG_GETARG_INT32(2); BpChar *result; char *str; int nbytes; str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); result = bpchar_input(str, nbytes, atttypmod); pfree(str); PG_RETURN_BPCHAR_P(result); }
/* * Converts a CHARACTER type to the specified size. * * maxlen is the typmod, ie, declared length plus VARHDRSZ bytes. * isExplicit is true if this is for an explicit cast to char(N). * * Truncation rules: for an explicit cast, silently truncate to the given * length; for an implicit cast, raise error unless extra characters are * all spaces. (This is sort-of per SQL: the spec would actually have us * raise a "completion condition" for the explicit cast case, but Postgres * hasn't got such a concept.) */ Datum bpchar(PG_FUNCTION_ARGS) { BpChar *source = PG_GETARG_BPCHAR_P(0); int32 maxlen = PG_GETARG_INT32(1); bool isExplicit = PG_GETARG_BOOL(2); BpChar *result; int32 len; char *r; char *s; int i; int charlen; /* number of characters in the input string + * VARHDRSZ */ /* No work if typmod is invalid */ if (maxlen < (int32) VARHDRSZ) PG_RETURN_BPCHAR_P(source); len = VARSIZE(source); charlen = pg_mbstrlen_with_len(VARDATA(source), len - VARHDRSZ) + VARHDRSZ; /* No work if supplied data matches typmod already */ if (charlen == maxlen) PG_RETURN_BPCHAR_P(source); if (charlen > maxlen) { /* Verify that extra characters are spaces, and clip them off */ size_t maxmblen; maxmblen = pg_mbcharcliplen(VARDATA(source), len - VARHDRSZ, maxlen - VARHDRSZ) + VARHDRSZ; if (!isExplicit) { for (i = maxmblen - VARHDRSZ; i < len - VARHDRSZ; i++) if (*(VARDATA(source) + i) != ' ') ereport(ERROR, (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), errmsg("value too long for type character(%d)", maxlen - VARHDRSZ))); } len = maxmblen; /* * XXX: at this point, maxlen is the necessary byte length+VARHDRSZ, * not the number of CHARACTERS! */ maxlen = len; } else { /* * XXX: at this point, maxlen is the necessary byte length+VARHDRSZ, * not the number of CHARACTERS! */ maxlen = len + (maxlen - charlen); } s = VARDATA(source); result = palloc(maxlen); VARATT_SIZEP(result) = maxlen; r = VARDATA(result); memcpy(r, s, len - VARHDRSZ); /* blank pad the string if necessary */ if (maxlen > len) memset(r + len - VARHDRSZ, ' ', maxlen - len); PG_RETURN_BPCHAR_P(result); }