From bd9671a8cc4988fd348db48b209fd2b16c1c28dd Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 8 Feb 2013 13:14:54 -0500 Subject: [PATCH] more convenience functions --- daemon/bencode.h | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/daemon/bencode.h b/daemon/bencode.h index 296ff41f8..ac3d5a2f5 100644 --- a/daemon/bencode.h +++ b/daemon/bencode.h @@ -113,6 +113,7 @@ static inline bencode_item_t *bencode_dictionary_add_string_dup(bencode_item_t * /* Ditto, but for a "str" object */ static inline bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, const char *key, const str *val); +static inline bencode_item_t *bencode_dictionary_add_str_dup(bencode_item_t *dict, const char *key, const str *val); /* Ditto, but adds a string created through an iovec array to the dictionary. See * bencode_string_iovec(). */ @@ -156,15 +157,16 @@ bencode_item_t *bencode_string_len(bencode_buffer_t *buf, const char *s, int len * to bencode_string_len(). */ static inline bencode_item_t *bencode_string(bencode_buffer_t *buf, const char *s); -/* Identical to the above two functions, but copies the string into the bencode_buffer_t object. - * Thus, the given string doesn't have to remain valid and accessible afterwards. */ -bencode_item_t *bencode_string_len_dup(bencode_buffer_t *buf, const char *s, int len); -static inline bencode_item_t *bencode_string_dup(bencode_buffer_t *buf, const char *s); - /* Creates a new byte-string object from a "str" object. The string does not have to be null- * terminated. */ static inline bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s); +/* Identical to the above three functions, but copies the string into the bencode_buffer_t object. + * Thus, the given string doesn't have to remain valid and accessible afterwards. */ +bencode_item_t *bencode_string_len_dup(bencode_buffer_t *buf, const char *s, int len); +static inline bencode_item_t *bencode_string_dup(bencode_buffer_t *buf, const char *s); +static inline bencode_item_t *bencode_str_dup(bencode_buffer_t *buf, const str *s); + /* Creates a new byte-string object from an iovec array. The created object has different internal * semantics (not a BENCODE_STRING, but a BENCODE_IOVEC) and must not be treated like other string * objects. The array pointer and contents must still be valid and accessible when the complete @@ -355,6 +357,10 @@ static inline bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s) { return bencode_string_len(buf, s->s, s->len); } +static inline bencode_item_t *bencode_str_dup(bencode_buffer_t *buf, const str *s) { + return bencode_string_len_dup(buf, s->s, s->len); +} + static inline bencode_item_t *bencode_dictionary_add(bencode_item_t *dict, const char *key, bencode_item_t *val) { if (!key) return NULL; @@ -379,6 +385,12 @@ static inline bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, c return bencode_dictionary_add(dict, key, bencode_str(dict->buffer, val)); } +static inline bencode_item_t *bencode_dictionary_add_str_dup(bencode_item_t *dict, const char *key, const str *val) { + if (!val) + return NULL; + return bencode_dictionary_add(dict, key, bencode_str_dup(dict->buffer, val)); +} + static inline bencode_item_t *bencode_dictionary_add_integer(bencode_item_t *dict, const char *key, long long int val) { return bencode_dictionary_add(dict, key, bencode_integer(dict->buffer, val)); }