diff --git a/dist/rax/rax.c b/dist/rax/rax.c index d5ec8fae3..59bbbc1bc 100644 --- a/dist/rax/rax.c +++ b/dist/rax/rax.c @@ -30,16 +30,13 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "rax.h" - -#undef NDEBUG +#include +#include #include - +#include #include #include -#include -#include -#include +#include "rax.h" #ifndef RAX_MALLOC_INCLUDE #define RAX_MALLOC_INCLUDE "rax_malloc.h" @@ -47,11 +44,6 @@ #include RAX_MALLOC_INCLUDE -/* This is a special pointer that is guaranteed to never have the same value - * of a radix tree node. It's used in order to report "not found" error without - * requiring the function to have multiple return values. */ -void *raxNotFound = (void*)"rax-not-found-pointer"; - /* -------------------------------- Debugging ------------------------------ */ void raxDebugShowNode(const char *msg, raxNode *n); @@ -915,18 +907,19 @@ int raxTryInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old) return raxGenericInsert(rax,s,len,data,old,0); } -/* Find a key in the rax, returns raxNotFound special void pointer value - * if the item was not found, otherwise the value associated with the - * item is returned. */ -void *raxFind(rax *rax, unsigned char *s, size_t len) { +/* Find a key in the rax: return 1 if the item is found, 0 otherwise. + * If there is an item and 'value' is passed in a non-NULL pointer, + * the value associated with the item is set at that address. */ +int raxFind(rax *rax, unsigned char *s, size_t len, void **value) { raxNode *h; debugf("### Lookup: %.*s\n", (int)len, s); int splitpos = 0; size_t i = raxLowWalk(rax,s,len,&h,NULL,&splitpos,NULL); if (i != len || (h->iscompr && splitpos != 0) || !h->iskey) - return raxNotFound; - return raxGetData(h); + return 0; + if (value != NULL) *value = raxGetData(h); + return 1; } /* Return the memory address where the 'parent' node stores the specified diff --git a/dist/rax/rax.h b/dist/rax/rax.h index e9817d6f8..594bb7bd0 100644 --- a/dist/rax/rax.h +++ b/dist/rax/rax.h @@ -186,15 +186,12 @@ typedef struct raxIterator { raxNodeCallback node_cb; /* Optional node callback. Normally set to NULL. */ } raxIterator; -/* A special pointer returned for not found items. */ -extern void *raxNotFound; - /* Exported API. */ rax *raxNew(void); int raxInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old); int raxTryInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old); int raxRemove(rax *rax, unsigned char *s, size_t len, void **old); -void *raxFind(rax *rax, unsigned char *s, size_t len); +int raxFind(rax *rax, unsigned char *s, size_t len, void **value); void raxFree(rax *rax); void raxFreeWithCallback(rax *rax, void (*free_callback)(void*)); void raxStart(raxIterator *it, rax *rt); diff --git a/src/lib/cache_rax_album.c b/src/lib/cache_rax_album.c index ff0909f91..5ffa591b7 100644 --- a/src/lib/cache_rax_album.c +++ b/src/lib/cache_rax_album.c @@ -371,8 +371,8 @@ struct mpd_song *album_cache_get_album(struct t_cache *album_cache, sds key) { return NULL; } //try to get album - void *data = raxFind(album_cache->cache, (unsigned char*)key, sdslen(key)); - if (data == raxNotFound) { + void *data; + if (raxFind(album_cache->cache, (unsigned char*)key, sdslen(key), &data) == 0) { MYMPD_LOG_ERROR(NULL, "Album for key \"%s\" not found in cache", key); return NULL; } diff --git a/src/lib/webradio.c b/src/lib/webradio.c index 0c6125e9b..446e692cd 100644 --- a/src/lib/webradio.c +++ b/src/lib/webradio.c @@ -30,23 +30,20 @@ struct t_webradio_data *webradio_by_uri(struct t_webradios *webradio_favorites, struct t_webradios *webradiodb, const char *uri) { - void *data = raxNotFound; - if (webradio_favorites != NULL) { - if (webradio_favorites->idx_uris != NULL) { - data = raxFind(webradio_favorites->idx_uris, (unsigned char *)uri, strlen(uri)); - } + void *data; + if (webradio_favorites != NULL && + webradio_favorites->idx_uris != NULL && + raxFind(webradio_favorites->idx_uris, (unsigned char *)uri, strlen(uri), &data) == 1) + { + return (struct t_webradio_data *)data; } - if (webradiodb != NULL) { - if (data == raxNotFound) { - if (webradiodb->idx_uris != NULL) { - data = raxFind(webradiodb->idx_uris, (unsigned char *)uri, strlen(uri)); - } - } + if (webradiodb != NULL && + webradiodb->idx_uris != NULL && + raxFind(webradiodb->idx_uris, (unsigned char *)uri, strlen(uri), &data) == 1) + { + return (struct t_webradio_data *)data; } - if (data == raxNotFound) { - return NULL; - } - return (struct t_webradio_data *)data; + return NULL; } /** diff --git a/src/mpd_client/random_select.c b/src/mpd_client/random_select.c index cbb32eab8..e9488e1bf 100644 --- a/src/mpd_client/random_select.c +++ b/src/mpd_client/random_select.c @@ -410,12 +410,13 @@ static bool check_not_hated(rax *stickers_like, const char *uri, bool ignore_hat { return true; } - void *sticker_value_hated = raxFind(stickers_like, (unsigned char *)uri, strlen(uri)); - return sticker_value_hated == raxNotFound - ? true - : ((sds)sticker_value_hated)[0] == '0' - ? false - : true; + void *sticker_value_hated; + if (raxFind(stickers_like, (unsigned char *)uri, strlen(uri), &sticker_value_hated) == 0) { + return true; + } + return ((sds)sticker_value_hated)[0] == '0' + ? false + : true; } /** @@ -429,8 +430,8 @@ static bool check_last_played(rax *stickers_last_played, const char *uri, time_t if (stickers_last_played == NULL) { return true; } - void *sticker_value_last_played = raxFind(stickers_last_played, (unsigned char *)uri, strlen(uri)); - if (sticker_value_last_played == raxNotFound) { + void *sticker_value_last_played; + if (raxFind(stickers_last_played, (unsigned char *)uri, strlen(uri), &sticker_value_last_played) == 0) { return true; } int64_t sticker_last_played; diff --git a/src/mympd_api/webradio.c b/src/mympd_api/webradio.c index 26b347d9a..7ed947407 100644 --- a/src/mympd_api/webradio.c +++ b/src/mympd_api/webradio.c @@ -235,8 +235,8 @@ sds mympd_api_webradio_print(struct t_webradio_data *webradio, sds buffer, const sds mympd_api_webradio_radio_get_by_name(struct t_webradios *webradios, sds buffer, unsigned request_id, enum mympd_cmd_ids cmd_id, sds name) { - void *data = raxFind(webradios->db, (unsigned char *)name, strlen(name)); - if (data == raxNotFound) { + void *data; + if (raxFind(webradios->db, (unsigned char *)name, strlen(name), &data) == 0) { return jsonrpc_respond_message(buffer, cmd_id, request_id, JSONRPC_FACILITY_DATABASE, JSONRPC_SEVERITY_ERROR, "Webradio entry not found"); } @@ -259,8 +259,8 @@ sds mympd_api_webradio_radio_get_by_name(struct t_webradios *webradios, sds buff sds mympd_api_webradio_radio_get_by_uri(struct t_webradios *webradios, sds buffer, unsigned request_id, enum mympd_cmd_ids cmd_id, sds uri) { - void *data = raxFind(webradios->idx_uris, (unsigned char *)uri, strlen(uri)); - if (data == raxNotFound) { + void *data; + if (raxFind(webradios->idx_uris, (unsigned char *)uri, strlen(uri), &data) == 0) { return jsonrpc_respond_message(buffer, cmd_id, request_id, JSONRPC_FACILITY_DATABASE, JSONRPC_SEVERITY_ERROR, "Webradio entry not found"); } diff --git a/src/mympd_api/webradio_favorites.c b/src/mympd_api/webradio_favorites.c index 6fa1f2a96..2c324b8a1 100644 --- a/src/mympd_api/webradio_favorites.c +++ b/src/mympd_api/webradio_favorites.c @@ -34,8 +34,10 @@ bool mympd_api_webradio_favorite_save(struct t_webradios *webradio_favorites, st list_init(&old_names); if (sdslen(old_name) > 0) { list_push(&old_names, old_name, 0, NULL, NULL); - struct t_webradio_data *old_radio = raxFind(webradio_favorites->idx_uris, (unsigned char *)old_name, strlen(old_name)); - if (old_radio != raxNotFound) { + void *data; + if (raxFind(webradio_favorites->idx_uris, (unsigned char *)old_name, strlen(old_name), &data) == 1) { + // preserve added timestamp + struct t_webradio_data *old_radio = (struct t_webradio_data *)data; webradio->added = old_radio->added; } }