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-text-file.h"
27 #include "libpspp/line-reader.h"
28 #include "libpspp/message.h"
29 #include "libpspp/str.h"
30 #include "libpspp/i18n.h"
44 enum {MAX_LINE_LEN = 16384}; /* Max length of an acceptable line. */
48 read_lines (PsppireTextFile *tf)
50 if (tf->file_name && 0 != g_strcmp0 ("unset", tf->encoding))
52 struct line_reader *reader = line_reader_for_file (tf->encoding, tf->file_name, O_RDONLY);
56 msg_error (errno, _("Could not open `%s'"), tf->file_name);
61 ds_init_empty (&input);
62 for (tf->line_cnt = 0; tf->line_cnt < MAX_PREVIEW_LINES; tf->line_cnt++)
65 if (!line_reader_read (reader, &input, MAX_LINE_LEN + 1)
66 || ds_length (&input) > MAX_LINE_LEN)
69 if (line_reader_eof (reader))
71 else if (line_reader_error (reader))
72 msg (ME, _("Error reading `%s': %s"),
73 tf->file_name, strerror (line_reader_error (reader)));
75 msg (ME, _("Failed to read `%s', because it contains a line "
76 "over %d bytes long and therefore appears not to be "
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]);
87 tf->lines[tf->line_cnt]
88 = recode_substring_pool ("UTF-8",
89 line_reader_get_encoding (reader),
94 if (tf->line_cnt == 0)
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]);
105 if (tf->line_cnt < MAX_PREVIEW_LINES)
107 tf->total_lines = tf->line_cnt;
108 tf->total_is_exact = true;
112 /* Estimate the number of lines in the file. */
114 off_t position = line_reader_tell (reader);
115 if (fstat (line_reader_fileno (reader), &s) == 0 && position > 0)
117 tf->total_lines = (double) tf->line_cnt / position * s.st_size;
118 tf->total_is_exact = false;
123 tf->total_is_exact = true;
127 line_reader_close (reader);
132 psppire_text_file_set_property (GObject *object,
137 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
141 case PROP_MAXIMUM_LINES:
142 tf->maximum_lines = g_value_get_int (value);
145 tf->file_name = g_value_dup_string (value);
149 g_free (tf->encoding);
150 tf->encoding = g_value_dup_string (value);
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 psppire_text_file_get_property (GObject *object,
166 PsppireTextFile *text_file = PSPPIRE_TEXT_FILE (object);
170 case PROP_MAXIMUM_LINES:
171 g_value_set_int (value, text_file->maximum_lines);
173 case PROP_LINE_COUNT:
174 g_value_set_int (value, text_file->line_cnt);
177 g_value_set_string (value, text_file->file_name);
180 g_value_set_string (value, text_file->encoding);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 static void psppire_text_file_init (PsppireTextFile *text_file);
190 static void psppire_text_file_class_init (PsppireTextFileClass *class);
192 static void psppire_text_file_finalize (GObject *object);
193 static void psppire_text_file_dispose (GObject *object);
195 static GObjectClass *parent_class = NULL;
198 __tree_get_iter (GtkTreeModel *tree_model,
202 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
207 gint *indices = gtk_tree_path_get_indices (path);
211 if (n >= file->line_cnt)
214 iter->user_data = GINT_TO_POINTER (n);
215 iter->stamp = file->stamp;
222 __tree_iter_next (GtkTreeModel *tree_model,
225 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
226 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
228 gint n = GPOINTER_TO_INT (iter->user_data) + 1;
230 if (n >= file->line_cnt)
233 iter->user_data = GINT_TO_POINTER (n);
240 __tree_get_column_type (GtkTreeModel *tree_model,
246 return G_TYPE_STRING;
250 __iter_has_child (GtkTreeModel *tree_model,
258 __iter_parent (GtkTreeModel *tree_model,
266 __tree_get_path (GtkTreeModel *tree_model,
269 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
270 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
272 gint n = GPOINTER_TO_INT (iter->user_data);
274 return gtk_tree_path_new_from_indices (n, -1);
279 __iter_children (GtkTreeModel *tree_model,
288 __tree_model_iter_n_children (GtkTreeModel *tree_model,
291 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
292 g_assert (iter == NULL);
293 return file->line_cnt;
296 static GtkTreeModelFlags
297 __tree_model_get_flags (GtkTreeModel *model)
299 g_return_val_if_fail (PSPPIRE_IS_TEXT_FILE (model), (GtkTreeModelFlags) 0);
301 return GTK_TREE_MODEL_LIST_ONLY;
305 __tree_model_get_n_columns (GtkTreeModel *tree_model)
312 __iter_nth_child (GtkTreeModel *tree_model,
317 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
319 g_assert (parent == NULL);
321 g_return_val_if_fail (file, FALSE);
323 if (n >= file->line_cnt)
326 iter->user_data = NULL;
330 iter->user_data = GINT_TO_POINTER (n);
331 iter->stamp = file->stamp;
338 __get_value (GtkTreeModel *tree_model,
343 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
345 g_return_if_fail (iter->stamp == file->stamp);
347 gint n = GPOINTER_TO_INT (iter->user_data);
349 g_return_if_fail (n < file->line_cnt);
353 g_value_init (value, G_TYPE_INT);
354 g_value_set_int (value, n + 1);
358 g_value_init (value, G_TYPE_STRING);
362 char *s = ss_xstrdup (file->lines[n]);
363 g_value_set_string (value, s);
368 g_assert_not_reached ();
373 __tree_model_init (GtkTreeModelIface *iface)
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;
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;
392 psppire_text_file_get_type (void)
394 static GType text_file_type = 0;
398 static const GTypeInfo text_file_info =
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),
408 (GInstanceInitFunc) psppire_text_file_init,
411 static const GInterfaceInfo tree_model_info = {
412 (GInterfaceInitFunc) __tree_model_init,
417 text_file_type = g_type_register_static (G_TYPE_OBJECT,
421 g_type_add_interface_static (text_file_type, GTK_TYPE_TREE_MODEL,
425 return text_file_type;
430 psppire_text_file_class_init (PsppireTextFileClass *class)
432 GObjectClass *object_class;
434 parent_class = g_type_class_peek_parent (class);
435 object_class = (GObjectClass*) class;
437 GParamSpec *maximum_lines_spec =
438 g_param_spec_int ("maximum-lines",
440 P_("An upper limit on the number of lines to consider"),
441 0, G_MAXINT, G_MAXINT,
444 GParamSpec *line_count_spec =
445 g_param_spec_int ("line-count",
447 P_("The number of lines in the file"),
448 0, G_MAXINT, G_MAXINT,
451 GParamSpec *file_name_spec =
452 g_param_spec_string ("file-name",
454 P_("The name of the file from which this object was constructed"),
456 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
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"),
463 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
465 object_class->set_property = psppire_text_file_set_property;
466 object_class->get_property = psppire_text_file_get_property;
468 g_object_class_install_property (object_class,
472 g_object_class_install_property (object_class,
476 g_object_class_install_property (object_class,
480 g_object_class_install_property (object_class,
484 object_class->finalize = psppire_text_file_finalize;
485 object_class->dispose = psppire_text_file_dispose;
489 psppire_text_file_init (PsppireTextFile *text_file)
491 text_file->encoding = g_strdup ("unset");
492 text_file->file_name = NULL;
494 text_file->dispose_has_run = FALSE;
495 text_file->stamp = g_random_int ();
500 psppire_text_file_new (const gchar *file_name, const gchar *encoding)
502 PsppireTextFile *retval =
503 g_object_new (PSPPIRE_TYPE_TEXT_FILE,
504 "file-name", file_name,
505 "encoding", encoding,
512 psppire_text_file_finalize (GObject *object)
514 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
516 g_free (tf->encoding);
517 g_free (tf->file_name);
520 (* parent_class->finalize) (object);
525 psppire_text_file_dispose (GObject *object)
527 PsppireTextFile *ds = PSPPIRE_TEXT_FILE (object);
529 if (ds->dispose_has_run)
533 (* parent_class->dispose) (object);
535 ds->dispose_has_run = TRUE;
539 psppire_text_file_get_total_exact (PsppireTextFile *tf)
541 return tf->total_is_exact;
545 psppire_text_file_get_n_lines (PsppireTextFile *tf)
547 return tf->total_lines;