+static gboolean
+is_valid_insert (const gchar *text, gint len)
+{
+ gunichar c;
+ gunichar next_c;
+ gint i= 0;
+ gboolean quoted = FALSE;
+ const gchar *current, *next_current;
+ if (text == NULL)
+ return TRUE;
+ current = text;
+
+ while (((len == -1)||(i < len)) && (*current != '\0')) {
+ c = g_utf8_get_char (current);
+ next_current = g_utf8_next_char (current);
+ if (next_current && *next_current != '\0')
+ next_c = g_utf8_get_char (g_utf8_next_char (current));
+ else
+ next_c = 0;
+ if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
+ if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
+ return FALSE;
+ else {
+ current = g_utf8_next_char (next_current);
+ continue;
+ }
+ }
+ if (c == 0x2022 || c == 0xfffc ||
+ c == g_utf8_get_char ("\n") ||
+ c == g_utf8_get_char ("\t"))
+ return FALSE;
+ if (c == g_utf8_get_char ("\""))
+ quoted = !quoted;
+ current = g_utf8_next_char (current);
+ i = current - text;
+ }
+ return TRUE;
+}
+
+static gchar *
+create_valid_text (const gchar *text, gint len)
+{
+ gunichar c;
+ gunichar next_c;
+ gint i= 0;
+ GString *str;
+ gboolean quoted = FALSE;
+ const gchar *current, *next_current;
+
+ if (text == NULL)
+ return NULL;
+
+ str = g_string_new ("");
+ current = text;
+
+ while (((len == -1)||(i < len)) && (*current != '\0')) {
+ c = g_utf8_get_char (current);
+ next_current = g_utf8_next_char (current);
+ if (next_current && *next_current != '\0')
+ next_c = g_utf8_get_char (g_utf8_next_char (current));
+ else
+ next_c = 0;
+ if (c != 0x2022 && c != 0xfffc &&
+ c != g_utf8_get_char ("\n") &&
+ c != g_utf8_get_char ("\t"))
+ str = g_string_append_unichar (str, c);
+ if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
+ if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
+ str = g_string_append_c (str, '\n');
+ }
+ if (c == g_utf8_get_char ("\""))
+ quoted = !quoted;
+ current = g_utf8_next_char (current);
+ i = current - text;
+ }
+ return g_string_free (str, FALSE);
+}
+