aboutsummaryrefslogtreecommitdiff
path: root/src/sc_parse.c
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2016-04-26 22:34:25 +0200
committerThomas White <taw@bitwiz.org.uk>2016-04-26 22:34:25 +0200
commit1d2e905763d2c403e54850d61044065b238e16bc (patch)
tree29ab44993df243530f8b4005d8250a812498aa16 /src/sc_parse.c
parente90d663864cb60067f90b964adc2686aba69e333 (diff)
Copy slide
Diffstat (limited to 'src/sc_parse.c')
-rw-r--r--src/sc_parse.c98
1 files changed, 71 insertions, 27 deletions
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;
}
}