Fix xmpp_stanza_get_text() to work on text stanzas, and add xmpp_stanza_get_text_ptr() to optimize for data translation.
This commit is contained in:
29
src/stanza.c
29
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:
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user