Avoid copious strcatting for stylesheet lookups
authorThomas White <taw@bitwiz.me.uk>
Sat, 27 Oct 2018 16:05:27 +0000 (18:05 +0200)
committerThomas White <taw@bitwiz.me.uk>
Sat, 27 Oct 2018 16:05:27 +0000 (18:05 +0200)
src/presentation.c
src/sc_interp.c
src/stylesheet.c
src/stylesheet.h
src/stylesheet_editor.c

index 686c97d..4d01db8 100644 (file)
@@ -230,7 +230,7 @@ static void set_slide_size_from_stylesheet(struct presentation *p)
 {
        char *result;
 
-       result = stylesheet_lookup(p->stylesheet, "$.slide.size");
+       result = stylesheet_lookup(p->stylesheet, "$.slide", "size");
        if ( result != NULL ) {
                float v[2];
                if ( parse_double(result, v) == 0 ) {
index ef3c7df..73fc263 100644 (file)
@@ -935,64 +935,40 @@ static int add_text(struct frame *fr, PangoContext *pc, SCBlock *bl,
 
 static void apply_style(SCInterpreter *scin, Stylesheet *ss, const char *path)
 {
-       char fullpath[256];
-       size_t len;
        char *result;
 
-       len = strlen(path);
-       if ( len > 160 ) {
-               fprintf(stderr, "Can't apply style: path too long.\n");
-               return;
-       }
-
        if ( ss == NULL ) return;
 
        /* Font */
-       strcpy(fullpath, path);
-       strcat(fullpath, ".font");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "font");
        if ( result != NULL ) set_font(scin, result);
 
        /* Foreground colour */
-       strcpy(fullpath, path);
-       strcat(fullpath, ".fgcol");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "fgcol");
        if ( result != NULL ) set_colour(scin, result);
 
        /* Background (vertical gradient) */
-       strcpy(fullpath, path);
-       strcat(fullpath, ".bggradv");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "bggradv");
        if ( result != NULL ) set_bggrad(scin, result, GRAD_VERT);
 
        /* Background (horizontal gradient) */
-       strcpy(fullpath, path);
-       strcat(fullpath, ".bggradh");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "bggradh");
        if ( result != NULL ) set_bggrad(scin, result, GRAD_HORIZ);
 
        /* Background (solid colour) */
-       strcpy(fullpath, path);
-       strcat(fullpath, ".bgcol");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "bgcol");
        if ( result != NULL ) set_bgcol(scin, result);
 
        /* Padding */
-       strcpy(fullpath, path);
-       strcat(fullpath, ".pad");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "pad");
        if ( result != NULL ) set_padding(sc_interp_get_frame(scin), result);
 
        /* Paragraph spacing */
-       strcpy(fullpath, path);
-       strcat(fullpath, ".paraspace");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "paraspace");
        if ( result != NULL ) set_paraspace(scin, result);
 
        /* Alignment */
-       strcpy(fullpath, path);
-       strcat(fullpath, ".alignment");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "alignment");
        if ( result != NULL ) {
                if ( strcmp(result, "center") == 0 ) {
                        set_alignment(scin, PANGO_ALIGN_CENTER);
@@ -1015,16 +991,8 @@ static void output_frame(SCInterpreter *scin, SCBlock *bl, Stylesheet *ss,
        struct frame *fr;
        SCBlock *child = sc_block_child(bl);
        const char *options = sc_block_options(bl);
-       char fullpath[256];
-       size_t len;
        char *result;
 
-       len = strlen(stylename);
-       if ( len > 160 ) {
-               fprintf(stderr, "Can't apply style: path too long.\n");
-               return;
-       }
-
        fr = add_subframe(sc_interp_get_frame(scin));
        fr->scblocks = bl;
        fr->resizable = 1;
@@ -1037,9 +1005,7 @@ static void output_frame(SCInterpreter *scin, SCBlock *bl, Stylesheet *ss,
        set_frame_default_style(fr, scin);
 
        /* Next priority: geometry from stylesheet */
-       strcpy(fullpath, stylename);
-       strcat(fullpath, ".geometry");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, stylename, "geometry");
        if ( result != NULL ) {
                parse_frame_options(fr, sc_interp_get_frame(scin), result);
        }
index 12beb69..dc0f5dd 100644 (file)
@@ -75,30 +75,43 @@ Stylesheet *stylesheet_load(GFile *file)
 }
 
 
-char *stylesheet_lookup(Stylesheet *ss, const char *path)
+char *stylesheet_lookup(Stylesheet *ss, const char *path, const char *key)
 {
        JsonNode *node;
+       JsonObject *obj;
        JsonArray *array;
        GError *err = NULL;
-       char *ret;
-       const gchar *v;
+       char *ret = NULL;
 
        node = json_path_query(path, ss->root, &err);
        array = json_node_get_array(node);
 
-       if ( json_array_get_length(array) < 1 ) {
+       if ( json_array_get_length(array) != 1 ) {
                json_node_unref(node);
+               fprintf(stderr, "More than one result in SS lookup (%s)!\n", path);
                return NULL;
        }
 
-       v = json_array_get_string_element(array, 0);
-       if ( v == NULL ) {
-               printf("%s not a string\n", path);
+       obj = json_array_get_object_element(array, 0);
+       if ( obj == NULL ) {
+               printf("%s not a JSON object\n", path);
                return NULL;
        }
 
-       ret = strdup(v);
+       if ( json_object_has_member(obj, key) ) {
+
+               const gchar *v;
+               v = json_object_get_string_member(obj, key);
+               if ( v != NULL ) {
+                       ret = strdup(v);
+               } else {
+                       fprintf(stderr, "Error retrieving %s.%s\n", path, key);
+               }
+
+       } /* else not found, too bad */
+
        json_node_unref(node);
+
        return ret;
 }
 
index aa5867f..15584b1 100644 (file)
@@ -32,7 +32,9 @@
 typedef struct _stylesheet Stylesheet;
 
 extern Stylesheet *stylesheet_load(GFile *file);
-extern char *stylesheet_lookup(Stylesheet *ss, const char *path);
+
+extern char *stylesheet_lookup(Stylesheet *ss, const char *path, const char *key);
+
 extern void stylesheet_free(Stylesheet *ss);
 
 #endif /* STYLESHEET_H */
index 03eac36..a508ea6 100644 (file)
@@ -77,7 +77,7 @@ static int colour_duo_parse(const char *a, GdkRGBA *col1, GdkRGBA *col2)
 
 static void set_font_from_ss(Stylesheet *ss, const char *path, GtkWidget *w)
 {
-       char *result = stylesheet_lookup(ss, path);
+       char *result = stylesheet_lookup(ss, path, "font");
        if ( result != NULL ) {
                gtk_font_button_set_font_name(GTK_FONT_BUTTON(w), result);
        }
@@ -86,7 +86,7 @@ static void set_font_from_ss(Stylesheet *ss, const char *path, GtkWidget *w)
 
 static void set_col_from_ss(Stylesheet *ss, const char *path, GtkWidget *w)
 {
-       char *result = stylesheet_lookup(ss, path);
+       char *result = stylesheet_lookup(ss, path, "fgcol");
        if ( result != NULL ) {
                GdkRGBA rgba;
                if ( gdk_rgba_parse(&rgba, result) == TRUE ) {
@@ -96,11 +96,11 @@ static void set_col_from_ss(Stylesheet *ss, const char *path, GtkWidget *w)
 }
 
 
-static void set_vals_from_ss(Stylesheet *ss, const char *path,
+static void set_vals_from_ss(Stylesheet *ss, const char *path, const char *key,
                              GtkWidget *wl, GtkWidget *wr,
                              GtkWidget *wt, GtkWidget *wb)
 {
-       char *result = stylesheet_lookup(ss, path);
+       char *result = stylesheet_lookup(ss, path, key);
        if ( result != NULL ) {
                float v[4];
                if ( parse_tuple(result, v) == 0 ) {
@@ -120,7 +120,7 @@ static void set_vals_from_ss(Stylesheet *ss, const char *path,
 static void set_size_from_ss(Stylesheet *ss, const char *path,
                              GtkWidget *ww, GtkWidget *wh)
 {
-       char *result = stylesheet_lookup(ss, path);
+       char *result = stylesheet_lookup(ss, path, "size");
        if ( result != NULL ) {
                float v[2];
                if ( parse_double(result, v) == 0 ) {
@@ -138,12 +138,9 @@ static void set_bg_from_ss(Stylesheet *ss, const char *path, GtkWidget *wcol,
                            GtkWidget *wcol2, GtkWidget *wgrad)
 {
        char *result;
-       char fullpath[256];
        int found = 0;
 
-       strcpy(fullpath, path);
-       strcat(fullpath, ".bgcol");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "bgcol");
        if ( result != NULL ) {
                GdkRGBA rgba;
                found = 1;
@@ -163,9 +160,7 @@ static void set_bg_from_ss(Stylesheet *ss, const char *path, GtkWidget *wcol,
                }
        }
 
-       strcpy(fullpath, path);
-       strcat(fullpath, ".bggradv");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "bggradv");
        if ( result != NULL ) {
                GdkRGBA rgba1, rgba2;
                found = 1;
@@ -178,9 +173,7 @@ static void set_bg_from_ss(Stylesheet *ss, const char *path, GtkWidget *wcol,
                }
        }
 
-       strcpy(fullpath, path);
-       strcat(fullpath, ".bggradh");
-       result = stylesheet_lookup(ss, fullpath);
+       result = stylesheet_lookup(ss, path, "bggradh");
        if ( result != NULL ) {
                GdkRGBA rgba1, rgba2;
                found = 1;
@@ -206,41 +199,41 @@ static void set_values_from_presentation(StylesheetEditor *se)
        Stylesheet *ss = se->priv->p->stylesheet;
 
        /* Narrative */
-       set_font_from_ss(ss, "$.narrative.font", se->narrative_style_font);
-       set_col_from_ss(ss, "$.narrative.fgcol", se->narrative_style_fgcol);
+       set_font_from_ss(ss, "$.narrative", se->narrative_style_font);
+       set_col_from_ss(ss, "$.narrative", se->narrative_style_fgcol);
        set_bg_from_ss(ss, "$.narrative", se->narrative_style_bgcol,
                                          se->narrative_style_bgcol2,
                                          se->narrative_style_bggrad);
-       set_vals_from_ss(ss, "$.narrative.pad", se->narrative_style_padding_l,
-                                               se->narrative_style_padding_r,
-                                               se->narrative_style_padding_t,
-                                               se->narrative_style_padding_b);
-       set_vals_from_ss(ss, "$.narrative.paraspace", se->narrative_style_paraspace_l,
-                                                     se->narrative_style_paraspace_r,
-                                                     se->narrative_style_paraspace_t,
-                                                     se->narrative_style_paraspace_b);
+       set_vals_from_ss(ss, "$.narrative", "pad", se->narrative_style_padding_l,
+                                                  se->narrative_style_padding_r,
+                                                  se->narrative_style_padding_t,
+                                                  se->narrative_style_padding_b);
+       set_vals_from_ss(ss, "$.narrative", "paraspace", se->narrative_style_paraspace_l,
+                                                        se->narrative_style_paraspace_r,
+                                                        se->narrative_style_paraspace_t,
+                                                        se->narrative_style_paraspace_b);
 
        /* Slides */
-       set_size_from_ss(ss, "$.slide.size", se->slide_size_w, se->slide_size_h);
+       set_size_from_ss(ss, "$.slide", se->slide_size_w, se->slide_size_h);
        set_bg_from_ss(ss, "$.slide", se->slide_style_bgcol,
                                      se->slide_style_bgcol2,
                                      se->slide_style_bggrad);
 
 
        /* Frames */
-       set_font_from_ss(ss, "$.slide.frame.font", se->frame_style_font);
-       set_col_from_ss(ss, "$.slide.frame.fgcol", se->frame_style_fgcol);
+       set_font_from_ss(ss, "$.slide.frame", se->frame_style_font);
+       set_col_from_ss(ss, "$.slide.frame", se->frame_style_fgcol);
        set_bg_from_ss(ss, "$.slide.frame", se->frame_style_bgcol,
                                            se->frame_style_bgcol2,
                                            se->frame_style_bggrad);
-       set_vals_from_ss(ss, "$.slide.frame.pad", se->frame_style_padding_l,
-                                                 se->frame_style_padding_r,
-                                                 se->frame_style_padding_t,
-                                                 se->frame_style_padding_b);
-       set_vals_from_ss(ss, "$.slide.frame.paraspace", se->frame_style_paraspace_l,
-                                                       se->frame_style_paraspace_r,
-                                                       se->frame_style_paraspace_t,
-                                                       se->frame_style_paraspace_b);
+       set_vals_from_ss(ss, "$.slide.frame", "pad", se->frame_style_padding_l,
+                                                    se->frame_style_padding_r,
+                                                    se->frame_style_padding_t,
+                                                    se->frame_style_padding_b);
+       set_vals_from_ss(ss, "$.slide.frame", "paraspace", se->frame_style_paraspace_l,
+                                                          se->frame_style_paraspace_r,
+                                                          se->frame_style_paraspace_t,
+                                                          se->frame_style_paraspace_b);
 }