1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2017, 2020 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"
46 read_lines (PsppireTextFile *tf)
48 /* Max length of an acceptable line. */
49 static const int MAX_LINE_LEN = 16384;
51 if (tf->file_name && 0 != g_strcmp0 ("unset", tf->encoding))
53 struct line_reader *reader = line_reader_for_file (tf->encoding, tf->file_name, O_RDONLY);
57 msg_error (errno, _("Could not open `%s'"), tf->file_name);
62 ds_init_empty (&input);
63 for (tf->n_lines = 0; tf->n_lines < MAX_PREVIEW_LINES; tf->n_lines++)
66 if (!line_reader_read (reader, &input, MAX_LINE_LEN + 1)
67 || ds_length (&input) > MAX_LINE_LEN)
70 if (line_reader_eof (reader))
72 else if (line_reader_error (reader))
73 msg (ME, _("Error reading `%s': %s"),
74 tf->file_name, strerror (line_reader_error (reader)));
76 msg (ME, _("Failed to read `%s', because it contains a line "
77 "over %d bytes long and therefore appears not to be "
79 tf->file_name, MAX_LINE_LEN);
80 line_reader_close (reader);
81 for (i = 0; i < tf->n_lines; i++)
82 g_free (tf->lines[i].string);
88 tf->lines[tf->n_lines]
89 = recode_substring_pool ("UTF-8",
90 line_reader_get_encoding (reader),
98 msg (ME, _("`%s' is empty."), tf->file_name);
99 line_reader_close (reader);
100 for (i = 0; i < tf->n_lines; i++)
101 g_free (tf->lines[i].string);
106 if (tf->n_lines < MAX_PREVIEW_LINES)
108 tf->total_lines = tf->n_lines;
109 tf->total_is_exact = true;
113 /* Estimate the number of lines in the file. */
115 off_t position = line_reader_tell (reader);
116 if (fstat (line_reader_fileno (reader), &s) == 0 && position > 0)
118 tf->total_lines = (double) tf->n_lines / position * s.st_size;
119 tf->total_is_exact = false;
124 tf->total_is_exact = true;
128 line_reader_close (reader);
133 psppire_text_file_set_property (GObject *object,
138 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
142 case PROP_MAXIMUM_LINES:
143 tf->maximum_lines = g_value_get_int (value);
146 tf->file_name = g_value_dup_string (value);
150 g_free (tf->encoding);
151 tf->encoding = g_value_dup_string (value);
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162 psppire_text_file_get_property (GObject *object,
167 PsppireTextFile *text_file = PSPPIRE_TEXT_FILE (object);
171 case PROP_MAXIMUM_LINES:
172 g_value_set_int (value, text_file->maximum_lines);
174 case PROP_LINE_COUNT:
175 g_value_set_int (value, text_file->n_lines);
178 g_value_set_string (value, text_file->file_name);
181 g_value_set_string (value, text_file->encoding);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 static void psppire_text_file_finalize (GObject *object);
190 static void psppire_text_file_dispose (GObject *object);
192 static GObjectClass *parent_class = NULL;
195 __tree_get_iter (GtkTreeModel *tree_model,
199 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
204 gint *indices = gtk_tree_path_get_indices (path);
208 if (n >= file->n_lines)
211 iter->user_data = GINT_TO_POINTER (n);
212 iter->stamp = file->stamp;
219 __tree_iter_next (GtkTreeModel *tree_model,
222 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
223 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
225 gint n = GPOINTER_TO_INT (iter->user_data) + 1;
227 if (n >= file->n_lines)
230 iter->user_data = GINT_TO_POINTER (n);
237 __tree_get_column_type (GtkTreeModel *tree_model,
243 return G_TYPE_STRING;
247 __iter_has_child (GtkTreeModel *tree_model,
255 __iter_parent (GtkTreeModel *tree_model,
263 __tree_get_path (GtkTreeModel *tree_model,
266 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
267 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
269 gint n = GPOINTER_TO_INT (iter->user_data);
271 return gtk_tree_path_new_from_indices (n, -1);
276 __iter_children (GtkTreeModel *tree_model,
285 __tree_model_iter_n_children (GtkTreeModel *tree_model,
288 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
289 g_assert (iter == NULL);
290 return file->n_lines;
293 static GtkTreeModelFlags
294 __tree_model_get_flags (GtkTreeModel *model)
296 g_return_val_if_fail (PSPPIRE_IS_TEXT_FILE (model), (GtkTreeModelFlags) 0);
298 return GTK_TREE_MODEL_LIST_ONLY;
302 __tree_model_get_n_columns (GtkTreeModel *tree_model)
309 __iter_nth_child (GtkTreeModel *tree_model,
314 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
316 g_assert (parent == NULL);
318 g_return_val_if_fail (file, FALSE);
320 if (n >= file->n_lines)
323 iter->user_data = NULL;
327 iter->user_data = GINT_TO_POINTER (n);
328 iter->stamp = file->stamp;
335 __get_value (GtkTreeModel *tree_model,
340 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
342 g_return_if_fail (iter->stamp == file->stamp);
344 gint n = GPOINTER_TO_INT (iter->user_data);
346 g_return_if_fail (n < file->n_lines);
350 g_value_init (value, G_TYPE_INT);
351 g_value_set_int (value, n + 1);
355 g_value_init (value, G_TYPE_STRING);
359 char *s = ss_xstrdup (file->lines[n]);
360 g_value_set_string (value, s);
365 g_assert_not_reached ();
370 __tree_model_init (GtkTreeModelIface *iface)
372 iface->get_flags = __tree_model_get_flags;
373 iface->get_n_columns = __tree_model_get_n_columns ;
374 iface->get_column_type = __tree_get_column_type;
375 iface->get_iter = __tree_get_iter;
376 iface->iter_next = __tree_iter_next;
377 iface->get_path = __tree_get_path;
378 iface->get_value = __get_value;
380 iface->iter_children = __iter_children;
381 iface->iter_has_child = __iter_has_child;
382 iface->iter_n_children = __tree_model_iter_n_children;
383 iface->iter_nth_child = __iter_nth_child;
384 iface->iter_parent = __iter_parent;
387 G_DEFINE_TYPE_WITH_CODE (PsppireTextFile, psppire_text_file, G_TYPE_OBJECT,
388 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
392 psppire_text_file_class_init (PsppireTextFileClass *class)
394 GObjectClass *object_class;
396 parent_class = g_type_class_peek_parent (class);
397 object_class = G_OBJECT_CLASS (class);
399 GParamSpec *maximum_lines_spec =
400 g_param_spec_int ("maximum-lines",
402 P_("An upper limit on the number of lines to consider"),
403 0, G_MAXINT, G_MAXINT,
406 GParamSpec *line_count_spec =
407 g_param_spec_int ("line-count",
409 P_("The number of lines in the file"),
410 0, G_MAXINT, G_MAXINT,
413 GParamSpec *file_name_spec =
414 g_param_spec_string ("file-name",
416 P_("The name of the file from which this object was constructed"),
418 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
420 GParamSpec *encoding_spec =
421 g_param_spec_string ("encoding",
422 "Character Encoding",
423 P_("The character encoding of the file from which this object was constructed"),
425 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
427 object_class->set_property = psppire_text_file_set_property;
428 object_class->get_property = psppire_text_file_get_property;
430 g_object_class_install_property (object_class,
434 g_object_class_install_property (object_class,
438 g_object_class_install_property (object_class,
442 g_object_class_install_property (object_class,
446 object_class->finalize = psppire_text_file_finalize;
447 object_class->dispose = psppire_text_file_dispose;
451 psppire_text_file_init (PsppireTextFile *text_file)
453 text_file->encoding = g_strdup ("unset");
454 text_file->file_name = NULL;
456 text_file->dispose_has_run = FALSE;
457 text_file->stamp = g_random_int ();
462 psppire_text_file_new (const gchar *file_name, const gchar *encoding)
464 PsppireTextFile *retval =
465 g_object_new (PSPPIRE_TYPE_TEXT_FILE,
466 "file-name", file_name,
467 "encoding", encoding,
474 psppire_text_file_finalize (GObject *object)
476 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
478 for (int i = 0; i < tf->n_lines; i++)
479 g_free (tf->lines[i].string);
481 g_free (tf->encoding);
482 g_free (tf->file_name);
485 (* parent_class->finalize) (object);
490 psppire_text_file_dispose (GObject *object)
492 PsppireTextFile *ds = PSPPIRE_TEXT_FILE (object);
494 if (ds->dispose_has_run)
498 (* parent_class->dispose) (object);
500 ds->dispose_has_run = TRUE;
504 psppire_text_file_get_total_exact (PsppireTextFile *tf)
506 return tf->total_is_exact;
510 psppire_text_file_get_n_lines (PsppireTextFile *tf)
512 return tf->total_lines;