From 82088320c652f34543c82b790adf82019320d5a4 Mon Sep 17 00:00:00 2001 From: Claudio Saavedra Date: Wed, 8 Jun 2011 14:28:10 +0300 Subject: Add ephy_string_commandline_args_to_uris() to ephy string utilities This is the first patch in an attempt to simplify epiphany's main() method, which is quite bulky by now. https://bugzilla.gnome.org/show_bug.cgi?id=652119 --- lib/ephy-string.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'lib/ephy-string.c') diff --git a/lib/ephy-string.c b/lib/ephy-string.c index 0b07dc794..72aa582be 100644 --- a/lib/ephy-string.c +++ b/lib/ephy-string.c @@ -497,3 +497,74 @@ ephy_string_expand_initial_tilde (const char *path) slash_after_user_name, NULL); } + +/** + * ephy_string_commandline_args_to_uris: + * @arguments: a %NULL-terminated array of chars. + * + * Transform commandline arguments to URIs if they exist, + * otherwise simply transform them to UTF-8. + * + * Returns: a newly allocated array with the URIs and + * UTF-8 strings. + **/ +char ** +ephy_string_commandline_args_to_uris (char **arguments, GError **error) +{ + gchar **args; + guint i; + + if (arguments == NULL) + return NULL; + + args = g_malloc0 (sizeof (gchar *) * (g_strv_length (arguments) + 1)); + + for (i = 0; arguments[i] != NULL; ++i) + { + char *uri, *path; +#ifdef PATH_MAX + char rpath[PATH_MAX]; +#else + char *rpath = NULL; +#endif + + path = realpath (arguments[i], rpath); + if (path != NULL) + { + uri = g_locale_to_utf8 (path, -1, + NULL, NULL, error); +#ifndef PATH_MAX + free (path); +#endif + } + else + { + uri = g_locale_to_utf8 (arguments[i], -1, + NULL, NULL, error); + } + + if (uri != NULL) + { + /* If it's a file, use g_file_new_for_commandline_arg, + * so we get the right escaping. + */ + if (path != NULL) + { + GFile *file; + file = g_file_new_for_commandline_arg (uri); + args[i] = g_file_get_uri (file); + g_object_unref (file); + g_free (uri); + } + else + { + args[i] = uri; + } + } else { + g_strfreev (args); + return NULL; + } + } + + return args; +} -- cgit v1.2.3