Code

gtk-tpdfv: Support relative pathnames as well. master
authorSebastian Harl <sh@tokkee.org>
Thu, 5 Jan 2012 10:53:19 +0000 (11:53 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 5 Jan 2012 10:53:19 +0000 (11:53 +0100)
src/gtk-tpdfv.c

index d1a377ce4e44bd3d1204b259982f02ded02f533b..e17a4eb22073f252910f0ee96afe48e2b941b9fa 100644 (file)
@@ -56,7 +56,7 @@ typedef enum {
 } tpdfv_zoommode_t;
 
 typedef struct {
-       char *filename;
+       gchar *filename;
 
        time_t mtime;
 
@@ -129,24 +129,32 @@ tpdfv_close(gtk_tpdfv_t *pdf)
 static gboolean
 tpdfv_init(gtk_tpdfv_t *pdf, const char *filename)
 {
-       if (strstr(filename, "://"))
-               pdf->filename = strdup(filename);
+       GError *err = NULL;
+
+       char *scheme;
+
+       if ((! pdf) || (! filename))
+               return FALSE;
+
+       scheme = g_uri_parse_scheme(filename);
+       if (scheme)
+               filename += strlen(scheme);
+
+       if (g_path_is_absolute(filename)) {
+               pdf->filename = g_filename_to_uri(filename,
+                               /* hostname = */ NULL, &err);
+       }
        else {
-               size_t len = strlen("file://") + strlen(filename);
-               pdf->filename = (char *)malloc(len + 1);
-               if (pdf->filename) {
-                       *pdf->filename = '\0';
-                       strncat(pdf->filename, "file://", len);
-                       strncat(pdf->filename, filename, len - strlen("file://"));
-               }
+               gchar *tmp = g_build_filename(g_get_current_dir(), filename, NULL);
+               pdf->filename = g_filename_to_uri(tmp, /* hostname = */ NULL, &err);
+               g_free(tmp);
        }
 
        /* XXX: error reporting mechanism */
 
        if (! pdf->filename) {
-               char errbuf[1024];
-               strerror_r(errno, errbuf, sizeof(errbuf));
-               fprintf(stderr, "Failed to allocate string: %s.\n", errbuf);
+               if (err)
+                       fprintf(stderr, "Failed to allocate string: %s.\n", err->message);
                return FALSE;
        }
 
@@ -175,7 +183,7 @@ tpdfv_clean(gtk_tpdfv_t *pdf)
 
        tpdfv_close(pdf);
        pdf->current_page = NULL;
-       free(pdf->filename);
+       g_free(pdf->filename);
        pdf->filename = NULL;
        return;
 } /* tpdfv_clean */