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"
39 count_delims (PsppireDelimitedText *tf)
43 tf->max_delimiters = 0;
46 for (valid = gtk_tree_model_get_iter_first (tf->child, &iter);
48 valid = gtk_tree_model_iter_next (tf->child, &iter))
50 // FIXME: Box these lines to avoid constant allocation/deallocation
52 gtk_tree_model_get (tf->child, &iter, 1, &foo, -1);
59 for (del = tf->delimiters; del; del = g_slist_next (del))
61 if (*line == GPOINTER_TO_INT (del->data))
66 tf->max_delimiters = MAX (tf->max_delimiters, count);
74 cache_invalidate (PsppireDelimitedText *tf)
76 memset (tf->cache_starts, 0, 512);
77 if (tf->const_cache.string)
79 ss_dealloc (&tf->const_cache);
80 tf->const_cache.string = NULL;
86 psppire_delimited_text_set_property (GObject *object,
91 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (object);
96 tf->first_line = g_value_get_int (value);
99 tf->child = g_value_get_object (value);
100 g_return_if_fail (PSPPIRE_IS_TEXT_FILE (tf->child));
102 case PROP_DELIMITERS:
103 g_slist_free (tf->delimiters);
104 tf->delimiters = g_slist_copy (g_value_get_pointer (value));
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 cache_invalidate (tf);
116 psppire_delimited_text_get_property (GObject *object,
121 PsppireDelimitedText *text_file = PSPPIRE_DELIMITED_TEXT (object);
125 case PROP_FIRST_LINE:
126 g_value_set_int (value, text_file->first_line);
128 case PROP_DELIMITERS:
129 g_value_set_pointer (value, text_file->delimiters);
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 static void psppire_delimited_text_init (PsppireDelimitedText *text_file);
139 static void psppire_delimited_text_class_init (PsppireDelimitedTextClass *class);
141 static void psppire_delimited_text_finalize (GObject *object);
142 static void psppire_delimited_text_dispose (GObject *object);
144 static GObjectClass *parent_class = NULL;
147 n_lines (PsppireDelimitedText *file)
149 PsppireTextFile *child = PSPPIRE_TEXT_FILE (file->child);
151 return child->maximum_lines;
155 __tree_get_iter (GtkTreeModel *tree_model,
159 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
164 gint *indices = gtk_tree_path_get_indices (path);
171 gint children = n_lines (file);
173 if (n >= children - file->first_line)
177 iter->user_data = GINT_TO_POINTER (n);
178 iter->stamp = file->stamp;
185 __tree_iter_next (GtkTreeModel *tree_model,
188 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
189 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
191 gint n = GPOINTER_TO_INT (iter->user_data);
194 gint children = n_lines (file);
196 if (n + 1 >= children - file->first_line)
199 iter->user_data = GINT_TO_POINTER (n + 1);
206 __tree_get_column_type (GtkTreeModel *tree_model,
212 return G_TYPE_STRING;
216 __iter_has_child (GtkTreeModel *tree_model,
224 __iter_parent (GtkTreeModel *tree_model,
232 __tree_get_path (GtkTreeModel *tree_model,
235 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
236 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
238 gint n = GPOINTER_TO_INT (iter->user_data);
240 gint children = n_lines (file);
242 if (n >= children - file->first_line)
245 return gtk_tree_path_new_from_indices (n, -1);
250 __iter_children (GtkTreeModel *tree_model,
259 __tree_model_iter_n_children (GtkTreeModel *tree_model,
262 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
263 g_assert (iter == NULL);
265 gint children = n_lines (file);
267 return children - file->first_line;
270 static GtkTreeModelFlags
271 __tree_model_get_flags (GtkTreeModel *model)
273 g_return_val_if_fail (PSPPIRE_IS_DELIMITED_TEXT (model), (GtkTreeModelFlags) 0);
275 return GTK_TREE_MODEL_LIST_ONLY;
279 __tree_model_get_n_columns (GtkTreeModel *tree_model)
281 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (tree_model);
283 /* + 1 for the trailing field and +1 for the leading line number column */
284 return tf->max_delimiters + 1 + 1;
289 __iter_nth_child (GtkTreeModel *tree_model,
294 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
296 g_assert (parent == NULL);
298 g_return_val_if_fail (file, FALSE);
300 gint children = gtk_tree_model_iter_n_children (file->child, NULL);
302 if (n >= children - file->first_line)
305 iter->user_data = NULL;
309 iter->user_data = GINT_TO_POINTER (n);
310 iter->stamp = file->stamp;
316 /* Split row N into it's delimited fields (if it is not already cached)
317 and set this row as the current cache. */
319 split_row_into_fields (PsppireDelimitedText *file, gint n)
321 if (n == file->cache_row) /* Cache hit */
326 memset (file->cache_starts, 0, 512);
328 if (file->const_cache.string)
330 ss_dealloc (&file->const_cache);
332 ss_alloc_substring_pool (&file->const_cache,
333 PSPPIRE_TEXT_FILE (file->child)->lines[n], NULL);
334 struct substring cs = file->const_cache;
336 file->cache_starts[0] = cs.string;
338 UINT32_MAX != ss_first_mb (cs);
341 ucs4_t xx = ss_first_mb (cs);
343 for (del = file->delimiters; del; del = g_slist_next (del))
345 if (xx == GPOINTER_TO_INT (del->data))
348 int char_len = ss_first_mblen (cs);
349 file->cache_starts[field] = cs.string + char_len;
352 cs.string[char_len - 1] = '\0';
364 psppire_delimited_text_get_header_title (PsppireDelimitedText *file, gint column)
366 if (file->first_line <= 0)
369 split_row_into_fields (file, file->first_line - 1);
371 return file->cache_starts [column];
375 __get_value (GtkTreeModel *tree_model,
380 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
382 g_return_if_fail (iter->stamp == file->stamp);
384 gint n = GPOINTER_TO_INT (iter->user_data) + file->first_line;
389 g_value_init (value, G_TYPE_INT);
390 g_value_set_int (value, n + 1);
394 g_value_init (value, G_TYPE_STRING);
396 split_row_into_fields (file, n);
398 g_value_set_string (value, file->cache_starts [column - 1]);
403 __tree_model_init (GtkTreeModelIface *iface)
405 iface->get_flags = __tree_model_get_flags;
406 iface->get_n_columns = __tree_model_get_n_columns ;
407 iface->get_column_type = __tree_get_column_type;
408 iface->get_iter = __tree_get_iter;
409 iface->iter_next = __tree_iter_next;
410 iface->get_path = __tree_get_path;
411 iface->get_value = __get_value;
413 iface->iter_children = __iter_children;
414 iface->iter_has_child = __iter_has_child;
415 iface->iter_n_children = __tree_model_iter_n_children;
416 iface->iter_nth_child = __iter_nth_child;
417 iface->iter_parent = __iter_parent;
422 psppire_delimited_text_get_type (void)
424 static GType text_file_type = 0;
428 static const GTypeInfo text_file_info =
430 sizeof (PsppireDelimitedTextClass),
431 NULL, /* base_init */
432 NULL, /* base_finalize */
433 (GClassInitFunc) psppire_delimited_text_class_init,
434 NULL, /* class_finalize */
435 NULL, /* class_data */
436 sizeof (PsppireDelimitedText),
438 (GInstanceInitFunc) psppire_delimited_text_init,
441 static const GInterfaceInfo tree_model_info = {
442 (GInterfaceInitFunc) __tree_model_init,
447 text_file_type = g_type_register_static (G_TYPE_OBJECT,
448 "PsppireDelimitedText",
451 g_type_add_interface_static (text_file_type, GTK_TYPE_TREE_MODEL,
455 return text_file_type;
460 psppire_delimited_text_class_init (PsppireDelimitedTextClass *class)
462 GObjectClass *object_class;
464 parent_class = g_type_class_peek_parent (class);
465 object_class = (GObjectClass*) class;
467 GParamSpec *first_line_spec =
468 g_param_spec_int ("first-line",
470 P_("The first line to be considered."),
474 GParamSpec *delimiters_spec =
475 g_param_spec_pointer ("delimiters",
477 P_("A GSList of gunichars which delimit the fields."),
480 GParamSpec *child_spec =
481 g_param_spec_object ("child",
483 P_("The GtkTextModel which this object wraps."),
485 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
487 object_class->set_property = psppire_delimited_text_set_property;
488 object_class->get_property = psppire_delimited_text_get_property;
490 g_object_class_install_property (object_class,
494 g_object_class_install_property (object_class,
498 g_object_class_install_property (object_class,
502 object_class->finalize = psppire_delimited_text_finalize;
503 object_class->dispose = psppire_delimited_text_dispose;
508 psppire_delimited_text_init (PsppireDelimitedText *text_file)
510 text_file->child = NULL;
511 text_file->first_line = 0;
512 text_file->delimiters = g_slist_prepend (NULL, GINT_TO_POINTER (':'));
514 text_file->const_cache.string = NULL;
515 text_file->const_cache.length = 0;
516 text_file->cache_row = -1;
517 memset (text_file->cache_starts, 0, 512);
519 text_file->max_delimiters = 0;
521 text_file->dispose_has_run = FALSE;
522 text_file->stamp = g_random_int ();
527 psppire_delimited_text_new (GtkTreeModel *child)
529 PsppireDelimitedText *retval =
530 g_object_new (PSPPIRE_TYPE_DELIMITED_TEXT,
538 psppire_delimited_text_finalize (GObject *object)
540 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (object);
542 g_slist_free (tf->delimiters);
544 ss_dealloc (&tf->const_cache);
547 (* parent_class->finalize) (object);
552 psppire_delimited_text_dispose (GObject *object)
554 PsppireDelimitedText *ds = PSPPIRE_DELIMITED_TEXT (object);
556 if (ds->dispose_has_run)
560 (* parent_class->dispose) (object);
562 ds->dispose_has_run = TRUE;