Convert to and from iban type

This commit is contained in:
yorickdewid 2016-03-05 20:33:30 +01:00
parent 1884010ceb
commit b65daaba8b
1 changed files with 60 additions and 8 deletions

66
iban.cc
View File

@ -390,13 +390,13 @@ Validate::Validate() {
"UA511234567890123456789012345")); "UA511234567890123456789012345"));
addSpecification(new Specification( addSpecification(new Specification(
"EG", 27, "F23", "EG", 27, "F23",
"EG1100006001880800100014553"))); "EG1100006001880800100014553"));
addSpecification(new Specification( addSpecification(new Specification(
"CG", 27, "F23", "CG", 27, "F23",
"CG5230011000202151234567890"))); "CG5230011000202151234567890"));
addSpecification(new Specification( addSpecification(new Specification(
"GA", 27, "F23", "GA", 27, "F23",
"GA2140002000055602673300064"))); "GA2140002000055602673300064"));
} }
@ -409,33 +409,85 @@ Validate::Validate() {
* @returns {bool} * @returns {bool}
*/ */
namespace { namespace {
bool account_validate(text *iban) { bool account_validate_text(text *iban) {
char *ciban; char *ciban;
bool result; bool result;
Validate val; Validate val;
ciban = text_to_cstring(iban); ciban = text_to_cstring(iban);
try { try {
result = val.isValid(std::string(ciban)); result = val.isValid(std::string(ciban));
} catch (std::exception& e) { } catch (std::exception& e) {
elog(ERROR, "%s", e.what()); elog(ERROR, "%s", e.what());
return false;
} }
return result;
}
bool account_validate_str(char *iban) {
bool result;
Validate val;
try {
result = val.isValid(std::string(iban));
} catch (std::exception& e) {
elog(ERROR, "%s", e.what());
return false; return false;
} }
return result;
}
} }
extern "C" extern "C"
{ {
/* Convert type input */
PG_FUNCTION_INFO_V1(ibanin);
Datum
ibanin(PG_FUNCTION_ARGS) {
char *str = PG_GETARG_CSTRING(0);
char *iban = (char *) palloc(strlen(str) + 1);
memcpy(iban, str, strlen(str));
iban[strlen(str)] = '\0';
if (!account_validate_str(iban))
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid syntax or iban account: \"%s\"",
iban)));
PG_RETURN_POINTER(iban);
}
/* Convert type output */
PG_FUNCTION_INFO_V1(ibanout);
Datum
ibanout(PG_FUNCTION_ARGS) {
char *iban = (char *) PG_GETARG_POINTER(0);
char *result;
result = psprintf("%s", iban);
PG_RETURN_CSTRING(result);
}
/* Manually verify a text */
PG_FUNCTION_INFO_V1(iban_validate); PG_FUNCTION_INFO_V1(iban_validate);
Datum Datum
iban_validate(PG_FUNCTION_ARGS) { iban_validate(PG_FUNCTION_ARGS) {
text *iban = PG_GETARG_TEXT_P(0); text *iban = PG_GETARG_TEXT_P(0);
bool r = account_validate(iban); bool result = account_validate_text(iban);
PG_RETURN_BOOL(r); PG_RETURN_BOOL(result);
} }
} }