From bcace6ad116688ad75691579f5fa6edcde1df0d8 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Sun, 17 Apr 2016 11:30:12 -0400
Subject: [PATCH] more whitespace cleanup, comment and improve
 image_get_filename code so it doesn't look like a crasher bug

---
 externals/moonlib/image.c | 109 +++++++++++++++++++++-----------------
 1 file changed, 61 insertions(+), 48 deletions(-)

diff --git a/externals/moonlib/image.c b/externals/moonlib/image.c
index efc30e2c4..0b5a405a6 100644
--- a/externals/moonlib/image.c
+++ b/externals/moonlib/image.c
@@ -20,25 +20,30 @@ typedef struct _image
     t_glist *x_glist;
     int x_width;
     int x_height;
-    t_symbol  *x_image;
+    t_symbol *x_image;
     int x_type; //0=file 1=tk_image
-    t_int  x_localimage; //localimage "img%x" done
+    t_int x_localimage; //localimage "img%x" done
 } t_image;
 
 /* widget helper functions */
 
-const char *image_get_filename(t_image *x,char *file)
+const char *image_get_filename(t_image *x, char *file)
 {
-    static char fname[MAXPDSTRING];
-    char *bufptr;
+    static char dirresult[MAXPDSTRING];
+    char *fileresult, *fullpath;
     int fd;
-    fd=open_via_path(canvas_getdir(glist_getcanvas(x->x_glist))->s_name,
-                     file, "",fname, &bufptr, MAXPDSTRING, 1);
+    fd = open_via_path(canvas_getdir(glist_getcanvas(x->x_glist))->s_name,
+        file, "", dirresult, &fileresult, MAXPDSTRING, 1);
     if (fd > 0)
     {
-        fname[strlen(fname)] = '/';
+        /* dirresult and fileresult are in the same buffer (see comment 
+           for do_open_via_path in s_path.c). This means we can change
+           the null terminator that separates them to a backslash to
+           retrieve the full path... */
+        fullpath = dirresult;
+        fullpath[strlen(fullpath)] = '/';
         close(fd);
-        return fname;
+        return fullpath;
     }
     else return 0;
 }
@@ -57,25 +62,31 @@ void image_drawme(t_image *x, t_glist *glist, int firsttime)
         {
             sys_vgui(".x%lx.c create image %d %d -tags %xS\n",
                      glist_getcanvas(glist),
-                     text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),x);
+                     text_xpix(&x->x_obj, glist),
+                     text_ypix(&x->x_obj, glist),
+                     x);
             sys_vgui(".x%lx.c itemconfigure %xS -image %s\n",
-                     glist_getcanvas(glist),x,x->x_image->s_name);
+                     glist_getcanvas(glist), x, x->x_image->s_name);
         }
         else
         {
-            const char *fname=image_get_filename(x,x->x_image->s_name);
-            if(!x->x_localimage)
+            const char *fname = image_get_filename(x,x->x_image->s_name);
+            if (!x->x_localimage)
             {
-                sys_vgui("image create photo img%x\n",x);
-                x->x_localimage=1;
+                sys_vgui("image create photo img%x\n", x);
+                x->x_localimage = 1;
             }
             if (fname)
             {
-                sys_vgui("::moonlib::image::configure .x%lx img%x {%s}\n",x,x,fname);
+                sys_vgui("::moonlib::image::configure .x%lx img%x {%s}\n",
+                    x, x, fname);
             }
             sys_vgui(".x%lx.c create image %d %d -image img%x -tags %xS\n",
                      glist_getcanvas(glist),
-                     text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),x,x);
+                     text_xpix(&x->x_obj, glist),
+                     text_ypix(&x->x_obj, glist),
+                     x,
+                     x);
         }
         /* TODO callback from gui
           sys_vgui("image_size logo");
@@ -89,8 +100,7 @@ void image_drawme(t_image *x, t_glist *glist, int firsttime)
     }
 }
 
-
-void image_erase(t_image *x,t_glist *glist)
+void image_erase(t_image *x, t_glist *glist)
 {
     sys_vgui(".x%lx.c delete %xS\n", glist_getcanvas(glist), x);
 }
@@ -112,19 +122,19 @@ static void image_getrect(t_gobj *z, t_glist *glist,
     *yp2 = text_ypix(&x->x_obj, glist) + height;
 }
 
-static void image_displace(t_gobj *z, t_glist *glist,
-    int dx, int dy)
+static void image_displace(t_gobj *z, t_glist *glist, int dx, int dy)
 {
     t_image *x = (t_image *)z;
     x->x_obj.te_xpix += dx;
     x->x_obj.te_ypix += dy;
     sys_vgui(".x%lx.c coords %xSEL %d %d %d %d\n",
-             glist_getcanvas(glist), x,
-             text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
-             text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height);
-
+        glist_getcanvas(glist), x,
+        text_xpix(&x->x_obj, glist),
+        text_ypix(&x->x_obj, glist),
+        text_xpix(&x->x_obj, glist) + x->x_width,
+        text_ypix(&x->x_obj, glist) + x->x_height);
     image_drawme(x, glist, 0);
-    canvas_fixlinesfor(glist,(t_text *) x);
+    canvas_fixlinesfor(glist,(t_text *)x);
 }
 
 static void image_select(t_gobj *z, t_glist *glist, int state)
@@ -132,17 +142,18 @@ static void image_select(t_gobj *z, t_glist *glist, int state)
     t_image *x = (t_image *)z;
     if (state)
     {
-        sys_vgui(".x%lx.c create rectangle \
-%d %d %d %d -tags %xSEL -outline blue\n",
-                 glist_getcanvas(glist),
-                 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
-                 text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height,
-                 x);
+        sys_vgui(".x%lx.c create rectangle "
+                 "%d %d %d %d -tags %xSEL -outline blue\n",
+            glist_getcanvas(glist),
+            text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
+            text_xpix(&x->x_obj, glist) + x->x_width,
+            text_ypix(&x->x_obj, glist) + x->x_height,
+            x);
     }
     else
     {
         sys_vgui(".x%lx.c delete %xSEL\n",
-                 glist_getcanvas(glist), x);
+            glist_getcanvas(glist), x);
     }
 }
 
@@ -165,7 +176,7 @@ static void image_vis(t_gobj *z, t_glist *glist, int vis)
     if (vis)
         image_drawme(s, glist, 1);
     else
-        image_erase(s,glist);
+        image_erase(s, glist);
 }
 
 /* can we use the normal text save function ?? */
@@ -186,26 +197,26 @@ static void image_save(t_gobj *z, t_binbuf *b)
 
 t_widgetbehavior image_widgetbehavior;
 
-void image_size(t_image *x,t_floatarg w,t_floatarg h)
+void image_size(t_image *x, t_floatarg w, t_floatarg h)
 {
     x->x_width = w;
     x->x_height = h;
 }
 
-void image_color(t_image *x,t_symbol *col)
+void image_color(t_image *x, t_symbol *col)
 {
     /*     outlet_bang(x->x_obj.ob_outlet); only bang if there was a bang ..
            so color black does the same as bang, but doesn't forward the bang
     */
 }
 
-void image_open(t_gobj *z,t_symbol *file)
+void image_open(t_gobj *z, t_symbol *file)
 {
     t_image *x = (t_image *)z;
     const char *fname;
     int oldtype = x->x_type;
 
-    fname = image_get_filename(x,file->s_name);
+    fname = image_get_filename(x, file->s_name);
     if (fname)
     {
         x->x_image = file;
@@ -214,36 +225,38 @@ void image_open(t_gobj *z,t_symbol *file)
         {
             if (!x->x_localimage)
             {
-                sys_vgui("image create photo img%x\n",x);
+                sys_vgui("image create photo img%x\n", x);
                 x->x_localimage = 1;
             }
-            sys_vgui("img%x blank\n",x);
-            sys_vgui("::moonlib::image::configure .x%lx img%x {%s}\n",x,x,fname);
+            sys_vgui("img%x blank\n", x);
+            sys_vgui("::moonlib::image::configure .x%lx img%x {%s}\n",
+                x, x, fname);
             if (oldtype) sys_vgui(".x%lx.c itemconfigure %xS -image img%x\n",
-                                     glist_getcanvas(x->x_glist),x,x);
+                             glist_getcanvas(x->x_glist), x, x);
         }
     }
     else
         pd_error(x, "[image]: error opening file '%s'", file->s_name);
 }
 
-void image_load(t_gobj *z,t_symbol *image,t_symbol *file)
+void image_load(t_gobj *z, t_symbol *image, t_symbol *file)
 {
     t_image *x = (t_image *)z;
     const char *fname;
-    fname = image_get_filename(x,file->s_name);
+    fname = image_get_filename(x, file->s_name);
     if (fname)
-        sys_vgui("::moonlib::image::create_photo .x%lx %s {%s}\n",x,image->s_name,fname);
+        sys_vgui("::moonlib::image::create_photo .x%lx %s {%s}\n",
+            x, image->s_name, fname);
 }
 
-void image_set(t_gobj *z,t_symbol *image)
+void image_set(t_gobj *z, t_symbol *image)
 {
     t_image *x = (t_image *)z;
     x->x_image = image;
     x->x_type = 1;
     if (glist_isvisible(x->x_glist))
         sys_vgui(".x%lx.c itemconfigure %xS -image %s\n",
-                 glist_getcanvas(x->x_glist),x,x->x_image->s_name);
+                 glist_getcanvas(x->x_glist), x, x->x_image->s_name);
 }
 
 static void image_setwidget(void)
@@ -284,7 +297,7 @@ static void *image_new(t_symbol *image, t_float type)
 void image_setup(void)
 {
     image_class = class_new(gensym("image"), (t_newmethod)image_new, 0,
-                            sizeof(t_image),0, A_DEFSYM,A_DEFFLOAT,0);
+                            sizeof(t_image), 0, A_DEFSYM, A_DEFFLOAT, 0);
     /*
         class_addmethod(image_class, (t_method)image_size, gensym("size"),
         	A_FLOAT, A_FLOAT, 0);
-- 
GitLab