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"
44 enum {MAX_LINE_LEN = 16384}; /* Max length of an acceptable line. */
47 read_lines (PsppireTextFile *tf)
49 if (tf->file_name && 0 != g_strcmp0 ("unset", tf->encoding))
51 struct line_reader *reader = line_reader_for_file (tf->encoding, tf->file_name, O_RDONLY);
55 msg_error (errno, _("Could not open `%s'"), tf->file_name);
60 ds_init_empty (&input);
61 for (tf->line_cnt = 0; tf->line_cnt < MAX_PREVIEW_LINES; tf->line_cnt++)
64 if (!line_reader_read (reader, &input, MAX_LINE_LEN + 1)
65 || ds_length (&input) > MAX_LINE_LEN)
68 if (line_reader_eof (reader))
70 else if (line_reader_error (reader))
71 msg (ME, _("Error reading `%s': %s"),
72 tf->file_name, strerror (line_reader_error (reader)));
74 msg (ME, _("Failed to read `%s', because it contains a line "
75 "over %d bytes long and therefore appears not to be "
77 tf->file_name, MAX_LINE_LEN);
78 line_reader_close (reader);
79 for (i = 0; i < tf->line_cnt; i++)
80 g_free (tf->lines[i].string);
86 tf->lines[tf->line_cnt]
87 = recode_substring_pool ("UTF-8",
88 line_reader_get_encoding (reader),
93 if (tf->line_cnt == 0)
96 msg (ME, _("`%s' is empty."), tf->file_name);
97 line_reader_close (reader);
98 for (i = 0; i < tf->line_cnt; i++)
99 g_free (tf->lines[i].string);
104 if (tf->line_cnt < MAX_PREVIEW_LINES)
106 tf->total_lines = tf->line_cnt;
107 tf->total_is_exact = true;
111 /* Estimate the number of lines in the file. */
113 off_t position = line_reader_tell (reader);
114 if (fstat (line_reader_fileno (reader), &s) == 0 && position > 0)
116 tf->total_lines = (double) tf->line_cnt / position * s.st_size;
117 tf->total_is_exact = false;
122 tf->total_is_exact = true;
126 line_reader_close (reader);
131 psppire_text_file_set_property (GObject *object,
136 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
140 case PROP_MAXIMUM_LINES:
141 tf->maximum_lines = g_value_get_int (value);
144 tf->file_name = g_value_dup_string (value);
148 g_free (tf->encoding);
149 tf->encoding = g_value_dup_string (value);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 psppire_text_file_get_property (GObject *object,
165 PsppireTextFile *text_file = PSPPIRE_TEXT_FILE (object);
169 case PROP_MAXIMUM_LINES:
170 g_value_set_int (value, text_file->maximum_lines);
172 case PROP_LINE_COUNT:
173 g_value_set_int (value, text_file->line_cnt);
176 g_value_set_string (value, text_file->file_name);
179 g_value_set_string (value, text_file->encoding);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 static void psppire_text_file_finalize (GObject *object);
188 static void psppire_text_file_dispose (GObject *object);
190 static GObjectClass *parent_class = NULL;
193 __tree_get_iter (GtkTreeModel *tree_model,
197 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
202 gint *indices = gtk_tree_path_get_indices (path);
206 if (n >= file->line_cnt)
209 iter->user_data = GINT_TO_POINTER (n);
210 iter->stamp = file->stamp;
217 __tree_iter_next (GtkTreeModel *tree_model,
220 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
221 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
223 gint n = GPOINTER_TO_INT (iter->user_data) + 1;
225 if (n >= file->line_cnt)
228 iter->user_data = GINT_TO_POINTER (n);
235 __tree_get_column_type (GtkTreeModel *tree_model,
241 return G_TYPE_STRING;
245 __iter_has_child (GtkTreeModel *tree_model,
253 __iter_parent (GtkTreeModel *tree_model,
261 __tree_get_path (GtkTreeModel *tree_model,
264 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
265 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
267 gint n = GPOINTER_TO_INT (iter->user_data);
269 return gtk_tree_path_new_from_indices (n, -1);
274 __iter_children (GtkTreeModel *tree_model,
283 __tree_model_iter_n_children (GtkTreeModel *tree_model,
286 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
287 g_assert (iter == NULL);
288 return file->line_cnt;
291 static GtkTreeModelFlags
292 __tree_model_get_flags (GtkTreeModel *model)
294 g_return_val_if_fail (PSPPIRE_IS_TEXT_FILE (model), (GtkTreeModelFlags) 0);
296 return GTK_TREE_MODEL_LIST_ONLY;
300 __tree_model_get_n_columns (GtkTreeModel *tree_model)
307 __iter_nth_child (GtkTreeModel *tree_model,
312 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
314 g_assert (parent == NULL);
316 g_return_val_if_fail (file, FALSE);
318 if (n >= file->line_cnt)
321 iter->user_data = NULL;
325 iter->user_data = GINT_TO_POINTER (n);
326 iter->stamp = file->stamp;
333 __get_value (GtkTreeModel *tree_model,
338 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
340 g_return_if_fail (iter->stamp == file->stamp);
342 gint n = GPOINTER_TO_INT (iter->user_data);
344 g_return_if_fail (n < file->line_cnt);
348 g_value_init (value, G_TYPE_INT);
349 g_value_set_int (value, n + 1);
353 g_value_init (value, G_TYPE_STRING);
357 char *s = ss_xstrdup (file->lines[n]);
358 g_value_set_string (value, s);
363 g_assert_not_reached ();
368 __tree_model_init (GtkTreeModelIface *iface)
370 iface->get_flags = __tree_model_get_flags;
371 iface->get_n_columns = __tree_model_get_n_columns ;
372 iface->get_column_type = __tree_get_column_type;
373 iface->get_iter = __tree_get_iter;
374 iface->iter_next = __tree_iter_next;
375 iface->get_path = __tree_get_path;
376 iface->get_value = __get_value;
378 iface->iter_children = __iter_children;
379 iface->iter_has_child = __iter_has_child;
380 iface->iter_n_children = __tree_model_iter_n_children;
381 iface->iter_nth_child = __iter_nth_child;
382 iface->iter_parent = __iter_parent;
385 G_DEFINE_TYPE_WITH_CODE (PsppireTextFile, psppire_text_file, G_TYPE_OBJECT,
386 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
390 psppire_text_file_class_init (PsppireTextFileClass *class)
392 GObjectClass *object_class;
394 parent_class = g_type_class_peek_parent (class);
395 object_class = G_OBJECT_CLASS (class);
397 GParamSpec *maximum_lines_spec =
398 g_param_spec_int ("maximum-lines",
400 P_("An upper limit on the number of lines to consider"),
401 0, G_MAXINT, G_MAXINT,
404 GParamSpec *line_count_spec =
405 g_param_spec_int ("line-count",
407 P_("The number of lines in the file"),
408 0, G_MAXINT, G_MAXINT,
411 GParamSpec *file_name_spec =
412 g_param_spec_string ("file-name",
414 P_("The name of the file from which this object was constructed"),
416 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
418 GParamSpec *encoding_spec =
419 g_param_spec_string ("encoding",
420 "Character Encoding",
421 P_("The character encoding of the file from which this object was constructed"),
423 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
425 object_class->set_property = psppire_text_file_set_property;
426 object_class->get_property = psppire_text_file_get_property;
428 g_object_class_install_property (object_class,
432 g_object_class_install_property (object_class,
436 g_object_class_install_property (object_class,
440 g_object_class_install_property (object_class,
444 object_class->finalize = psppire_text_file_finalize;
445 object_class->dispose = psppire_text_file_dispose;
449 psppire_text_file_init (PsppireTextFile *text_file)
451 text_file->encoding = g_strdup ("unset");
452 text_file->file_name = NULL;
454 text_file->dispose_has_run = FALSE;
455 text_file->stamp = g_random_int ();
460 psppire_text_file_new (const gchar *file_name, const gchar *encoding)
462 PsppireTextFile *retval =
463 g_object_new (PSPPIRE_TYPE_TEXT_FILE,
464 "file-name", file_name,
465 "encoding", encoding,
472 psppire_text_file_finalize (GObject *object)
474 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
476 for (int i = 0; i < tf->line_cnt; i++)
477 g_free (tf->lines[i].string);
479 g_free (tf->encoding);
480 g_free (tf->file_name);
483 (* parent_class->finalize) (object);
488 psppire_text_file_dispose (GObject *object)
490 PsppireTextFile *ds = PSPPIRE_TEXT_FILE (object);
492 if (ds->dispose_has_run)
496 (* parent_class->dispose) (object);
498 ds->dispose_has_run = TRUE;
502 psppire_text_file_get_total_exact (PsppireTextFile *tf)
504 return tf->total_is_exact;
508 psppire_text_file_get_n_lines (PsppireTextFile *tf)
510 return tf->total_lines;