[PATCH v2 11/16] trailer: add new_trailer_item() function
From: Christian Couder <hidden>
Date: 2016-06-15 22:59:42
Subsystem:
the rest · Maintainer:
Linus Torvalds
This is a small refactoring to prepare for the next steps. Signed-off-by: Christian Couder <redacted> --- trailer.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-)
diff --git a/trailer.c b/trailer.c
index 9026337..43a3735 100644
--- a/trailer.c
+++ b/trailer.c@@ -400,11 +400,27 @@ static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tr } } +static struct trailer_item *new_trailer_item(struct trailer_item *conf_item, + const char* tok, const char* val) +{ + struct trailer_item *new = xcalloc(sizeof(struct trailer_item), 1); + new->value = val; + + if (conf_item) { + new->conf = conf_item->conf; + new->token = xstrdup(conf_item->conf->key); + } else { + new->conf = xcalloc(sizeof(struct conf_info), 1); + new->token = tok; + } + + return new; +} + static struct trailer_item *create_trailer_item(const char *string) { struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; - struct trailer_item *new; parse_trailer(&tok, &val, string);
@@ -416,21 +432,12 @@ static struct trailer_item *create_trailer_item(const char *string) { if (!strncasecmp(tok.buf, item->conf->key, tok_alnum_len) || !strncasecmp(tok.buf, item->conf->name, tok_alnum_len)) { - new = xcalloc(sizeof(struct trailer_item), 1); - new->conf = item->conf; - new->token = xstrdup(item->conf->key); - new->value = strbuf_detach(&val, NULL); strbuf_release(&tok); - return new; + return new_trailer_item(item, NULL, strbuf_detach(&val, NULL)); } } - new = xcalloc(sizeof(struct trailer_item), 1); - new->conf = xcalloc(sizeof(struct conf_info), 1); - new->token = strbuf_detach(&tok, NULL); - new->value = strbuf_detach(&val, NULL); - - return new; + return new_trailer_item(NULL, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL));; } static void add_trailer_item(struct trailer_item **first,
--
1.8.5.2.201.gacc5987