1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
33 #include "gdummyfile.h"
35 static void g_dummy_file_file_iface_init (GFileIface *iface);
41 int port; /* -1 => not in uri */
49 GObject parent_instance;
51 GDecodedUri *decoded_uri;
55 G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
56 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
57 g_dummy_file_file_iface_init))
59 #define SUB_DELIM_CHARS "!$&'()*+,;="
61 static char * _g_encode_uri (GDecodedUri *decoded);
62 static void _g_decoded_uri_free (GDecodedUri *decoded);
63 static GDecodedUri *_g_decode_uri (const char *uri);
64 static GDecodedUri *_g_decoded_uri_new (void);
66 static char * unescape_string (const gchar *escaped_string,
67 const gchar *escaped_string_end,
68 const gchar *illegal_characters);
70 static void g_string_append_encoded (GString *string, const char *encoded,
71 const char *reserved_chars_allowed);
74 g_dummy_file_finalize (GObject *object)
78 dummy = G_DUMMY_FILE (object);
80 if (dummy->decoded_uri)
81 _g_decoded_uri_free (dummy->decoded_uri);
83 g_free (dummy->text_uri);
85 if (G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize)
86 (*G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize) (object);
90 g_dummy_file_class_init (GDummyFileClass *klass)
92 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94 gobject_class->finalize = g_dummy_file_finalize;
98 g_dummy_file_init (GDummyFile *dummy)
104 * @uri: Universal Resource Identifier for the dummy file object.
106 * Returns: a new #GFile.
109 g_dummy_file_new (const char *uri)
113 g_return_val_if_fail (uri != NULL, NULL);
115 dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
116 dummy->text_uri = g_strdup (uri);
117 dummy->decoded_uri = _g_decode_uri (uri);
119 return G_FILE (dummy);
123 g_dummy_file_is_native (GFile *file)
129 g_dummy_file_get_basename (GFile *file)
131 GDummyFile *dummy = G_DUMMY_FILE (file);
133 if (dummy->decoded_uri)
134 return g_path_get_basename (dummy->decoded_uri->path);
135 return g_strdup (dummy->text_uri);
139 g_dummy_file_get_path (GFile *file)
141 GDummyFile *dummy = G_DUMMY_FILE (file);
143 if (dummy->decoded_uri)
144 return g_strdup (dummy->decoded_uri->path);
149 g_dummy_file_get_uri (GFile *file)
151 return g_strdup (G_DUMMY_FILE (file)->text_uri);
155 g_dummy_file_get_parse_name (GFile *file)
157 return g_strdup (G_DUMMY_FILE (file)->text_uri);
161 g_dummy_file_get_parent (GFile *file)
163 GDummyFile *dummy = G_DUMMY_FILE (file);
167 GDecodedUri new_decoded_uri;
169 if (dummy->decoded_uri == NULL)
172 dirname = g_path_get_dirname (dummy->decoded_uri->path);
174 if (strcmp (dirname, ".") == 0)
180 new_decoded_uri = *dummy->decoded_uri;
181 new_decoded_uri.path = dirname;
182 uri = _g_encode_uri (&new_decoded_uri);
185 parent = g_dummy_file_new (uri);
192 g_dummy_file_dup (GFile *file)
194 GDummyFile *dummy = G_DUMMY_FILE (file);
196 return g_dummy_file_new (dummy->text_uri);
200 g_dummy_file_hash (GFile *file)
202 GDummyFile *dummy = G_DUMMY_FILE (file);
204 return g_str_hash (dummy->text_uri);
208 g_dummy_file_equal (GFile *file1,
211 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
212 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
214 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
218 safe_strcmp (const char *a, const char *b)
225 return strcmp (a, b);
229 uri_same_except_path (GDecodedUri *a,
232 if (safe_strcmp (a->scheme, b->scheme) != 0)
234 if (safe_strcmp (a->userinfo, b->userinfo) != 0)
236 if (safe_strcmp (a->host, b->host) != 0)
238 if (a->port != b->port)
245 match_prefix (const char *path, const char *prefix)
249 prefix_len = strlen (prefix);
250 if (strncmp (path, prefix, prefix_len) != 0)
252 return path + prefix_len;
256 g_dummy_file_contains_file (GFile *parent,
259 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
260 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
261 const char *remainder;
263 if (parent_dummy->decoded_uri != NULL &&
264 descendant_dummy->decoded_uri != NULL)
266 if (uri_same_except_path (parent_dummy->decoded_uri,
267 descendant_dummy->decoded_uri)) {
268 remainder = match_prefix (descendant_dummy->decoded_uri->path,
269 parent_dummy->decoded_uri->path);
270 if (remainder != NULL && *remainder == '/')
272 while (*remainder == '/')
281 remainder = match_prefix (descendant_dummy->text_uri,
282 parent_dummy->text_uri);
283 if (remainder != NULL && *remainder == '/')
285 while (*remainder == '/')
296 g_dummy_file_get_relative_path (GFile *parent,
299 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
300 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
301 const char *remainder;
303 if (parent_dummy->decoded_uri != NULL &&
304 descendant_dummy->decoded_uri != NULL)
306 if (uri_same_except_path (parent_dummy->decoded_uri,
307 descendant_dummy->decoded_uri)) {
308 remainder = match_prefix (descendant_dummy->decoded_uri->path,
309 parent_dummy->decoded_uri->path);
310 if (remainder != NULL && *remainder == '/')
312 while (*remainder == '/')
315 return g_strdup (remainder);
321 remainder = match_prefix (descendant_dummy->text_uri,
322 parent_dummy->text_uri);
323 if (remainder != NULL && *remainder == '/')
325 while (*remainder == '/')
328 return unescape_string (remainder, NULL, "/");
337 g_dummy_file_resolve_relative_path (GFile *file,
338 const char *relative_path)
340 GDummyFile *dummy = G_DUMMY_FILE (file);
343 GDecodedUri new_decoded_uri;
346 if (dummy->decoded_uri == NULL)
348 str = g_string_new (dummy->text_uri);
349 g_string_append (str, "/");
350 g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
351 child = g_dummy_file_new (str->str);
352 g_string_free (str, TRUE);
356 new_decoded_uri = *dummy->decoded_uri;
358 if (g_path_is_absolute (relative_path))
359 new_decoded_uri.path = g_strdup (relative_path);
361 new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
363 uri = _g_encode_uri (&new_decoded_uri);
364 g_free (new_decoded_uri.path);
366 child = g_dummy_file_new (uri);
374 g_dummy_file_get_child_for_display_name (GFile *file,
375 const char *display_name,
378 return g_file_get_child (file, display_name);
382 g_dummy_file_has_uri_scheme (GFile *file,
383 const char *uri_scheme)
385 GDummyFile *dummy = G_DUMMY_FILE (file);
387 if (dummy->decoded_uri)
388 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
393 g_dummy_file_get_uri_scheme (GFile *file)
395 GDummyFile *dummy = G_DUMMY_FILE (file);
397 if (dummy->decoded_uri)
398 return g_strdup (dummy->decoded_uri->scheme);
405 g_dummy_file_file_iface_init (GFileIface *iface)
407 iface->dup = g_dummy_file_dup;
408 iface->hash = g_dummy_file_hash;
409 iface->equal = g_dummy_file_equal;
410 iface->is_native = g_dummy_file_is_native;
411 iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
412 iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
413 iface->get_basename = g_dummy_file_get_basename;
414 iface->get_path = g_dummy_file_get_path;
415 iface->get_uri = g_dummy_file_get_uri;
416 iface->get_parse_name = g_dummy_file_get_parse_name;
417 iface->get_parent = g_dummy_file_get_parent;
418 iface->contains_file = g_dummy_file_contains_file;
419 iface->get_relative_path = g_dummy_file_get_relative_path;
420 iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
421 iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
424 /* Uri handling helper functions: */
427 unescape_character (const char *scanner)
432 first_digit = g_ascii_xdigit_value (*scanner++);
436 second_digit = g_ascii_xdigit_value (*scanner++);
437 if (second_digit < 0)
440 return (first_digit << 4) | second_digit;
444 unescape_string (const gchar *escaped_string,
445 const gchar *escaped_string_end,
446 const gchar *illegal_characters)
452 if (escaped_string == NULL)
455 if (escaped_string_end == NULL)
456 escaped_string_end = escaped_string + strlen (escaped_string);
458 result = g_malloc (escaped_string_end - escaped_string + 1);
461 for (in = escaped_string; in < escaped_string_end; in++) {
465 if (escaped_string_end - in < 2)
471 character = unescape_character (in);
473 /* Check for an illegal character. We consider '\0' illegal here. */
474 if (character <= 0 ||
475 (illegal_characters != NULL &&
476 strchr (illegal_characters, (char)character) != NULL))
481 in++; /* The other char will be eaten in the loop header */
483 *out++ = (char)character;
487 g_assert (out - result <= strlen (escaped_string));
492 _g_decoded_uri_free (GDecodedUri *decoded)
497 g_free (decoded->scheme);
498 g_free (decoded->query);
499 g_free (decoded->fragment);
500 g_free (decoded->userinfo);
501 g_free (decoded->host);
502 g_free (decoded->path);
507 _g_decoded_uri_new (void)
511 uri = g_new0 (GDecodedUri, 1);
518 _g_decode_uri (const char *uri)
520 GDecodedUri *decoded;
521 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
525 /* From RFC 3986 Decodes:
526 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
532 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
535 if (!g_ascii_isalpha (*p))
545 if (!(g_ascii_isalnum(c) ||
552 decoded = _g_decoded_uri_new ();
554 decoded->scheme = g_malloc (p - uri);
555 out = decoded->scheme;
556 for (in = uri; in < p - 1; in++)
557 *out++ = g_ascii_tolower (*in);
562 query_start = strchr (p, '?');
565 hier_part_end = query_start++;
566 fragment_start = strchr (query_start, '#');
569 decoded->query = g_strndup (query_start, fragment_start - query_start);
570 decoded->fragment = g_strdup (fragment_start+1);
574 decoded->query = g_strdup (query_start);
575 decoded->fragment = NULL;
581 decoded->query = NULL;
582 fragment_start = strchr (p, '#');
585 hier_part_end = fragment_start++;
586 decoded->fragment = g_strdup (fragment_start);
590 hier_part_end = p + strlen (p);
591 decoded->fragment = NULL;
596 hier-part = "//" authority path-abempty
603 if (hier_part_start[0] == '/' &&
604 hier_part_start[1] == '/')
606 const char *authority_start, *authority_end;
607 const char *userinfo_start, *userinfo_end;
608 const char *host_start, *host_end;
609 const char *port_start;
611 authority_start = hier_part_start + 2;
612 /* authority is always followed by / or nothing */
613 authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
614 if (authority_end == NULL)
615 authority_end = hier_part_end;
618 authority = [ userinfo "@" ] host [ ":" port ]
621 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
624 userinfo_start = authority_start;
625 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
626 if (decoded->userinfo == NULL)
628 _g_decoded_uri_free (decoded);
631 host_start = userinfo_end + 1;
634 host_start = authority_start;
636 port_start = memchr (host_start, ':', authority_end - host_start);
639 host_end = port_start++;
641 decoded->port = atoi(port_start);
645 host_end = authority_end;
649 decoded->host = g_strndup (host_start, host_end - host_start);
651 hier_part_start = authority_end;
654 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
656 if (decoded->path == NULL)
658 _g_decoded_uri_free (decoded);
666 is_valid (char c, const char *reserved_chars_allowed)
668 if (g_ascii_isalnum (c) ||
675 if (reserved_chars_allowed &&
676 strchr (reserved_chars_allowed, c) != NULL)
683 g_string_append_encoded (GString *string, const char *encoded,
684 const char *reserved_chars_allowed)
688 static const gchar hex[16] = "0123456789ABCDEF";
690 end = encoded + strlen (encoded);
692 while ((c = *encoded) != 0)
694 if (is_valid (c, reserved_chars_allowed))
696 g_string_append_c (string, c);
701 g_string_append_c (string, '%');
702 g_string_append_c (string, hex[((guchar)c) >> 4]);
703 g_string_append_c (string, hex[((guchar)c) & 0xf]);
710 _g_encode_uri (GDecodedUri *decoded)
714 uri = g_string_new (NULL);
716 g_string_append (uri, decoded->scheme);
717 g_string_append (uri, "://");
719 if (decoded->host != NULL)
721 if (decoded->userinfo)
723 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
724 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
725 g_string_append_c (uri, '@');
728 g_string_append (uri, decoded->host);
730 if (decoded->port != -1)
732 g_string_append_c (uri, ':');
733 g_string_append_printf (uri, "%d", decoded->port);
737 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
741 g_string_append_c (uri, '?');
742 g_string_append (uri, decoded->query);
745 if (decoded->fragment)
747 g_string_append_c (uri, '#');
748 g_string_append (uri, decoded->fragment);
751 return g_string_free (uri, FALSE);