Author: fejj
Date: 2008-02-05 15:22:12 -0500 (Tue, 05 Feb 2008)
New Revision: 94941

Added:
   trunk/moon/src/utils.cpp
   trunk/moon/src/utils.h
Removed:
   trunk/moon/src/garray-ext.cpp
   trunk/moon/src/garray-ext.h
Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/Makefile.am
   trunk/moon/src/animation.cpp
   trunk/moon/src/collection.cpp
   trunk/moon/src/collection.h
   trunk/moon/src/downloader.cpp
   trunk/moon/src/font.cpp
   trunk/moon/src/runtime.cpp
Log:
2008-02-05  Jeffrey Stedfast  <[EMAIL PROTECTED]>

        * downloader.cpp (Downloader::ll_downloader_get_response_file):
        Simplified by using the new ExtractFile utility function from
        utils.cpp.

        * font.cpp: Moved ExtractFile() and make_tmpdir() into utils.cpp

        * utils.cpp: New collection of utility functions.



Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/ChangeLog    2008-02-05 20:22:12 UTC (rev 94941)
@@ -1,5 +1,15 @@
 2008-02-05  Jeffrey Stedfast  <[EMAIL PROTECTED]>
 
+       * downloader.cpp (Downloader::ll_downloader_get_response_file):
+       Simplified by using the new ExtractFile utility function from
+       utils.cpp.
+
+       * font.cpp: Moved ExtractFile() and make_tmpdir() into utils.cpp
+
+       * utils.cpp: New collection of utility functions.
+
+2008-02-05  Jeffrey Stedfast  <[EMAIL PROTECTED]>
+
        * font.cpp: If a font is requested from a zip archive, extract the
        contents and try to figure out which font is the closest match to
        the one requested. Fixes bug #356044.

Modified: trunk/moon/src/Makefile.am
===================================================================
--- trunk/moon/src/Makefile.am  2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/Makefile.am  2008-02-05 20:22:12 UTC (rev 94941)
@@ -29,7 +29,6 @@
        font.h          \
        frameworkelement.h \
        fullscreen.h    \
-       garray-ext.h    \
        geometry.h      \
        list.h          \
        mango.h         \
@@ -54,6 +53,7 @@
        type.h          \
        uielement.h     \
        uri.h           \
+       utils.h         \
        value.h         \
        visual.h        \
        xaml.h          \
@@ -83,7 +83,6 @@
        eventargs.cpp   \
        font.cpp        \
        frameworkelement.cpp    \
-       garray-ext.cpp  \
        geometry.cpp    \
        list.cpp        \
        mango.cpp       \
@@ -108,6 +107,7 @@
        type.cpp        \
        uielement.cpp   \
        uri.cpp         \
+       utils.cpp       \
        value.cpp       \
        visual.cpp      \
        xaml.cpp        \

Modified: trunk/moon/src/animation.cpp
===================================================================
--- trunk/moon/src/animation.cpp        2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/animation.cpp        2008-02-05 20:22:12 UTC (rev 94941)
@@ -18,10 +18,10 @@
 #include <stdlib.h>
 #include <math.h>
 
-#include "garray-ext.h"
 #include "animation.h"
 #include "color.h"
 #include "runtime.h"
+#include "utils.h"
 
 #define LERP(f,t,p) ((f) + ((t) - (f)) * (p))
 

Modified: trunk/moon/src/collection.cpp
===================================================================
--- trunk/moon/src/collection.cpp       2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/collection.cpp       2008-02-05 20:22:12 UTC (rev 94941)
@@ -11,7 +11,6 @@
 
 #include <gtk/gtk.h>
 
-#include "garray-ext.h"
 #include "collection.h"
 #include "panel.h"
 #include "geometry.h"
@@ -19,6 +18,7 @@
 #include "transform.h"
 #include "trigger.h"
 #include "namescope.h"
+#include "utils.h"
 
 Collection::Node::Node (DependencyObject *dob, DependencyObject *parent)
 {

Modified: trunk/moon/src/collection.h
===================================================================
--- trunk/moon/src/collection.h 2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/collection.h 2008-02-05 20:22:12 UTC (rev 94941)
@@ -11,6 +11,7 @@
 #define __MOON_COLLECTION_H__
 
 #include "dependencyobject.h"
+#include "utils.h"
 #include "list.h"
 
 //

Modified: trunk/moon/src/downloader.cpp
===================================================================
--- trunk/moon/src/downloader.cpp       2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/downloader.cpp       2008-02-05 20:22:12 UTC (rev 94941)
@@ -33,18 +33,23 @@
  * See the LICENSE file included with the distribution for details.
  * 
  */
+
+
 #include <config.h>
-#include <string.h>
+
 #include <gtk/gtk.h>
-#include <malloc.h>
-#include <glib.h>
+
 #include <stdlib.h>
+#include <string.h>
+#include <malloc.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
+
 #include "downloader.h"
 #include "zip/unzip.h"
+#include "utils.h"
 
 //
 // Downloader
@@ -151,74 +156,43 @@
 char *
 Downloader::ll_downloader_get_response_file (const char *PartName)
 {
-       char buffer[32 * 1024];
        char *tmpname = NULL;
-       FILE *fp;
+       unzFile zipfile;
        int fd;
        
        if (filename == NULL)
                return NULL;
-
+       
        // Null or empty, get the original file.
        if (PartName == NULL || *PartName == 0)
                return g_strdup (filename);
-
-       //
-       // Open zip file
-       //
-       unzFile zipfile = unzOpen (filename);
-       if (zipfile == NULL)
+       
+       // open the zip archive...
+       if (!(zipfile = unzOpen (filename)))
                return NULL;
-
-       if (unzLocateFile (zipfile, PartName, 0) != UNZ_OK)
-               goto leave;
-
-       if (unzOpenCurrentFile (zipfile) != UNZ_OK)
-               goto leave;
-
-       // 
-       // Create the file where the content is extracted
-       //
+       
+       // locate the file we want to extract...
+       if (unzLocateFile (zipfile, PartName, 0) != UNZ_OK) {
+               unzClose (zipfile);
+               return NULL;
+       }
+       
+       // create a tmp file...
        tmpname = g_build_filename (g_get_tmp_dir (), 
"MoonlightDownloaderStream.XXXXXX", NULL);
        if ((fd = g_mkstemp (tmpname)) == -1) {
+               unzClose (zipfile);
                g_free (tmpname);
-               tmpname = NULL;
-               goto leave1;
+               return NULL;
        }
        
-       if (!(fp = fdopen (fd, "w"))) {
+       // extract the file from the zip archive... (closes the fd on success 
and fail)
+       if (!ExtractFile (zipfile, fd)) {
+               unzClose (zipfile);
                unlink (tmpname);
                g_free (tmpname);
-               tmpname = NULL;
-               close (fd);
-               goto leave1;
+               return NULL;
        }
        
-       int n;
-       do {
-               n = unzReadCurrentFile (zipfile, buffer, sizeof (buffer));
-               if (n < 0) {
-                       unlink (tmpname);
-                       g_free (tmpname);
-                       tmpname = NULL;
-                       goto leave2;
-               }
-               
-               if (n != 0 && fwrite (buffer, n, 1, fp) != 1) {
-                       unlink (tmpname);
-                       g_free (tmpname);
-                       tmpname = NULL;
-                       goto leave2;
-               }
-       } while (n > 0);
-       
-leave2:
-       fclose (fp);
-       
-leave1:
-       unzCloseCurrentFile (zipfile);
-       
-leave:
        unzClose (zipfile);
        
        return tmpname;

Modified: trunk/moon/src/font.cpp
===================================================================
--- trunk/moon/src/font.cpp     2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/font.cpp     2008-02-05 20:22:12 UTC (rev 94941)
@@ -24,6 +24,7 @@
 
 #include "zip/unzip.h"
 #include "moon-path.h"
+#include "utils.h"
 #include "list.h"
 #include "font.h"
 
@@ -33,6 +34,14 @@
 #define d(x) x
 
 
+static const FT_Matrix invert_y = {
+        65535, 0,
+        0, -65535,
+};
+
+#define LOAD_FLAGS (FT_LOAD_NO_BITMAP | FT_LOAD_TARGET_NORMAL)
+
+
 struct GlyphBitmap {
        cairo_surface_t *surface;
        unsigned char *buffer;
@@ -342,103 +351,6 @@
 }
 
 
-static const FT_Matrix invert_y = {
-        65535, 0,
-        0, -65535,
-};
-
-
-#define LOAD_FLAGS (FT_LOAD_NO_BITMAP | FT_LOAD_TARGET_NORMAL)
-
-static bool
-mktmpname (char *buf, size_t sz, const char *base)
-{
-       char *outend = buf + (sz - 8);
-       const char *inptr = base;
-       char *outptr = buf;
-       
-       if (sz < 8)
-               return false;
-       
-       while (*inptr && outptr < outend)
-               *outptr++ = *inptr++;
-       
-       strcpy (outptr, ".XXXXXX");
-       
-       return true;
-}
-
-static bool
-ExtractFile (unzFile zip, int fd)
-{
-       int nwritten, nread;
-       char buf[4096];
-       ssize_t n;
-       
-       do {
-               n = 0;
-               if ((nread = unzReadCurrentFile (zip, buf, sizeof (buf))) > 0) {
-                       nwritten = 0;
-                       
-                       do {
-                               do {
-                                       n = write (fd, buf + nwritten, nread - 
nwritten);
-                               } while (n == -1 && errno == EINTR);
-                               
-                               if (n == -1)
-                                       break;
-                               
-                               nwritten += n;
-                       } while (nwritten < nread);
-                       
-                       if (n == -1)
-                               break;
-               }
-       } while (nread > 0);
-       
-       if (nread != 0 || n == -1 || fsync (fd) == -1) {
-               close (fd);
-               
-               return false;
-       }
-       
-       close (fd);
-       
-       return true;
-}
-
-static char *
-my_mkdtemp (char *tmpdir)
-{
-       char *path, *xxx;
-       size_t n;
-       
-       if ((n = strlen (tmpdir)) < 6)
-               return NULL;
-       
-       xxx = tmpdir + (n - 6);
-       if (strcmp (xxx, "XXXXXX") != 0)
-               return NULL;
-       
-       do {
-               if (!(path = mktemp (tmpdir)))
-                       return NULL;
-               
-               if (g_mkdir_with_parents (tmpdir, 0700) != -1)
-                       break;
-               
-               if (errno != EEXIST) {
-                       // don't bother trying again...
-                       return NULL;
-               }
-               
-               // that path already exists, try a new one...
-               strcpy (xxx, "XXXXXX");
-       } while (1);
-       
-       return tmpdir;
-}
-
 FontPackFileFace::FontPackFileFace (FontPackFile *file, FT_Face face)
 {
        style_info_parse (face->style_name, &style);
@@ -528,7 +440,7 @@
        g_string_append (packdir, name);
        g_string_append (packdir, ".XXXXXX");
        
-       if (!my_mkdtemp (packdir->str)) {
+       if (!make_tmpdir (packdir->str)) {
                g_string_free (packdir, true);
                unzClose (zip);
                return NULL;

Deleted: trunk/moon/src/garray-ext.cpp
===================================================================
--- trunk/moon/src/garray-ext.cpp       2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/garray-ext.cpp       2008-02-05 20:22:12 UTC (rev 94941)
@@ -1,88 +0,0 @@
-/*
- * garray-ext.cpp: 
- *
- * Author: Jeffrey Stedfast <[EMAIL PROTECTED]>
- *
- * Copyright 2007 Novell, Inc. (http://www.novell.com)
- *
- * See the LICENSE file included with the distribution for details.
- */
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-#include <sys/types.h>
-
-#include "garray-ext.h"
-
-
-
-/**
- * MID:
- * @lo: the low bound
- * @hi: the high bound
- *
- * Finds the midpoint between positive integer values, @lo and @hi.
- *
- * Notes: Typically expressed as '(@lo + @hi) / 2', this is incorrect
- * when @lo and @hi are sufficiently large enough that combining them
- * would overflow their integer type. To work around this, we use the
- * formula, '@lo + ((@hi - @lo) / 2)', thus preventing this problem
- * from occuring.
- *
- * Returns the midpoint between @lo and @hi (rounded down).
- **/
-#define MID(lo, hi) (lo + ((hi - lo) >> 1))
-
-
-static int
-bsearch (GPtrArray *array, bool stable, GCompareFunc cmp, void *item)
-{
-       register guint lo, hi;
-       guint m;
-       int c;
-       
-       if (array->len == 0)
-               return 0;
-       
-       lo = 0, hi = array->len;
-       
-       do {
-               m = MID (lo, hi);
-               if ((c = cmp (&item, &array->pdata[m])) > 0) {
-                       lo = m + 1;
-                       m = lo;
-               } else if (c < 0) {
-                       hi = m;
-               } else if (stable) {
-                       lo = m + 1;
-                       m = lo;
-               } else {
-                       break;
-               }
-       } while (lo < hi);
-       
-       return m;
-}
-
-void
-g_ptr_array_insert_sorted (GPtrArray *array, GCompareFunc cmp, void *item)
-{
-       guint ins = bsearch (array, true, cmp, item);
-       
-       g_ptr_array_set_size (array, array->len + 1);
-       
-       if (ins < array->len) {
-               uint8_t *dest = ((uint8_t *) array->pdata) + (sizeof (void *) * 
(ins + 1));
-               uint8_t *src = ((uint8_t *) array->pdata) + (sizeof (void *) * 
ins);
-               guint n = array->len - ins - 1;
-               
-               g_memmove (dest, src, (sizeof (void *) * n));
-       }
-       
-       array->pdata[ins] = item;
-}

Deleted: trunk/moon/src/garray-ext.h
===================================================================
--- trunk/moon/src/garray-ext.h 2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/garray-ext.h 2008-02-05 20:22:12 UTC (rev 94941)
@@ -1,23 +0,0 @@
-/*
- * garray-ext.h: 
- *
- * Author: Jeffrey Stedfast <[EMAIL PROTECTED]>
- *
- * Copyright 2007 Novell, Inc. (http://www.novell.com)
- *
- * See the LICENSE file included with the distribution for details.
- */
-
-
-#ifndef __MOON_GARRAY_EXT_H__
-#define __MOON_GARRAY_EXT_H__
-
-#include <glib.h>
-
-G_BEGIN_DECLS
-
-void g_ptr_array_insert_sorted (GPtrArray *array, GCompareFunc cmp, void 
*item);
-
-G_END_DECLS
-
-#endif /* __MOON_GARRAY_EXT_H__ */

Modified: trunk/moon/src/runtime.cpp
===================================================================
--- trunk/moon/src/runtime.cpp  2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/runtime.cpp  2008-02-05 20:22:12 UTC (rev 94941)
@@ -47,7 +47,7 @@
 #include "xaml.h"
 #include "dirty.h"
 #include "fullscreen.h"
-#include "garray-ext.h"
+#include "utils.h"
 
 
 //#define DEBUG_INVALIDATE 1

Added: trunk/moon/src/utils.cpp
===================================================================
--- trunk/moon/src/utils.cpp    2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/utils.cpp    2008-02-05 20:22:12 UTC (rev 94941)
@@ -0,0 +1,169 @@
+/*
+ * utils.cpp: 
+ *
+ * Author: Jeffrey Stedfast <[EMAIL PROTECTED]>
+ *
+ * Copyright 2007 Novell, Inc. (http://www.novell.com)
+ *
+ * See the LICENSE file included with the distribution for details.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "utils.h"
+
+
+
+/**
+ * MID:
+ * @lo: the low bound
+ * @hi: the high bound
+ *
+ * Finds the midpoint between positive integer values, @lo and @hi.
+ *
+ * Notes: Typically expressed as '(@lo + @hi) / 2', this is incorrect
+ * when @lo and @hi are sufficiently large enough that combining them
+ * would overflow their integer type. To work around this, we use the
+ * formula, '@lo + ((@hi - @lo) / 2)', thus preventing this problem
+ * from occuring.
+ *
+ * Returns the midpoint between @lo and @hi (rounded down).
+ **/
+#define MID(lo, hi) (lo + ((hi - lo) >> 1))
+
+
+static int
+bsearch (GPtrArray *array, bool stable, GCompareFunc cmp, void *item)
+{
+       register guint lo, hi;
+       guint m;
+       int c;
+       
+       if (array->len == 0)
+               return 0;
+       
+       lo = 0, hi = array->len;
+       
+       do {
+               m = MID (lo, hi);
+               if ((c = cmp (&item, &array->pdata[m])) > 0) {
+                       lo = m + 1;
+                       m = lo;
+               } else if (c < 0) {
+                       hi = m;
+               } else if (stable) {
+                       lo = m + 1;
+                       m = lo;
+               } else {
+                       break;
+               }
+       } while (lo < hi);
+       
+       return m;
+}
+
+void
+g_ptr_array_insert_sorted (GPtrArray *array, GCompareFunc cmp, void *item)
+{
+       guint ins = bsearch (array, true, cmp, item);
+       
+       g_ptr_array_set_size (array, array->len + 1);
+       
+       if (ins < array->len) {
+               uint8_t *dest = ((uint8_t *) array->pdata) + (sizeof (void *) * 
(ins + 1));
+               uint8_t *src = ((uint8_t *) array->pdata) + (sizeof (void *) * 
ins);
+               guint n = array->len - ins - 1;
+               
+               g_memmove (dest, src, (sizeof (void *) * n));
+       }
+       
+       array->pdata[ins] = item;
+}
+
+
+bool
+ExtractFile (unzFile zip, int fd)
+{
+       int nwritten, nread;
+       char buf[4096];
+       ssize_t n;
+       
+       do {
+               n = 0;
+               if ((nread = unzReadCurrentFile (zip, buf, sizeof (buf))) > 0) {
+                       nwritten = 0;
+                       
+                       do {
+                               do {
+                                       n = write (fd, buf + nwritten, nread - 
nwritten);
+                               } while (n == -1 && errno == EINTR);
+                               
+                               if (n == -1)
+                                       break;
+                               
+                               nwritten += n;
+                       } while (nwritten < nread);
+                       
+                       if (n == -1)
+                               break;
+               }
+       } while (nread > 0);
+       
+       if (nread != 0 || n == -1 || fsync (fd) == -1) {
+               close (fd);
+               
+               return false;
+       }
+       
+       close (fd);
+       
+       return true;
+}
+
+
+char *
+make_tmpdir (char *tmpdir)
+{
+       char *path, *xxx;
+       size_t n;
+       
+       if ((n = strlen (tmpdir)) < 6)
+               return NULL;
+       
+       xxx = tmpdir + (n - 6);
+       if (strcmp (xxx, "XXXXXX") != 0)
+               return NULL;
+       
+       do {
+               if (!(path = mktemp (tmpdir)))
+                       return NULL;
+               
+               if (g_mkdir_with_parents (tmpdir, 0700) != -1)
+                       return tmpdir;
+               
+               if (errno != EEXIST) {
+                       // don't bother trying again...
+                       return NULL;
+               }
+               
+               // that path already exists, try a new one...
+               strcpy (xxx, "XXXXXX");
+       } while (1);
+       
+       return NULL;
+}

Added: trunk/moon/src/utils.h
===================================================================
--- trunk/moon/src/utils.h      2008-02-05 20:17:21 UTC (rev 94940)
+++ trunk/moon/src/utils.h      2008-02-05 20:22:12 UTC (rev 94941)
@@ -0,0 +1,29 @@
+/*
+ * utils.h: 
+ *
+ * Author: Jeffrey Stedfast <[EMAIL PROTECTED]>
+ *
+ * Copyright 2007 Novell, Inc. (http://www.novell.com)
+ *
+ * See the LICENSE file included with the distribution for details.
+ */
+
+
+#ifndef __MOON_GARRAY_EXT_H__
+#define __MOON_GARRAY_EXT_H__
+
+#include <glib.h>
+
+#include "zip/unzip.h"
+
+G_BEGIN_DECLS
+
+void g_ptr_array_insert_sorted (GPtrArray *array, GCompareFunc cmp, void 
*item);
+
+bool ExtractFile (unzFile zip, int fd);
+
+char *make_tmpdir (char *tmpdir);
+
+G_END_DECLS
+
+#endif /* __MOON_GARRAY_EXT_H__ */

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to