From fe212419abae3340b58dd3e0c187063a4c267c92 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 9 Sep 2012 21:20:13 +0200 Subject: Move actual loading/saving stuff to presentation --- src/loadsave.c | 192 +---------------------------------------------------- src/loadsave.h | 7 +- src/presentation.c | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/presentation.h | 3 + 4 files changed, 199 insertions(+), 192 deletions(-) diff --git a/src/loadsave.c b/src/loadsave.c index 8200200..7fd7b85 100644 --- a/src/loadsave.c +++ b/src/loadsave.c @@ -33,7 +33,6 @@ #include "presentation.h" #include "stylesheet.h" -#include "mainwindow.h" static int alloc_children(struct ds_node *node) @@ -49,7 +48,7 @@ static int alloc_children(struct ds_node *node) } -static struct ds_node *new_ds_node(const char *key) +struct ds_node *new_ds_node(const char *key) { struct ds_node *new; @@ -337,7 +336,7 @@ static char *fgets_long(FILE *fh) } -static int deserialize_file(FILE *fh, struct ds_node *root) +int deserialize_file(FILE *fh, struct ds_node *root) { char *line; struct ds_node *cur_node = root; @@ -359,7 +358,7 @@ static int deserialize_file(FILE *fh, struct ds_node *root) } -static void free_ds_tree(struct ds_node *root) +void free_ds_tree(struct ds_node *root) { int i; @@ -552,133 +551,6 @@ int get_field_s(struct ds_node *root, const char *key, char **val) } -static struct slide *tree_to_slide(struct presentation *p, struct ds_node *root) -{ - struct slide *s; - - s = new_slide(); - s->parent = p; - - /* FIXME: Load stuff */ - - return s; -} - - -static int tree_to_slides(struct ds_node *root, struct presentation *p) -{ - int i; - - for ( i=0; in_children; i++ ) { - - struct slide *s; - - s = tree_to_slide(p, root->children[i]); - if ( s != NULL ) { - insert_slide(p, s, p->num_slides-1); - } - - } - - return 0; -} - - -int tree_to_presentation(struct ds_node *root, struct presentation *p) -{ - struct ds_node *node; - char *check; - int i; - - p->cur_edit_slide = NULL; - p->cur_proj_slide = NULL; - - node = find_node(root, "slide-properties/width", 0); - if ( node == NULL ) return 1; - p->slide_width = strtod(node->value, &check); - if ( check == node->value ) { - fprintf(stderr, "Invalid slide width\n"); - return 1; - } - - node = find_node(root, "slide-properties/height", 0); - if ( node == NULL ) return 1; - p->slide_height = strtod(node->value, &check); - if ( check == node->value ) { - fprintf(stderr, "Invalid slide height\n"); - return 1; - } - - node = find_node(root, "stylesheet", 0); - if ( node != NULL ) { - free_stylesheet(p->ss); - p->ss = tree_to_stylesheet(node); - if ( p->ss == NULL ) { - fprintf(stderr, "Invalid style sheet\n"); - return 1; - } - } - - for ( i=0; inum_slides; i++ ) { - free_slide(p->slides[i]); - p->num_slides = 0; - } - - node = find_node(root, "slides", 0); - if ( node != NULL ) { - tree_to_slides(node, p); - if ( p->num_slides == 0 ) { - fprintf(stderr, "Failed to load any slides\n"); - p->cur_edit_slide = add_slide(p, 0); - return 1; - } - } - - return 0; -} - - -int load_presentation(struct presentation *p, const char *filename) -{ - FILE *fh; - struct ds_node *root; - int r; - - assert(p->completely_empty); - - fh = fopen(filename, "r"); - if ( fh == NULL ) return 1; - - root = new_ds_node("root"); - if ( root == NULL ) return 1; - - if ( deserialize_file(fh, root) ) { - fclose(fh); - return 1; - } - - r = tree_to_presentation(root, p); - free_ds_tree(root); - - fclose(fh); - - if ( r ) { - p->cur_edit_slide = new_slide(); - insert_slide(p, p->cur_edit_slide, 0); - p->completely_empty = 1; - return r; /* Error */ - } - - assert(p->filename == NULL); - p->filename = strdup(filename); - update_titlebar(p); - - p->cur_edit_slide = p->slides[0]; - - return 0; -} - - static void rebuild_prefix(struct serializer *ser) { int i; @@ -758,61 +630,3 @@ void serialize_end(struct serializer *ser) ser->empty_set = 1; } - -int save_presentation(struct presentation *p, const char *filename) -{ - FILE *fh; - int i; - struct serializer ser; - char *old_fn; - - //grab_current_notes(p); - - 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"); - serialize_f(&ser, "version", 0.1); - - serialize_start(&ser, "slide-properties"); - serialize_f(&ser, "width", p->slide_width); - serialize_f(&ser, "height", p->slide_height); - serialize_end(&ser); - - serialize_start(&ser, "stylesheet"); - write_stylesheet(p->ss, &ser); - serialize_end(&ser); - - serialize_start(&ser, "slides"); - for ( i=0; inum_slides; i++ ) { - - struct slide *s; - char s_id[32]; - - s = p->slides[i]; - - snprintf(s_id, 31, "%i", i); - serialize_start(&ser, s_id); - - /* FIXME: Save stuff */ - - serialize_end(&ser); - - } - serialize_end(&ser); - - /* Slightly fiddly because someone might - * do save_presentation(p, p->filename) */ - old_fn = p->filename; - p->filename = strdup(filename); - if ( old_fn != NULL ) free(old_fn); - update_titlebar(p); - - fclose(fh); - return 0; -} diff --git a/src/loadsave.h b/src/loadsave.h index 14fb6ff..825b932 100644 --- a/src/loadsave.h +++ b/src/loadsave.h @@ -51,6 +51,10 @@ struct serializer int blank_written; }; +extern struct ds_node *new_ds_node(const char *key); +extern void free_ds_tree(struct ds_node *root); +extern int deserialize_file(FILE *fh, struct ds_node *root); + extern void show_tree(struct ds_node *root, const char *path); extern char *escape_text(const char *a); @@ -69,7 +73,4 @@ extern int get_field_s(struct ds_node *root, const char *key, char **val); extern struct ds_node *find_node(struct ds_node *root, const char *path, int cr); -extern int load_presentation(struct presentation *p, const char *filename); -extern int save_presentation(struct presentation *p, const char *filename); - #endif /* LOADSAVE_H */ diff --git a/src/presentation.c b/src/presentation.c index 405cf49..a94b220 100644 --- a/src/presentation.c +++ b/src/presentation.c @@ -32,6 +32,8 @@ #include "presentation.h" #include "stylesheet.h" +#include "loadsave.h" +#include "mainwindow.h" static int num_presentations = 0; @@ -224,3 +226,190 @@ struct presentation *new_presentation() return new; } + + +int save_presentation(struct presentation *p, const char *filename) +{ + FILE *fh; + int i; + struct serializer ser; + char *old_fn; + + //grab_current_notes(p); + + 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"); + serialize_f(&ser, "version", 0.1); + + serialize_start(&ser, "slide-properties"); + serialize_f(&ser, "width", p->slide_width); + serialize_f(&ser, "height", p->slide_height); + serialize_end(&ser); + + serialize_start(&ser, "stylesheet"); + write_stylesheet(p->ss, &ser); + serialize_end(&ser); + + serialize_start(&ser, "slides"); + for ( i=0; inum_slides; i++ ) { + + struct slide *s; + char s_id[32]; + + s = p->slides[i]; + + snprintf(s_id, 31, "%i", i); + serialize_start(&ser, s_id); + + /* FIXME: Save stuff */ + + serialize_end(&ser); + + } + serialize_end(&ser); + + /* Slightly fiddly because someone might + * do save_presentation(p, p->filename) */ + old_fn = p->filename; + p->filename = strdup(filename); + if ( old_fn != NULL ) free(old_fn); + update_titlebar(p); + + fclose(fh); + return 0; +} + + +static struct slide *tree_to_slide(struct presentation *p, struct ds_node *root) +{ + struct slide *s; + + s = new_slide(); + s->parent = p; + + /* FIXME: Load stuff */ + + return s; +} + + +static int tree_to_slides(struct ds_node *root, struct presentation *p) +{ + int i; + + for ( i=0; in_children; i++ ) { + + struct slide *s; + + s = tree_to_slide(p, root->children[i]); + if ( s != NULL ) { + insert_slide(p, s, p->num_slides-1); + } + + } + + return 0; +} + + +int tree_to_presentation(struct ds_node *root, struct presentation *p) +{ + struct ds_node *node; + char *check; + int i; + + p->cur_edit_slide = NULL; + p->cur_proj_slide = NULL; + + node = find_node(root, "slide-properties/width", 0); + if ( node == NULL ) return 1; + p->slide_width = strtod(node->value, &check); + if ( check == node->value ) { + fprintf(stderr, "Invalid slide width\n"); + return 1; + } + + node = find_node(root, "slide-properties/height", 0); + if ( node == NULL ) return 1; + p->slide_height = strtod(node->value, &check); + if ( check == node->value ) { + fprintf(stderr, "Invalid slide height\n"); + return 1; + } + + node = find_node(root, "stylesheet", 0); + if ( node != NULL ) { + free_stylesheet(p->ss); + p->ss = tree_to_stylesheet(node); + if ( p->ss == NULL ) { + fprintf(stderr, "Invalid style sheet\n"); + return 1; + } + } + + for ( i=0; inum_slides; i++ ) { + free_slide(p->slides[i]); + p->num_slides = 0; + } + + node = find_node(root, "slides", 0); + if ( node != NULL ) { + tree_to_slides(node, p); + if ( p->num_slides == 0 ) { + fprintf(stderr, "Failed to load any slides\n"); + p->cur_edit_slide = add_slide(p, 0); + return 1; + } + } + + return 0; +} + + +int load_presentation(struct presentation *p, const char *filename) +{ + FILE *fh; + struct ds_node *root; + int r; + + assert(p->completely_empty); + + fh = fopen(filename, "r"); + if ( fh == NULL ) return 1; + + root = new_ds_node("root"); + if ( root == NULL ) return 1; + + if ( deserialize_file(fh, root) ) { + fclose(fh); + return 1; + } + + r = tree_to_presentation(root, p); + free_ds_tree(root); + + fclose(fh); + + if ( r ) { + p->cur_edit_slide = new_slide(); + insert_slide(p, p->cur_edit_slide, 0); + p->completely_empty = 1; + return r; /* Error */ + } + + assert(p->filename == NULL); + p->filename = strdup(filename); + update_titlebar(p); + + p->cur_edit_slide = p->slides[0]; + + return 0; +} + diff --git a/src/presentation.h b/src/presentation.h index 62237c0..efb2e95 100644 --- a/src/presentation.h +++ b/src/presentation.h @@ -136,6 +136,9 @@ extern void get_titlebar_string(struct presentation *p); extern int slide_number(struct presentation *p, struct slide *s); +extern int load_presentation(struct presentation *p, const char *filename); +extern int save_presentation(struct presentation *p, const char *filename); + #define UNUSED __attribute__((unused)) -- cgit v1.2.3