From 37d97288306faefa193c1f6347665c8f58da6534 Mon Sep 17 00:00:00 2001 From: Jman012 Date: Mon, 18 Nov 2013 20:05:35 -0800 Subject: [PATCH] Added functions tox_count_chatlist and tox_copy_chatlist. These functions are akin to the tox_count_friendlist and tox_copy_friendlist, made available on the public API. --- toxcore/Messenger.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ toxcore/Messenger.h | 12 ++++++++++++ toxcore/tox.c | 19 +++++++++++++++++++ toxcore/tox.h | 12 ++++++++++++ 4 files changed, 87 insertions(+) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 19bd7edf..43572eff 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2175,3 +2175,47 @@ int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length) return 0; } +/* Return the number of chat in the instance m. + * You should use this to determine how much memory to allocate + * for copy_grouplist. */ +uint32_t count_chatlist(Messenger *m) +{ + uint32_t ret = 0; + uint32_t i; + + for (i = 0; i < m->numchats; i++) { + ret++; + } + + return ret; +} + +/* Copy a list of valid chat IDs into the array out_list. + * If out_list is NULL, returns 0. + * Otherwise, returns the number of elements copied. + * If the array was too small, the contents + * of out_list will be truncated to list_size. */ +uint32_t copy_chatlist(Messenger *m, int *out_list, uint32_t list_size) +{ + if (!out_list) + return 0; + + if (m->numchats == 0) { + return 0; + } + + uint32_t i; + uint32_t ret = 0; + + for (i = 0; i < m->numchats; i++) { + if (i >= list_size) { + break; /* Abandon ship */ + } + + out_list[i] = i; + ret++; + } + + return ret; +} + diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 10ac0eae..1e463e36 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -633,5 +633,17 @@ uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size); */ int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length); +/* Return the number of chats in the instance m. + * You should use this to determine how much memory to allocate + * for copy_grouplist. */ +uint32_t count_chatlist(Messenger *m); + +/* Copy a list of valid chat IDs into the array out_list. + * If out_list is NULL, returns 0. + * Otherwise, returns the number of elements copied. + * If the array was too small, the contents + * of out_list will be truncated to list_size. */ +uint32_t copy_chatlist(Messenger *m, int *out_list, uint32_t list_size); + #endif diff --git a/toxcore/tox.c b/toxcore/tox.c index 6a0c6a62..26ab3429 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -477,6 +477,25 @@ int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t return group_message_send(m, groupnumber, message, length); } +/* Return the number of chats in the instance m. + * You should use this to determine how much memory to allocate + * for copy_friendlist. */ +uint32_t tox_count_chatlist(Tox *tox) +{ + Messenger *m = tox; + return count_chatlist(m); +} + +/* Copy a list of valid chat IDs into the array out_list. + * If out_list is NULL, returns 0. + * Otherwise, returns the number of elements copied. + * If the array was too small, the contents + * of out_list will be truncated to list_size. */ +uint32_t tox_copy_chatlist(Tox *tox, int *out_list, uint32_t list_size) +{ + Messenger *m = tox; + return copy_chatlist(m, out_list, list_size); +} /****************FILE SENDING FUNCTIONS*****************/ diff --git a/toxcore/tox.h b/toxcore/tox.h index 5d3916b4..497fa3bc 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -404,6 +404,18 @@ int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_ * return -1 on failure */ int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length); + +/* Return the number of chats in the instance m. + * You should use this to determine how much memory to allocate + * for copy_friendlist. */ +uint32_t tox_count_chatlist(Tox *tox); + +/* Copy a list of valid chat IDs into the array out_list. + * If out_list is NULL, returns 0. + * Otherwise, returns the number of elements copied. + * If the array was too small, the contents + * of out_list will be truncated to list_size. */ +uint32_t tox_copy_chatlist(Tox *tox, int *out_list, uint32_t list_size); /****************FILE SENDING FUNCTIONS*****************/