From 1054c74c4a42a53ed8c0723652b167a05de158d7 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Tue, 26 Aug 2008 04:30:05 +0000 Subject: [PATCH] Fix xmpp_stanza_get_text() to work on text stanzas, and add xmpp_stanza_get_text_ptr() to optimize for data translation. --- src/stanza.c | 29 +++++++++++++++++++++++++++++ strophe.h | 1 + 2 files changed, 30 insertions(+) diff --git a/src/stanza.c b/src/stanza.c index 2a36b6e..a93d7de 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -778,11 +778,20 @@ char *xmpp_stanza_get_text(xmpp_stanza_t * const stanza) xmpp_stanza_t *child; char *text; + if (stanza->type == XMPP_STANZA_TEXT) { + if (stanza->data) + return xmpp_strdup(stanza->ctx, stanza->data); + else + return NULL; + } + len = 0; for (child = stanza->children; child; child = child->next) if (child->type == XMPP_STANZA_TEXT) len += strlen(child->data); + if (len == 0) return NULL; + text = (char *)xmpp_alloc(stanza->ctx, len + 1); if (!text) return NULL; @@ -799,6 +808,26 @@ char *xmpp_stanza_get_text(xmpp_stanza_t * const stanza) return text; } +/** Get the text data pointer for a text stanza. + * This function copies returns the raw pointer to the text data in the + * stanza. This should only be used in very special cases where the + * caller needs to translate the datatype as this will save a double + * allocation. The caller should not hold onto this pointer, and is + * responsible for allocating a copy if it needs one. + * + * @param stanza a Strophe stanza object + * + * @return an string pointer to the data or NULL + * + * @ingroup Stanza + */ +char *xmpp_stanza_get_text_ptr(xmpp_stanza_t * const stanza) +{ + if (stanza->type == XMPP_STANZA_TEXT) + return stanza->data; + return NULL; +} + /** Set the 'id' attribute of a stanza. * * This is a convenience function for: diff --git a/strophe.h b/strophe.h index 930dcb6..abc1039 100644 --- a/strophe.h +++ b/strophe.h @@ -301,6 +301,7 @@ char * xmpp_stanza_get_ns(xmpp_stanza_t * const stanza); /* concatenate all child text nodes. this function * returns a string that must be freed by the caller */ char *xmpp_stanza_get_text(xmpp_stanza_t * const stanza); +char *xmpp_stanza_get_text_ptr(xmpp_stanza_t * const stanza); char *xmpp_stanza_get_name(xmpp_stanza_t * const stanza); int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child);