3 * This file is part of anjuta
5 * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
6 * Copyright (C) 2000, 2002 Chema Celorio, Paolo Maggi
7 * Copyright (C) 2003-2005 Paolo Maggi
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * Modified by the anjuta Team, 1998-2005. See the AUTHORS file for a
27 * list of people on the anjuta Team.
28 * See the ChangeLog files for a list of changes.
38 #include <sys/types.h>
44 #include <X11/Xutil.h>
45 #include <X11/Xatom.h>
48 #include <glib/gunicode.h>
49 #include <glib/gi18n.h>
50 #include <glade/glade-xml.h>
51 #include <libgnomevfs/gnome-vfs.h>
52 #include <libgnome/gnome-url.h>
54 #include "anjuta-utils.h"
56 #include "anjuta-document.h"
57 #include "anjuta-convert.h"
59 #define STDIN_DELAY_MICROSECONDS 100000
62 anjuta_utils_uri_has_file_scheme (const gchar
*uri
)
64 gchar
*canonical_uri
= NULL
;
67 canonical_uri
= gnome_vfs_make_uri_canonical (uri
);
68 g_return_val_if_fail (canonical_uri
!= NULL
, FALSE
);
70 res
= g_str_has_prefix (canonical_uri
, "file:");
72 g_free (canonical_uri
);
78 anjuta_utils_uri_has_writable_scheme (const gchar
*uri
)
84 GSList
*writable_schemes
;
87 canonical_uri
= gnome_vfs_make_uri_canonical (uri
);
88 g_return_val_if_fail (canonical_uri
!= NULL
, FALSE
);
90 scheme
= gnome_vfs_get_uri_scheme (canonical_uri
);
91 g_return_val_if_fail (scheme
!= NULL
, FALSE
);
93 g_free (canonical_uri
);
95 writable_schemes
= anjuta_prefs_manager_get_writable_vfs_schemes ();
97 /* CHECK: should we use g_ascii_strcasecmp? - Paolo (Nov 6, 2005) */
98 res
= (g_slist_find_custom (writable_schemes
,
100 (GCompareFunc
)strcmp
) != NULL
);
102 g_slist_foreach (writable_schemes
, (GFunc
)g_free
, NULL
);
103 g_slist_free (writable_schemes
);
113 * n: len of the string in bytes
116 g_utf8_caselessnmatch (const char *s1
, const char *s2
, gssize n1
, gssize n2
)
119 gchar
*normalized_s1
;
120 gchar
*normalized_s2
;
123 gboolean ret
= FALSE
;
125 g_return_val_if_fail (s1
!= NULL
, FALSE
);
126 g_return_val_if_fail (s2
!= NULL
, FALSE
);
127 g_return_val_if_fail (n1
> 0, FALSE
);
128 g_return_val_if_fail (n2
> 0, FALSE
);
130 casefold
= g_utf8_casefold (s1
, n1
);
131 normalized_s1
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
134 casefold
= g_utf8_casefold (s2
, n2
);
135 normalized_s2
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
138 len_s1
= strlen (normalized_s1
);
139 len_s2
= strlen (normalized_s2
);
144 ret
= (strncmp (normalized_s1
, normalized_s2
, len_s2
) == 0);
147 g_free (normalized_s1
);
148 g_free (normalized_s2
);
154 anjuta_utils_uri_exists (const gchar
* text_uri
)
159 g_return_val_if_fail (text_uri
!= NULL
, FALSE
);
161 uri
= gnome_vfs_uri_new (text_uri
);
162 g_return_val_if_fail (uri
!= NULL
, FALSE
);
164 res
= gnome_vfs_uri_exists (uri
);
166 gnome_vfs_uri_unref (uri
);
172 anjuta_utils_make_valid_utf8 (const char *name
)
175 const char *remainder
, *invalid
;
176 int remaining_bytes
, valid_bytes
;
180 remaining_bytes
= strlen (name
);
182 while (remaining_bytes
!= 0) {
183 if (g_utf8_validate (remainder
, remaining_bytes
, &invalid
)) {
186 valid_bytes
= invalid
- remainder
;
188 if (string
== NULL
) {
189 string
= g_string_sized_new (remaining_bytes
);
191 g_string_append_len (string
, remainder
, valid_bytes
);
192 /* append U+FFFD REPLACEMENT CHARACTER */
193 g_string_append (string
, "\357\277\275");
195 remaining_bytes
-= valid_bytes
+ 1;
196 remainder
= invalid
+ 1;
199 if (string
== NULL
) {
200 return g_strdup (name
);
203 g_string_append (string
, remainder
);
205 g_assert (g_utf8_validate (string
->str
, -1, NULL
));
207 return g_string_free (string
, FALSE
);
211 is_valid_scheme_character (gchar c
)
213 return g_ascii_isalnum (c
) || c
== '+' || c
== '-' || c
== '.';
217 has_valid_scheme (const gchar
*uri
)
223 if (!is_valid_scheme_character (*p
)) {
229 } while (is_valid_scheme_character (*p
));
235 anjuta_utils_is_valid_uri (const gchar
*uri
)
242 if (!has_valid_scheme (uri
))
245 /* We expect to have a fully valid set of characters */
246 for (p
= (const guchar
*)uri
; *p
; p
++) {
250 if (!g_ascii_isxdigit (*p
))
254 if (!g_ascii_isxdigit (*p
))
259 if (*p
<= 32 || *p
>= 128)