/* * anjuta-utils.c * This file is part of anjuta * * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence * Copyright (C) 2000, 2002 Chema Celorio, Paolo Maggi * Copyright (C) 2003-2005 Paolo Maggi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ /* * Modified by the anjuta Team, 1998-2005. See the AUTHORS file for a * list of people on the anjuta Team. * See the ChangeLog files for a list of changes. * * $Id: anjuta-utils.c 2401 2006-03-03 13:53:04Z jhs $ */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "anjuta-utils.h" #include "anjuta-document.h" #include "anjuta-convert.h" #define STDIN_DELAY_MICROSECONDS 100000 gboolean anjuta_utils_uri_has_file_scheme (const gchar *uri) { gchar *canonical_uri = NULL; gboolean res; canonical_uri = gnome_vfs_make_uri_canonical (uri); g_return_val_if_fail (canonical_uri != NULL, FALSE); res = g_str_has_prefix (canonical_uri, "file:"); g_free (canonical_uri); return res; } gboolean anjuta_utils_uri_has_writable_scheme (const gchar *uri) { #if 0 gchar *canonical_uri; gchar *scheme; GSList *writable_schemes; gboolean res; canonical_uri = gnome_vfs_make_uri_canonical (uri); g_return_val_if_fail (canonical_uri != NULL, FALSE); scheme = gnome_vfs_get_uri_scheme (canonical_uri); g_return_val_if_fail (scheme != NULL, FALSE); g_free (canonical_uri); writable_schemes = anjuta_prefs_manager_get_writable_vfs_schemes (); /* CHECK: should we use g_ascii_strcasecmp? - Paolo (Nov 6, 2005) */ res = (g_slist_find_custom (writable_schemes, scheme, (GCompareFunc)strcmp) != NULL); g_slist_foreach (writable_schemes, (GFunc)g_free, NULL); g_slist_free (writable_schemes); g_free (scheme); return res; #endif return FALSE; } /* * n: len of the string in bytes */ gboolean g_utf8_caselessnmatch (const char *s1, const char *s2, gssize n1, gssize n2) { gchar *casefold; gchar *normalized_s1; gchar *normalized_s2; gint len_s1; gint len_s2; gboolean ret = FALSE; g_return_val_if_fail (s1 != NULL, FALSE); g_return_val_if_fail (s2 != NULL, FALSE); g_return_val_if_fail (n1 > 0, FALSE); g_return_val_if_fail (n2 > 0, FALSE); casefold = g_utf8_casefold (s1, n1); normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD); g_free (casefold); casefold = g_utf8_casefold (s2, n2); normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD); g_free (casefold); len_s1 = strlen (normalized_s1); len_s2 = strlen (normalized_s2); if (len_s1 < len_s2) goto finally_2; ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0); finally_2: g_free (normalized_s1); g_free (normalized_s2); return ret; } gboolean anjuta_utils_uri_exists (const gchar* text_uri) { GnomeVFSURI *uri; gboolean res; g_return_val_if_fail (text_uri != NULL, FALSE); uri = gnome_vfs_uri_new (text_uri); g_return_val_if_fail (uri != NULL, FALSE); res = gnome_vfs_uri_exists (uri); gnome_vfs_uri_unref (uri); return res; } gchar * anjuta_utils_make_valid_utf8 (const char *name) { GString *string; const char *remainder, *invalid; int remaining_bytes, valid_bytes; string = NULL; remainder = name; remaining_bytes = strlen (name); while (remaining_bytes != 0) { if (g_utf8_validate (remainder, remaining_bytes, &invalid)) { break; } valid_bytes = invalid - remainder; if (string == NULL) { string = g_string_sized_new (remaining_bytes); } g_string_append_len (string, remainder, valid_bytes); /* append U+FFFD REPLACEMENT CHARACTER */ g_string_append (string, "\357\277\275"); remaining_bytes -= valid_bytes + 1; remainder = invalid + 1; } if (string == NULL) { return g_strdup (name); } g_string_append (string, remainder); g_assert (g_utf8_validate (string->str, -1, NULL)); return g_string_free (string, FALSE); } static gboolean is_valid_scheme_character (gchar c) { return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.'; } static gboolean has_valid_scheme (const gchar *uri) { const gchar *p; p = uri; if (!is_valid_scheme_character (*p)) { return FALSE; } do { p++; } while (is_valid_scheme_character (*p)); return *p == ':'; } gboolean anjuta_utils_is_valid_uri (const gchar *uri) { const guchar *p; if (uri == NULL) return FALSE; if (!has_valid_scheme (uri)) return FALSE; /* We expect to have a fully valid set of characters */ for (p = (const guchar *)uri; *p; p++) { if (*p == '%') { ++p; if (!g_ascii_isxdigit (*p)) return FALSE; ++p; if (!g_ascii_isxdigit (*p)) return FALSE; } else { if (*p <= 32 || *p >= 128) return FALSE; } } return TRUE; }