]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/commitdiff
example.c: improve GError handling.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 4 Aug 2023 18:43:15 +0000 (14:43 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 4 Aug 2023 18:43:15 +0000 (14:43 -0400)
example.c

index 5c1f5ff3298059a99ef2e2cfefcb29cc43a12fd0..13b692b2be1a93a68b59ae59228bdd4065032440 100644 (file)
--- a/example.c
+++ b/example.c
@@ -64,7 +64,7 @@ static void render_path(cairo_t* cr, shape_t* path) {
       j += 7;
       break;
     default:
-      printf("error ");
+      fprintf(stderr, "error: unmatched case in render_path\n");
       j += 1;
     }
   }
@@ -134,7 +134,7 @@ static diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
   free(buffer);
 
   if (code != svgtiny_OK) {
-    fprintf(stderr, "svgtiny_parse failed: ");
+    fprintf(stderr, "svgtiny_parse failed with ");
     switch (code) {
     case svgtiny_OUT_OF_MEMORY:
       fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
@@ -250,20 +250,27 @@ static cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
   return cr;
 }
 
-static GdkPixbuf* gdk_pixbuf_from_svg_file_stream(FILE *fp, GError **error) {
+static GdkPixbuf* gdk_pixbuf_from_svg_file_stream(FILE *fp, GError** error) {
   diagram_t* diagram;
   cairo_t* cr = 0;
-
   GdkPixbuf* pb;
 
   diagram = svgtiny_diagram_from_file(fp, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
   if (!diagram) {
+    g_set_error_literal(error,
+                       GDK_PIXBUF_ERROR,
+                       GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                       "Could not parse SVG diagram from file");
     return NULL;
   }
 
   cr = cairo_context_from_diagram(diagram);
   if (!cr) {
     svgtiny_free(diagram);
+    g_set_error_literal(error,
+                       GDK_PIXBUF_ERROR,
+                       GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                       "Could not create Cairo surface from SVG diagram");
     return NULL;
   }
 
@@ -286,7 +293,10 @@ static GdkPixbuf* gdk_pixbuf_from_svg_file_stream(FILE *fp, GError **error) {
 
 
   if (!pb) {
-    fprintf(stderr, "gdk_pixbuf_get_from_surface failed!\n");
+    g_set_error_literal(error,
+                       GDK_PIXBUF_ERROR,
+                       GDK_PIXBUF_ERROR_FAILED,
+                       "Failed to obtain a GdkPixbuf from Cairo surface");
   }
 
   return pb;
@@ -339,6 +349,7 @@ int main(int argc, char** argv) {
   char* svgpath;
   char* pngpath;
   FILE* fp;
+  GError* err = NULL;
   GdkPixbuf* pb;
 
   /* Parse arguments, and maybe print usage */
@@ -357,11 +368,17 @@ int main(int argc, char** argv) {
     return 1;
   }
 
-  pb = gdk_pixbuf_from_svg_file_stream(fp, NULL);
-  if (pb) {
-    gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
-    g_object_unref(pb);
+  pb = gdk_pixbuf_from_svg_file_stream(fp, &err);
+  if (!pb) {
+    fprintf(stderr,
+           "Error %d in gdk_pixbuf_from_svg_file_stream: %s\n",
+           err->code,
+           err->message);
+    g_error_free(err);
+    return 1;
   }
 
+  gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
+  g_object_unref(pb);
   return 0;
 }