Updated Spanish translation
[anjuta-git-plugin.git] / plugins / sourceview / anjuta-utils.c
blobaff27ad248684ac542237c9e7f41420b90c64965
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"
57 #include "anjuta-convert.h"
59 #define STDIN_DELAY_MICROSECONDS 100000
61 gboolean
62 anjuta_utils_uri_has_file_scheme (const gchar *uri)
64 gchar *canonical_uri = NULL;
65 gboolean res;
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);
74 return res;
77 gboolean
78 anjuta_utils_uri_has_writable_scheme (const gchar *uri)
81 #if 0
82 gchar *canonical_uri;
83 gchar *scheme;
84 GSList *writable_schemes;
85 gboolean res;
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,
99 scheme,
100 (GCompareFunc)strcmp) != NULL);
102 g_slist_foreach (writable_schemes, (GFunc)g_free, NULL);
103 g_slist_free (writable_schemes);
105 g_free (scheme);
107 return res;
108 #endif
109 return FALSE;
113 * n: len of the string in bytes
115 gboolean
116 g_utf8_caselessnmatch (const char *s1, const char *s2, gssize n1, gssize n2)
118 gchar *casefold;
119 gchar *normalized_s1;
120 gchar *normalized_s2;
121 gint len_s1;
122 gint len_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);
132 g_free (casefold);
134 casefold = g_utf8_casefold (s2, n2);
135 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
136 g_free (casefold);
138 len_s1 = strlen (normalized_s1);
139 len_s2 = strlen (normalized_s2);
141 if (len_s1 < len_s2)
142 goto finally_2;
144 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
146 finally_2:
147 g_free (normalized_s1);
148 g_free (normalized_s2);
150 return ret;
153 gboolean
154 anjuta_utils_uri_exists (const gchar* text_uri)
156 GnomeVFSURI *uri;
157 gboolean res;
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);
168 return res;
171 gchar *
172 anjuta_utils_make_valid_utf8 (const char *name)
174 GString *string;
175 const char *remainder, *invalid;
176 int remaining_bytes, valid_bytes;
178 string = NULL;
179 remainder = name;
180 remaining_bytes = strlen (name);
182 while (remaining_bytes != 0) {
183 if (g_utf8_validate (remainder, remaining_bytes, &invalid)) {
184 break;
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);
210 static gboolean
211 is_valid_scheme_character (gchar c)
213 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
216 static gboolean
217 has_valid_scheme (const gchar *uri)
219 const gchar *p;
221 p = uri;
223 if (!is_valid_scheme_character (*p)) {
224 return FALSE;
227 do {
228 p++;
229 } while (is_valid_scheme_character (*p));
231 return *p == ':';
234 gboolean
235 anjuta_utils_is_valid_uri (const gchar *uri)
237 const guchar *p;
239 if (uri == NULL)
240 return FALSE;
242 if (!has_valid_scheme (uri))
243 return FALSE;
245 /* We expect to have a fully valid set of characters */
246 for (p = (const guchar *)uri; *p; p++) {
247 if (*p == '%')
249 ++p;
250 if (!g_ascii_isxdigit (*p))
251 return FALSE;
253 ++p;
254 if (!g_ascii_isxdigit (*p))
255 return FALSE;
257 else
259 if (*p <= 32 || *p >= 128)
260 return FALSE;
264 return TRUE;