From 07acadaf8505c10b11c317667bb00522ad64856c Mon Sep 17 00:00:00 2001 From: khamidou Date: Thu, 5 Mar 2009 09:57:54 +0100 Subject: [PATCH] refactoring string package --- dynstring.c | 37 +++++++++++++++++++++++-------------- dynstring.h | 13 ++++++------- macro.c | 1 + macro.h | 4 ++-- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/dynstring.c b/dynstring.c index 2c692a6..2b330f2 100644 --- a/dynstring.c +++ b/dynstring.c @@ -1,30 +1,36 @@ #include "dynstring.h" -void init_dynstring(void) +void init_dynstring(struct croma_block *b) { - TAILQ_INIT(&dstrings_head); + if (b == NULL) + return; + + TAILQ_INIT(&b->dstrings_head); } -struct dstring* alloc_and_insert_string(size_t len) +struct dstring* alloc_and_insert_string(struct ccroma_block *b, size_t len) { + if (b == NULL) + return NULL; + struct dstring *s = calloc(len, sizeof(char)); if (s == NULL) fail("Unable to alloc() memory"); - TAILQ_INSERT_TAIL(&dstrings_head, s, strings); + TAILQ_INSERT_TAIL(&b->dstrings_head, s, strings); return block; } -struct dstring* alloc_after(struct dstring *before) +struct dstring* alloc_after(struct croma_block *b, struct dstring *before) { - if (before == NULL) + if (b == NULL || before == NULL) return NULL; struct dstring *ptr; - TAILQ_FOREACH(ptr, &dstrings_head, strings) { + TAILQ_FOREACH(ptr, &b->dstrings_head, strings) { if (strncmp(ptr->p, before->p, 32) == 0) { struct dstring *s = calloc(len, sizeof(char)); @@ -40,12 +46,12 @@ struct dstring* alloc_after(struct dstring *before) return ptr; } -void free_dstring(struct dstring *s) +void free_dstring(struct croma_block *b, struct dstring *s) { - if (s == NULL) + if (b == NULL || s == NULL) return; - TAILQ_REMOVE(&dstrings_head, s, strings); + TAILQ_REMOVE(&b->dstrings_head, s, strings); if (s->p != NULL) free(s->p); @@ -55,10 +61,13 @@ void free_dstring(struct dstring *s) return; } -void free_all_dstrings(void) +void free_all_dstrings(struct croma_block *b) { + if (b == NULL) + return; + struct dstring *s; - TAILQ_FOREACH(s, &dstrings_head, strings) { + TAILQ_FOREACH(s, &b->dstrings_head, strings) { TAILQ_REMOVE(&dstrings_head, s, strings); free_dstring(s); } @@ -66,9 +75,9 @@ void free_all_dstrings(void) /* break_after breaks a string after *s and allocates it after "before". */ -struct dstring* break_after(struct dstring *before, char *s) +struct dstring* break_after(struct croma_block *b, struct dstring *before, char *s) { - if (before == NULL) + if (b == NULL || before == NULL) return NULL; int s_len = before->length - (s - p); /* the length of the string after s */ diff --git a/dynstring.h b/dynstring.h index b9811ce..39050dd 100644 --- a/dynstring.h +++ b/dynstring.h @@ -3,6 +3,7 @@ #include #include "queue.h" +#include "macro.h" struct dstring { char *p; @@ -10,13 +11,11 @@ struct dstring { TAILQ_ENTRY(dstring) strings; }; -TAILQ_HEAD(dstring_heads, dstring) dstrings_head; - -void init_dynstring(void); -struct dstring* alloc_and_insert_string(size_t len); -struct dstring* alloc_after(struct dstring *before); -void free_dstring(struct dstring *s); -void free_all_dstrings(void); +void init_dynstring(struct croma_block *b); +struct dstring* alloc_and_insert_string(struct croma_block *b, size_t len); +struct dstring* alloc_after(struct croma_block *b, struct dstring *before); +void free_dstring(struct croma_block *b, struct dstring *s); +void free_all_dstrings(struct croma_block *b); struct dstring* break_after(struct dstring *before, char *s); diff --git a/macro.c b/macro.c index aad891b..475c1df 100644 --- a/macro.c +++ b/macro.c @@ -13,6 +13,7 @@ struct croma_block* alloc_and_insert_block(void) TAILQ_INSERT_TAIL(&blocks_head, block, blocks); TAILQ_INIT(&block->args_head); + TAILQ_INIT(&block->strings_head); return block; } diff --git a/macro.h b/macro.h index 6d07919..820c310 100644 --- a/macro.h +++ b/macro.h @@ -22,10 +22,10 @@ struct croma_arg { struct croma_block { char *name; -// char **parameters; - char *contents; + //char *contents; int contents_length; TAILQ_HEAD(croma_args_head, croma_arg) args_head; + TAILQ_HEAD(dstring_heads, dstring) dstrings_head; TAILQ_ENTRY(croma_block) blocks; }; -- 2.43.0