introduced defines for error codes

This commit is contained in:
Tim Ruehsen 2014-06-20 12:36:51 +02:00
parent d34938703a
commit 1c20931896
4 changed files with 28 additions and 19 deletions

View File

@ -38,6 +38,14 @@
extern "C" {
#endif
typedef enum {
PSL_SUCCESS = 0,
PSL_ERR_INVALID_ARG = -1,
PSL_ERR_CONVERTER = -2, /* failed to open libicu utf-16 converter */
PSL_ERR_TO_UTF16 = -3, /* failed to convert to utf-16 */
PSL_ERR_TO_LOWER = -4, /* failed to convert utf-16 to lowercase */
PSL_ERR_TO_UTF8 = -5 /* failed to convert utf-16 to utf-8 */
} psl_error_t;
typedef struct _psl_ctx_st psl_ctx_t;

View File

@ -852,24 +852,25 @@ int psl_is_cookie_domain_acceptable(const psl_ctx_t *psl, const char *hostname,
* @lower is %NULL on error.
* The return value 'lower' must be freed after usage.
*
* Returns: 0 on success, negative value on error.
* -1 @str is a %NULL value
* -2 failed to open converter with name @encoding
* -3 failed to convert @str to unicode
* -4 failed to convert unicode to lowercase
* -5 failed to convert unicode to UTF-8
* Returns: psl_error_t value.
* PSL_SUCCESS: Success
* PSL_ERR_INVALID_ARG: @str is a %NULL value.
* PSL_ERR_CONVERTER: Failed to open the unicode converter with name @encoding
* PSL_ERR_TO_UTF16: Failed to convert @str to unicode
* PSL_ERR_TO_LOWER: Failed to convert unicode to lowercase
* PSL_ERR_TO_UTF8: Failed to convert unicode to UTF-8
*
* Since: 0.4
*/
int psl_str_to_utf8lower(const char *str, const char *encoding, const char *locale, char **lower)
psl_error_t psl_str_to_utf8lower(const char *str, const char *encoding, const char *locale, char **lower)
{
int ret = -1;
int ret = PSL_ERR_INVALID_ARG;
if (lower)
*lower = NULL;
if (!str)
return -1;
return PSL_ERR_INVALID_ARG;
/* shortcut to avoid costly conversion */
if (_str_is_ascii(str)) {
@ -883,7 +884,7 @@ int psl_str_to_utf8lower(const char *str, const char *encoding, const char *loca
if (isupper(*p))
*p = tolower(*p);
}
return 0;
return PSL_SUCCESS;
}
#ifdef WITH_LIBICU
@ -912,21 +913,21 @@ int psl_str_to_utf8lower(const char *str, const char *encoding, const char *loca
if (U_SUCCESS(status)) {
if (lower)
*lower = strdup(utf8_lower);
ret = 0;
ret = PSL_SUCCESS;
} else {
ret = -5;
ret = PSL_ERR_TO_UTF8;
/* fprintf(stderr, "Failed to convert UTF-16 to UTF-8 (status %d)\n", status); */
}
} else {
ret = -4;
ret = PSL_ERR_TO_LOWER;
/* fprintf(stderr, "Failed to convert UTF-16 to lowercase (status %d)\n", status); */
}
} else {
ret = -3;
ret = PSL_ERR_TO_UTF16;
/* fprintf(stderr, "Failed to convert string to UTF-16 (status %d)\n", status); */
}
} else {
ret = -2;
ret = PSL_ERR_CONVERTER;
/* fprintf(stderr, "Failed to open converter for '%s' (status %d)\n", encoding, status); */
}
} while (0);

View File

@ -55,7 +55,7 @@ static void test(const psl_ctx_t *psl, const char *domain, const char *expected_
char *lower;
/* our test data is fixed to UTF-8 (english), so provide it here */
if (psl_str_to_utf8lower(domain, "utf-8", "en", &lower) == 0)
if (psl_str_to_utf8lower(domain, "utf-8", "en", &lower) == PSL_SUCCESS)
domain = lower;
result = psl_registrable_domain(psl, domain);

View File

@ -149,7 +149,7 @@ int main(int argc, const char *const *argv)
if (arg >= argv + argc) {
char buf[256], *domain, *lower;
size_t len;
int rc;
psl_error_t rc;
/* read URLs from STDIN */
while (fgets(buf, sizeof(buf), stdin)) {
@ -158,7 +158,7 @@ int main(int argc, const char *const *argv)
for (len = strlen(domain); len && isspace(domain[len - 1]); len--); /* skip trailing spaces */
domain[len] = 0;
if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &lower)) != 0)
if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &lower)) != PSL_SUCCESS)
fprintf(stderr, "%s: Failed to convert to lowercase UTF-8 (%d)\n", domain, rc);
else if (mode == 1)
printf("%s: %d (%s)\n", domain, psl_is_public_suffix(psl, lower), lower);
@ -169,7 +169,7 @@ int main(int argc, const char *const *argv)
else if (mode == 4) {
char *cookie_domain_lower;
if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &cookie_domain_lower)) != 0)
if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &cookie_domain_lower)) != PSL_SUCCESS)
fprintf(stderr, "%s: Failed to convert cookie domain '%s' to lowercase UTF-8 (%d)\n", domain, cookie_domain, rc);
else
printf("%s: %d\n", domain, psl_is_cookie_domain_acceptable(psl, lower, cookie_domain));