]> khamidou — GitHub mirrors - croma.git/commitdiff
refactoring string package
authorkhamidou <karim@lambda-expr.org>
Thu, 5 Mar 2009 08:57:54 +0000 (09:57 +0100)
committerkhamidou <karim@lambda-expr.org>
Thu, 5 Mar 2009 08:57:54 +0000 (09:57 +0100)
dynstring.c
dynstring.h
macro.c
macro.h

index 2c692a62a18c84682e22a61ef17aab970a959ce2..2b330f2feb1fbd02f3c55f4e00342b0902af8f29 100644 (file)
@@ -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 */
index b9811ce1c48a13b72003cecb6c4740ee02eb26f6..39050dd778d360878327c654803c1c48d6f7f55c 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <stdlib.h>
 #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 aad891b17409d560e0abd3b5a398b0f63650a8db..475c1df0e24b6674d3d65d46548e2803b53f0ec2 100644 (file)
--- 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 6d07919d8245eb913973bbbc9fcf0b71a274d892..820c310d4df5fcc800aa904a1beaac25dc7e77a4 100644 (file)
--- 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;
 };