add functions for allocating and adding a string buffer field
authorFelix Fietkau <nbd@openwrt.org>
Fri, 4 Feb 2011 20:57:59 +0000 (21:57 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 4 Feb 2011 20:57:59 +0000 (21:57 +0100)
blobmsg.c
blobmsg.h

index e5de129..ed7a880 100644 (file)
--- a/blobmsg.c
+++ b/blobmsg.c
@@ -310,6 +310,37 @@ blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
        return (void *)offset;
 }
 
        return (void *)offset;
 }
 
+void *
+blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen)
+{
+       struct blob_attr *attr;
+       void *data_dest;
+
+       attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
+       if (!attr)
+               return NULL;
+
+       data_dest = blobmsg_data(attr);
+       blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_raw_len(attr));
+       blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
+
+       return data_dest;
+}
+
+void
+blobmsg_add_string_buffer(struct blob_buf *buf)
+{
+       struct blob_attr *attr;
+       int len, attrlen;
+
+       attr = blob_next(buf->head);
+       len = strlen(blobmsg_data(attr)) + 1;
+
+       attrlen = blob_raw_len(attr) + len;
+       blob_set_raw_len(attr, attrlen);
+       blob_set_raw_len(buf->head, blob_raw_len(buf->head) + attrlen);
+}
+
 int
 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
                   const void *data, int len)
 int
 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
                   const void *data, int len)
index e978235..dce5b85 100644 (file)
--- a/blobmsg.h
+++ b/blobmsg.h
@@ -139,6 +139,9 @@ static inline int blobmsg_buf_init(struct blob_buf *buf)
        return blob_buf_init(buf, BLOBMSG_TYPE_TABLE);
 }
 
        return blob_buf_init(buf, BLOBMSG_TYPE_TABLE);
 }
 
+void *blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen);
+void blobmsg_add_string_buffer(struct blob_buf *buf);
+
 char *blobmsg_format_json(struct blob_attr *attr, bool list);
 
 #define blobmsg_for_each_attr(pos, attr, rem) \
 char *blobmsg_format_json(struct blob_attr *attr, bool list);
 
 #define blobmsg_for_each_attr(pos, attr, rem) \