From fea924350b6ba50dc0cb0e5e133cbbd914137e61 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 6 Aug 2019 15:44:18 +0200 Subject: Added a proper subscriptions listing for the direct conversations closer --- common/CMakeLists.txt | 1 + common/subscriptions.c | 39 +++++++++++++++++++++++++++++++++++++++ common/subscriptions.h | 28 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 common/subscriptions.c create mode 100644 common/subscriptions.h (limited to 'common') diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index aa5402a..876571b 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -2,3 +2,4 @@ file(GLOB_RECURSE SOURCES *.c) include_directories("${CMAKE_CURRENT_BINARY_DIR}") ADD_LIBRARY(common STATIC ${SOURCES}) +target_link_libraries(common config curl) diff --git a/common/subscriptions.c b/common/subscriptions.c new file mode 100644 index 0000000..d6d15aa --- /dev/null +++ b/common/subscriptions.c @@ -0,0 +1,39 @@ +#include "subscriptions.h" + +void common_subscription_add(struct subscription** subscriptions, const char* id, const char* name, enum subscription_type type) +{ + struct subscription * subscription = common_subscription_new(id, name, type); + HASH_ADD_KEYPTR(hh, *subscriptions, subscription->id, strlen(id), subscription); +} + +struct subscription* common_subscription_new(const char* id, const char* name, enum subscription_type type) +{ + struct subscription* subscription = malloc(sizeof(struct subscription)); + subscription->id = malloc(strlen(id) + 1); + strcpy(subscription->id, id); + subscription->name = malloc(strlen(name) + 1); + strcpy(subscription->name, name); + subscription->type = type; + return subscription; +} + +void common_subscriptions_free(struct subscription* subscriptions) +{ + struct subscription *sub, *tmp; + + HASH_ITER(hh, subscriptions, sub, tmp) { + HASH_DEL(subscriptions, sub); + free(sub->id); + free(sub->name); + free(sub); + } +} + +void common_subscriptions_const_walk(const struct subscription* subscriptions, void (*func)(const struct subscription*)) +{ + const struct subscription *sub, *tmp; + + HASH_ITER(hh, subscriptions, sub, tmp) { + func(sub); + } +} diff --git a/common/subscriptions.h b/common/subscriptions.h new file mode 100644 index 0000000..6c10a24 --- /dev/null +++ b/common/subscriptions.h @@ -0,0 +1,28 @@ +#ifndef COMMON_SUBSCRIPTIONS_H_ +#define COMMON_SUBSCRIPTIONS_H_ + +#include + +static const char *subscription_type_str[] = { + "channel", "direct", "private", +}; + +enum subscription_type { + SUBSCRIPTION_CHANNEL, + SUBSCRIPTION_DIRECT, + SUBSCRIPTION_PRIVATE, +}; + +struct subscription { + char* id; + char* name; + enum subscription_type type; + UT_hash_handle hh; +}; + +void common_subscription_add(struct subscription** subscriptions, const char* id, const char* name, enum subscription_type type); +struct subscription* common_subscription_new(const char* id, const char* name, enum subscription_type type); +void common_subscriptions_free(struct subscription* subscriptions); +void common_subscriptions_const_walk(const struct subscription* subscriptions, void (*func)(const struct subscription*)); + +#endif -- cgit v1.2.3