PsppireDelimitedText: Remove a useless member variable
[pspp] / src / ui / gui / psppire-delimited-text.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2017 Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18 #include <gettext.h>
19 #define _(msgid) gettext (msgid)
20 #define P_(msgid) msgid
21
22 #include "psppire-delimited-text.h"
23 #include "psppire-text-file.h"
24 #include "libpspp/str.h"
25 #include "libpspp/i18n.h"
26
27 #include <gtk/gtk.h>
28
29 /* Properties */
30 enum
31   {
32     PROP_0,
33     PROP_CHILD,
34     PROP_DELIMITERS,
35     PROP_FIRST_LINE
36   };
37
38 static void
39 count_delims (PsppireDelimitedText *tf)
40 {
41   if (tf->child)
42     {
43       tf->max_delimiters = 0;
44       GtkTreeIter iter;
45       gboolean valid;
46       for (valid = gtk_tree_model_get_iter_first (tf->child, &iter);
47            valid;
48            valid = gtk_tree_model_iter_next (tf->child, &iter))
49         {
50           // FIXME: Box these lines to avoid constant allocation/deallocation
51           gchar *foo = 0;
52           gtk_tree_model_get (tf->child, &iter, 1, &foo, -1);
53           {
54             char *line = foo;
55             gint count = 0;
56             while (*line)
57               {
58                 GSList *del;
59                 for (del = tf->delimiters; del; del = g_slist_next (del))
60                   {
61                     if (*line == GPOINTER_TO_INT (del->data))
62                       count++;
63                   }
64                 line++;
65               }
66             tf->max_delimiters = MAX (tf->max_delimiters, count);
67           }
68           g_free (foo);
69         }
70     }
71   //  g_print ("Max Number of delimiters per row: %d\n", tf->max_delimiters);
72 }
73
74 static void
75 psppire_delimited_text_set_property (GObject         *object,
76                                 guint            prop_id,
77                                 const GValue    *value,
78                                 GParamSpec      *pspec)
79 {
80   PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (object);
81
82   switch (prop_id)
83     {
84     case PROP_FIRST_LINE:
85       tf->first_line = g_value_get_int (value);
86       if (tf->const_cache.string)
87         {
88           ss_dealloc (&tf->const_cache);
89           tf->cache_row = -1;
90         }
91       break;
92     case PROP_CHILD:
93       tf->child = g_value_get_object (value);
94       break;
95     case PROP_DELIMITERS:
96       g_slist_free (tf->delimiters);
97       tf->delimiters =  g_slist_copy (g_value_get_pointer (value));
98       break;
99     default:
100       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
101       break;
102     };
103
104   if (tf->child)
105     count_delims (tf);
106 }
107
108 static void
109 psppire_delimited_text_get_property (GObject         *object,
110                                 guint            prop_id,
111                                 GValue          *value,
112                                 GParamSpec      *pspec)
113 {
114   PsppireDelimitedText *text_file = PSPPIRE_DELIMITED_TEXT (object);
115
116   switch (prop_id)
117     {
118     case PROP_FIRST_LINE:
119       g_value_set_int (value, text_file->first_line);
120       break;
121     case PROP_DELIMITERS:
122       g_value_set_pointer (value, text_file->delimiters);
123       break;
124     default:
125       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126       break;
127     };
128 }
129
130
131 static void psppire_delimited_text_init            (PsppireDelimitedText      *text_file);
132 static void psppire_delimited_text_class_init      (PsppireDelimitedTextClass *class);
133
134 static void psppire_delimited_text_finalize        (GObject           *object);
135 static void psppire_delimited_text_dispose        (GObject           *object);
136
137 static GObjectClass *parent_class = NULL;
138
139
140 static gboolean
141 __tree_get_iter (GtkTreeModel *tree_model,
142                  GtkTreeIter *iter,
143                  GtkTreePath *path)
144 {
145   PsppireDelimitedText *file = PSPPIRE_DELIMITED_TEXT (tree_model);
146   if (path == NULL)
147     return FALSE;
148
149   //  g_print ("%s:%d %s %s\n", __FILE__, __LINE__, __FUNCTION__, gtk_tree_path_to_string (path));
150
151   gint *indices = gtk_tree_path_get_indices (path);
152
153   if (!indices)
154     return FALSE;
155
156   gint n = *indices;
157
158   gint children = gtk_tree_model_iter_n_children (file->child, NULL);
159
160   if (n >= children - file->first_line)
161     return FALSE;
162
163   //  g_print ("%s:%d %s  %d Children: %d\n", __FILE__, __LINE__, __FUNCTION__, n, children);
164
165   iter->user_data = GINT_TO_POINTER (n);
166   iter->stamp = file->stamp;
167
168   return TRUE;
169 }
170
171
172 static gboolean
173 __tree_iter_next (GtkTreeModel *tree_model,
174                   GtkTreeIter *iter)
175 {
176   PsppireDelimitedText *file  = PSPPIRE_DELIMITED_TEXT (tree_model);
177   g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
178
179   gint n = GPOINTER_TO_INT (iter->user_data);
180
181   //  g_print ("%s:%d %s %d\n", __FILE__, __LINE__, __FUNCTION__, n);
182
183   gint children = gtk_tree_model_iter_n_children (file->child, NULL);
184
185   if (n + 1 >= children - file->first_line)
186     return FALSE;
187
188   iter->user_data = GINT_TO_POINTER (n + 1);
189
190   return TRUE;
191 }
192
193
194 static GType
195 __tree_get_column_type (GtkTreeModel *tree_model,
196                         gint          index)
197 {
198   //  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
199   if (index == 0)
200     return G_TYPE_INT;
201
202   return G_TYPE_STRING;
203 }
204
205 static gboolean
206 __iter_has_child (GtkTreeModel *tree_model,
207                   GtkTreeIter  *iter)
208 {
209   g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
210   return 0;
211 }
212
213
214 static gboolean
215 __iter_parent     (GtkTreeModel *tree_model,
216                    GtkTreeIter  *iter,
217                    GtkTreeIter  *child)
218 {
219   g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
220   return 0;
221 }
222
223 static GtkTreePath *
224 __tree_get_path (GtkTreeModel *tree_model,
225                  GtkTreeIter  *iter)
226 {
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);
230
231   gint n = GPOINTER_TO_INT (iter->user_data);
232
233   gint children = gtk_tree_model_iter_n_children (file->child, NULL);
234
235   if (n >= children - file->first_line)
236     return NULL;
237
238   return gtk_tree_path_new_from_indices (n, -1);
239 }
240
241
242 static gboolean
243 __iter_children (GtkTreeModel *tree_model,
244                               GtkTreeIter *iter,
245                               GtkTreeIter *parent)
246 {
247   g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
248   return 0;
249 }
250
251
252 static gint
253 __tree_model_iter_n_children (GtkTreeModel *tree_model,
254                               GtkTreeIter *iter)
255 {
256   g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
257   g_assert (iter == NULL);
258   return 0;
259 }
260
261 static GtkTreeModelFlags
262 __tree_model_get_flags (GtkTreeModel *model)
263 {
264   //  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
265   g_return_val_if_fail (PSPPIRE_IS_DELIMITED_TEXT (model), (GtkTreeModelFlags) 0);
266
267   return GTK_TREE_MODEL_LIST_ONLY;
268 }
269
270 static gint
271 __tree_model_get_n_columns (GtkTreeModel *tree_model)
272 {
273   //  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
274   PsppireDelimitedText *tf  = PSPPIRE_DELIMITED_TEXT (tree_model);
275
276   /* + 1 for the trailing field and +1 for the leading line number column */
277   return tf->max_delimiters + 1 + 1;
278 }
279
280
281 static gboolean
282 __iter_nth_child (GtkTreeModel *tree_model,
283                   GtkTreeIter *iter,
284                   GtkTreeIter *parent,
285                   gint n)
286 {
287   //  g_print ("%s:%d %s %d\n", __FILE__, __LINE__, __FUNCTION__, n);
288   PsppireDelimitedText *file  = PSPPIRE_DELIMITED_TEXT (tree_model);
289
290   g_assert (parent == NULL);
291
292   g_return_val_if_fail (file, FALSE);
293
294   gint children = gtk_tree_model_iter_n_children (file->child, NULL);
295
296   if (n >= children - file->first_line)
297     {
298       iter->stamp = -1;
299       iter->user_data = NULL;
300       return FALSE;
301     }
302
303   iter->user_data = GINT_TO_POINTER (n);
304   iter->stamp = file->stamp;
305
306   return TRUE;
307 }
308
309
310 /* Split row N into it's delimited fields (if it is not already cached)
311    and set this row as the current cache. */
312 static void
313 split_row_into_fields (PsppireDelimitedText *file, gint n)
314 {
315   if (n == file->cache_row)  /* Cache hit */
316     return;
317
318   /* Cache miss */
319   if (file->const_cache.string)
320     {
321       ss_dealloc (&file->const_cache);
322     }
323   ss_alloc_substring (&file->const_cache, PSPPIRE_TEXT_FILE (file->child)->lines[n]);
324   struct substring cs = file->const_cache;
325   int field = 0;
326   file->cache_starts[0] = cs.string;
327   for (;
328        UINT32_MAX != ss_first_mb (cs);
329        ss_get_mb (&cs))
330     {
331       ucs4_t xx = ss_first_mb (cs);
332       GSList *del;
333       for (del = file->delimiters; del; del = g_slist_next (del))
334         {
335           if (xx == GPOINTER_TO_INT (del->data))
336             {
337               field++;
338               int char_len = ss_first_mblen (cs);
339               file->cache_starts[field] = cs.string + char_len;
340               while (char_len > 0)
341                 {
342                   cs.string[char_len - 1] = '\0';
343                   char_len--;
344                 }
345               break;
346             }
347         }
348     }
349
350   file->cache_row = n;
351 }
352
353 const gchar *
354 psppire_delimited_text_get_header_title (PsppireDelimitedText *file, gint column)
355 {
356   if (file->first_line <= 0)
357     return NULL;
358
359   split_row_into_fields (file, file->first_line - 1);
360
361   return file->cache_starts [column];
362 }
363
364 static void
365 __get_value (GtkTreeModel *tree_model,
366              GtkTreeIter *iter,
367              gint column,
368              GValue *value)
369 {
370   //  g_print ("%s:%d %s Col: %d\n", __FILE__, __LINE__, __FUNCTION__, column);
371   PsppireDelimitedText *file  = PSPPIRE_DELIMITED_TEXT (tree_model);
372
373   g_return_if_fail (iter->stamp == file->stamp);
374
375   gint n = GPOINTER_TO_INT (iter->user_data) + file->first_line;
376
377   //  g_print ("%s:%d Row: %d\n", __FILE__, __LINE__, n);
378
379   if (column == 0)
380     {
381       g_value_init (value, G_TYPE_INT);
382       g_value_set_int (value, n + 1);
383       return;
384     }
385
386   g_value_init (value, G_TYPE_STRING);
387
388   split_row_into_fields (file, n);
389
390   g_value_set_string (value, file->cache_starts [column - 1]);
391 }
392
393
394 static void
395 __tree_model_init (GtkTreeModelIface *iface)
396 {
397   iface->get_flags       = __tree_model_get_flags;
398   iface->get_n_columns   = __tree_model_get_n_columns ;
399   iface->get_column_type = __tree_get_column_type;
400   iface->get_iter        = __tree_get_iter;
401   iface->iter_next       = __tree_iter_next;
402   iface->get_path        = __tree_get_path;
403   iface->get_value       = __get_value;
404
405   iface->iter_children   = __iter_children;
406   iface->iter_has_child  = __iter_has_child;
407   iface->iter_n_children = __tree_model_iter_n_children;
408   iface->iter_nth_child  = __iter_nth_child;
409   iface->iter_parent     = __iter_parent;
410 }
411
412
413 GType
414 psppire_delimited_text_get_type (void)
415 {
416   static GType text_file_type = 0;
417
418   if (!text_file_type)
419     {
420       static const GTypeInfo text_file_info =
421         {
422           sizeof (PsppireDelimitedTextClass),
423           NULL,         /* base_init */
424           NULL,         /* base_finalize */
425           (GClassInitFunc) psppire_delimited_text_class_init,
426           NULL,         /* class_finalize */
427           NULL,         /* class_data */
428           sizeof (PsppireDelimitedText),
429           0,
430           (GInstanceInitFunc) psppire_delimited_text_init,
431         };
432
433       static const GInterfaceInfo tree_model_info = {
434         (GInterfaceInitFunc) __tree_model_init,
435         NULL,
436         NULL
437       };
438
439       text_file_type = g_type_register_static (G_TYPE_OBJECT,
440                                                "PsppireDelimitedText",
441                                                &text_file_info, 0);
442
443       g_type_add_interface_static (text_file_type, GTK_TYPE_TREE_MODEL,
444                                    &tree_model_info);
445     }
446
447   return text_file_type;
448 }
449
450
451 static void
452 psppire_delimited_text_class_init (PsppireDelimitedTextClass *class)
453 {
454   GObjectClass *object_class;
455
456   parent_class = g_type_class_peek_parent (class);
457   object_class = (GObjectClass*) class;
458
459   GParamSpec *first_line_spec =
460     g_param_spec_int ("first-line",
461                       "First Line",
462                       P_("The first line to be considered."),
463                       0, 1000, 0,
464                       G_PARAM_READWRITE);
465
466   GParamSpec *delimiters_spec =
467     g_param_spec_pointer ("delimiters",
468                           "Field Delimiters",
469                           P_("A GSList of gunichars which delimit the fields."),
470                           G_PARAM_READWRITE);
471
472   GParamSpec *child_spec =
473     g_param_spec_object ("child",
474                          "Child Model",
475                          P_("The GtkTextModel which this object wraps."),
476                          GTK_TYPE_TREE_MODEL,
477                          G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
478
479   object_class->set_property = psppire_delimited_text_set_property;
480   object_class->get_property = psppire_delimited_text_get_property;
481
482   g_object_class_install_property (object_class,
483                                    PROP_CHILD,
484                                    child_spec);
485
486   g_object_class_install_property (object_class,
487                                    PROP_DELIMITERS,
488                                    delimiters_spec);
489
490   g_object_class_install_property (object_class,
491                                    PROP_FIRST_LINE,
492                                    first_line_spec);
493
494   object_class->finalize = psppire_delimited_text_finalize;
495   object_class->dispose = psppire_delimited_text_dispose;
496 }
497
498
499 static void
500 psppire_delimited_text_init (PsppireDelimitedText *text_file)
501 {
502   text_file->child = NULL;
503   text_file->first_line = 0;
504   text_file->delimiters = g_slist_prepend (NULL, GINT_TO_POINTER (':'));
505
506   text_file->const_cache.string = NULL;
507   text_file->const_cache.length = 0;
508   text_file->cache_row = -1;
509
510   text_file->max_delimiters = 0;
511
512   text_file->dispose_has_run = FALSE;
513   text_file->stamp = g_random_int ();
514 }
515
516
517 PsppireDelimitedText *
518 psppire_delimited_text_new (GtkTreeModel *child)
519 {
520   PsppireDelimitedText *retval =
521     g_object_new (PSPPIRE_TYPE_DELIMITED_TEXT,
522                   "child", child,
523                   NULL);
524
525   return retval;
526 }
527
528 static void
529 psppire_delimited_text_finalize (GObject *object)
530 {
531   PsppireDelimitedText *tf = PSPPIRE_DELIMITED_TEXT (object);
532
533   g_slist_free (tf->delimiters);
534
535   ss_dealloc (&tf->const_cache);
536
537   /* must chain up */
538   (* parent_class->finalize) (object);
539 }
540
541
542 static void
543 psppire_delimited_text_dispose (GObject *object)
544 {
545   PsppireDelimitedText *ds = PSPPIRE_DELIMITED_TEXT (object);
546
547   if (ds->dispose_has_run)
548     return;
549
550   /* must chain up */
551   (* parent_class->dispose) (object);
552
553   ds->dispose_has_run = TRUE;
554 }