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"
37 static void g_dummy_file_file_iface_init (GFileIface *iface);
43 int port; /* -1 => not in uri */
51 GObject parent_instance;
53 GDecodedUri *decoded_uri;
57 #define g_dummy_file_get_type _g_dummy_file_get_type
58 G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
59 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
60 g_dummy_file_file_iface_init))
62 #define SUB_DELIM_CHARS "!$&'()*+,;="
64 static char * _g_encode_uri (GDecodedUri *decoded);
65 static void _g_decoded_uri_free (GDecodedUri *decoded);
66 static GDecodedUri *_g_decode_uri (const char *uri);
67 static GDecodedUri *_g_decoded_uri_new (void);
69 static char * unescape_string (const gchar *escaped_string,
70 const gchar *escaped_string_end,
71 const gchar *illegal_characters);
73 static void g_string_append_encoded (GString *string,
75 const char *reserved_chars_allowed);
78 g_dummy_file_finalize (GObject *object)
82 dummy = G_DUMMY_FILE (object);
84 if (dummy->decoded_uri)
85 _g_decoded_uri_free (dummy->decoded_uri);
87 g_free (dummy->text_uri);
89 if (G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize)
90 (*G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize) (object);
94 g_dummy_file_class_init (GDummyFileClass *klass)
96 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98 gobject_class->finalize = g_dummy_file_finalize;
102 g_dummy_file_init (GDummyFile *dummy)
108 * @uri: Universal Resource Identifier for the dummy file object.
110 * Returns: a new #GFile.
113 _g_dummy_file_new (const char *uri)
117 g_return_val_if_fail (uri != NULL, NULL);
119 dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
120 dummy->text_uri = g_strdup (uri);
121 dummy->decoded_uri = _g_decode_uri (uri);
123 return G_FILE (dummy);
127 g_dummy_file_is_native (GFile *file)
133 g_dummy_file_get_basename (GFile *file)
135 GDummyFile *dummy = G_DUMMY_FILE (file);
137 if (dummy->decoded_uri)
138 return g_path_get_basename (dummy->decoded_uri->path);
139 return g_strdup (dummy->text_uri);
143 g_dummy_file_get_path (GFile *file)
145 GDummyFile *dummy = G_DUMMY_FILE (file);
147 if (dummy->decoded_uri)
148 return g_strdup (dummy->decoded_uri->path);
153 g_dummy_file_get_uri (GFile *file)
155 return g_strdup (G_DUMMY_FILE (file)->text_uri);
159 g_dummy_file_get_parse_name (GFile *file)
161 return g_strdup (G_DUMMY_FILE (file)->text_uri);
165 g_dummy_file_get_parent (GFile *file)
167 GDummyFile *dummy = G_DUMMY_FILE (file);
171 GDecodedUri new_decoded_uri;
173 if (dummy->decoded_uri == NULL)
176 dirname = g_path_get_dirname (dummy->decoded_uri->path);
178 if (strcmp (dirname, ".") == 0)
184 new_decoded_uri = *dummy->decoded_uri;
185 new_decoded_uri.path = dirname;
186 uri = _g_encode_uri (&new_decoded_uri);
189 parent = _g_dummy_file_new (uri);
196 g_dummy_file_dup (GFile *file)
198 GDummyFile *dummy = G_DUMMY_FILE (file);
200 return _g_dummy_file_new (dummy->text_uri);
204 g_dummy_file_hash (GFile *file)
206 GDummyFile *dummy = G_DUMMY_FILE (file);
208 return g_str_hash (dummy->text_uri);
212 g_dummy_file_equal (GFile *file1,
215 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
216 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
218 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
222 safe_strcmp (const char *a,
230 return strcmp (a, b);
234 uri_same_except_path (GDecodedUri *a,
237 if (safe_strcmp (a->scheme, b->scheme) != 0)
239 if (safe_strcmp (a->userinfo, b->userinfo) != 0)
241 if (safe_strcmp (a->host, b->host) != 0)
243 if (a->port != b->port)
250 match_prefix (const char *path,
255 prefix_len = strlen (prefix);
256 if (strncmp (path, prefix, prefix_len) != 0)
258 return path + prefix_len;
262 g_dummy_file_contains_file (GFile *parent,
265 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
266 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
267 const char *remainder;
269 if (parent_dummy->decoded_uri != NULL &&
270 descendant_dummy->decoded_uri != NULL)
272 if (uri_same_except_path (parent_dummy->decoded_uri,
273 descendant_dummy->decoded_uri)) {
274 remainder = match_prefix (descendant_dummy->decoded_uri->path,
275 parent_dummy->decoded_uri->path);
276 if (remainder != NULL && *remainder == '/')
278 while (*remainder == '/')
287 remainder = match_prefix (descendant_dummy->text_uri,
288 parent_dummy->text_uri);
289 if (remainder != NULL && *remainder == '/')
291 while (*remainder == '/')
302 g_dummy_file_get_relative_path (GFile *parent,
305 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
306 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
307 const char *remainder;
309 if (parent_dummy->decoded_uri != NULL &&
310 descendant_dummy->decoded_uri != NULL)
312 if (uri_same_except_path (parent_dummy->decoded_uri,
313 descendant_dummy->decoded_uri)) {
314 remainder = match_prefix (descendant_dummy->decoded_uri->path,
315 parent_dummy->decoded_uri->path);
316 if (remainder != NULL && *remainder == '/')
318 while (*remainder == '/')
321 return g_strdup (remainder);
327 remainder = match_prefix (descendant_dummy->text_uri,
328 parent_dummy->text_uri);
329 if (remainder != NULL && *remainder == '/')
331 while (*remainder == '/')
334 return unescape_string (remainder, NULL, "/");
343 g_dummy_file_resolve_relative_path (GFile *file,
344 const char *relative_path)
346 GDummyFile *dummy = G_DUMMY_FILE (file);
349 GDecodedUri new_decoded_uri;
352 if (dummy->decoded_uri == NULL)
354 str = g_string_new (dummy->text_uri);
355 g_string_append (str, "/");
356 g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
357 child = _g_dummy_file_new (str->str);
358 g_string_free (str, TRUE);
362 new_decoded_uri = *dummy->decoded_uri;
364 if (g_path_is_absolute (relative_path))
365 new_decoded_uri.path = g_strdup (relative_path);
367 new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
369 uri = _g_encode_uri (&new_decoded_uri);
370 g_free (new_decoded_uri.path);
372 child = _g_dummy_file_new (uri);
380 g_dummy_file_get_child_for_display_name (GFile *file,
381 const char *display_name,
384 return g_file_get_child (file, display_name);
388 g_dummy_file_has_uri_scheme (GFile *file,
389 const char *uri_scheme)
391 GDummyFile *dummy = G_DUMMY_FILE (file);
393 if (dummy->decoded_uri)
394 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
399 g_dummy_file_get_uri_scheme (GFile *file)
401 GDummyFile *dummy = G_DUMMY_FILE (file);
403 if (dummy->decoded_uri)
404 return g_strdup (dummy->decoded_uri->scheme);
411 g_dummy_file_file_iface_init (GFileIface *iface)
413 iface->dup = g_dummy_file_dup;
414 iface->hash = g_dummy_file_hash;
415 iface->equal = g_dummy_file_equal;
416 iface->is_native = g_dummy_file_is_native;
417 iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
418 iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
419 iface->get_basename = g_dummy_file_get_basename;
420 iface->get_path = g_dummy_file_get_path;
421 iface->get_uri = g_dummy_file_get_uri;
422 iface->get_parse_name = g_dummy_file_get_parse_name;
423 iface->get_parent = g_dummy_file_get_parent;
424 iface->contains_file = g_dummy_file_contains_file;
425 iface->get_relative_path = g_dummy_file_get_relative_path;
426 iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
427 iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
430 /* Uri handling helper functions: */
433 unescape_character (const char *scanner)
438 first_digit = g_ascii_xdigit_value (*scanner++);
442 second_digit = g_ascii_xdigit_value (*scanner++);
443 if (second_digit < 0)
446 return (first_digit << 4) | second_digit;
450 unescape_string (const gchar *escaped_string,
451 const gchar *escaped_string_end,
452 const gchar *illegal_characters)
458 if (escaped_string == NULL)
461 if (escaped_string_end == NULL)
462 escaped_string_end = escaped_string + strlen (escaped_string);
464 result = g_malloc (escaped_string_end - escaped_string + 1);
467 for (in = escaped_string; in < escaped_string_end; in++) {
471 if (escaped_string_end - in < 2)
477 character = unescape_character (in);
479 /* Check for an illegal character. We consider '\0' illegal here. */
480 if (character <= 0 ||
481 (illegal_characters != NULL &&
482 strchr (illegal_characters, (char)character) != NULL))
487 in++; /* The other char will be eaten in the loop header */
489 *out++ = (char)character;
493 g_assert (out - result <= strlen (escaped_string));
498 _g_decoded_uri_free (GDecodedUri *decoded)
503 g_free (decoded->scheme);
504 g_free (decoded->query);
505 g_free (decoded->fragment);
506 g_free (decoded->userinfo);
507 g_free (decoded->host);
508 g_free (decoded->path);
513 _g_decoded_uri_new (void)
517 uri = g_new0 (GDecodedUri, 1);
524 _g_decode_uri (const char *uri)
526 GDecodedUri *decoded;
527 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
531 /* From RFC 3986 Decodes:
532 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
538 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
541 if (!g_ascii_isalpha (*p))
551 if (!(g_ascii_isalnum(c) ||
558 decoded = _g_decoded_uri_new ();
560 decoded->scheme = g_malloc (p - uri);
561 out = decoded->scheme;
562 for (in = uri; in < p - 1; in++)
563 *out++ = g_ascii_tolower (*in);
568 query_start = strchr (p, '?');
571 hier_part_end = query_start++;
572 fragment_start = strchr (query_start, '#');
575 decoded->query = g_strndup (query_start, fragment_start - query_start);
576 decoded->fragment = g_strdup (fragment_start+1);
580 decoded->query = g_strdup (query_start);
581 decoded->fragment = NULL;
587 decoded->query = NULL;
588 fragment_start = strchr (p, '#');
591 hier_part_end = fragment_start++;
592 decoded->fragment = g_strdup (fragment_start);
596 hier_part_end = p + strlen (p);
597 decoded->fragment = NULL;
602 hier-part = "//" authority path-abempty
609 if (hier_part_start[0] == '/' &&
610 hier_part_start[1] == '/')
612 const char *authority_start, *authority_end;
613 const char *userinfo_start, *userinfo_end;
614 const char *host_start, *host_end;
615 const char *port_start;
617 authority_start = hier_part_start + 2;
618 /* authority is always followed by / or nothing */
619 authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
620 if (authority_end == NULL)
621 authority_end = hier_part_end;
624 authority = [ userinfo "@" ] host [ ":" port ]
627 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
630 userinfo_start = authority_start;
631 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
632 if (decoded->userinfo == NULL)
634 _g_decoded_uri_free (decoded);
637 host_start = userinfo_end + 1;
640 host_start = authority_start;
642 port_start = memchr (host_start, ':', authority_end - host_start);
645 host_end = port_start++;
647 decoded->port = atoi(port_start);
651 host_end = authority_end;
655 decoded->host = g_strndup (host_start, host_end - host_start);
657 hier_part_start = authority_end;
660 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
662 if (decoded->path == NULL)
664 _g_decoded_uri_free (decoded);
672 is_valid (char c, const char *reserved_chars_allowed)
674 if (g_ascii_isalnum (c) ||
681 if (reserved_chars_allowed &&
682 strchr (reserved_chars_allowed, c) != NULL)
689 g_string_append_encoded (GString *string,
691 const char *reserved_chars_allowed)
695 static const gchar hex[16] = "0123456789ABCDEF";
697 end = encoded + strlen (encoded);
699 while ((c = *encoded) != 0)
701 if (is_valid (c, reserved_chars_allowed))
703 g_string_append_c (string, c);
708 g_string_append_c (string, '%');
709 g_string_append_c (string, hex[((guchar)c) >> 4]);
710 g_string_append_c (string, hex[((guchar)c) & 0xf]);
717 _g_encode_uri (GDecodedUri *decoded)
721 uri = g_string_new (NULL);
723 g_string_append (uri, decoded->scheme);
724 g_string_append (uri, "://");
726 if (decoded->host != NULL)
728 if (decoded->userinfo)
730 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
731 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
732 g_string_append_c (uri, '@');
735 g_string_append (uri, decoded->host);
737 if (decoded->port != -1)
739 g_string_append_c (uri, ':');
740 g_string_append_printf (uri, "%d", decoded->port);
744 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
748 g_string_append_c (uri, '?');
749 g_string_append (uri, decoded->query);
752 if (decoded->fragment)
754 g_string_append_c (uri, '#');
755 g_string_append (uri, decoded->fragment);
758 return g_string_free (uri, FALSE);