Use byte array

This commit is contained in:
yorickdewid 2016-03-05 21:33:02 +01:00
parent b65daaba8b
commit 0efb9e6dca
1 changed files with 19 additions and 14 deletions

33
iban.cc
View File

@ -15,13 +15,19 @@ extern "C"
#include "specification.h" #include "specification.h"
#include "validate.h" #include "validate.h"
#define PG_RETURN_IBAN_P(x) PG_RETURN_POINTER(x)
extern "C" extern "C"
{ {
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC; PG_MODULE_MAGIC;
#endif
extern Datum ibanin(PG_FUNCTION_ARGS);
extern Datum ibanout(PG_FUNCTION_ARGS);
extern Datum iban_validate(PG_FUNCTION_ARGS); extern Datum iban_validate(PG_FUNCTION_ARGS);
typedef struct Iban {
char account[34];
} Iban;
} }
void Validate::addSpecification(Specification* specPtr) { void Validate::addSpecification(Specification* specPtr) {
@ -449,19 +455,19 @@ extern "C"
Datum Datum
ibanin(PG_FUNCTION_ARGS) { ibanin(PG_FUNCTION_ARGS) {
char *str = PG_GETARG_CSTRING(0); char *str = PG_GETARG_CSTRING(0);
char *iban = (char *) palloc(strlen(str) + 1); Iban *result;
memcpy(iban, str, strlen(str)); if (!account_validate_str(str))
iban[strlen(str)] = '\0';
if (!account_validate_str(iban))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid syntax or iban account: \"%s\"", errmsg("invalid iban format for value: \"%s\"",
iban))); str)));
PG_RETURN_POINTER(iban); result = (Iban *) palloc0(sizeof(Iban));
memcpy(result->account, str, strlen(str));
PG_RETURN_POINTER(result);
} }
/* Convert type output */ /* Convert type output */
@ -470,11 +476,10 @@ extern "C"
Datum Datum
ibanout(PG_FUNCTION_ARGS) { ibanout(PG_FUNCTION_ARGS) {
char *iban = (char *) PG_GETARG_POINTER(0); Iban *iban = (Iban *) PG_GETARG_POINTER(0);
char *result; char *result;
result = psprintf("%s", iban); result = psprintf("%s", iban->account);
PG_RETURN_CSTRING(result); PG_RETURN_CSTRING(result);
} }