fixed goto case dialog
[pspp] / src / ui / gui / psppire-text-file.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-text-file.h"
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 #include "libpspp/line-reader.h"
28 #include "libpspp/message.h"
29 #include "libpspp/str.h"
30 #include "libpspp/i18n.h"
31
32 #include <gtk/gtk.h>
33
34 /* Properties */
35 enum
36   {
37     PROP_0,
38     PROP_FILE_NAME,
39     PROP_ENCODING,
40     PROP_MAXIMUM_LINES,
41     PROP_LINE_COUNT
42   };
43
44 enum {MAX_LINE_LEN = 16384};  /* Max length of an acceptable line. */
45
46
47 static void
48 read_lines (PsppireTextFile *tf)
49 {
50   if (tf->file_name && 0 != g_strcmp0 ("unset", tf->encoding))
51     {
52       struct line_reader *reader = line_reader_for_file (tf->encoding, tf->file_name, O_RDONLY);
53
54       if (reader == NULL)
55         {
56           msg_error (errno, _("Could not open `%s'"),  tf->file_name);
57           return;
58         }
59
60       struct string input;
61       ds_init_empty (&input);
62       for (tf->line_cnt = 0; tf->line_cnt < MAX_PREVIEW_LINES; tf->line_cnt++)
63         {
64           ds_clear (&input);
65           if (!line_reader_read (reader, &input, MAX_LINE_LEN + 1)
66               || ds_length (&input) > MAX_LINE_LEN)
67             {
68               int i;
69               if (line_reader_eof (reader))
70                 break;
71               else if (line_reader_error (reader))
72                 msg (ME, _("Error reading `%s': %s"),
73                      tf->file_name, strerror (line_reader_error (reader)));
74               else
75                 msg (ME, _("Failed to read `%s', because it contains a line "
76                            "over %d bytes long and therefore appears not to be "
77                            "a text file."),
78                      tf->file_name, MAX_LINE_LEN);
79               line_reader_close (reader);
80               for (i = 0; i < tf->line_cnt; i++)
81                 g_free (&tf->lines[i]);
82               tf->line_cnt = 0;
83               ds_destroy (&input);
84               return;
85             }
86
87           tf->lines[tf->line_cnt]
88             = recode_substring_pool ("UTF-8",
89                                      line_reader_get_encoding (reader),
90                                      input.ss, NULL);
91         }
92       ds_destroy (&input);
93
94       if (tf->line_cnt == 0)
95         {
96           int i;
97           msg (ME, _("`%s' is empty."), tf->file_name);
98           line_reader_close (reader);
99           for (i = 0; i < tf->line_cnt; i++)
100             g_free (&tf->lines[i]);
101           tf->line_cnt = 0;
102           goto done;
103         }
104
105       if (tf->line_cnt < MAX_PREVIEW_LINES)
106         {
107           tf->total_lines = tf->line_cnt;
108           tf->total_is_exact = true;
109         }
110       else
111         {
112           /* Estimate the number of lines in the file. */
113           struct stat s;
114           off_t position = line_reader_tell (reader);
115           if (fstat (line_reader_fileno (reader), &s) == 0 && position > 0)
116             {
117               tf->total_lines = (double) tf->line_cnt / position * s.st_size;
118               tf->total_is_exact = false;
119             }
120           else
121             {
122               tf->total_lines = 0;
123               tf->total_is_exact = true;
124             }
125         }
126     done:
127       line_reader_close (reader);
128     }
129 }
130
131 static void
132 psppire_text_file_set_property (GObject         *object,
133                                 guint            prop_id,
134                                 const GValue    *value,
135                                 GParamSpec      *pspec)
136 {
137   PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
138
139   switch (prop_id)
140     {
141     case PROP_MAXIMUM_LINES:
142       tf->maximum_lines = g_value_get_int (value);
143       break;
144     case PROP_FILE_NAME:
145       tf->file_name = g_value_dup_string (value);
146       read_lines (tf);
147       break;
148     case PROP_ENCODING:
149       g_free (tf->encoding);
150       tf->encoding = g_value_dup_string (value);
151       read_lines (tf);
152       break;
153     default:
154       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
155       break;
156     };
157
158 }
159
160 static void
161 psppire_text_file_get_property (GObject         *object,
162                                 guint            prop_id,
163                                 GValue          *value,
164                                 GParamSpec      *pspec)
165 {
166   PsppireTextFile *text_file = PSPPIRE_TEXT_FILE (object);
167
168   switch (prop_id)
169     {
170     case PROP_MAXIMUM_LINES:
171       g_value_set_int (value, text_file->maximum_lines);
172       break;
173     case PROP_LINE_COUNT:
174       g_value_set_int (value, text_file->line_cnt);
175       break;
176     case PROP_FILE_NAME:
177       g_value_set_string (value, text_file->file_name);
178       break;
179     case PROP_ENCODING:
180       g_value_set_string (value, text_file->encoding);
181       break;
182     default:
183       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184       break;
185     };
186 }
187
188
189 static void psppire_text_file_init            (PsppireTextFile      *text_file);
190 static void psppire_text_file_class_init      (PsppireTextFileClass *class);
191
192 static void psppire_text_file_finalize        (GObject           *object);
193 static void psppire_text_file_dispose        (GObject           *object);
194
195 static GObjectClass *parent_class = NULL;
196
197 static gboolean
198 __tree_get_iter (GtkTreeModel *tree_model,
199                  GtkTreeIter *iter,
200                  GtkTreePath *path)
201 {
202   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
203
204   if (path == NULL)
205     return FALSE;
206
207   gint *indices = gtk_tree_path_get_indices (path);
208
209   gint n = *indices;
210
211   if (n >= file->line_cnt)
212     return FALSE;
213
214   iter->user_data = GINT_TO_POINTER (n);
215   iter->stamp = file->stamp;
216
217   return TRUE;
218 }
219
220
221 static gboolean
222 __tree_iter_next (GtkTreeModel *tree_model,
223                   GtkTreeIter *iter)
224 {
225   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
226   g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
227
228   gint n = GPOINTER_TO_INT (iter->user_data) + 1;
229
230   if (n >= file->line_cnt)
231     return FALSE;
232
233   iter->user_data = GINT_TO_POINTER (n);
234
235   return TRUE;
236 }
237
238
239 static GType
240 __tree_get_column_type (GtkTreeModel *tree_model,
241                         gint          index)
242 {
243   if (index == 0)
244     return G_TYPE_INT;
245
246   return G_TYPE_STRING;
247 }
248
249 static gboolean
250 __iter_has_child (GtkTreeModel *tree_model,
251                   GtkTreeIter  *iter)
252 {
253   return 0;
254 }
255
256
257 static gboolean
258 __iter_parent     (GtkTreeModel *tree_model,
259                    GtkTreeIter  *iter,
260                    GtkTreeIter  *child)
261 {
262   return 0;
263 }
264
265 static GtkTreePath *
266 __tree_get_path (GtkTreeModel *tree_model,
267                  GtkTreeIter  *iter)
268 {
269   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
270   g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
271
272   gint n = GPOINTER_TO_INT (iter->user_data);
273
274   return gtk_tree_path_new_from_indices (n, -1);
275 }
276
277
278 static gboolean
279 __iter_children (GtkTreeModel *tree_model,
280                               GtkTreeIter *iter,
281                               GtkTreeIter *parent)
282 {
283   return 0;
284 }
285
286
287 static gint
288 __tree_model_iter_n_children (GtkTreeModel *tree_model,
289                               GtkTreeIter *iter)
290 {
291   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
292   g_assert (iter == NULL);
293   return file->line_cnt;
294 }
295
296 static GtkTreeModelFlags
297 __tree_model_get_flags (GtkTreeModel *model)
298 {
299   g_return_val_if_fail (PSPPIRE_IS_TEXT_FILE (model), (GtkTreeModelFlags) 0);
300
301   return GTK_TREE_MODEL_LIST_ONLY;
302 }
303
304 static gint
305 __tree_model_get_n_columns (GtkTreeModel *tree_model)
306 {
307   return 2;
308 }
309
310
311 static gboolean
312 __iter_nth_child (GtkTreeModel *tree_model,
313                   GtkTreeIter *iter,
314                   GtkTreeIter *parent,
315                   gint n)
316 {
317   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
318
319   g_assert (parent == NULL);
320
321   g_return_val_if_fail (file, FALSE);
322
323   if (n >= file->line_cnt)
324     {
325       iter->stamp = -1;
326       iter->user_data = NULL;
327       return FALSE;
328     }
329
330   iter->user_data = GINT_TO_POINTER (n);
331   iter->stamp = file->stamp;
332
333   return TRUE;
334 }
335
336
337 static void
338 __get_value (GtkTreeModel *tree_model,
339              GtkTreeIter *iter,
340              gint column,
341              GValue *value)
342 {
343   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
344
345   g_return_if_fail (iter->stamp == file->stamp);
346
347   gint n = GPOINTER_TO_INT (iter->user_data);
348
349   g_return_if_fail (n < file->line_cnt);
350
351   if (column == 0)
352     {
353       g_value_init (value, G_TYPE_INT);
354       g_value_set_int (value, n + 1);
355       return;
356     }
357
358   g_value_init (value, G_TYPE_STRING);
359
360   if (column == 1)
361     {
362       char *s = ss_xstrdup (file->lines[n]);
363       g_value_set_string (value, s);
364       free (s);
365       return;
366     }
367
368   g_assert_not_reached ();
369 }
370
371
372 static void
373 __tree_model_init (GtkTreeModelIface *iface)
374 {
375   iface->get_flags       = __tree_model_get_flags;
376   iface->get_n_columns   = __tree_model_get_n_columns ;
377   iface->get_column_type = __tree_get_column_type;
378   iface->get_iter        = __tree_get_iter;
379   iface->iter_next       = __tree_iter_next;
380   iface->get_path        = __tree_get_path;
381   iface->get_value       = __get_value;
382
383   iface->iter_children   = __iter_children;
384   iface->iter_has_child  = __iter_has_child;
385   iface->iter_n_children = __tree_model_iter_n_children;
386   iface->iter_nth_child  = __iter_nth_child;
387   iface->iter_parent     = __iter_parent;
388 }
389
390
391 GType
392 psppire_text_file_get_type (void)
393 {
394   static GType text_file_type = 0;
395
396   if (!text_file_type)
397     {
398       static const GTypeInfo text_file_info =
399         {
400           sizeof (PsppireTextFileClass),
401           NULL,         /* base_init */
402           NULL,         /* base_finalize */
403           (GClassInitFunc) psppire_text_file_class_init,
404           NULL,         /* class_finalize */
405           NULL,         /* class_data */
406           sizeof (PsppireTextFile),
407           0,
408           (GInstanceInitFunc) psppire_text_file_init,
409         };
410
411       static const GInterfaceInfo tree_model_info = {
412         (GInterfaceInitFunc) __tree_model_init,
413         NULL,
414         NULL
415       };
416
417       text_file_type = g_type_register_static (G_TYPE_OBJECT,
418                                                "PsppireTextFile",
419                                                &text_file_info, 0);
420
421       g_type_add_interface_static (text_file_type, GTK_TYPE_TREE_MODEL,
422                                    &tree_model_info);
423     }
424
425   return text_file_type;
426 }
427
428
429 static void
430 psppire_text_file_class_init (PsppireTextFileClass *class)
431 {
432   GObjectClass *object_class;
433
434   parent_class = g_type_class_peek_parent (class);
435   object_class = (GObjectClass*) class;
436
437   GParamSpec *maximum_lines_spec =
438     g_param_spec_int ("maximum-lines",
439                       "Maximum Lines",
440                       P_("An upper limit on the number of lines to consider"),
441                       0, G_MAXINT, G_MAXINT,
442                       G_PARAM_READWRITE);
443
444   GParamSpec *line_count_spec =
445     g_param_spec_int ("line-count",
446                       "Line Count",
447                       P_("The number of lines in the file"),
448                       0, G_MAXINT, G_MAXINT,
449                       G_PARAM_READABLE);
450
451   GParamSpec *file_name_spec =
452     g_param_spec_string ("file-name",
453                          "File Name",
454                          P_("The name of the file from which this object was constructed"),
455                          NULL,
456                          G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
457
458   GParamSpec *encoding_spec =
459     g_param_spec_string ("encoding",
460                          "Character Encoding",
461                          P_("The character encoding of the file from which this object was constructed"),
462                          "unset",
463                          G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
464
465   object_class->set_property = psppire_text_file_set_property;
466   object_class->get_property = psppire_text_file_get_property;
467
468   g_object_class_install_property (object_class,
469                                    PROP_MAXIMUM_LINES,
470                                    maximum_lines_spec);
471
472   g_object_class_install_property (object_class,
473                                    PROP_LINE_COUNT,
474                                    line_count_spec);
475
476   g_object_class_install_property (object_class,
477                                    PROP_FILE_NAME,
478                                    file_name_spec);
479
480   g_object_class_install_property (object_class,
481                                    PROP_ENCODING,
482                                    encoding_spec);
483
484   object_class->finalize = psppire_text_file_finalize;
485   object_class->dispose = psppire_text_file_dispose;
486 }
487
488 static void
489 psppire_text_file_init (PsppireTextFile *text_file)
490 {
491   text_file->encoding = g_strdup ("unset");
492   text_file->file_name = NULL;
493
494   text_file->dispose_has_run = FALSE;
495   text_file->stamp = g_random_int ();
496 }
497
498
499 PsppireTextFile *
500 psppire_text_file_new (const gchar *file_name, const gchar *encoding)
501 {
502   PsppireTextFile *retval =
503     g_object_new (PSPPIRE_TYPE_TEXT_FILE,
504                   "file-name", file_name,
505                   "encoding", encoding,
506                   NULL);
507
508   return retval;
509 }
510
511 static void
512 psppire_text_file_finalize (GObject *object)
513 {
514   PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
515
516   g_free (tf->encoding);
517   g_free (tf->file_name);
518
519   /* must chain up */
520   (* parent_class->finalize) (object);
521 }
522
523
524 static void
525 psppire_text_file_dispose (GObject *object)
526 {
527   PsppireTextFile *ds = PSPPIRE_TEXT_FILE (object);
528
529   if (ds->dispose_has_run)
530     return;
531
532   /* must chain up */
533   (* parent_class->dispose) (object);
534
535   ds->dispose_has_run = TRUE;
536 }
537
538 gboolean
539 psppire_text_file_get_total_exact (PsppireTextFile *tf)
540 {
541   return tf->total_is_exact;
542 }
543
544 gulong
545 psppire_text_file_get_n_lines (PsppireTextFile *tf)
546 {
547   return tf->total_lines;
548 }