From 65b623afe376e0331e106f00f487b755d555b7d1 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 3 Apr 2019 00:03:02 +0200 Subject: Stylesheet saving skeleton --- libstorycode/narrative.c | 22 +----------- libstorycode/storycode.c | 42 +++++++++++++++++++++++ libstorycode/storycode.h | 1 + libstorycode/stylesheet.c | 86 +++++++++++++++++++++++++++++++++++------------ libstorycode/stylesheet.h | 1 + 5 files changed, 110 insertions(+), 42 deletions(-) diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index 287e181..d8af742 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -115,26 +115,6 @@ Narrative *narrative_load(GFile *file) } -static int write_narrative(GOutputStream *fh, Narrative *n) -{ - int i; - - for ( i=0; in_items; i++ ) { - - gssize r; - GError *error = NULL; - char *a = "Hello"; - - r = g_output_stream_write(fh, a, strlen(a), NULL, &error); - if ( r == -1 ) { - fprintf(stderr, "Write failed: %s\n", error->message); - return 1; - } - } - return 0; -} - - int narrative_save(Narrative *n, GFile *file) { GFileOutputStream *fh; @@ -151,7 +131,7 @@ int narrative_save(Narrative *n, GFile *file) fprintf(stderr, _("Open failed: %s\n"), error->message); return 1; } - r = write_narrative(G_OUTPUT_STREAM(fh), n); + r = storycode_write_presentation(n, G_OUTPUT_STREAM(fh)); if ( r ) { fprintf(stderr, _("Couldn't save presentation\n")); } diff --git a/libstorycode/storycode.c b/libstorycode/storycode.c index b34785d..7fe46d7 100644 --- a/libstorycode/storycode.c +++ b/libstorycode/storycode.c @@ -27,10 +27,13 @@ #include #include +#include #include "narrative.h" #include "slide.h" #include "stylesheet.h" +#include "narrative_priv.h" +#include "slide_priv.h" #include "storycode_parse.h" #include "storycode_lex.h" @@ -48,3 +51,42 @@ Narrative *storycode_parse_presentation(const char *sc) return parse_ctx.n; } + + +static int write_string(GOutputStream *fh, char *str) +{ + gssize r; + GError *error = NULL; + r = g_output_stream_write(fh, str, strlen(str), NULL, &error); + if ( r == -1 ) { + fprintf(stderr, "Write failed: %s\n", error->message); + return 1; + } + return 0; +} + + +int storycode_write_presentation(Narrative *n, GOutputStream *fh) +{ + int i; + char *ss_text; + + /* Stylesheet */ + ss_text = stylesheet_serialise(n->stylesheet); + if ( ss_text == NULL ) return 1; + if ( write_string(fh, ss_text) ) return 1; + + for ( i=0; in_items; i++ ) { + + gssize r; + GError *error = NULL; + char *a = "Hello"; + + r = g_output_stream_write(fh, a, strlen(a), NULL, &error); + if ( r == -1 ) { + fprintf(stderr, "Write failed: %s\n", error->message); + return 1; + } + } + return 0; +} diff --git a/libstorycode/storycode.h b/libstorycode/storycode.h index f941493..c3d5d5b 100644 --- a/libstorycode/storycode.h +++ b/libstorycode/storycode.h @@ -30,6 +30,7 @@ #include "narrative.h" extern Narrative *storycode_parse_presentation(const char *sc); +extern int storycode_write_presentation(Narrative *n, GOutputStream *fh); #endif /* STORYCODE_H */ diff --git a/libstorycode/stylesheet.c b/libstorycode/stylesheet.c index 2310e37..6dc2daf 100644 --- a/libstorycode/stylesheet.c +++ b/libstorycode/stylesheet.c @@ -160,27 +160,6 @@ static struct style *lookup_style(struct style *sty, const char *path) } -static void show_style(struct style *sty, char *prefix) -{ - char *prefix2; - int i; - printf("%s%s:\n", prefix, sty->name); - prefix2 = malloc(strlen(prefix)+3); - strcpy(prefix2, prefix); - strcat(prefix2, " "); - for ( i=0; in_substyles; i++ ) { - show_style(&sty->substyles[i], prefix2); - } - free(prefix2); -} - - -static void show_ss(Stylesheet *ss) -{ - show_style(&ss->top, ""); -} - - static struct style *create_style(Stylesheet *ss, const char *path, const char *name) { struct style *sty; @@ -417,3 +396,68 @@ int stylesheet_get_paraspace(Stylesheet *s, enum style_element el, for ( i=0; i<4; i++ ) paraspace[i] = sty->paraspace[i]; return 0; } + + +static void add_text(char **text, size_t *len, size_t *lenmax, const char *prefix, + const char *tadd) +{ + size_t taddlen, prefixlen; + + if ( *text == NULL ) return; + taddlen = strlen(tadd); + prefixlen = strlen(prefix); + + if ( *len + taddlen + prefixlen + 1 > *lenmax ) { + *lenmax += taddlen + prefixlen + 32; + char *text_new = realloc(*text, *lenmax); + if ( text_new == NULL ) { + *text = NULL; + return; + } + *text = text_new; + } + + strcat(*text, prefix); + strcat(*text, tadd); + *len += taddlen + prefixlen; +} + + +static void add_style(char **text, size_t *len, size_t *lenmax, const char *prefix, + struct style *sty) +{ + char *prefix2; + int i; + + /* FIXME: Write style details */ + + prefix2 = malloc(strlen(prefix)+3); + strcpy(prefix2, prefix); + strcat(prefix2, " "); + for ( i=0; in_substyles; i++ ) { + add_text(text, len, lenmax, prefix, sty->substyles[i].name); + add_text(text, len, lenmax, "", " {\n"); + add_style(text, len, lenmax, prefix2, &sty->substyles[i]); + add_text(text, len, lenmax, prefix, "}\n"); + } + free(prefix2); + +} + + +char *stylesheet_serialise(Stylesheet *s) +{ + size_t len = 0; /* Current length of "text", not including \0 */ + size_t lenmax = 32; /* Current amount of memory allocated to "text" */ + char *text; + + text = malloc(lenmax*sizeof(char)); + if ( text == NULL ) return NULL; + text[0] = '\0'; + + add_text(&text, &len, &lenmax, "", "STYLES {\n"); + add_style(&text, &len, &lenmax, " ", &s->top); + add_text(&text, &len, &lenmax, "", "}\n\n"); + + return text; +} diff --git a/libstorycode/stylesheet.h b/libstorycode/stylesheet.h index 26b1fe5..98ac089 100644 --- a/libstorycode/stylesheet.h +++ b/libstorycode/stylesheet.h @@ -109,5 +109,6 @@ extern int stylesheet_get_padding(Stylesheet *s, enum style_element el, extern int stylesheet_get_paraspace(Stylesheet *s, enum style_element el, struct length paraspace[4]); +extern char *stylesheet_serialise(Stylesheet *s); #endif /* STYLESHEET_H */ -- cgit v1.2.3