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"
42 enum {MAX_LINE_LEN = 16384}; /* Max length of an acceptable line. */
46 read_lines (PsppireTextFile *tf)
48 if (tf->file_name && 0 != g_strcmp0 ("unset", tf->encoding))
50 struct line_reader *reader = line_reader_for_file (tf->encoding, tf->file_name, O_RDONLY);
54 msg_error (errno, _("Could not open `%s'"), tf->file_name);
59 ds_init_empty (&input);
60 for (tf->line_cnt = 0; tf->line_cnt < MAX_PREVIEW_LINES; tf->line_cnt++)
63 if (!line_reader_read (reader, &input, MAX_LINE_LEN + 1)
64 || ds_length (&input) > MAX_LINE_LEN)
67 if (line_reader_eof (reader))
69 else if (line_reader_error (reader))
70 msg (ME, _("Error reading `%s': %s"),
71 tf->file_name, strerror (line_reader_error (reader)));
73 msg (ME, _("Failed to read `%s', because it contains a line "
74 "over %d bytes long and therefore appears not to be "
76 tf->file_name, MAX_LINE_LEN);
77 line_reader_close (reader);
78 for (i = 0; i < tf->line_cnt; i++)
79 g_free (&tf->lines[i]);
85 tf->lines[tf->line_cnt]
86 = recode_substring_pool ("UTF-8",
87 line_reader_get_encoding (reader),
92 if (tf->line_cnt == 0)
95 msg (ME, _("`%s' is empty."), tf->file_name);
96 line_reader_close (reader);
97 for (i = 0; i < tf->line_cnt; i++)
98 g_free (&tf->lines[i]);
103 if (tf->line_cnt < MAX_PREVIEW_LINES)
105 tf->total_lines = tf->line_cnt;
106 tf->total_is_exact = true;
110 /* Estimate the number of lines in the file. */
112 off_t position = line_reader_tell (reader);
113 if (fstat (line_reader_fileno (reader), &s) == 0 && position > 0)
115 tf->total_lines = (double) tf->line_cnt / position * s.st_size;
116 tf->total_is_exact = false;
121 tf->total_is_exact = true;
125 line_reader_close (reader);
131 psppire_text_file_set_property (GObject *object,
136 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
141 tf->file_name = g_value_dup_string (value);
145 g_free (tf->encoding);
146 tf->encoding = g_value_dup_string (value);
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 psppire_text_file_get_property (GObject *object,
162 PsppireTextFile *text_file = PSPPIRE_TEXT_FILE (object);
167 g_value_set_string (value, text_file->file_name);
170 g_value_set_string (value, text_file->encoding);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 static void psppire_text_file_init (PsppireTextFile *text_file);
180 static void psppire_text_file_class_init (PsppireTextFileClass *class);
182 static void psppire_text_file_finalize (GObject *object);
183 static void psppire_text_file_dispose (GObject *object);
185 static GObjectClass *parent_class = NULL;
189 __tree_get_iter (GtkTreeModel *tree_model,
193 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
198 gint *indices = gtk_tree_path_get_indices (path);
202 if (n >= file->line_cnt)
205 iter->user_data = GINT_TO_POINTER (n);
206 iter->stamp = file->stamp;
213 __tree_iter_next (GtkTreeModel *tree_model,
216 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
217 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
219 gint n = GPOINTER_TO_INT (iter->user_data) + 1;
221 if (n >= file->line_cnt)
224 iter->user_data = GINT_TO_POINTER (n);
231 __tree_get_column_type (GtkTreeModel *tree_model,
237 return G_TYPE_STRING;
241 __iter_has_child (GtkTreeModel *tree_model,
249 __iter_parent (GtkTreeModel *tree_model,
257 __tree_get_path (GtkTreeModel *tree_model,
260 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
261 g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
263 gint n = GPOINTER_TO_INT (iter->user_data);
265 return gtk_tree_path_new_from_indices (n, -1);
270 __iter_children (GtkTreeModel *tree_model,
279 __tree_model_iter_n_children (GtkTreeModel *tree_model,
282 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
283 g_assert (iter == NULL);
284 return file->line_cnt;
287 static GtkTreeModelFlags
288 __tree_model_get_flags (GtkTreeModel *model)
290 g_return_val_if_fail (PSPPIRE_IS_TEXT_FILE (model), (GtkTreeModelFlags) 0);
292 return GTK_TREE_MODEL_LIST_ONLY;
296 __tree_model_get_n_columns (GtkTreeModel *tree_model)
298 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (tree_model);
304 __iter_nth_child (GtkTreeModel *tree_model,
309 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
311 g_assert (parent == NULL);
313 g_return_val_if_fail (file, FALSE);
315 if (n >= file->line_cnt)
318 iter->user_data = NULL;
322 iter->user_data = GINT_TO_POINTER (n);
323 iter->stamp = file->stamp;
330 __get_value (GtkTreeModel *tree_model,
335 PsppireTextFile *file = PSPPIRE_TEXT_FILE (tree_model);
337 g_return_if_fail (iter->stamp == file->stamp);
339 gint n = GPOINTER_TO_INT (iter->user_data);
341 g_return_if_fail (n < file->line_cnt);
345 g_value_init (value, G_TYPE_INT);
346 g_value_set_int (value, n + 1);
350 g_value_init (value, G_TYPE_STRING);
354 char *s = ss_xstrdup (file->lines[n]);
355 g_value_set_string (value, s);
360 g_assert_not_reached ();
365 __tree_model_init (GtkTreeModelIface *iface)
367 iface->get_flags = __tree_model_get_flags;
368 iface->get_n_columns = __tree_model_get_n_columns ;
369 iface->get_column_type = __tree_get_column_type;
370 iface->get_iter = __tree_get_iter;
371 iface->iter_next = __tree_iter_next;
372 iface->get_path = __tree_get_path;
373 iface->get_value = __get_value;
375 iface->iter_children = __iter_children;
376 iface->iter_has_child = __iter_has_child;
377 iface->iter_n_children = __tree_model_iter_n_children;
378 iface->iter_nth_child = __iter_nth_child;
379 iface->iter_parent = __iter_parent;
384 psppire_text_file_get_type (void)
386 static GType text_file_type = 0;
390 static const GTypeInfo text_file_info =
392 sizeof (PsppireTextFileClass),
393 NULL, /* base_init */
394 NULL, /* base_finalize */
395 (GClassInitFunc) psppire_text_file_class_init,
396 NULL, /* class_finalize */
397 NULL, /* class_data */
398 sizeof (PsppireTextFile),
400 (GInstanceInitFunc) psppire_text_file_init,
403 static const GInterfaceInfo tree_model_info = {
404 (GInterfaceInitFunc) __tree_model_init,
409 text_file_type = g_type_register_static (G_TYPE_OBJECT,
413 g_type_add_interface_static (text_file_type, GTK_TYPE_TREE_MODEL,
417 return text_file_type;
422 psppire_text_file_class_init (PsppireTextFileClass *class)
424 GObjectClass *object_class;
426 parent_class = g_type_class_peek_parent (class);
427 object_class = (GObjectClass*) class;
429 GParamSpec *file_name_spec =
430 g_param_spec_string ("file-name",
432 P_("The name of the file from which this object was constructed"),
434 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
436 GParamSpec *encoding_spec =
437 g_param_spec_string ("encoding",
438 "Character Encoding",
439 P_("The character encoding of the file from which this object was constructed"),
441 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
443 object_class->set_property = psppire_text_file_set_property;
444 object_class->get_property = psppire_text_file_get_property;
446 g_object_class_install_property (object_class,
450 g_object_class_install_property (object_class,
454 object_class->finalize = psppire_text_file_finalize;
455 object_class->dispose = psppire_text_file_dispose;
460 psppire_text_file_init (PsppireTextFile *text_file)
462 text_file->encoding = g_strdup ("unset");
463 text_file->file_name = NULL;
465 text_file->dispose_has_run = FALSE;
466 text_file->stamp = g_random_int ();
471 psppire_text_file_new (const gchar *file_name, const gchar *encoding)
473 PsppireTextFile *retval =
474 g_object_new (PSPPIRE_TYPE_TEXT_FILE,
475 "file-name", file_name,
476 "encoding", encoding,
483 psppire_text_file_finalize (GObject *object)
485 PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
487 g_free (tf->encoding);
488 g_free (tf->file_name);
491 (* parent_class->finalize) (object);
496 psppire_text_file_dispose (GObject *object)
498 PsppireTextFile *ds = PSPPIRE_TEXT_FILE (object);
500 if (ds->dispose_has_run)
504 (* parent_class->dispose) (object);
506 ds->dispose_has_run = TRUE;
510 psppire_text_file_get_total_exact (PsppireTextFile *tf)
512 return tf->total_is_exact;
516 psppire_text_file_get_n_lines (PsppireTextFile *tf)
518 return tf->total_lines;