From: Sebastian Harl Date: Thu, 5 Jan 2012 10:53:19 +0000 (+0100) Subject: gtk-tpdfv: Support relative pathnames as well. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;ds=sidebyside;p=tpdfview.git gtk-tpdfv: Support relative pathnames as well. --- diff --git a/src/gtk-tpdfv.c b/src/gtk-tpdfv.c index d1a377c..e17a4eb 100644 --- a/src/gtk-tpdfv.c +++ b/src/gtk-tpdfv.c @@ -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 */