[sr-dev] git:master: libsrjson: new helper functions to deal with str and unlink node

Daniel-Constantin Mierla miconda at gmail.com
Fri Feb 17 09:37:45 CET 2012


Module: sip-router
Branch: master
Commit: f7b72bf06870c3fd3ad602051e8695c11a2fcdc6
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=f7b72bf06870c3fd3ad602051e8695c11a2fcdc6

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Fri Feb 17 09:20:44 2012 +0100

libsrjson: new helper functions to deal with str and unlink node

---

 lib/srutils/srjson.c |   23 +++++++++++++++++++++++
 lib/srutils/srjson.h |    3 +++
 2 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/lib/srutils/srjson.c b/lib/srutils/srjson.c
index 55b43b0..4a7e261 100644
--- a/lib/srutils/srjson.c
+++ b/lib/srutils/srjson.c
@@ -822,6 +822,15 @@ void srjson_AddItemToObject(srjson_doc_t *doc, srjson_t *object, const char *str
 	srjson_AddItemToArray(doc, object, item);
 }
 
+void srjson_AddStrItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, int len, srjson_t *item) {
+	if (!item)
+		return;
+	if (item->string)
+		doc->free_fn(item->string);
+	item->string = srjson_strndupz(doc, string, len);
+	srjson_AddItemToArray(doc, object, item);
+}
+
 void srjson_AddItemReferenceToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item) {
 	srjson_AddItemToArray(doc, array, create_reference(doc, item));
 }
@@ -830,6 +839,20 @@ void srjson_AddItemReferenceToObject(srjson_doc_t *doc, srjson_t *object, const
 	srjson_AddItemToObject(doc, object, string, create_reference(doc, item));
 }
 
+srjson_t *srjson_UnlinkItemFromObj(srjson_doc_t *doc, srjson_t *obj, srjson_t *c)
+{
+	if (!c)
+		return 0;
+	if (c->prev)
+		c->prev->next = c->next;
+	if (c->next)
+		c->next->prev = c->prev;
+	if (c == obj->child)
+		obj->child = c->next;
+	c->prev = c->next = 0;
+	return c;
+}
+
 srjson_t *srjson_DetachItemFromArray(srjson_doc_t *doc, srjson_t *array, int which)
 {
 	srjson_t *c = array->child;
diff --git a/lib/srutils/srjson.h b/lib/srutils/srjson.h
index 3740e2a..4d8306f 100644
--- a/lib/srutils/srjson.h
+++ b/lib/srutils/srjson.h
@@ -148,6 +148,7 @@ extern srjson_t *srjson_CreateStringArray(srjson_doc_t *doc, const char **string
 /* Append item to the specified array/object. */
 extern void srjson_AddItemToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item);
 extern void srjson_AddItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *item);
+extern void srjson_AddStrItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, int len, srjson_t *item);
 
 /*
  * Append reference to item to the specified array/object. Use this
@@ -158,6 +159,7 @@ extern void srjson_AddItemReferenceToArray(srjson_doc_t *doc, srjson_t *array, s
 extern void srjson_AddItemReferenceToObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *item);
 
 /* Remove/Detatch items from Arrays/Objects. */
+extern srjson_t *srjson_UnlinkItemFromObj(srjson_doc_t *doc, srjson_t *obj, srjson_t *item);
 extern srjson_t *srjson_DetachItemFromArray(srjson_doc_t *doc, srjson_t *array, int which);
 extern void srjson_DeleteItemFromArray(srjson_doc_t *doc, srjson_t *array, int which);
 extern srjson_t *srjson_DetachItemFromObject(srjson_doc_t *doc, srjson_t *object, const char *string);
@@ -173,6 +175,7 @@ extern void srjson_ReplaceItemInObject(srjson_doc_t *doc, srjson_t *object, cons
 #define srjson_AddNumberToObject(doc, object,name,n)	srjson_AddItemToObject(doc, object, name, srjson_CreateNumber(doc,n))
 #define srjson_AddStringToObject(doc, object,name,s)	srjson_AddItemToObject(doc, object, name, srjson_CreateString(doc,s))
 #define srjson_AddStrToObject(doc, object,name,s,l)		srjson_AddItemToObject(doc, object, name, srjson_CreateStr(doc,s,l))
+#define srjson_AddStrStrToObject(doc, object,name,ln,s,l)	srjson_AddStrItemToObject(doc, object, name, ln, srjson_CreateStr(doc,s,l))
 
 #ifdef __cplusplus
 }




More information about the sr-dev mailing list