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
61 /* Page where the user chooses field separators. */
62 struct separators_page
64 /* How to break lines into columns. */
65 struct string separators; /* Field separators. */
66 struct string quotes; /* Quote characters. */
67 bool escape; /* Doubled quotes yield a quote mark? */
71 GtkWidget *custom_entry;
73 GtkWidget *quote_combo;
74 GtkEntry *quote_entry;
76 GtkTreeView *fields_tree_view;
79 /* The "separators" page of the assistant. */
81 static void revise_fields_preview (struct import_assistant *ia);
82 static void choose_likely_separators (struct import_assistant *ia);
83 static void find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
84 const char *targets, const char *def,
85 struct string *result);
86 static void clear_fields (struct import_assistant *ia);
87 static void revise_fields_preview (struct import_assistant *);
88 static void set_separators (struct import_assistant *);
89 static void get_separators (struct import_assistant *);
90 static void on_separators_custom_entry_notify (GObject *UNUSED,
92 struct import_assistant *);
93 static void on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
94 struct import_assistant *);
95 static void on_quote_combo_change (GtkComboBox *combo,
96 struct import_assistant *);
97 static void on_quote_cb_toggle (GtkToggleButton *quote_cb,
98 struct import_assistant *);
99 static void on_separator_toggle (GtkToggleButton *, struct import_assistant *);
101 /* A common field separator and its identifying name. */
104 const char *name; /* Name (for use with get_widget_assert). */
105 int c; /* Separator character. */
108 /* All the separators in the dialog box. */
109 static const struct separator separators[] =
121 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
124 set_quote_list (GtkComboBoxEntry *cb)
126 GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
129 const gchar *seperator[3] = {"'\"", "\'", "\""};
131 for (i = 0; i < 3; i++)
133 const gchar *s = seperator[i];
135 /* Add a new row to the model */
136 gtk_list_store_append (list, &iter);
137 gtk_list_store_set (list, &iter,
143 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
144 g_object_unref (list);
146 gtk_combo_box_entry_set_text_column (cb, 0);
149 /* Initializes IA's separators substructure. */
151 struct separators_page *
152 separators_page_create (struct import_assistant *ia)
154 GtkBuilder *builder = ia->asst.builder;
158 struct separators_page *p = xzalloc (sizeof *p);
160 p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Separators"),
161 GTK_ASSISTANT_PAGE_CONTENT);
162 p->custom_cb = get_widget_assert (builder, "custom-cb");
163 p->custom_entry = get_widget_assert (builder, "custom-entry");
164 p->quote_combo = get_widget_assert (builder, "quote-combo");
165 p->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (p->quote_combo)));
166 p->quote_cb = get_widget_assert (builder, "quote-cb");
167 p->escape_cb = get_widget_assert (builder, "escape");
169 set_quote_list (GTK_COMBO_BOX_ENTRY (p->quote_combo));
170 p->fields_tree_view = GTK_TREE_VIEW (get_widget_assert (builder, "fields"));
171 g_signal_connect (p->quote_combo, "changed",
172 G_CALLBACK (on_quote_combo_change), ia);
173 g_signal_connect (p->quote_cb, "toggled",
174 G_CALLBACK (on_quote_cb_toggle), ia);
175 g_signal_connect (p->custom_entry, "notify::text",
176 G_CALLBACK (on_separators_custom_entry_notify), ia);
177 g_signal_connect (p->custom_cb, "toggled",
178 G_CALLBACK (on_separators_custom_cb_toggle), ia);
179 for (i = 0; i < SEPARATOR_CNT; i++)
180 g_signal_connect (get_widget_assert (builder, separators[i].name),
181 "toggled", G_CALLBACK (on_separator_toggle), ia);
182 g_signal_connect (p->escape_cb, "toggled",
183 G_CALLBACK (on_separator_toggle), ia);
188 /* Frees IA's separators substructure. */
190 destroy_separators_page (struct import_assistant *ia)
192 struct separators_page *s = ia->separators;
194 ds_destroy (&s->separators);
195 ds_destroy (&s->quotes);
199 /* Called just before the separators page becomes visible in the
202 prepare_separators_page (struct import_assistant *ia)
204 revise_fields_preview (ia);
207 /* Called when the Reset button is clicked on the separators
208 page, resets the separators to the defaults. */
210 reset_separators_page (struct import_assistant *ia)
212 choose_likely_separators (ia);
216 /* Frees and clears the column data in IA's separators
219 clear_fields (struct import_assistant *ia)
221 if (ia->column_cnt > 0)
226 for (row = 0; row < ia->file.line_cnt; row++)
228 const struct string *line = &ia->file.lines[row];
229 const char *line_start = ds_data (line);
230 const char *line_end = ds_end (line);
232 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
234 char *s = ss_data (col->contents[row]);
235 if (!(s >= line_start && s <= line_end))
236 ss_dealloc (&col->contents[row]);
240 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
243 free (col->contents);
252 /* Breaks the file data in IA into columns based on the
253 separators set in IA's separators substructure. */
255 split_fields (struct import_assistant *ia)
257 struct separators_page *s = ia->separators;
258 size_t columns_allocated;
264 /* Is space in the set of separators? */
265 space_sep = ss_find_byte (ds_ss (&s->separators), ' ') != SIZE_MAX;
267 /* Split all the lines, not just those from
268 ia->first_line.skip_lines on, so that we split the line that
269 contains variables names if ia->first_line.variable_names is
271 columns_allocated = 0;
272 for (row = 0; row < ia->file.line_cnt; row++)
274 struct string *line = &ia->file.lines[row];
275 struct substring text = ds_ss (line);
278 for (column_idx = 0; ; column_idx++)
280 struct substring field;
281 struct column *column;
284 ss_ltrim (&text, ss_cstr (" "));
285 if (ss_is_empty (text))
291 else if (!ds_is_empty (&s->quotes)
292 && ds_find_byte (&s->quotes, text.string[0]) != SIZE_MAX)
294 int quote = ss_get_byte (&text);
296 ss_get_until (&text, quote, &field);
303 while ((c = ss_get_byte (&text)) != EOF)
306 else if (ss_match_byte (&text, quote))
307 ds_put_byte (&s, quote);
314 ss_get_bytes (&text, ss_cspan (text, ds_ss (&s->separators)),
317 if (column_idx >= ia->column_cnt)
319 struct column *column;
321 if (ia->column_cnt >= columns_allocated)
322 ia->columns = x2nrealloc (ia->columns, &columns_allocated,
323 sizeof *ia->columns);
324 column = &ia->columns[ia->column_cnt++];
327 column->contents = xcalloc (ia->file.line_cnt,
328 sizeof *column->contents);
330 column = &ia->columns[column_idx];
331 column->contents[row] = field;
332 if (ss_length (field) > column->width)
333 column->width = ss_length (field);
336 ss_ltrim (&text, ss_cstr (" "));
337 if (ss_is_empty (text))
339 if (ss_find_byte (ds_ss (&s->separators), ss_first (text))
341 ss_advance (&text, 1);
346 /* Chooses a name for each column on the separators page */
348 choose_column_names (struct import_assistant *ia)
350 struct dictionary *dict;
351 unsigned long int generated_name_count = 0;
355 dict = dict_create (get_default_encoding ());
356 name_row = ia->variable_names && ia->skip_lines ? ia->skip_lines : 0;
357 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
361 hint = name_row ? ss_xstrdup (col->contents[name_row - 1]) : NULL;
362 name = dict_make_unique_var_name (dict, hint, &generated_name_count);
366 dict_create_var_assert (dict, name, 0);
371 /* Picks the most likely separator and quote characters based on
374 choose_likely_separators (struct import_assistant *ia)
376 unsigned long int histogram[UCHAR_MAX + 1] = { 0 };
379 /* Construct a histogram of all the characters used in the
381 for (row = 0; row < ia->file.line_cnt; row++)
383 struct substring line = ds_ss (&ia->file.lines[row]);
384 size_t length = ss_length (line);
386 for (i = 0; i < length; i++)
387 histogram[(unsigned char) line.string[i]]++;
390 find_commonest_chars (histogram, "\"'", "", &ia->separators->quotes);
391 find_commonest_chars (histogram, ",;:/|!\t-", ",", &ia->separators->separators);
392 ia->separators->escape = true;
395 /* Chooses the most common character among those in TARGETS,
396 based on the frequency data in HISTOGRAM, and stores it in
397 RESULT. If there is a tie for the most common character among
398 those in TARGETS, the earliest character is chosen. If none
399 of the TARGETS appear at all, then DEF is used as a
402 find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
403 const char *targets, const char *def,
404 struct string *result)
406 unsigned char max = 0;
407 unsigned long int max_count = 0;
409 for (; *targets != '\0'; targets++)
411 unsigned char c = *targets;
412 unsigned long int count = histogram[c];
413 if (count > max_count)
422 ds_put_byte (result, max);
425 ds_assign_cstr (result, def);
428 /* Revises the contents of the fields tree view based on the
429 currently chosen set of separators. */
431 revise_fields_preview (struct import_assistant *ia)
435 push_watch_cursor (ia);
437 w = GTK_WIDGET (ia->separators->fields_tree_view);
438 gtk_widget_destroy (w);
441 choose_column_names (ia);
442 ia->separators->fields_tree_view = create_data_tree_view (
444 GTK_CONTAINER (get_widget_assert (ia->asst.builder, "fields-scroller")),
447 pop_watch_cursor (ia);
450 /* Sets the widgets to match IA's separators substructure. */
452 set_separators (struct import_assistant *ia)
454 struct separators_page *s = ia->separators;
456 struct string custom;
461 ds_init_empty (&custom);
463 for (i = 0; i < ds_length (&s->separators); i++)
465 unsigned char c = ds_at (&s->separators, i);
468 for (j = 0; j < SEPARATOR_CNT; j++)
470 const struct separator *s = &separators[j];
478 ds_put_byte (&custom, c);
482 for (i = 0; i < SEPARATOR_CNT; i++)
484 const struct separator *s = &separators[i];
485 GtkWidget *button = get_widget_assert (ia->asst.builder, s->name);
486 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
487 (seps & (1u << i)) != 0);
489 any_custom = !ds_is_empty (&custom);
490 gtk_entry_set_text (GTK_ENTRY (s->custom_entry), ds_cstr (&custom));
491 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->custom_cb),
493 gtk_widget_set_sensitive (s->custom_entry, any_custom);
494 ds_destroy (&custom);
496 any_quotes = !ds_is_empty (&s->quotes);
498 gtk_entry_set_text (s->quote_entry,
499 any_quotes ? ds_cstr (&s->quotes) : "\"");
500 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->quote_cb),
502 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->escape_cb),
504 gtk_widget_set_sensitive (s->quote_combo, any_quotes);
505 gtk_widget_set_sensitive (s->escape_cb, any_quotes);
508 /* Sets IA's separators substructure to match the widgets. */
510 get_separators (struct import_assistant *ia)
512 struct separators_page *s = ia->separators;
515 ds_clear (&s->separators);
516 for (i = 0; i < SEPARATOR_CNT; i++)
518 const struct separator *sep = &separators[i];
519 GtkWidget *button = get_widget_assert (ia->asst.builder, sep->name);
520 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
521 ds_put_byte (&s->separators, sep->c);
524 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->custom_cb)))
525 ds_put_cstr (&s->separators,
526 gtk_entry_get_text (GTK_ENTRY (s->custom_entry)));
528 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->quote_cb)))
530 gchar *text = gtk_combo_box_get_active_text (
531 GTK_COMBO_BOX (s->quote_combo));
532 ds_assign_cstr (&s->quotes, text);
536 ds_clear (&s->quotes);
537 s->escape = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->escape_cb));
540 /* Called when the user changes the entry field for custom
543 on_separators_custom_entry_notify (GObject *gobject UNUSED,
544 GParamSpec *arg1 UNUSED,
545 struct import_assistant *ia)
547 revise_fields_preview (ia);
550 /* Called when the user toggles the checkbox that enables custom
553 on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
554 struct import_assistant *ia)
556 bool is_active = gtk_toggle_button_get_active (custom_cb);
557 gtk_widget_set_sensitive (ia->separators->custom_entry, is_active);
558 revise_fields_preview (ia);
561 /* Called when the user changes the selection in the combo box
562 that selects a quote character. */
564 on_quote_combo_change (GtkComboBox *combo, struct import_assistant *ia)
566 revise_fields_preview (ia);
569 /* Called when the user toggles the checkbox that enables
572 on_quote_cb_toggle (GtkToggleButton *quote_cb, struct import_assistant *ia)
574 bool is_active = gtk_toggle_button_get_active (quote_cb);
575 gtk_widget_set_sensitive (ia->separators->quote_combo, is_active);
576 gtk_widget_set_sensitive (ia->separators->escape_cb, is_active);
577 revise_fields_preview (ia);
580 /* Called when the user toggles one of the separators
583 on_separator_toggle (GtkToggleButton *toggle UNUSED,
584 struct import_assistant *ia)
586 revise_fields_preview (ia);