diff options
author | Vincent Sanders <vince@kyllikki.org> | 2025-03-19 22:55:17 +0000 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2025-03-19 22:55:17 +0000 |
commit | c2ac0f37cafe912dac13b6d1798e84de7bdce8a8 (patch) | |
tree | 6396100327389f51f59716dded8f447ee0ae1bbf | |
parent | df37b7ee37e3b94a96703eee4417a9f304050ecb (diff) | |
download | netsurf-c2ac0f37cafe912dac13b6d1798e84de7bdce8a8.tar.gz netsurf-c2ac0f37cafe912dac13b6d1798e84de7bdce8a8.tar.bz2 |
Alter hash add to return standard error code
-rw-r--r-- | test/hashtable.c | 12 | ||||
-rw-r--r-- | utils/hashtable.c | 17 | ||||
-rw-r--r-- | utils/hashtable.h | 4 |
3 files changed, 17 insertions, 16 deletions
diff --git a/test/hashtable.c b/test/hashtable.c index ea74b78b2..cf0169a7e 100644 --- a/test/hashtable.c +++ b/test/hashtable.c @@ -159,7 +159,7 @@ END_TEST START_TEST(hashtable_negative_test) { struct hash_table *ht; - bool added; + nserror ret; const char *res; /* create hash */ @@ -167,8 +167,8 @@ START_TEST(hashtable_negative_test) ck_assert(ht != NULL); /* add entry */ - added = hash_add(ht, "cow", "moo"); - ck_assert(added == true); + ret = hash_add(ht, "cow", "moo"); + ck_assert(ret == NSERROR_OK); res = hash_get(ht, "sheep"); ck_assert(res == NULL); @@ -187,7 +187,7 @@ END_TEST START_TEST(hashtable_positive_test) { struct hash_table *ht; - bool added; + nserror ret; const char *res; /* create hash */ @@ -195,8 +195,8 @@ START_TEST(hashtable_positive_test) ck_assert(ht != NULL); /* add entry */ - added = hash_add(ht, "cow", "moo"); - ck_assert(added == true); + ret = hash_add(ht, "cow", "moo"); + ck_assert(ret == NSERROR_OK); res = hash_get(ht, "cow"); ck_assert(res != NULL); diff --git a/utils/hashtable.c b/utils/hashtable.c index aa162cbc4..95e2ba1df 100644 --- a/utils/hashtable.c +++ b/utils/hashtable.c @@ -96,6 +96,7 @@ process_line(struct hash_table *hash, uint8_t *ln, int lnlen) uint8_t *key; uint8_t *value; uint8_t *colon; + nserror res; key = ln; /* set key to start of line */ value = ln + lnlen; /* set value to end of line */ @@ -125,12 +126,12 @@ process_line(struct hash_table *hash, uint8_t *ln, int lnlen) *colon = 0; /* terminate key */ value = colon + 1; - if (hash_add(hash, (char *)key, (char *)value) == false) { + res = hash_add(hash, (char *)key, (char *)value); + if (res != NSERROR_OK) { NSLOG(netsurf, INFO, "Unable to add %s:%s to hash table", ln, value); - return NSERROR_INVALID; } - return NSERROR_OK; + return res; } @@ -300,18 +301,18 @@ void hash_destroy(struct hash_table *ht) /* exported interface documented in utils/hashtable.h */ -bool hash_add(struct hash_table *ht, const char *key, const char *value) +nserror hash_add(struct hash_table *ht, const char *key, const char *value) { unsigned int h, c, v; struct hash_entry *e; if (ht == NULL || key == NULL || value == NULL) - return false; + return NSERROR_BAD_PARAMETER; e = malloc(sizeof(struct hash_entry)); if (e == NULL) { NSLOG(netsurf, INFO, "Not enough memory for hash entry."); - return false; + return NSERROR_NOMEM; } h = hash_string_fnv(key, &(e->key_length)); @@ -323,7 +324,7 @@ bool hash_add(struct hash_table *ht, const char *key, const char *value) NSLOG(netsurf, INFO, "Not enough memory for string duplication."); free(e); - return false; + return NSERROR_NOMEM; } memcpy(e->pairing, key, e->key_length + 1); memcpy(e->pairing + e->key_length + 1, value, v + 1); @@ -331,7 +332,7 @@ bool hash_add(struct hash_table *ht, const char *key, const char *value) e->next = ht->chain[c]; ht->chain[c] = e; - return true; + return NSERROR_OK; } diff --git a/utils/hashtable.h b/utils/hashtable.h index b1c0d5c41..4fccd7942 100644 --- a/utils/hashtable.h +++ b/utils/hashtable.h @@ -64,10 +64,10 @@ void hash_destroy(struct hash_table *ht); * \param ht The hash table context to add the key/value pair to. * \param key The key to associate the value with. A copy is made. * \param value The value to associate the key with. A copy is made. - * \return true if the add succeeded, false otherwise. (Failure most likely + * \return NSERROR_OK if the add succeeded else error code. (Failure most likely * indicates insufficent memory to make copies of the key and value. */ -bool hash_add(struct hash_table *ht, const char *key, const char *value); +nserror hash_add(struct hash_table *ht, const char *key, const char *value); /** * Looks up a the value associated with with a key from a specific hash table. |