From 1d2e905763d2c403e54850d61044065b238e16bc Mon Sep 17 00:00:00 2001 From: Thomas White Date: Tue, 26 Apr 2016 22:34:25 +0200 Subject: Copy slide --- src/sc_parse.c | 98 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 27 deletions(-) (limited to 'src/sc_parse.c') diff --git a/src/sc_parse.c b/src/sc_parse.c index 8265a13..689a31e 100644 --- a/src/sc_parse.c +++ b/src/sc_parse.c @@ -241,44 +241,88 @@ void sc_block_free(SCBlock *bl) } -void save_sc_block(FILE *fh, const SCBlock *bl) +char *serialise_sc_block(const SCBlock *bl) { - while ( bl != NULL ) { + char *a; + SCBlock *ch; + size_t len = 3; - if ( bl->name == NULL ) { - fprintf(fh, "%s", bl->contents); - } else { + if ( bl == NULL ) return strdup(""); - fprintf(fh, "\\%s", bl->name); - if ( bl->options != NULL ) { - fprintf(fh, "[%s]", bl->options); - } - if ( (bl->contents != NULL) || (bl->child != NULL) ) { - fprintf(fh, "{"); - } - if ( bl->contents != NULL ) { - fprintf(fh, "%s", bl->contents); - } + if ( bl->name != NULL ) len += 1+strlen(bl->name); + if ( bl->options != NULL ) len += 2+strlen(bl->options); + if ( bl->contents != NULL ) len += 2+strlen(bl->contents); + a = malloc(len); + if ( a == NULL ) return NULL; + a[0] = '\0'; - /* Special case to prevent "\somethingSome text" */ - if ( (bl->name != NULL) && (bl->options == NULL) - && (bl->contents == NULL) && (bl->next != NULL) - && (bl->next->name == NULL) && (bl->child == NULL) ) - { - fprintf(fh, "{}"); - } + if ( bl->name == NULL ) { + strcat(a, bl->contents); + } else { + strcat(a, "\\"); + strcat(a, bl->name); + if ( bl->options != NULL ) { + strcat(a, "["); + strcat(a, bl->options); + strcat(a, "]"); + } + if ( (bl->contents != NULL) || (bl->child != NULL) ) { + strcat(a, "{"); + } + if ( bl->contents != NULL ) { + strcat(a, bl->contents); } - if ( bl->child != NULL ) { - save_sc_block(fh, bl->child); + /* Special case to prevent "\somethingSome text" */ + if ( (bl->name != NULL) && (bl->options == NULL) + && (bl->contents == NULL) && (bl->next != NULL) + && (bl->next->name == NULL) && (bl->child == NULL) ) + { + strcat(a, "{}"); } - if ( (bl->name != NULL) && - ((bl->contents != NULL) || (bl->child != NULL)) ) { - fprintf(fh, "}"); + } + + /* Add ALL child blocks of this one */ + ch = bl->child; + while ( ch != NULL ) { + + char *c = serialise_sc_block(ch); + if ( c == NULL ) { + free(a); + return NULL; } + len += strlen(c); + a = realloc(a, len); + if ( a == NULL ) return NULL; + strcat(a, c); + free(c); + + ch = ch->next; + + } + + if ( (bl->name != NULL) && + ((bl->contents != NULL) || (bl->child != NULL)) ) { + strcat(a, "}"); + } + + return a; +} + + +void save_sc_block(FILE *fh, const SCBlock *bl) +{ + while ( bl != NULL ) { + char *a = serialise_sc_block(bl); + if ( a == NULL ) { + fprintf(stderr, "Failed to serialise block\n"); + return; + } + fprintf(fh, a); + free(a); bl = bl->next; } } -- cgit v1.2.3