aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.me.uk>2019-04-03 00:03:02 +0200
committerThomas White <taw@bitwiz.me.uk>2019-04-03 00:03:02 +0200
commit65b623afe376e0331e106f00f487b755d555b7d1 (patch)
treeac74d02c668b8fc51d8ab5918c98616c26249af0
parent50f6327a047673818e9b121ae9128b54c92cb90f (diff)
Stylesheet saving skeleton
-rw-r--r--libstorycode/narrative.c22
-rw-r--r--libstorycode/storycode.c42
-rw-r--r--libstorycode/storycode.h1
-rw-r--r--libstorycode/stylesheet.c86
-rw-r--r--libstorycode/stylesheet.h1
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; i<n->n_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 <stdlib.h>
#include <string.h>
+#include <gio/gio.h>
#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; i<n->n_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; i<sty->n_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; i<sty->n_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 */