diff options
author | Thomas White <taw@bitwiz.org.uk> | 2011-10-20 17:09:25 +0100 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2011-10-20 17:09:25 +0100 |
commit | 32124e557dd9d1e25b0b279fb94e83f717a83ab0 (patch) | |
tree | a179370560b552ede4fda27c819b3ae72a432e25 /src | |
parent | deaca1631241ceb0e9e487b1d44bc3401320c5d9 (diff) |
The serializer
Diffstat (limited to 'src')
-rw-r--r-- | src/loadsave.c | 125 | ||||
-rw-r--r-- | src/loadsave.h | 11 | ||||
-rw-r--r-- | src/objects.h | 4 | ||||
-rw-r--r-- | src/stylesheet.c | 81 | ||||
-rw-r--r-- | src/stylesheet.h | 5 |
5 files changed, 183 insertions, 43 deletions
diff --git a/src/loadsave.c b/src/loadsave.c index 01c8a64..45b8ab3 100644 --- a/src/loadsave.c +++ b/src/loadsave.c @@ -25,12 +25,27 @@ #include <config.h> #endif +#include <string.h> +#include <stdio.h> +#include <stdlib.h> #include "presentation.h" #include "objects.h" #include "stylesheet.h" +struct serializer +{ + FILE *fh; + + char *stack[32]; + int stack_depth; + char *prefix; + int empty_set; + int blank_written; +}; + + int load_presentation(struct presentation *p, const char *filename) { return 0; @@ -47,8 +62,70 @@ static const char *type_text(enum objtype t) } -static void write_stylesheet(StyleSheet *ss, FILE *fh) +static void rebuild_prefix(struct serializer *ser) { + int i; + size_t sz = 1; /* Space for terminator */ + + for ( i=0; i<ser->stack_depth; i++ ) { + sz += strlen(ser->stack[i]) + 1; + } + + free(ser->prefix); + ser->prefix = malloc(sz); + if ( ser->prefix == NULL ) return; /* Probably bad! */ + + ser->prefix[0] = '\0'; + for ( i=0; i<ser->stack_depth; i++ ) { + if ( i != 0 ) strcat(ser->prefix, "/"); + strcat(ser->prefix, ser->stack[i]); + } +} + + +void serialize_start(struct serializer *ser, const char *id) +{ + ser->stack[ser->stack_depth++] = strdup(id); + rebuild_prefix(ser); + ser->empty_set = 1; +} + + +static void check_prefix_output(struct serializer *ser) +{ + if ( ser->empty_set ) { + ser->empty_set = 0; + fprintf(ser->fh, "\n"); + fprintf(ser->fh, "[%s]\n", ser->prefix); + } +} + + +void serialize_s(struct serializer *ser, const char *key, const char *val) +{ + check_prefix_output(ser); + fprintf(ser->fh, "%s = \"%s\"\n", key, val); +} + + +void serialize_f(struct serializer *ser, const char *key, double val) +{ + check_prefix_output(ser); + fprintf(ser->fh, "%s = %.2ff\n", key, val); +} + + +void serialize_b(struct serializer *ser, const char *key, int val) +{ + check_prefix_output(ser); + fprintf(ser->fh, "%s = %i\n", key, val); +} + + +void serialize_end(struct serializer *ser) +{ + free(ser->stack[--ser->stack_depth]); + rebuild_prefix(ser); } @@ -56,47 +133,57 @@ int save_presentation(struct presentation *p, const char *filename) { FILE *fh; int i; + struct serializer ser; fh = fopen(filename, "w"); if ( fh == NULL ) return 1; + /* Set up the serializer */ + ser.fh = fh; + ser.stack_depth = 0; + ser.prefix = NULL; + fprintf(fh, "# Colloquium presentation file\n"); - fprintf(fh, "version=0\n"); + serialize_f(&ser, "version", 0.1); - fprintf(fh, "width=%.2f\n", p->slide_width); - fprintf(fh, "height=%.2f\n", p->slide_height); + serialize_start(&ser, "slide-properties"); + serialize_f(&ser, "width", p->slide_width); + serialize_f(&ser, "height", p->slide_height); + serialize_end(&ser); - write_stylesheet(p->ss, fh); + serialize_start(&ser, "stylesheet"); + write_stylesheet(p->ss, &ser); + serialize_end(&ser); + serialize_start(&ser, "slides"); for ( i=0; i<p->num_slides; i++ ) { int j; struct slide *s; + char s_id[32]; s = p->slides[i]; - fprintf(fh, "++slide\n"); - + snprintf(s_id, 31, "%i", i); + serialize_start(&ser, s_id); for ( j=0; j<s->num_objects; j++ ) { - struct object *o; - - o = s->objects[j]; + struct object *o = s->objects[j]; + char o_id[32]; if ( o->empty ) continue; + snprintf(o_id, 31, "%i", j); - fprintf(fh, "++object\n"); - fprintf(fh, "type=%s\n", type_text(o->type)); - fprintf(fh, "x=%.2f\n", o->x); - fprintf(fh, "y=%.2f\n", o->y); - fprintf(fh, "w=%.2f\n", o->bb_width); - fprintf(fh, "h=%.2f\n", o->bb_height); - fprintf(fh, "--object\n"); - } + serialize_start(&ser, o_id); + serialize_s(&ser, "type", type_text(o->type)); + //o->serialize(o, &ser); + serialize_end(&ser); - fprintf(fh, "--slide\n"); + } + serialize_end(&ser); } + serialize_end(&ser); fclose(fh); return 0; diff --git a/src/loadsave.h b/src/loadsave.h index 68fdc4c..981e0ab 100644 --- a/src/loadsave.h +++ b/src/loadsave.h @@ -27,6 +27,17 @@ #include <config.h> #endif +/* Forward declaration */ +struct presentation; + +/* Opaque structure */ +struct serializer; + +extern void serialize_start(struct serializer *s, const char *id); +extern void serialize_s(struct serializer *s, const char *key, const char *val); +extern void serialize_f(struct serializer *s, const char *key, double val); +extern void serialize_b(struct serializer *s, const char *key, int val); +extern void serialize_end(struct serializer *s); extern int load_presentation(struct presentation *p, const char *filename); extern int save_presentation(struct presentation *p, const char *filename); diff --git a/src/objects.h b/src/objects.h index dd68c02..adaf8fc 100644 --- a/src/objects.h +++ b/src/objects.h @@ -28,6 +28,9 @@ #endif +#include "loadsave.h" + + enum objtype { TEXT, @@ -54,6 +57,7 @@ struct object void (*render_object)(cairo_t *cr, struct object *o); void (*update_object)(struct object *o); void (*delete_object)(struct object *o); + void (*serialize)(struct object *o, struct serializer *ser); }; diff --git a/src/stylesheet.c b/src/stylesheet.c index 554b1ab..fea78c1 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -29,11 +29,11 @@ #include <string.h> #include <gtk/gtk.h> #include <assert.h> -#include <libconfig.h> #include "presentation.h" #include "stylesheet.h" #include "objects.h" +#include "loadsave.h" struct _stylesheetwindow @@ -576,39 +576,16 @@ StyleSheet *new_stylesheet() void save_stylesheet(StyleSheet *ss, const char *filename) { - config_t config; - - config_init(&config); - - if ( config_write_file(&config, filename) == CONFIG_FALSE ) { - printf("Couldn't save style sheet to '%s'\n", filename); - } - config_destroy(&config); } StyleSheet *load_stylesheet(const char *filename) { StyleSheet *ss; - config_t config; ss = new_stylesheet(); if ( ss == NULL ) return NULL; - config_init(&config); - if ( config_read_file(&config, filename) != CONFIG_TRUE ) { - printf("Style sheet parse error: %s at line %i.\n", - config_error_text(&config), - config_error_line(&config)); - config_destroy(&config); - default_stylesheet(ss); - return ss; - } - - - - config_destroy(&config); - return ss; } @@ -661,6 +638,62 @@ StylesheetWindow *open_stylesheet(struct presentation *p) } +static const char *str_halign(enum justify halign) +{ + switch ( halign ) { + case J_LEFT : return "left"; + case J_RIGHT : return "right"; + case J_CENTER : return "center"; + default : return "???"; + } +} + + +static const char *str_valign(enum vert_pos valign) +{ + switch ( valign ) { + case V_TOP : return "top"; + case V_BOTTOM : return "bottom"; + case V_CENTER : return "center"; + default : return "???"; + } +} + + +void write_stylesheet(StyleSheet *ss, struct serializer *ser) +{ + int i; + + serialize_start(ser, "styles"); + for ( i=0; i<ss->n_styles; i++ ) { + + struct style *s = ss->styles[i]; + char id[32]; + + snprintf(id, 31, "%i", i); + + serialize_start(ser, id); + serialize_s(ser, "name", s->name); + serialize_f(ser, "margin_left", s->margin_left); + serialize_f(ser, "margin_right", s->margin_right); + serialize_f(ser, "margin_top", s->margin_top); + serialize_f(ser, "margin_bottom", s->margin_bottom); + serialize_b(ser, "use_max_width", s->use_max_width); + serialize_f(ser, "max_width", s->max_width); + serialize_f(ser, "offset_x", s->offset_x); + serialize_f(ser, "offset_y", s->offset_y); + serialize_s(ser, "font", s->font); + serialize_s(ser, "colour", s->colour); + serialize_f(ser, "alpha", s->alpha); + serialize_s(ser, "halign", str_halign(s->halign)); + serialize_s(ser, "valign", str_valign(s->valign)); + serialize_end(ser); + + } + serialize_end(ser); +} + + struct style *find_style(StyleSheet *ss, const char *name) { int i; diff --git a/src/stylesheet.h b/src/stylesheet.h index 5b3bdb1..113e38e 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -28,6 +28,9 @@ #endif +#include "loadsave.h" + + enum justify { J_LEFT = 0, @@ -87,4 +90,6 @@ extern StyleSheet *load_stylesheet(const char *filename); extern struct style *find_style(StyleSheet *ss, const char *name); +extern void write_stylesheet(StyleSheet *ss, struct serializer *ser); + #endif /* STYLESHEET_H */ |