m_url_unescaped_form: Check if g_uri_unescape_string fails
[libquvi.git] / src / misc / url.c
blobfc4ee576b5599c59b5bdcf1640f5ab2036ff723e
1 /* libquvi
2 * Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public
17 * License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <glib.h>
24 /* -- */
25 #include "misc/url.h"
27 static gboolean _is_unescaped(const gchar *s)
29 gboolean r;
30 gchar *u;
31 u = g_uri_unescape_string(s, NULL);
32 r = g_strcmp0(u,s) == 0 ? TRUE:FALSE;
33 g_free(u);
34 return (r);
37 gchar *m_url_unescaped_form(const gchar *s)
39 gchar *r = g_strdup(s);
42 gchar *u = g_uri_unescape_string(r, NULL);
43 if (u == NULL)
44 break;
45 g_free(r);
46 r = u;
48 while (_is_unescaped(r) == FALSE);
49 return (r);
52 gchar *m_url_escaped_form(const gchar *s)
54 static const gchar *reserved_chars = "!*'();:@&=+$,/?#[]";
55 gchar *u, *r;
56 u = m_url_unescaped_form(s);
57 r = g_uri_escape_string(u, reserved_chars, FALSE);
58 g_free(u);
59 return (r);
62 /* vim: set ts=2 sw=2 tw=72 expandtab: */