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, const char *encoded,
74 const char *reserved_chars_allowed);
77 g_dummy_file_finalize (GObject *object)
81 dummy = G_DUMMY_FILE (object);
83 if (dummy->decoded_uri)
84 _g_decoded_uri_free (dummy->decoded_uri);
86 g_free (dummy->text_uri);
88 if (G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize)
89 (*G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize) (object);
93 g_dummy_file_class_init (GDummyFileClass *klass)
95 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
97 gobject_class->finalize = g_dummy_file_finalize;
101 g_dummy_file_init (GDummyFile *dummy)
107 * @uri: Universal Resource Identifier for the dummy file object.
109 * Returns: a new #GFile.
112 _g_dummy_file_new (const char *uri)
116 g_return_val_if_fail (uri != NULL, NULL);
118 dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
119 dummy->text_uri = g_strdup (uri);
120 dummy->decoded_uri = _g_decode_uri (uri);
122 return G_FILE (dummy);
126 g_dummy_file_is_native (GFile *file)
132 g_dummy_file_get_basename (GFile *file)
134 GDummyFile *dummy = G_DUMMY_FILE (file);
136 if (dummy->decoded_uri)
137 return g_path_get_basename (dummy->decoded_uri->path);
138 return g_strdup (dummy->text_uri);
142 g_dummy_file_get_path (GFile *file)
144 GDummyFile *dummy = G_DUMMY_FILE (file);
146 if (dummy->decoded_uri)
147 return g_strdup (dummy->decoded_uri->path);
152 g_dummy_file_get_uri (GFile *file)
154 return g_strdup (G_DUMMY_FILE (file)->text_uri);
158 g_dummy_file_get_parse_name (GFile *file)
160 return g_strdup (G_DUMMY_FILE (file)->text_uri);
164 g_dummy_file_get_parent (GFile *file)
166 GDummyFile *dummy = G_DUMMY_FILE (file);
170 GDecodedUri new_decoded_uri;
172 if (dummy->decoded_uri == NULL)
175 dirname = g_path_get_dirname (dummy->decoded_uri->path);
177 if (strcmp (dirname, ".") == 0)
183 new_decoded_uri = *dummy->decoded_uri;
184 new_decoded_uri.path = dirname;
185 uri = _g_encode_uri (&new_decoded_uri);
188 parent = _g_dummy_file_new (uri);
195 g_dummy_file_dup (GFile *file)
197 GDummyFile *dummy = G_DUMMY_FILE (file);
199 return _g_dummy_file_new (dummy->text_uri);
203 g_dummy_file_hash (GFile *file)
205 GDummyFile *dummy = G_DUMMY_FILE (file);
207 return g_str_hash (dummy->text_uri);
211 g_dummy_file_equal (GFile *file1,
214 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
215 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
217 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
221 safe_strcmp (const char *a, const char *b)
228 return strcmp (a, b);
232 uri_same_except_path (GDecodedUri *a,
235 if (safe_strcmp (a->scheme, b->scheme) != 0)
237 if (safe_strcmp (a->userinfo, b->userinfo) != 0)
239 if (safe_strcmp (a->host, b->host) != 0)
241 if (a->port != b->port)
248 match_prefix (const char *path, const char *prefix)
252 prefix_len = strlen (prefix);
253 if (strncmp (path, prefix, prefix_len) != 0)
255 return path + prefix_len;
259 g_dummy_file_contains_file (GFile *parent,
262 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
263 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
264 const char *remainder;
266 if (parent_dummy->decoded_uri != NULL &&
267 descendant_dummy->decoded_uri != NULL)
269 if (uri_same_except_path (parent_dummy->decoded_uri,
270 descendant_dummy->decoded_uri)) {
271 remainder = match_prefix (descendant_dummy->decoded_uri->path,
272 parent_dummy->decoded_uri->path);
273 if (remainder != NULL && *remainder == '/')
275 while (*remainder == '/')
284 remainder = match_prefix (descendant_dummy->text_uri,
285 parent_dummy->text_uri);
286 if (remainder != NULL && *remainder == '/')
288 while (*remainder == '/')
299 g_dummy_file_get_relative_path (GFile *parent,
302 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
303 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
304 const char *remainder;
306 if (parent_dummy->decoded_uri != NULL &&
307 descendant_dummy->decoded_uri != NULL)
309 if (uri_same_except_path (parent_dummy->decoded_uri,
310 descendant_dummy->decoded_uri)) {
311 remainder = match_prefix (descendant_dummy->decoded_uri->path,
312 parent_dummy->decoded_uri->path);
313 if (remainder != NULL && *remainder == '/')
315 while (*remainder == '/')
318 return g_strdup (remainder);
324 remainder = match_prefix (descendant_dummy->text_uri,
325 parent_dummy->text_uri);
326 if (remainder != NULL && *remainder == '/')
328 while (*remainder == '/')
331 return unescape_string (remainder, NULL, "/");
340 g_dummy_file_resolve_relative_path (GFile *file,
341 const char *relative_path)
343 GDummyFile *dummy = G_DUMMY_FILE (file);
346 GDecodedUri new_decoded_uri;
349 if (dummy->decoded_uri == NULL)
351 str = g_string_new (dummy->text_uri);
352 g_string_append (str, "/");
353 g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
354 child = _g_dummy_file_new (str->str);
355 g_string_free (str, TRUE);
359 new_decoded_uri = *dummy->decoded_uri;
361 if (g_path_is_absolute (relative_path))
362 new_decoded_uri.path = g_strdup (relative_path);
364 new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
366 uri = _g_encode_uri (&new_decoded_uri);
367 g_free (new_decoded_uri.path);
369 child = _g_dummy_file_new (uri);
377 g_dummy_file_get_child_for_display_name (GFile *file,
378 const char *display_name,
381 return g_file_get_child (file, display_name);
385 g_dummy_file_has_uri_scheme (GFile *file,
386 const char *uri_scheme)
388 GDummyFile *dummy = G_DUMMY_FILE (file);
390 if (dummy->decoded_uri)
391 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
396 g_dummy_file_get_uri_scheme (GFile *file)
398 GDummyFile *dummy = G_DUMMY_FILE (file);
400 if (dummy->decoded_uri)
401 return g_strdup (dummy->decoded_uri->scheme);
408 g_dummy_file_file_iface_init (GFileIface *iface)
410 iface->dup = g_dummy_file_dup;
411 iface->hash = g_dummy_file_hash;
412 iface->equal = g_dummy_file_equal;
413 iface->is_native = g_dummy_file_is_native;
414 iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
415 iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
416 iface->get_basename = g_dummy_file_get_basename;
417 iface->get_path = g_dummy_file_get_path;
418 iface->get_uri = g_dummy_file_get_uri;
419 iface->get_parse_name = g_dummy_file_get_parse_name;
420 iface->get_parent = g_dummy_file_get_parent;
421 iface->contains_file = g_dummy_file_contains_file;
422 iface->get_relative_path = g_dummy_file_get_relative_path;
423 iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
424 iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
427 /* Uri handling helper functions: */
430 unescape_character (const char *scanner)
435 first_digit = g_ascii_xdigit_value (*scanner++);
439 second_digit = g_ascii_xdigit_value (*scanner++);
440 if (second_digit < 0)
443 return (first_digit << 4) | second_digit;
447 unescape_string (const gchar *escaped_string,
448 const gchar *escaped_string_end,
449 const gchar *illegal_characters)
455 if (escaped_string == NULL)
458 if (escaped_string_end == NULL)
459 escaped_string_end = escaped_string + strlen (escaped_string);
461 result = g_malloc (escaped_string_end - escaped_string + 1);
464 for (in = escaped_string; in < escaped_string_end; in++) {
468 if (escaped_string_end - in < 2)
474 character = unescape_character (in);
476 /* Check for an illegal character. We consider '\0' illegal here. */
477 if (character <= 0 ||
478 (illegal_characters != NULL &&
479 strchr (illegal_characters, (char)character) != NULL))
484 in++; /* The other char will be eaten in the loop header */
486 *out++ = (char)character;
490 g_assert (out - result <= strlen (escaped_string));
495 _g_decoded_uri_free (GDecodedUri *decoded)
500 g_free (decoded->scheme);
501 g_free (decoded->query);
502 g_free (decoded->fragment);
503 g_free (decoded->userinfo);
504 g_free (decoded->host);
505 g_free (decoded->path);
510 _g_decoded_uri_new (void)
514 uri = g_new0 (GDecodedUri, 1);
521 _g_decode_uri (const char *uri)
523 GDecodedUri *decoded;
524 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
528 /* From RFC 3986 Decodes:
529 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
535 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
538 if (!g_ascii_isalpha (*p))
548 if (!(g_ascii_isalnum(c) ||
555 decoded = _g_decoded_uri_new ();
557 decoded->scheme = g_malloc (p - uri);
558 out = decoded->scheme;
559 for (in = uri; in < p - 1; in++)
560 *out++ = g_ascii_tolower (*in);
565 query_start = strchr (p, '?');
568 hier_part_end = query_start++;
569 fragment_start = strchr (query_start, '#');
572 decoded->query = g_strndup (query_start, fragment_start - query_start);
573 decoded->fragment = g_strdup (fragment_start+1);
577 decoded->query = g_strdup (query_start);
578 decoded->fragment = NULL;
584 decoded->query = NULL;
585 fragment_start = strchr (p, '#');
588 hier_part_end = fragment_start++;
589 decoded->fragment = g_strdup (fragment_start);
593 hier_part_end = p + strlen (p);
594 decoded->fragment = NULL;
599 hier-part = "//" authority path-abempty
606 if (hier_part_start[0] == '/' &&
607 hier_part_start[1] == '/')
609 const char *authority_start, *authority_end;
610 const char *userinfo_start, *userinfo_end;
611 const char *host_start, *host_end;
612 const char *port_start;
614 authority_start = hier_part_start + 2;
615 /* authority is always followed by / or nothing */
616 authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
617 if (authority_end == NULL)
618 authority_end = hier_part_end;
621 authority = [ userinfo "@" ] host [ ":" port ]
624 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
627 userinfo_start = authority_start;
628 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
629 if (decoded->userinfo == NULL)
631 _g_decoded_uri_free (decoded);
634 host_start = userinfo_end + 1;
637 host_start = authority_start;
639 port_start = memchr (host_start, ':', authority_end - host_start);
642 host_end = port_start++;
644 decoded->port = atoi(port_start);
648 host_end = authority_end;
652 decoded->host = g_strndup (host_start, host_end - host_start);
654 hier_part_start = authority_end;
657 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
659 if (decoded->path == NULL)
661 _g_decoded_uri_free (decoded);
669 is_valid (char c, const char *reserved_chars_allowed)
671 if (g_ascii_isalnum (c) ||
678 if (reserved_chars_allowed &&
679 strchr (reserved_chars_allowed, c) != NULL)
686 g_string_append_encoded (GString *string, const char *encoded,
687 const char *reserved_chars_allowed)
691 static const gchar hex[16] = "0123456789ABCDEF";
693 end = encoded + strlen (encoded);
695 while ((c = *encoded) != 0)
697 if (is_valid (c, reserved_chars_allowed))
699 g_string_append_c (string, c);
704 g_string_append_c (string, '%');
705 g_string_append_c (string, hex[((guchar)c) >> 4]);
706 g_string_append_c (string, hex[((guchar)c) & 0xf]);
713 _g_encode_uri (GDecodedUri *decoded)
717 uri = g_string_new (NULL);
719 g_string_append (uri, decoded->scheme);
720 g_string_append (uri, "://");
722 if (decoded->host != NULL)
724 if (decoded->userinfo)
726 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
727 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
728 g_string_append_c (uri, '@');
731 g_string_append (uri, decoded->host);
733 if (decoded->port != -1)
735 g_string_append_c (uri, ':');
736 g_string_append_printf (uri, "%d", decoded->port);
740 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
744 g_string_append_c (uri, '?');
745 g_string_append (uri, decoded->query);
748 if (decoded->fragment)
750 g_string_append_c (uri, '#');
751 g_string_append (uri, decoded->fragment);
754 return g_string_free (uri, FALSE);