1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2017 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #define _(msgid) gettext (msgid)
20 #define P_(msgid) msgid
22 #include "psppire-delimited-text.h"
23 #include "psppire-text-file.h"
24 #include "libpspp/str.h"
25 #include "libpspp/i18n.h"
44 static const struct enclosure enclosures[3] =
52 count_delims (PsppireDelimitedText *tf)
54 if (tf->child == NULL)
57 tf->max_delimiters = 0;
60 for (valid = gtk_tree_model_get_iter_first (tf->child, &iter);
62 valid = gtk_tree_model_iter_next (tf->child, &iter))
65 // FIXME: Box these lines to avoid constant allocation/deallocation
67 gtk_tree_model_get (tf->child, &iter, 1, &line, -1);
71 for (p = line; ; p = g_utf8_find_next_char (p, NULL))
73 const gunichar c = g_utf8_get_char (p);
79 for (i = 0; i < 3; ++i)
81 if (c == enclosures[i].opening)
88 else if (c == enclosures[enc].closing)
95 for (del = tf->delimiters; del; del = g_slist_next (del))
97 if (c == GPOINTER_TO_INT (del->data))
102 tf->max_delimiters = MAX (tf->max_delimiters, count);
109 cache_invalidate (PsppireDelimitedText *tf)
111 memset (tf->cache_starts, 0, sizeof tf->cache_starts);
112 if (tf->const_cache.string)
114 ss_dealloc (&tf->const_cache);
115 tf->const_cache.string = NULL;
121 psppire_delimited_text_set_property (GObject *object,
126 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (object);
130 case PROP_FIRST_LINE:
131 tf->first_line = g_value_get_int (value);
134 tf->child = g_value_get_object (value);
135 g_return_if_fail (PSPPIRE_IS_TEXT_FILE (tf->child));
137 case PROP_DELIMITERS:
138 g_slist_free (tf->delimiters);
139 tf->delimiters = g_slist_copy (g_value_get_pointer (value));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 cache_invalidate (tf);
151 psppire_delimited_text_get_property (GObject *object,
156 PsppireDelimitedText *text_file = PSPPIRE_DELIMITED_TEXT (object);
160 case PROP_FIRST_LINE:
161 g_value_set_int (value, text_file->first_line);
163 case PROP_DELIMITERS:
164 g_value_set_pointer (value, text_file->delimiters);
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173 static void psppire_delimited_text_init (PsppireDelimitedText *text_file);
174 static void psppire_delimited_text_class_init (PsppireDelimitedTextClass *class);
176 static void psppire_delimited_text_finalize (GObject *object);
177 static void psppire_delimited_text_dispose (GObject *object);
179 static GObjectClass *parent_class = NULL;
182 n_lines (PsppireDelimitedText *file)
184 PsppireTextFile *child = PSPPIRE_TEXT_FILE (file->child);
186 return child->maximum_lines;
190 __tree_get_iter (GtkTreeModel *tree_model,
194 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
199 gint *indices = gtk_tree_path_get_indices (path);
206 gint children = n_lines (file);
208 if (n >= children - file->first_line)
212 iter->user_data = GINT_TO_POINTER (n);
213 iter->stamp = file->stamp;
220 __tree_iter_next (GtkTreeModel *tree_model,
223 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
224 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
226 gint n = GPOINTER_TO_INT (iter->user_data);
229 gint children = n_lines (file);
231 if (n + 1 >= children - file->first_line)
234 iter->user_data = GINT_TO_POINTER (n + 1);
241 __tree_get_column_type (GtkTreeModel *tree_model,
247 return G_TYPE_STRING;
251 __iter_has_child (GtkTreeModel *tree_model,
259 __iter_parent (GtkTreeModel *tree_model,
267 __tree_get_path (GtkTreeModel *tree_model,
270 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
271 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
273 gint n = GPOINTER_TO_INT (iter->user_data);
275 gint children = n_lines (file);
277 if (n >= children - file->first_line)
280 return gtk_tree_path_new_from_indices (n, -1);
285 __iter_children (GtkTreeModel *tree_model,
294 __tree_model_iter_n_children (GtkTreeModel *tree_model,
297 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
298 g_assert (iter == NULL);
300 gint children = n_lines (file);
302 return children - file->first_line;
305 static GtkTreeModelFlags
306 __tree_model_get_flags (GtkTreeModel *model)
308 g_return_val_if_fail (PSPPIRE_IS_DELIMITED_TEXT (model), (GtkTreeModelFlags) 0);
310 return GTK_TREE_MODEL_LIST_ONLY;
314 __tree_model_get_n_columns (GtkTreeModel *tree_model)
316 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (tree_model);
318 /* + 1 for the trailing field and +1 for the leading line number column */
319 return tf->max_delimiters + 1 + 1;
324 __iter_nth_child (GtkTreeModel *tree_model,
329 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
331 g_assert (parent == NULL);
333 g_return_val_if_fail (file, FALSE);
335 gint children = gtk_tree_model_iter_n_children (file->child, NULL);
337 if (n >= children - file->first_line)
340 iter->user_data = NULL;
344 iter->user_data = GINT_TO_POINTER (n);
345 iter->stamp = file->stamp;
352 nullify_char (struct substring cs)
354 int char_len = ss_first_mblen (cs);
357 cs.string[char_len - 1] = '\0';
363 /* Split row N into it's delimited fields (if it is not already cached)
364 and set this row as the current cache. */
366 split_row_into_fields (PsppireDelimitedText *file, gint n)
368 if (n == file->cache_row) /* Cache hit */
373 memset (file->cache_starts, 0, sizeof file->cache_starts);
375 if (file->const_cache.string)
377 ss_dealloc (&file->const_cache);
379 ss_alloc_substring_pool (&file->const_cache,
380 PSPPIRE_TEXT_FILE (file->child)->lines[n], NULL);
381 struct substring cs = file->const_cache;
383 file->cache_starts[0] = cs.string;
386 UINT32_MAX != ss_first_mb (cs);
389 ucs4_t character = ss_first_mb (cs);
390 gboolean char_is_quote = FALSE;
394 for (i = 0; i < 3; ++i)
396 if (character == enclosures[i].opening)
399 char_is_quote = TRUE;
400 file->cache_starts[field] += ss_first_mblen (cs);
405 else if (character == enclosures[enc].closing)
407 char_is_quote = TRUE;
412 if (enc == -1 && char_is_quote == FALSE)
415 for (del = file->delimiters; del; del = g_slist_next (del))
417 if (character == GPOINTER_TO_INT (del->data))
420 int char_len = ss_first_mblen (cs);
421 file->cache_starts[field] = cs.string + char_len;
433 psppire_delimited_text_get_header_title (PsppireDelimitedText *file, gint column)
435 if (file->first_line <= 0)
438 split_row_into_fields (file, file->first_line - 1);
440 return file->cache_starts [column];
444 __get_value (GtkTreeModel *tree_model,
449 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
451 g_return_if_fail (iter->stamp == file->stamp);
453 gint n = GPOINTER_TO_INT (iter->user_data) + file->first_line;
458 g_value_init (value, G_TYPE_INT);
459 g_value_set_int (value, n + 1);
463 g_value_init (value, G_TYPE_STRING);
465 split_row_into_fields (file, n);
467 g_value_set_string (value, file->cache_starts [column - 1]);
472 __tree_model_init (GtkTreeModelIface *iface)
474 iface->get_flags = __tree_model_get_flags;
475 iface->get_n_columns = __tree_model_get_n_columns ;
476 iface->get_column_type = __tree_get_column_type;
477 iface->get_iter = __tree_get_iter;
478 iface->iter_next = __tree_iter_next;
479 iface->get_path = __tree_get_path;
480 iface->get_value = __get_value;
482 iface->iter_children = __iter_children;
483 iface->iter_has_child = __iter_has_child;
484 iface->iter_n_children = __tree_model_iter_n_children;
485 iface->iter_nth_child = __iter_nth_child;
486 iface->iter_parent = __iter_parent;
491 psppire_delimited_text_get_type (void)
493 static GType text_file_type = 0;
497 static const GTypeInfo text_file_info =
499 sizeof (PsppireDelimitedTextClass),
500 NULL, /* base_init */
501 NULL, /* base_finalize */
502 (GClassInitFunc) psppire_delimited_text_class_init,
503 NULL, /* class_finalize */
504 NULL, /* class_data */
505 sizeof (PsppireDelimitedText),
507 (GInstanceInitFunc) psppire_delimited_text_init,
510 static const GInterfaceInfo tree_model_info = {
511 (GInterfaceInitFunc) __tree_model_init,
516 text_file_type = g_type_register_static (G_TYPE_OBJECT,
517 "PsppireDelimitedText",
520 g_type_add_interface_static (text_file_type, GTK_TYPE_TREE_MODEL,
524 return text_file_type;
529 psppire_delimited_text_class_init (PsppireDelimitedTextClass *class)
531 GObjectClass *object_class;
533 parent_class = g_type_class_peek_parent (class);
534 object_class = (GObjectClass*) class;
536 GParamSpec *first_line_spec =
537 g_param_spec_int ("first-line",
539 P_("The first line to be considered."),
543 GParamSpec *delimiters_spec =
544 g_param_spec_pointer ("delimiters",
546 P_("A GSList of gunichars which delimit the fields."),
549 GParamSpec *child_spec =
550 g_param_spec_object ("child",
552 P_("The GtkTextModel which this object wraps."),
554 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
556 object_class->set_property = psppire_delimited_text_set_property;
557 object_class->get_property = psppire_delimited_text_get_property;
559 g_object_class_install_property (object_class,
563 g_object_class_install_property (object_class,
567 g_object_class_install_property (object_class,
571 object_class->finalize = psppire_delimited_text_finalize;
572 object_class->dispose = psppire_delimited_text_dispose;
577 psppire_delimited_text_init (PsppireDelimitedText *text_file)
579 text_file->child = NULL;
580 text_file->first_line = 0;
581 text_file->delimiters = g_slist_prepend (NULL, GINT_TO_POINTER (':'));
583 text_file->const_cache.string = NULL;
584 text_file->const_cache.length = 0;
585 text_file->cache_row = -1;
586 memset (text_file->cache_starts, 0, sizeof text_file->cache_starts);
588 text_file->max_delimiters = 0;
590 text_file->dispose_has_run = FALSE;
591 text_file->stamp = g_random_int ();
595 PsppireDelimitedText *
596 psppire_delimited_text_new (GtkTreeModel *child)
599 g_object_new (PSPPIRE_TYPE_DELIMITED_TEXT,
605 psppire_delimited_text_finalize (GObject *object)
607 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (object);
609 g_slist_free (tf->delimiters);
611 ss_dealloc (&tf->const_cache);
614 (* parent_class->finalize) (object);
619 psppire_delimited_text_dispose (GObject *object)
621 PsppireDelimitedText *ds = PSPPIRE_DELIMITED_TEXT (object);
623 if (ds->dispose_has_run)
627 (* parent_class->dispose) (object);
629 ds->dispose_has_run = TRUE;