X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-delimited-text.c;h=b232ef309226ca052846f449d92f9fcfebc9ff2d;hb=e7913c62251710319b06c50702c5db9afb612be5;hp=c999f220eb1afb376d1b89578d2b5483403c6637;hpb=bbebcf3f9d827d8c68aeaf90b65c88ad1b77ba8e;p=pspp diff --git a/src/ui/gui/psppire-delimited-text.c b/src/ui/gui/psppire-delimited-text.c index c999f220eb..b232ef3092 100644 --- a/src/ui/gui/psppire-delimited-text.c +++ b/src/ui/gui/psppire-delimited-text.c @@ -35,45 +35,80 @@ enum PROP_FIRST_LINE }; +struct enclosure +{ + gunichar opening; + gunichar closing; +}; + +static const struct enclosure enclosures[3] = + { + {'(', ')'}, + {'"', '"'}, + {'\'', '\''} + }; + static void count_delims (PsppireDelimitedText *tf) { - if (tf->child) + if (tf->child == NULL) + return; + + tf->max_delimiters = 0; + GtkTreeIter iter; + gboolean valid; + for (valid = gtk_tree_model_get_iter_first (tf->child, &iter); + valid; + valid = gtk_tree_model_iter_next (tf->child, &iter)) { - tf->max_delimiters = 0; - GtkTreeIter iter; - gboolean valid; - for (valid = gtk_tree_model_get_iter_first (tf->child, &iter); - valid; - valid = gtk_tree_model_iter_next (tf->child, &iter)) - { - // FIXME: Box these lines to avoid constant allocation/deallocation - gchar *foo = 0; - gtk_tree_model_get (tf->child, &iter, 1, &foo, -1); + gint enc = -1; + // FIXME: Box these lines to avoid constant allocation/deallocation + gchar *line = NULL; + gtk_tree_model_get (tf->child, &iter, 1, &line, -1); + { + char *p; + gint count = 0; + for (p = line; ; p = g_utf8_find_next_char (p, NULL)) { - char *line = foo; - gint count = 0; - while (*line) + const gunichar c = g_utf8_get_char (p); + if (c == 0) + break; + if (enc == -1) + { + gint i; + for (i = 0; i < 3; ++i) + { + if (c == enclosures[i].opening) + { + enc = i; + break; + } + } + } + else if (c == enclosures[enc].closing) + { + enc = -1; + } + if (enc == -1) { GSList *del; for (del = tf->delimiters; del; del = g_slist_next (del)) { - if (*line == GPOINTER_TO_INT (del->data)) + if (c == GPOINTER_TO_INT (del->data)) count++; } - line++; } - tf->max_delimiters = MAX (tf->max_delimiters, count); } - g_free (foo); - } + tf->max_delimiters = MAX (tf->max_delimiters, count); + } + g_free (line); } } static void cache_invalidate (PsppireDelimitedText *tf) { - memset (tf->cache_starts, 0, 512); + memset (tf->cache_starts, 0, sizeof tf->cache_starts); if (tf->const_cache.string) { ss_dealloc (&tf->const_cache); @@ -134,10 +169,6 @@ psppire_delimited_text_get_property (GObject *object, }; } - -static void psppire_delimited_text_init (PsppireDelimitedText *text_file); -static void psppire_delimited_text_class_init (PsppireDelimitedTextClass *class); - static void psppire_delimited_text_finalize (GObject *object); static void psppire_delimited_text_dispose (GObject *object); @@ -313,6 +344,18 @@ __iter_nth_child (GtkTreeModel *tree_model, } +static void +nullify_char (struct substring cs) +{ + int char_len = ss_first_mblen (cs); + while (char_len > 0) + { + cs.string[char_len - 1] = '\0'; + char_len--; + } +} + + /* Split row N into it's delimited fields (if it is not already cached) and set this row as the current cache. */ static void @@ -323,7 +366,7 @@ split_row_into_fields (PsppireDelimitedText *file, gint n) return; } - memset (file->cache_starts, 0, 512); + memset (file->cache_starts, 0, sizeof file->cache_starts); /* Cache miss */ if (file->const_cache.string) { @@ -334,25 +377,47 @@ split_row_into_fields (PsppireDelimitedText *file, gint n) struct substring cs = file->const_cache; int field = 0; file->cache_starts[0] = cs.string; + gint enc = -1; for (; UINT32_MAX != ss_first_mb (cs); ss_get_mb (&cs)) { - ucs4_t xx = ss_first_mb (cs); - GSList *del; - for (del = file->delimiters; del; del = g_slist_next (del)) + ucs4_t character = ss_first_mb (cs); + gboolean char_is_quote = FALSE; + if (enc == -1) { - if (xx == GPOINTER_TO_INT (del->data)) + gint i; + for (i = 0; i < 3; ++i) { - field++; - int char_len = ss_first_mblen (cs); - file->cache_starts[field] = cs.string + char_len; - while (char_len > 0) + if (character == enclosures[i].opening) { - cs.string[char_len - 1] = '\0'; - char_len--; + enc = i; + char_is_quote = TRUE; + file->cache_starts[field] += ss_first_mblen (cs); + break; + } + } + } + else if (character == enclosures[enc].closing) + { + char_is_quote = TRUE; + nullify_char (cs); + enc = -1; + } + + if (enc == -1 && char_is_quote == FALSE) + { + GSList *del; + for (del = file->delimiters; del; del = g_slist_next (del)) + { + if (character == GPOINTER_TO_INT (del->data)) + { + field++; + int char_len = ss_first_mblen (cs); + file->cache_starts[field] = cs.string + char_len; + nullify_char (cs); + break; } - break; } } } @@ -417,44 +482,9 @@ __tree_model_init (GtkTreeModelIface *iface) iface->iter_parent = __iter_parent; } - -GType -psppire_delimited_text_get_type (void) -{ - static GType text_file_type = 0; - - if (!text_file_type) - { - static const GTypeInfo text_file_info = - { - sizeof (PsppireDelimitedTextClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) psppire_delimited_text_class_init, - NULL, /* class_finalize */ - NULL, /* class_data */ - sizeof (PsppireDelimitedText), - 0, - (GInstanceInitFunc) psppire_delimited_text_init, - }; - - static const GInterfaceInfo tree_model_info = { - (GInterfaceInitFunc) __tree_model_init, - NULL, - NULL - }; - - text_file_type = g_type_register_static (G_TYPE_OBJECT, - "PsppireDelimitedText", - &text_file_info, 0); - - g_type_add_interface_static (text_file_type, GTK_TYPE_TREE_MODEL, - &tree_model_info); - } - - return text_file_type; -} - +G_DEFINE_TYPE_WITH_CODE (PsppireDelimitedText, psppire_delimited_text, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL, + __tree_model_init)) static void psppire_delimited_text_class_init (PsppireDelimitedTextClass *class) @@ -462,7 +492,7 @@ psppire_delimited_text_class_init (PsppireDelimitedTextClass *class) GObjectClass *object_class; parent_class = g_type_class_peek_parent (class); - object_class = (GObjectClass*) class; + object_class = G_OBJECT_CLASS (class); GParamSpec *first_line_spec = g_param_spec_int ("first-line", @@ -514,7 +544,7 @@ psppire_delimited_text_init (PsppireDelimitedText *text_file) text_file->const_cache.string = NULL; text_file->const_cache.length = 0; text_file->cache_row = -1; - memset (text_file->cache_starts, 0, 512); + memset (text_file->cache_starts, 0, sizeof text_file->cache_starts); text_file->max_delimiters = 0; @@ -523,15 +553,13 @@ psppire_delimited_text_init (PsppireDelimitedText *text_file) } -GtkTreeModel * +PsppireDelimitedText * psppire_delimited_text_new (GtkTreeModel *child) { - PsppireDelimitedText *retval = + return g_object_new (PSPPIRE_TYPE_DELIMITED_TEXT, "child", child, NULL); - - return retval; } static void