summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Stal <stal@kirara.ca>2013-09-21 14:47:30 -0700
committerSebastian Stal <stal@kirara.ca>2013-09-21 14:47:30 -0700
commitcb68be00b0bb8802da94b877a24e68ee35b5658a (patch)
treebde4de8284321e6ac8f8c61c92b6d5eb399bb9c6
parenta6abf007cbb62f0147dd136bffaa9197736b280f (diff)
Change tox_get_friendlist API.
tox_get_friendlist() -> tox_copy_friendlist(). You now have to allocate your own memory to pass into tox_copy_friendlist. To help with this, tox_count_friendlist() has been added to get the length of the friend list.
-rw-r--r--toxcore/Messenger.c44
-rw-r--r--toxcore/Messenger.h12
-rw-r--r--toxcore/tox.c24
-rw-r--r--toxcore/tox.h18
4 files changed, 83 insertions, 15 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index e468a60a..cbbc80bf 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1640,6 +1640,50 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
1640 return Messenger_load_old(m, data, length); 1640 return Messenger_load_old(m, data, length);
1641} 1641}
1642 1642
1643/* Return the number of friends in the instance m.
1644 * You should use this to determine how much memory to allocate
1645 * for copy_friendlist. */
1646size_t count_friendlist(Messenger *m)
1647{
1648 size_t ret = 0;
1649 uint32_t i;
1650 for (i = 0; i < m->numfriends; i++) {
1651 if (m->friendlist[i].status > 0) {
1652 ret++;
1653 }
1654 }
1655 return ret;
1656}
1657
1658/* Copy a list of valid friend IDs into the array out_list.
1659 * If out_list is NULL, returns -1.
1660 * Otherwise, returns the number of elements copied.
1661 * If the array was too small, the contents
1662 * of out_list will be truncated to list_size. */
1663size_t copy_friendlist(Messenger *m, int *out_list, size_t list_size)
1664{
1665 if (!out_list)
1666 return -1;
1667
1668 if (m->numfriends == 0) {
1669 return 0;
1670 }
1671
1672 uint32_t i;
1673 size_t ret = 0;
1674 for (i = 0; i < m->numfriends; i++) {
1675 if (i >= list_size) {
1676 break; /* Abandon ship */
1677 }
1678 if (m->friendlist[i].status > 0) {
1679 out_list[i] = i;
1680 ret++;
1681 }
1682 }
1683
1684 return ret;
1685}
1686
1643/* Allocate and return a list of valid friend id's. List must be freed by the 1687/* Allocate and return a list of valid friend id's. List must be freed by the
1644 * caller. 1688 * caller.
1645 * 1689 *
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 78580dc0..81e661e1 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -456,6 +456,18 @@ void Messenger_save(Messenger *m, uint8_t *data);
456/* Load the messenger from data of size length. */ 456/* Load the messenger from data of size length. */
457int Messenger_load(Messenger *m, uint8_t *data, uint32_t length); 457int Messenger_load(Messenger *m, uint8_t *data, uint32_t length);
458 458
459/* Return the number of friends in the instance m.
460 * You should use this to determine how much memory to allocate
461 * for copy_friendlist. */
462size_t count_friendlist(Messenger *m);
463
464/* Copy a list of valid friend IDs into the array out_list.
465 * If out_list is NULL, returns -1.
466 * Otherwise, returns the number of elements copied.
467 * If the array was too small, the contents
468 * of out_list will be truncated to list_size. */
469size_t copy_friendlist(Messenger *m, int *out_list, size_t list_size);
470
459/* Allocate and return a list of valid friend id's. List must be freed by the 471/* Allocate and return a list of valid friend id's. List must be freed by the
460 * caller. 472 * caller.
461 * 473 *
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 4fba360b..dfafaf20 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -262,16 +262,24 @@ void tox_set_sends_receipts(void *tox, int friendnumber, int yesno)
262 m_set_sends_receipts(m, friendnumber, yesno); 262 m_set_sends_receipts(m, friendnumber, yesno);
263} 263}
264 264
265/* Allocate and return a list of valid friend id's. List must be freed by the 265/* Return the number of friends in the instance m.
266 * caller. 266 * You should use this to determine how much memory to allocate
267 * 267 * for copy_friendlist. */
268 * retun 0 if success. 268size_t tox_count_friendlist(void *tox)
269 * return -1 if failure. 269{
270 */ 270 Messenger *m = tox;
271int tox_get_friendlist(void *tox, int **out_list, uint32_t *out_list_length) 271 return count_friendlist(m);
272}
273
274/* Copy a list of valid friend IDs into the array out_list.
275 * If out_list is NULL, returns -1.
276 * Otherwise, returns the number of elements copied.
277 * If the array was too small, the contents
278 * of out_list will be truncated to list_size. */
279size_t tox_copy_friendlist(void *tox, int *out_list, size_t list_size)
272{ 280{
273 Messenger *m = tox; 281 Messenger *m = tox;
274 return get_friendlist(m, out_list, out_list_length); 282 return copy_friendlist(m, out_list, list_size);
275} 283}
276 284
277/* Set the function that will be executed when a friend request is received. 285/* Set the function that will be executed when a friend request is received.
diff --git a/toxcore/tox.h b/toxcore/tox.h
index b39008fe..ed471542 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -280,13 +280,17 @@ TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox);
280 */ 280 */
281void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); 281void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno);
282 282
283/* Allocate and return a list of valid friend id's. List must be freed by the 283/* Return the number of friends in the instance m.
284 * caller. 284 * You should use this to determine how much memory to allocate
285 * 285 * for copy_friendlist. */
286 * retun 0 if success. 286size_t tox_count_friendlist(void *tox);
287 * return -1 if failure. 287
288 */ 288/* Copy a list of valid friend IDs into the array out_list.
289int tox_get_friendlist(void *tox, int **out_list, uint32_t *out_list_length); 289 * If out_list is NULL, returns -1.
290 * Otherwise, returns the number of elements copied.
291 * If the array was too small, the contents
292 * of out_list will be truncated to list_size. */
293size_t tox_copy_friendlist(void *tox, int *out_list, size_t list_size);
290 294
291/* Set the function that will be executed when a friend request is received. 295/* Set the function that will be executed when a friend request is received.
292 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 296 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)