1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 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 #include "ui/gui/text-data-import-dialog.h"
23 #include <gtk-contrib/psppire-sheet.h>
29 #include "data/data-in.h"
30 #include "data/data-out.h"
31 #include "data/format-guesser.h"
32 #include "data/value-labels.h"
33 #include "language/data-io/data-parser.h"
34 #include "language/lexer/lexer.h"
35 #include "libpspp/assertion.h"
36 #include "libpspp/i18n.h"
37 #include "libpspp/line-reader.h"
38 #include "libpspp/message.h"
39 #include "ui/gui/checkbox-treeview.h"
40 #include "ui/gui/dialog-common.h"
41 #include "ui/gui/executor.h"
42 #include "ui/gui/helper.h"
43 #include "ui/gui/builder-wrapper.h"
44 #include "ui/gui/psppire-data-window.h"
45 #include "ui/gui/psppire-dialog.h"
46 #include "ui/gui/psppire-encoding-selector.h"
47 #include "ui/gui/psppire-empty-list-store.h"
48 #include "ui/gui/psppire-var-sheet.h"
49 #include "ui/gui/psppire-var-store.h"
50 #include "ui/gui/psppire-scanf.h"
51 #include "ui/syntax-gen.h"
54 #include "gl/intprops.h"
55 #include "gl/xalloc.h"
58 #define _(msgid) gettext (msgid)
59 #define N_(msgid) msgid
62 /* The "separators" page of the assistant. */
64 static void revise_fields_preview (struct import_assistant *ia);
65 static void choose_likely_separators (struct import_assistant *ia);
66 static void find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
67 const char *targets, const char *def,
68 struct string *result);
69 static void clear_fields (struct import_assistant *ia);
70 static void revise_fields_preview (struct import_assistant *);
71 static void set_separators (struct import_assistant *);
72 static void get_separators (struct import_assistant *);
73 static void on_separators_custom_entry_notify (GObject *UNUSED,
75 struct import_assistant *);
76 static void on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
77 struct import_assistant *);
78 static void on_quote_combo_change (GtkComboBox *combo,
79 struct import_assistant *);
80 static void on_quote_cb_toggle (GtkToggleButton *quote_cb,
81 struct import_assistant *);
82 static void on_separator_toggle (GtkToggleButton *, struct import_assistant *);
84 /* A common field separator and its identifying name. */
87 const char *name; /* Name (for use with get_widget_assert). */
88 int c; /* Separator character. */
91 /* All the separators in the dialog box. */
92 static const struct separator separators[] =
104 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
107 set_quote_list (GtkComboBoxEntry *cb)
109 GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
112 const gchar *seperator[3] = {"'\"", "\'", "\""};
114 for (i = 0; i < 3; i++)
116 const gchar *s = seperator[i];
118 /* Add a new row to the model */
119 gtk_list_store_append (list, &iter);
120 gtk_list_store_set (list, &iter,
126 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
127 g_object_unref (list);
129 gtk_combo_box_entry_set_text_column (cb, 0);
132 /* Initializes IA's separators substructure. */
134 init_separators_page (struct import_assistant *ia)
136 GtkBuilder *builder = ia->asst.builder;
137 struct separators_page *p = &ia->separators;
140 choose_likely_separators (ia);
142 p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Separators"),
143 GTK_ASSISTANT_PAGE_CONTENT);
144 p->custom_cb = get_widget_assert (builder, "custom-cb");
145 p->custom_entry = get_widget_assert (builder, "custom-entry");
146 p->quote_combo = get_widget_assert (builder, "quote-combo");
147 p->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (p->quote_combo)));
148 p->quote_cb = get_widget_assert (builder, "quote-cb");
149 p->escape_cb = get_widget_assert (builder, "escape");
152 set_quote_list (GTK_COMBO_BOX_ENTRY (p->quote_combo));
153 p->fields_tree_view = GTK_TREE_VIEW (get_widget_assert (builder, "fields"));
154 g_signal_connect (p->quote_combo, "changed",
155 G_CALLBACK (on_quote_combo_change), ia);
156 g_signal_connect (p->quote_cb, "toggled",
157 G_CALLBACK (on_quote_cb_toggle), ia);
158 g_signal_connect (p->custom_entry, "notify::text",
159 G_CALLBACK (on_separators_custom_entry_notify), ia);
160 g_signal_connect (p->custom_cb, "toggled",
161 G_CALLBACK (on_separators_custom_cb_toggle), ia);
162 for (i = 0; i < SEPARATOR_CNT; i++)
163 g_signal_connect (get_widget_assert (builder, separators[i].name),
164 "toggled", G_CALLBACK (on_separator_toggle), ia);
165 g_signal_connect (p->escape_cb, "toggled",
166 G_CALLBACK (on_separator_toggle), ia);
169 /* Frees IA's separators substructure. */
171 destroy_separators_page (struct import_assistant *ia)
173 struct separators_page *s = &ia->separators;
175 ds_destroy (&s->separators);
176 ds_destroy (&s->quotes);
180 /* Called just before the separators page becomes visible in the
183 prepare_separators_page (struct import_assistant *ia)
185 revise_fields_preview (ia);
188 /* Called when the Reset button is clicked on the separators
189 page, resets the separators to the defaults. */
191 reset_separators_page (struct import_assistant *ia)
193 choose_likely_separators (ia);
197 /* Frees and clears the column data in IA's separators
200 clear_fields (struct import_assistant *ia)
202 struct separators_page *s = &ia->separators;
204 if (s->column_cnt > 0)
209 for (row = 0; row < ia->file.line_cnt; row++)
211 const struct string *line = &ia->file.lines[row];
212 const char *line_start = ds_data (line);
213 const char *line_end = ds_end (line);
215 for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
217 char *s = ss_data (col->contents[row]);
218 if (!(s >= line_start && s <= line_end))
219 ss_dealloc (&col->contents[row]);
223 for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
226 free (col->contents);
235 /* Breaks the file data in IA into columns based on the
236 separators set in IA's separators substructure. */
238 split_fields (struct import_assistant *ia)
240 struct separators_page *s = &ia->separators;
241 size_t columns_allocated;
247 /* Is space in the set of separators? */
248 space_sep = ss_find_byte (ds_ss (&s->separators), ' ') != SIZE_MAX;
250 /* Split all the lines, not just those from
251 ia->first_line.skip_lines on, so that we split the line that
252 contains variables names if ia->first_line.variable_names is
254 columns_allocated = 0;
255 for (row = 0; row < ia->file.line_cnt; row++)
257 struct string *line = &ia->file.lines[row];
258 struct substring text = ds_ss (line);
261 for (column_idx = 0; ; column_idx++)
263 struct substring field;
264 struct column *column;
267 ss_ltrim (&text, ss_cstr (" "));
268 if (ss_is_empty (text))
274 else if (!ds_is_empty (&s->quotes)
275 && ds_find_byte (&s->quotes, text.string[0]) != SIZE_MAX)
277 int quote = ss_get_byte (&text);
279 ss_get_until (&text, quote, &field);
286 while ((c = ss_get_byte (&text)) != EOF)
289 else if (ss_match_byte (&text, quote))
290 ds_put_byte (&s, quote);
297 ss_get_bytes (&text, ss_cspan (text, ds_ss (&s->separators)),
300 if (column_idx >= s->column_cnt)
302 struct column *column;
304 if (s->column_cnt >= columns_allocated)
305 s->columns = x2nrealloc (s->columns, &columns_allocated,
307 column = &s->columns[s->column_cnt++];
310 column->contents = xcalloc (ia->file.line_cnt,
311 sizeof *column->contents);
313 column = &s->columns[column_idx];
314 column->contents[row] = field;
315 if (ss_length (field) > column->width)
316 column->width = ss_length (field);
319 ss_ltrim (&text, ss_cstr (" "));
320 if (ss_is_empty (text))
322 if (ss_find_byte (ds_ss (&s->separators), ss_first (text))
324 ss_advance (&text, 1);
329 /* Chooses a name for each column on the separators page */
331 choose_column_names (struct import_assistant *ia)
333 const struct first_line_page *f = &ia->first_line;
334 struct separators_page *s = &ia->separators;
335 struct dictionary *dict;
336 unsigned long int generated_name_count = 0;
340 dict = dict_create (get_default_encoding ());
341 name_row = f->variable_names && f->skip_lines ? f->skip_lines : 0;
342 for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
346 hint = name_row ? ss_xstrdup (col->contents[name_row - 1]) : NULL;
347 name = dict_make_unique_var_name (dict, hint, &generated_name_count);
351 dict_create_var_assert (dict, name, 0);
356 /* Picks the most likely separator and quote characters based on
359 choose_likely_separators (struct import_assistant *ia)
361 unsigned long int histogram[UCHAR_MAX + 1] = { 0 };
364 /* Construct a histogram of all the characters used in the
366 for (row = 0; row < ia->file.line_cnt; row++)
368 struct substring line = ds_ss (&ia->file.lines[row]);
369 size_t length = ss_length (line);
371 for (i = 0; i < length; i++)
372 histogram[(unsigned char) line.string[i]]++;
375 find_commonest_chars (histogram, "\"'", "", &ia->separators.quotes);
376 find_commonest_chars (histogram, ",;:/|!\t-", ",",
377 &ia->separators.separators);
378 ia->separators.escape = true;
381 /* Chooses the most common character among those in TARGETS,
382 based on the frequency data in HISTOGRAM, and stores it in
383 RESULT. If there is a tie for the most common character among
384 those in TARGETS, the earliest character is chosen. If none
385 of the TARGETS appear at all, then DEF is used as a
388 find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
389 const char *targets, const char *def,
390 struct string *result)
392 unsigned char max = 0;
393 unsigned long int max_count = 0;
395 for (; *targets != '\0'; targets++)
397 unsigned char c = *targets;
398 unsigned long int count = histogram[c];
399 if (count > max_count)
408 ds_put_byte (result, max);
411 ds_assign_cstr (result, def);
414 /* Revises the contents of the fields tree view based on the
415 currently chosen set of separators. */
417 revise_fields_preview (struct import_assistant *ia)
421 push_watch_cursor (ia);
423 w = GTK_WIDGET (ia->separators.fields_tree_view);
424 gtk_widget_destroy (w);
427 choose_column_names (ia);
428 ia->separators.fields_tree_view = create_data_tree_view (
430 GTK_CONTAINER (get_widget_assert (ia->asst.builder, "fields-scroller")),
433 pop_watch_cursor (ia);
436 /* Sets the widgets to match IA's separators substructure. */
438 set_separators (struct import_assistant *ia)
440 struct separators_page *s = &ia->separators;
442 struct string custom;
447 ds_init_empty (&custom);
449 for (i = 0; i < ds_length (&s->separators); i++)
451 unsigned char c = ds_at (&s->separators, i);
454 for (j = 0; j < SEPARATOR_CNT; j++)
456 const struct separator *s = &separators[j];
464 ds_put_byte (&custom, c);
468 for (i = 0; i < SEPARATOR_CNT; i++)
470 const struct separator *s = &separators[i];
471 GtkWidget *button = get_widget_assert (ia->asst.builder, s->name);
472 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
473 (seps & (1u << i)) != 0);
475 any_custom = !ds_is_empty (&custom);
476 gtk_entry_set_text (GTK_ENTRY (s->custom_entry), ds_cstr (&custom));
477 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->custom_cb),
479 gtk_widget_set_sensitive (s->custom_entry, any_custom);
480 ds_destroy (&custom);
482 any_quotes = !ds_is_empty (&s->quotes);
484 gtk_entry_set_text (s->quote_entry,
485 any_quotes ? ds_cstr (&s->quotes) : "\"");
486 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->quote_cb),
488 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->escape_cb),
490 gtk_widget_set_sensitive (s->quote_combo, any_quotes);
491 gtk_widget_set_sensitive (s->escape_cb, any_quotes);
494 /* Sets IA's separators substructure to match the widgets. */
496 get_separators (struct import_assistant *ia)
498 struct separators_page *s = &ia->separators;
501 ds_clear (&s->separators);
502 for (i = 0; i < SEPARATOR_CNT; i++)
504 const struct separator *sep = &separators[i];
505 GtkWidget *button = get_widget_assert (ia->asst.builder, sep->name);
506 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
507 ds_put_byte (&s->separators, sep->c);
510 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->custom_cb)))
511 ds_put_cstr (&s->separators,
512 gtk_entry_get_text (GTK_ENTRY (s->custom_entry)));
514 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->quote_cb)))
516 gchar *text = gtk_combo_box_get_active_text (
517 GTK_COMBO_BOX (s->quote_combo));
518 ds_assign_cstr (&s->quotes, text);
522 ds_clear (&s->quotes);
523 s->escape = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->escape_cb));
526 /* Called when the user changes the entry field for custom
529 on_separators_custom_entry_notify (GObject *gobject UNUSED,
530 GParamSpec *arg1 UNUSED,
531 struct import_assistant *ia)
533 revise_fields_preview (ia);
536 /* Called when the user toggles the checkbox that enables custom
539 on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
540 struct import_assistant *ia)
542 bool is_active = gtk_toggle_button_get_active (custom_cb);
543 gtk_widget_set_sensitive (ia->separators.custom_entry, is_active);
544 revise_fields_preview (ia);
547 /* Called when the user changes the selection in the combo box
548 that selects a quote character. */
550 on_quote_combo_change (GtkComboBox *combo, struct import_assistant *ia)
552 revise_fields_preview (ia);
555 /* Called when the user toggles the checkbox that enables
558 on_quote_cb_toggle (GtkToggleButton *quote_cb, struct import_assistant *ia)
560 bool is_active = gtk_toggle_button_get_active (quote_cb);
561 gtk_widget_set_sensitive (ia->separators.quote_combo, is_active);
562 gtk_widget_set_sensitive (ia->separators.escape_cb, is_active);
563 revise_fields_preview (ia);
566 /* Called when the user toggles one of the separators
569 on_separator_toggle (GtkToggleButton *toggle UNUSED,
570 struct import_assistant *ia)
572 revise_fields_preview (ia);