Updated Spanish translation
[anjuta-git-plugin.git] / plugins / sourceview / anjuta-utils.c
blobfa38c0930ce42b7b9a20a63d970f345f87657081
1 /*
2 * anjuta-utils.c
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.
30 * $Id$
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <fcntl.h>
42 #include <string.h>
43 #include <X11/Xlib.h>
44 #include <X11/Xutil.h>
45 #include <X11/Xatom.h>
47 #include <gdk/gdkx.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"
58 #include <libanjuta/anjuta-convert.h>
60 #define STDIN_DELAY_MICROSECONDS 100000
62 gboolean
63 anjuta_utils_uri_has_file_scheme (const gchar *uri)
65 gchar *canonical_uri = NULL;
66 gboolean res;
68 canonical_uri = gnome_vfs_make_uri_canonical (uri);
69 g_return_val_if_fail (canonical_uri != NULL, FALSE);
71 res = g_str_has_prefix (canonical_uri, "file:");
73 g_free (canonical_uri);
75 return res;
78 gboolean
79 anjuta_utils_uri_has_writable_scheme (const gchar *uri)
82 #if 0
83 gchar *canonical_uri;
84 gchar *scheme;
85 GSList *writable_schemes;
86 gboolean res;
88 canonical_uri = gnome_vfs_make_uri_canonical (uri);
89 g_return_val_if_fail (canonical_uri != NULL, FALSE);
91 scheme = gnome_vfs_get_uri_scheme (canonical_uri);
92 g_return_val_if_fail (scheme != NULL, FALSE);
94 g_free (canonical_uri);
96 writable_schemes = anjuta_prefs_manager_get_writable_vfs_schemes ();
98 /* CHECK: should we use g_ascii_strcasecmp? - Paolo (Nov 6, 2005) */
99 res = (g_slist_find_custom (writable_schemes,
100 scheme,
101 (GCompareFunc)strcmp) != NULL);
103 g_slist_foreach (writable_schemes, (GFunc)g_free, NULL);
104 g_slist_free (writable_schemes);
106 g_free (scheme);
108 return res;
109 #endif
110 return FALSE;
114 * n: len of the string in bytes
116 gboolean
117 g_utf8_caselessnmatch (const char *s1, const char *s2, gssize n1, gssize n2)
119 gchar *casefold;
120 gchar *normalized_s1;
121 gchar *normalized_s2;
122 gint len_s1;
123 gint len_s2;
124 gboolean ret = FALSE;
126 g_return_val_if_fail (s1 != NULL, FALSE);
127 g_return_val_if_fail (s2 != NULL, FALSE);
128 g_return_val_if_fail (n1 > 0, FALSE);
129 g_return_val_if_fail (n2 > 0, FALSE);
131 casefold = g_utf8_casefold (s1, n1);
132 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
133 g_free (casefold);
135 casefold = g_utf8_casefold (s2, n2);
136 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
137 g_free (casefold);
139 len_s1 = strlen (normalized_s1);
140 len_s2 = strlen (normalized_s2);
142 if (len_s1 < len_s2)
143 goto finally_2;
145 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
147 finally_2:
148 g_free (normalized_s1);
149 g_free (normalized_s2);
151 return ret;
154 gboolean
155 anjuta_utils_uri_exists (const gchar* text_uri)
157 GnomeVFSURI *uri;
158 gboolean res;
160 g_return_val_if_fail (text_uri != NULL, FALSE);
162 uri = gnome_vfs_uri_new (text_uri);
163 g_return_val_if_fail (uri != NULL, FALSE);
165 res = gnome_vfs_uri_exists (uri);
167 gnome_vfs_uri_unref (uri);
169 return res;
172 gchar *
173 anjuta_utils_make_valid_utf8 (const char *name)
175 GString *string;
176 const char *remainder, *invalid;
177 int remaining_bytes, valid_bytes;
179 string = NULL;
180 remainder = name;
181 remaining_bytes = strlen (name);
183 while (remaining_bytes != 0) {
184 if (g_utf8_validate (remainder, remaining_bytes, &invalid)) {
185 break;
187 valid_bytes = invalid - remainder;
189 if (string == NULL) {
190 string = g_string_sized_new (remaining_bytes);
192 g_string_append_len (string, remainder, valid_bytes);
193 /* append U+FFFD REPLACEMENT CHARACTER */
194 g_string_append (string, "\357\277\275");
196 remaining_bytes -= valid_bytes + 1;
197 remainder = invalid + 1;
200 if (string == NULL) {
201 return g_strdup (name);
204 g_string_append (string, remainder);
206 g_assert (g_utf8_validate (string->str, -1, NULL));
208 return g_string_free (string, FALSE);
211 static gboolean
212 is_valid_scheme_character (gchar c)
214 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
217 static gboolean
218 has_valid_scheme (const gchar *uri)
220 const gchar *p;
222 p = uri;
224 if (!is_valid_scheme_character (*p)) {
225 return FALSE;
228 do {
229 p++;
230 } while (is_valid_scheme_character (*p));
232 return *p == ':';
235 gboolean
236 anjuta_utils_is_valid_uri (const gchar *uri)
238 const guchar *p;
240 if (uri == NULL)
241 return FALSE;
243 if (!has_valid_scheme (uri))
244 return FALSE;
246 /* We expect to have a fully valid set of characters */
247 for (p = (const guchar *)uri; *p; p++) {
248 if (*p == '%')
250 ++p;
251 if (!g_ascii_isxdigit (*p))
252 return FALSE;
254 ++p;
255 if (!g_ascii_isxdigit (*p))
256 return FALSE;
258 else
260 if (*p <= 32 || *p >= 128)
261 return FALSE;
265 return TRUE;