[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: glib filename_to_uri docs



On the hostname bit, would the change below be right for utf8 hostnames
per the docs -- even if my wondering about non-ascii is answered by the
fact the two funcs currently reject non-ascii entirely!

(The filename.t passes with or without the xs change, it just avoids the
dreaded never-executed code ... :)

Index: Glib.xs
===================================================================
--- Glib.xs	(revision 1062)
+++ Glib.xs	(working copy)
@@ -454,8 +454,14 @@
 	if (!filename)
 		gperl_croak_gerror (NULL, error);
 	PUSHs (sv_2mortal (newSVpv (filename, 0)));
-	if (GIMME_V == G_ARRAY && hostname)
-		XPUSHs (sv_2mortal (newSVpv (hostname, 0)));
+	if (GIMME_V == G_ARRAY && hostname) {
+		/* The g_filename_from_uri() docs say hostname is utf8,
+		 * hence newSVGChar, though as of glib circa 2.16
+		 * hostname_validate() only actually allows ascii
+		 * alphanumerics, so utf8 doesn't actually come out.
+		 */
+		XPUSHs (sv_2mortal (newSVGChar (hostname)));
+	}
 	g_free (filename);
 	if (hostname) g_free (hostname);
 
@@ -469,12 +475,16 @@
 	char * hostname = NULL;
 	GError * error = NULL;
     CODE:
+	/* The g_filename_to_uri() docs say hostname is utf8, hence SvGChar,
+	 * though as of glib circa 2.16 hostname_validate() only actually
+	 * allows ascii alphanumerics, so you can't in fact pass in utf8.
+	 */
 	if (items == 2) {
 		filename = SvPV_nolen (ST (0));
-		hostname = gperl_sv_is_defined (ST (1)) ? SvPV_nolen (ST (1)) : NULL;
+		hostname = gperl_sv_is_defined (ST (1)) ? SvGChar (ST (1)) : NULL;
 	} else if (items == 3) {
 		filename = SvPV_nolen (ST (1));
-		hostname = gperl_sv_is_defined (ST (2)) ? SvPV_nolen (ST (2)) : NULL;
+		hostname = gperl_sv_is_defined (ST (2)) ? SvGChar (ST (2)) : NULL;
 	} else {
 		croak ("Usage: Glib::filename_to_uri (filename, hostname)\n"
 		       " -or-  Glib->filename_to_uri (filename, hostname)\n"
Index: t/filename.t
===================================================================
--- t/filename.t	(revision 1062)
+++ t/filename.t	(working copy)
@@ -8,7 +8,7 @@
 use strict;
 use warnings;
 use Glib qw(:functions);
-use Test::More tests => 21;
+use Test::More tests => 37;
 
 my $filename = "test";
 
@@ -40,7 +40,51 @@
 ok(!!(@info = Glib::filename_from_uri($uri)));
 ok(!!(@info = filename_from_uri($uri)));
 
+# filename_from_uri() class method
+is (Glib->filename_from_uri ('file:///foo/one.html'),
+    '/foo/one.html');
+{ my ($filename, $hostname)
+    = Glib->filename_from_uri ('file:///foo/two.html');
+  is ($filename, '/foo/two.html');
+  is ($hostname, undef);
+}
+{ my ($filename, $hostname)
+    = Glib->filename_from_uri ('file://hostname/three.html');
+  is ($filename, '/three.html');
+  is ($hostname, 'hostname');
+}
 
+# filename_from_uri() plain func
+is (Glib::filename_from_uri ('file:///foo/one.html'),
+    '/foo/one.html');
+{ my ($filename, $hostname)
+    = Glib::filename_from_uri ('file:///foo/two.html');
+  is ($filename, '/foo/two.html');
+  is ($hostname, undef);
+}
+{ my ($filename, $hostname)
+    = Glib::filename_from_uri ('file://hostname/three.html');
+  is ($filename, '/three.html');
+  is ($hostname, 'hostname');
+}
+
+# filename_to_uri() class method
+is (Glib->filename_to_uri ('/tmp/four.html', undef),
+    'file:///tmp/four.html');
+is (Glib->filename_to_uri ('/tmp/five.html', ''),
+    'file:///tmp/five.html');
+is (Glib->filename_to_uri ('/tmp/six.html', 'hostname'),
+    'file://hostname/tmp/six.html');
+
+# filename_to_uri() plain func
+is (Glib::filename_to_uri ('/tmp/four.html', undef),
+    'file:///tmp/four.html');
+is (Glib::filename_to_uri ('/tmp/five.html', ''),
+    'file:///tmp/five.html');
+is (Glib::filename_to_uri ('/tmp/six.html', 'hostname'),
+    'file://hostname/tmp/six.html');
+
+
 SKIP: {
 	skip "g_filename_display_name was added glib 2.6.0", 6
 		unless Glib->CHECK_VERSION (2, 6, 0);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]