From 36adc48b781105e55790d0049fec1b190104e480 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Fri, 21 Feb 2014 20:30:38 -0500 Subject: [PATCH] Added size functions for names and status messages --- toxcore/Messenger.c | 21 +++++++++++++++++++++ toxcore/Messenger.h | 11 +++++++++-- toxcore/tox.c | 25 +++++++++++++++++++++++-- toxcore/tox.h | 11 +++++++++-- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 9f75f89f..e3dcf2a1 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -533,6 +533,19 @@ int getname(Messenger *m, int32_t friendnumber, uint8_t *name) return m->friendlist[friendnumber].name_length; } +int m_get_name_size(Messenger *m, int32_t friendnumber) +{ + if (friend_not_valid(m, friendnumber)) + return -1; + + return m->friendlist[friendnumber].name_length; +} + +int m_get_self_name_size(Messenger *m) +{ + return m->name_length; +} + int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length) { if (length > MAX_STATUSMESSAGE_LENGTH) @@ -588,6 +601,14 @@ int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint3 return MIN(maxlen, m->friendlist[friendnumber].statusmessage_length); } +/* return the size of friendnumber's user status. + * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH. + */ +int m_get_self_statusmessage_size(Messenger *m) +{ + return m->statusmessage_length; +} + int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen) { memset(buf, 0, maxlen); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 8474bbe7..88e5e19d 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -370,6 +370,12 @@ uint16_t getself_name(Messenger *m, uint8_t *name); */ int getname(Messenger *m, int32_t friendnumber, uint8_t *name); +/* return the length of name, including null on success. + * return -1 on failure. + */ +int m_get_name_size(Messenger *m, int32_t friendnumber); +int m_get_self_name_size(Messenger *m); + /* returns valid ip port of connected friend on success * returns zeroed out IP_Port on failure */ @@ -384,10 +390,11 @@ IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber); int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); int m_set_userstatus(Messenger *m, USERSTATUS status); -/* return the length of friendnumber's status message, including null. - * Pass it into malloc. +/* return the length of friendnumber's status message, including null on success. + * return -1 on failure. */ int m_get_statusmessage_size(Messenger *m, int32_t friendnumber); +int m_get_self_statusmessage_size(Messenger *m); /* Copy friendnumber's status message into buf, truncating if size is over maxlen. * Get the size you need to allocate from m_get_statusmessage_size. diff --git a/toxcore/tox.c b/toxcore/tox.c index ae824697..f91879ea 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -210,6 +210,21 @@ int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name) return getname(m, friendnumber, name); } +/* returns the length of name on success. + * returns -1 on failure. + */ +int tox_get_name_size(Tox *tox, int32_t friendnumber) +{ + Messenger *m = tox; + return m_get_name_size(m, friendnumber); +} + +int tox_get_self_name_size(Tox *tox) +{ + Messenger *m = tox; + return m_get_self_name_size(m); +} + /* Set our user status; * you are responsible for freeing status after. * @@ -227,8 +242,8 @@ int tox_set_user_status(Tox *tox, TOX_USERSTATUS status) return m_set_userstatus(m, status); } -/* return the length of friendnumber's status message, including null. - * Pass it into malloc. +/* returns the length of status message on success. + * returns -1 on failure. */ int tox_get_status_message_size(Tox *tox, int32_t friendnumber) { @@ -236,6 +251,12 @@ int tox_get_status_message_size(Tox *tox, int32_t friendnumber) return m_get_statusmessage_size(m, friendnumber); } +int tox_get_self_status_message_size(Tox *tox) +{ + Messenger *m = tox; + return m_get_self_statusmessage_size(m, friendnumber); +} + /* Copy friendnumber's status message into buf, truncating if size is over maxlen. * Get the size you need to allocate from m_get_statusmessage_size. * The self variant will copy our own status message. diff --git a/toxcore/tox.h b/toxcore/tox.h index 348ef6e5..17bd4e1a 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -223,6 +223,12 @@ uint16_t tox_get_self_name(Tox *tox, uint8_t *name); */ int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name); +/* returns the length of name on success. + * returns -1 on failure. + */ +int tox_get_name_size(Tox *tox, int32_t friendnumber); +int tox_get_self_name_size(Tox *tox); + /* Set our user status. * You are responsible for freeing status after. * @@ -234,10 +240,11 @@ int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name); int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length); int tox_set_user_status(Tox *tox, TOX_USERSTATUS userstatus); -/* return the length of friendnumber's status message. - * Pass it into malloc +/* returns the length of status message on success. + * returns -1 on failure. */ int tox_get_status_message_size(Tox *tox, int32_t friendnumber); +int tox_get_self_status_message_size(Tox *tox); /* Copy friendnumber's status message into buf, truncating if size is over maxlen. * Get the size you need to allocate from m_get_statusmessage_size.