From 4b04db6b2d0593671377bdac097b5bb288b1d240 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Fri, 10 Jan 2020 00:48:13 +0200 Subject: [PATCH] stanza: add xmpp_stanza_add_child_ex() This function is introduced by NetXMS project. --- ChangeLog | 1 + src/stanza.c | 33 ++++++++++++++++++++++++++++----- strophe.h | 2 ++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index e3f02f1..95adbf3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,7 @@ - xmpp_conn_is_connecting() - xmpp_conn_is_connected() - xmpp_conn_is_disconnected() + - xmpp_stanza_add_child_ex() - xmpp_stanza_get_context() 0.9.3 diff --git a/src/stanza.c b/src/stanza.c index 6a025d3..4924e73 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -632,22 +632,29 @@ int xmpp_stanza_set_ns(xmpp_stanza_t * const stanza, } /** Add a child stanza to a stanza object. - * This function clones the child and appends it to the stanza object's - * children. + * If do_clone is TRUE, user keeps reference to the child stanza and must call + * xmpp_stanza_release() to release the reference. If do_clone is FALSE, user + * transfers ownership and must not neither call xmpp_stanza_release() for + * the child stanza nor use it. * * @param stanza a Strophe stanza object * @param child the child stanza object + * @param do_clone TRUE to increase ref count of child (default for + * xmpp_stanza_add_child()) * * @return XMPP_EOK (0) on success or a number less than 0 on failure * * @ingroup Stanza */ -int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child) +int xmpp_stanza_add_child_ex(xmpp_stanza_t *stanza, xmpp_stanza_t *child, + int do_clone) { xmpp_stanza_t *s; - /* get a reference to the child */ - xmpp_stanza_clone(child); + if (do_clone) { + /* get a reference to the child */ + xmpp_stanza_clone(child); + } child->parent = stanza; @@ -663,6 +670,22 @@ int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child) return XMPP_EOK; } +/** Add a child stanza to a stanza object. + * This function clones the child and appends it to the stanza object's + * children. + * + * @param stanza a Strophe stanza object + * @param child the child stanza object + * + * @return XMPP_EOK (0) on success or a number less than 0 on failure + * + * @ingroup Stanza + */ +int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child) +{ + return xmpp_stanza_add_child_ex(stanza, child, 1); +} + /** Set the text data for a text stanza. * This function copies the text given and sets the stanza object's text to * it. Attempting to use this function on a stanza that has a name will diff --git a/strophe.h b/strophe.h index 2aa8ed6..8426be9 100644 --- a/strophe.h +++ b/strophe.h @@ -345,6 +345,8 @@ xmpp_stanza_t *xmpp_stanza_get_child_by_name_and_ns(xmpp_stanza_t * const stanza const char * const ns); xmpp_stanza_t *xmpp_stanza_get_next(xmpp_stanza_t * const stanza); int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child); +int xmpp_stanza_add_child_ex(xmpp_stanza_t *stanza, xmpp_stanza_t *child, + int do_clone); const char *xmpp_stanza_get_attribute(xmpp_stanza_t * const stanza, const char * const name);