/*------------------------------------------------------------------------- * Copyright (c) 2000-2002 Kenneth W. Sodemann (stuffle@charter.net) *------------------------------------------------------------------------- * gstr_utils * * Synopsis: * A collection of utilities for working with GStrings * * $Id: gstr_utils.c,v 1.1 2003/03/09 20:59:11 stuffle Exp $ * * 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 * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA *------------------------------------------------------------------------- */ #include #include #include GString* g_string_strip (GString *str) { gint new_len = str->len; assert (str); while (new_len > 0 && isspace (str->str[new_len - 1])) { new_len--; } if (new_len != str->len) { return g_string_truncate (str, new_len); } return str; } void g_string_delete_char (GString *str, gint pos) { GString *tmp; assert (pos < str->len); /* * Create a tmp string containing all of the characters after the * one to delete. * * 'pos' doubles as the number of chars before the position to be * deleted (since c strings are 0 indexed), so truncating the string * to that length, then reattaching the tmp string will do the trick. */ tmp = g_string_new (str->str + pos + 1); str = g_string_truncate (str, pos); str = g_string_append (str, tmp->str); g_string_free (tmp, TRUE); } void g_string_insert_char (GString *str, gchar chr, gint pos) { GString *tmp; assert (pos <= str->len); /* * We are doing the insert before the given position, so copy the * characters at that position and beyond to a temp string, truncate * the original string to that length, then paste in the new char, * and reattach the end of the string. */ tmp = g_string_new (str->str + pos); str = g_string_truncate (str, pos); str = g_string_append_c (str, chr); str = g_string_append (str, tmp->str); g_string_free (tmp, TRUE); } void g_string_insert_str (GString *str1, gchar *str2, gint pos) { GString *tmp; assert (pos <= str1->len); tmp = g_string_new (str1->str + pos); str1 = g_string_truncate (str1, pos); str1 = g_string_append (str1, str2); str1 = g_string_append (str1, tmp->str); g_string_free (tmp, TRUE); } void g_string_escape_char (GString *str, gchar chr) { gint i; for (i = str->len - 1; i >= 0; --i) { if (str->str[i] == chr) { g_string_insert_char (str, '\\', i); } } } void g_string_prepare_db_instr (GString *str) { str = g_string_strip (str); if (str->len) { g_string_escape_char (str, '\\'); g_string_escape_char (str, '\''); g_string_insert_char (str, '\'', 0); g_string_insert_char (str, '\'', str->len); } else { str = g_string_assign (str, "NULL"); } } void g_string_prepare_html (GString *str) { gboolean prev_space = FALSE; gint i = 0; while (i < str->len) { switch (str->str[i]) { case '&': g_string_insert_str (str, "amp;", i + 1); i += 5; prev_space = FALSE; break; case '<': str->str[i] = '&'; g_string_insert_str (str, "lt;", i + 1); i += 4; prev_space = FALSE; break; case '>': str->str[i] = '&'; g_string_insert_str (str, "gt;", i + 1); i += 4; prev_space = FALSE; break; case '"': str->str[i] = '&'; g_string_insert_str (str, "quot;", i + 1); i += 6; prev_space = FALSE; break; case '\n': str->str[i] = '<'; g_string_insert_str (str, "br>\n", i + 1); i += 5; prev_space = FALSE; break; case ' ': if (prev_space) { str->str[i] = '&'; g_string_insert_str (str, "nbsp;", i + 1); i += 5; /* xtra char added outside if */ } i += 1; prev_space = TRUE; break; default: prev_space = FALSE; i += 1; } /* switch */ } /* while */ }