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);
71 // g_print ("Max Number of delimiters per row: %d\n", tf->max_delimiters);
75 psppire_delimited_text_set_property (GObject *object,
80 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (object);
85 tf->first_line = g_value_get_int (value);
86 if (tf->const_cache.string)
88 ss_dealloc (&tf->const_cache);
93 tf->child = g_value_get_object (value);
96 g_slist_free (tf->delimiters);
97 tf->delimiters = g_slist_copy (g_value_get_pointer (value));
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 psppire_delimited_text_get_property (GObject *object,
114 PsppireDelimitedText *text_file = PSPPIRE_DELIMITED_TEXT (object);
118 case PROP_FIRST_LINE:
119 g_value_set_int (value, text_file->first_line);
121 case PROP_DELIMITERS:
122 g_value_set_pointer (value, text_file->delimiters);
125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131 static void psppire_delimited_text_init (PsppireDelimitedText *text_file);
132 static void psppire_delimited_text_class_init (PsppireDelimitedTextClass *class);
134 static void psppire_delimited_text_finalize (GObject *object);
135 static void psppire_delimited_text_dispose (GObject *object);
137 static GObjectClass *parent_class = NULL;
141 __tree_get_iter (GtkTreeModel *tree_model,
145 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
149 // g_print ("%s:%d %s %s\n", __FILE__, __LINE__, __FUNCTION__, gtk_tree_path_to_string (path));
151 gint *indices = gtk_tree_path_get_indices (path);
158 gint children = gtk_tree_model_iter_n_children (file->child, NULL);
160 if (n >= children - file->first_line)
163 // g_print ("%s:%d %s %d Children: %d\n", __FILE__, __LINE__, __FUNCTION__, n, children);
165 iter->user_data = GINT_TO_POINTER (n);
166 iter->stamp = file->stamp;
173 __tree_iter_next (GtkTreeModel *tree_model,
176 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
177 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
179 gint n = GPOINTER_TO_INT (iter->user_data);
181 // g_print ("%s:%d %s %d\n", __FILE__, __LINE__, __FUNCTION__, n);
183 gint children = gtk_tree_model_iter_n_children (file->child, NULL);
185 if (n + 1 >= children - file->first_line)
188 iter->user_data = GINT_TO_POINTER (n + 1);
195 __tree_get_column_type (GtkTreeModel *tree_model,
198 // g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
202 return G_TYPE_STRING;
206 __iter_has_child (GtkTreeModel *tree_model,
209 g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
215 __iter_parent (GtkTreeModel *tree_model,
219 g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
224 __tree_get_path (GtkTreeModel *tree_model,
227 // g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
228 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
229 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
231 gint n = GPOINTER_TO_INT (iter->user_data);
233 gint children = gtk_tree_model_iter_n_children (file->child, NULL);
235 if (n >= children - file->first_line)
238 return gtk_tree_path_new_from_indices (n, -1);
243 __iter_children (GtkTreeModel *tree_model,
247 g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
253 __tree_model_iter_n_children (GtkTreeModel *tree_model,
256 g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
257 g_assert (iter == NULL);
261 static GtkTreeModelFlags
262 __tree_model_get_flags (GtkTreeModel *model)
264 // g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
265 g_return_val_if_fail (PSPPIRE_IS_DELIMITED_TEXT (model), (GtkTreeModelFlags) 0);
267 return GTK_TREE_MODEL_LIST_ONLY;
271 __tree_model_get_n_columns (GtkTreeModel *tree_model)
273 // g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
274 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (tree_model);
276 /* + 1 for the trailing field and +1 for the leading line number column */
277 return tf->max_delimiters + 1 + 1;
282 __iter_nth_child (GtkTreeModel *tree_model,
287 // g_print ("%s:%d %s %d\n", __FILE__, __LINE__, __FUNCTION__, n);
288 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
290 g_assert (parent == NULL);
292 g_return_val_if_fail (file, FALSE);
294 gint children = gtk_tree_model_iter_n_children (file->child, NULL);
296 if (n >= children - file->first_line)
299 iter->user_data = NULL;
303 iter->user_data = GINT_TO_POINTER (n);
304 iter->stamp = file->stamp;
310 /* Split row N into it's delimited fields (if it is not already cached)
311 and set this row as the current cache. */
313 split_row_into_fields (PsppireDelimitedText *file, gint n)
315 if (n != file->cache_row)
317 if (file->const_cache.string)
319 ss_dealloc (&file->const_cache);
321 ss_alloc_substring (&file->const_cache, PSPPIRE_TEXT_FILE (file->child)->lines[n]);
322 file->cache = file->const_cache;
324 file->cache_starts[0] = file->cache.string;
326 UINT32_MAX != ss_first_mb (file->cache);
327 ss_get_mb (&file->cache))
329 ucs4_t xx = ss_first_mb (file->cache);
331 for (del = file->delimiters; del; del = g_slist_next (del))
333 if (xx == GPOINTER_TO_INT (del->data))
336 int char_len = ss_first_mblen (file->cache);
337 file->cache_starts[field] = file->cache.string + char_len;
340 file->cache.string[char_len - 1] = '\0';
353 psppire_delimited_text_get_header_title (PsppireDelimitedText *file, gint column)
355 if (file->first_line <= 0)
358 split_row_into_fields (file, file->first_line - 1);
360 return file->cache_starts [column];
364 __get_value (GtkTreeModel *tree_model,
369 // g_print ("%s:%d %s Col: %d\n", __FILE__, __LINE__, __FUNCTION__, column);
370 PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
372 g_return_if_fail (iter->stamp == file->stamp);
374 gint n = GPOINTER_TO_INT (iter->user_data) + file->first_line;
376 // g_print ("%s:%d Row: %d\n", __FILE__, __LINE__, n);
380 g_value_init (value, G_TYPE_INT);
381 g_value_set_int (value, n + 1);
385 g_value_init (value, G_TYPE_STRING);
387 split_row_into_fields (file, n);
389 g_value_set_string (value, file->cache_starts [column - 1]);
394 __tree_model_init (GtkTreeModelIface *iface)
396 iface->get_flags = __tree_model_get_flags;
397 iface->get_n_columns = __tree_model_get_n_columns ;
398 iface->get_column_type = __tree_get_column_type;
399 iface->get_iter = __tree_get_iter;
400 iface->iter_next = __tree_iter_next;
401 iface->get_path = __tree_get_path;
402 iface->get_value = __get_value;
404 iface->iter_children = __iter_children;
405 iface->iter_has_child = __iter_has_child;
406 iface->iter_n_children = __tree_model_iter_n_children;
407 iface->iter_nth_child = __iter_nth_child;
408 iface->iter_parent = __iter_parent;
413 psppire_delimited_text_get_type (void)
415 static GType text_file_type = 0;
419 static const GTypeInfo text_file_info =
421 sizeof (PsppireDelimitedTextClass),
422 NULL, /* base_init */
423 NULL, /* base_finalize */
424 (GClassInitFunc) psppire_delimited_text_class_init,
425 NULL, /* class_finalize */
426 NULL, /* class_data */
427 sizeof (PsppireDelimitedText),
429 (GInstanceInitFunc) psppire_delimited_text_init,
432 static const GInterfaceInfo tree_model_info = {
433 (GInterfaceInitFunc) __tree_model_init,
438 text_file_type = g_type_register_static (G_TYPE_OBJECT,
439 "PsppireDelimitedText",
442 g_type_add_interface_static (text_file_type, GTK_TYPE_TREE_MODEL,
446 return text_file_type;
451 psppire_delimited_text_class_init (PsppireDelimitedTextClass *class)
453 GObjectClass *object_class;
455 parent_class = g_type_class_peek_parent (class);
456 object_class = (GObjectClass*) class;
458 GParamSpec *first_line_spec =
459 g_param_spec_int ("first-line",
461 P_("The first line to be considered."),
465 GParamSpec *delimiters_spec =
466 g_param_spec_pointer ("delimiters",
468 P_("A GSList of gunichars which delimit the fields."),
471 GParamSpec *child_spec =
472 g_param_spec_object ("child",
474 P_("The GtkTextModel which this object wraps."),
476 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
478 object_class->set_property = psppire_delimited_text_set_property;
479 object_class->get_property = psppire_delimited_text_get_property;
481 g_object_class_install_property (object_class,
485 g_object_class_install_property (object_class,
489 g_object_class_install_property (object_class,
493 object_class->finalize = psppire_delimited_text_finalize;
494 object_class->dispose = psppire_delimited_text_dispose;
499 psppire_delimited_text_init (PsppireDelimitedText *text_file)
501 text_file->child = NULL;
502 text_file->first_line = 0;
503 text_file->delimiters = g_slist_prepend (NULL, GINT_TO_POINTER (':'));
505 text_file->const_cache.string = NULL;
506 text_file->const_cache.length = 0;
507 text_file->cache_row = -1;
509 text_file->max_delimiters = 0;
511 text_file->dispose_has_run = FALSE;
512 text_file->stamp = g_random_int ();
516 PsppireDelimitedText *
517 psppire_delimited_text_new (GtkTreeModel *child)
519 PsppireDelimitedText *retval =
520 g_object_new (PSPPIRE_TYPE_DELIMITED_TEXT,
528 psppire_delimited_text_finalize (GObject *object)
530 PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (object);
532 g_slist_free (tf->delimiters);
534 ss_dealloc (&tf->const_cache);
537 (* parent_class->finalize) (object);
542 psppire_delimited_text_dispose (GObject *object)
544 PsppireDelimitedText *ds = PSPPIRE_DELIMITED_TEXT (object);
546 if (ds->dispose_has_run)
550 (* parent_class->dispose) (object);
552 ds->dispose_has_run = TRUE;