/*
* string2ean --- try to parse a string into an ean13.
*
- * If errorOK is false, ereport a useful error message if the string is bad.
- * If errorOK is true, just return "false" for bad input.
+ * ereturn false with a useful error message if the string is bad.
+ * Otherwise return true.
*
* if the input string ends with '!' it will always be treated as invalid
* (even if the check digit is valid)
*/
static bool
-string2ean(const char *str, bool errorOK, ean13 *result,
+string2ean(const char *str, struct Node *escontext, ean13 *result,
enum isn_type accept)
{
bool digit,
return true;
}
- if (!errorOK)
+ if (rcheck == (unsigned) -1)
{
- if (rcheck == (unsigned) -1)
- {
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
- errmsg("invalid %s number: \"%s\"",
- isn_names[accept], str)));
- }
- else
- {
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
- errmsg("invalid check digit for %s number: \"%s\", should be %c",
- isn_names[accept], str, (rcheck == 10) ? ('X') : (rcheck + '0'))));
- }
+ ereturn(escontext, false,
+ (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+ errmsg("invalid %s number: \"%s\"",
+ isn_names[accept], str)));
+ }
+ else
+ {
+ ereturn(escontext, false,
+ (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+ errmsg("invalid check digit for %s number: \"%s\", should be %c",
+ isn_names[accept], str, (rcheck == 10) ? ('X') : (rcheck + '0'))));
}
- return false;
eaninvalid:
- if (!errorOK)
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
- errmsg("invalid input syntax for %s number: \"%s\"",
- isn_names[accept], str)));
- return false;
+ ereturn(escontext, false,
+ (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+ errmsg("invalid input syntax for %s number: \"%s\"",
+ isn_names[accept], str)));
eanwrongtype:
- if (!errorOK)
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
- errmsg("cannot cast %s to %s for number: \"%s\"",
- isn_names[type], isn_names[accept], str)));
- return false;
+ ereturn(escontext, false,
+ (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+ errmsg("cannot cast %s to %s for number: \"%s\"",
+ isn_names[type], isn_names[accept], str)));
eantoobig:
- if (!errorOK)
- ereport(ERROR,
- (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
- errmsg("value \"%s\" is out of range for %s type",
- str, isn_names[accept])));
- return false;
+ ereturn(escontext, false,
+ (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+ errmsg("value \"%s\" is out of range for %s type",
+ str, isn_names[accept])));
}
/*----------------------------------------------------------
char *result;
char buf[MAXEAN13LEN + 1];
- (void) ean2string(val, false, buf, true);
+ (void) ean2string(val, fcinfo->context, buf, true);
result = pstrdup(buf);
PG_RETURN_CSTRING(result);
char *result;
char buf[MAXEAN13LEN + 1];
- (void) ean2string(val, false, buf, false);
+ (void) ean2string(val, fcinfo->context, buf, false);
result = pstrdup(buf);
PG_RETURN_CSTRING(result);
const char *str = PG_GETARG_CSTRING(0);
ean13 result;
- (void) string2ean(str, false, &result, EAN13);
+ if (!string2ean(str, fcinfo->context, &result, EAN13))
+ PG_RETURN_NULL();
PG_RETURN_EAN13(result);
}
const char *str = PG_GETARG_CSTRING(0);
ean13 result;
- (void) string2ean(str, false, &result, ISBN);
+ if (!string2ean(str, fcinfo->context, &result, ISBN))
+ PG_RETURN_NULL();
PG_RETURN_EAN13(result);
}
const char *str = PG_GETARG_CSTRING(0);
ean13 result;
- (void) string2ean(str, false, &result, ISMN);
+ if (!string2ean(str, fcinfo->context, &result, ISMN))
+ PG_RETURN_NULL();
PG_RETURN_EAN13(result);
}
const char *str = PG_GETARG_CSTRING(0);
ean13 result;
- (void) string2ean(str, false, &result, ISSN);
+ if (!string2ean(str, fcinfo->context, &result, ISSN))
+ PG_RETURN_NULL();
PG_RETURN_EAN13(result);
}
const char *str = PG_GETARG_CSTRING(0);
ean13 result;
- (void) string2ean(str, false, &result, UPC);
+ if (!string2ean(str, fcinfo->context, &result, UPC))
+ PG_RETURN_NULL();
PG_RETURN_EAN13(result);
}