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 "page-separators.h"
21 #include "ui/gui/text-data-import-dialog.h"
30 #include "data/data-in.h"
31 #include "data/data-out.h"
32 #include "data/format-guesser.h"
33 #include "data/value-labels.h"
34 #include "language/data-io/data-parser.h"
35 #include "language/lexer/lexer.h"
36 #include "libpspp/assertion.h"
37 #include "libpspp/i18n.h"
38 #include "libpspp/line-reader.h"
39 #include "libpspp/message.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-scanf.h"
50 #include "ui/syntax-gen.h"
52 #include "gl/intprops.h"
53 #include "gl/xalloc.h"
56 #define _(msgid) gettext (msgid)
57 #define N_(msgid) msgid
59 /* Page where the user chooses field separators. */
60 struct separators_page
62 /* How to break lines into columns. */
63 struct string separators; /* Field separators. */
64 struct string quotes; /* Quote characters. */
65 bool escape; /* Doubled quotes yield a quote mark? */
69 GtkWidget *custom_entry;
71 GtkWidget *quote_combo;
72 GtkEntry *quote_entry;
74 PsppSheetView *fields_tree_view;
77 /* The "separators" page of the assistant. */
79 static void revise_fields_preview (struct import_assistant *ia);
80 static void choose_likely_separators (struct import_assistant *ia);
81 static void find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
82 const char *targets, const char *def,
83 struct string *result);
84 static void clear_fields (struct import_assistant *ia);
85 static void revise_fields_preview (struct import_assistant *);
86 static void set_separators (struct import_assistant *);
87 static void get_separators (struct import_assistant *);
88 static void on_separators_custom_entry_notify (GObject *UNUSED,
90 struct import_assistant *);
91 static void on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
92 struct import_assistant *);
93 static void on_quote_combo_change (GtkComboBox *combo,
94 struct import_assistant *);
95 static void on_quote_cb_toggle (GtkToggleButton *quote_cb,
96 struct import_assistant *);
97 static void on_separator_toggle (GtkToggleButton *, struct import_assistant *);
99 /* A common field separator and its identifying name. */
102 const char *name; /* Name (for use with get_widget_assert). */
103 int c; /* Separator character. */
106 /* All the separators in the dialog box. */
107 static const struct separator separators[] =
119 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
122 set_quote_list (GtkComboBox *cb)
124 GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
127 const gchar *seperator[3] = {"'\"", "\'", "\""};
129 for (i = 0; i < 3; i++)
131 const gchar *s = seperator[i];
133 /* Add a new row to the model */
134 gtk_list_store_append (list, &iter);
135 gtk_list_store_set (list, &iter,
141 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
142 g_object_unref (list);
144 gtk_combo_box_set_entry_text_column (cb, 0);
147 /* Initializes IA's separators substructure. */
149 struct separators_page *
150 separators_page_create (struct import_assistant *ia)
152 GtkBuilder *builder = ia->asst.builder;
156 struct separators_page *p = xzalloc (sizeof *p);
158 p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Separators"),
159 GTK_ASSISTANT_PAGE_CONTENT);
161 p->custom_cb = get_widget_assert (builder, "custom-cb");
162 p->custom_entry = get_widget_assert (builder, "custom-entry");
163 p->quote_combo = get_widget_assert (builder, "quote-combo");
164 p->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (p->quote_combo)));
165 p->quote_cb = get_widget_assert (builder, "quote-cb");
166 p->escape_cb = get_widget_assert (builder, "escape");
168 set_quote_list (GTK_COMBO_BOX (p->quote_combo));
169 p->fields_tree_view = PSPP_SHEET_VIEW (get_widget_assert (builder, "fields"));
170 g_signal_connect (p->quote_combo, "changed",
171 G_CALLBACK (on_quote_combo_change), ia);
172 g_signal_connect (p->quote_cb, "toggled",
173 G_CALLBACK (on_quote_cb_toggle), ia);
174 g_signal_connect (p->custom_entry, "notify::text",
175 G_CALLBACK (on_separators_custom_entry_notify), ia);
176 g_signal_connect (p->custom_cb, "toggled",
177 G_CALLBACK (on_separators_custom_cb_toggle), ia);
178 for (i = 0; i < SEPARATOR_CNT; i++)
179 g_signal_connect (get_widget_assert (builder, separators[i].name),
180 "toggled", G_CALLBACK (on_separator_toggle), ia);
181 g_signal_connect (p->escape_cb, "toggled",
182 G_CALLBACK (on_separator_toggle), ia);
187 /* Frees IA's separators substructure. */
189 destroy_separators_page (struct import_assistant *ia)
191 struct separators_page *s = ia->separators;
193 ds_destroy (&s->separators);
194 ds_destroy (&s->quotes);
198 /* Called just before the separators page becomes visible in the
201 prepare_separators_page (struct import_assistant *ia)
203 revise_fields_preview (ia);
206 /* Called when the Reset button is clicked on the separators
207 page, resets the separators to the defaults. */
209 reset_separators_page (struct import_assistant *ia)
211 choose_likely_separators (ia);
215 /* Frees and clears the column data in IA's separators
218 clear_fields (struct import_assistant *ia)
220 if (ia->column_cnt > 0)
225 for (row = 0; row < ia->file.line_cnt; row++)
227 const struct string *line = &ia->file.lines[row];
228 const char *line_start = ds_data (line);
229 const char *line_end = ds_end (line);
231 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
233 char *s = ss_data (col->contents[row]);
234 if (!(s >= line_start && s <= line_end))
235 ss_dealloc (&col->contents[row]);
239 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
242 free (col->contents);
251 /* Breaks the file data in IA into columns based on the
252 separators set in IA's separators substructure. */
254 split_fields (struct import_assistant *ia)
256 struct separators_page *s = ia->separators;
257 size_t columns_allocated;
263 /* Is space in the set of separators? */
264 space_sep = ss_find_byte (ds_ss (&s->separators), ' ') != SIZE_MAX;
266 /* Split all the lines, not just those from
267 ia->first_line.skip_lines on, so that we split the line that
268 contains variables names if ia->first_line.variable_names is
270 columns_allocated = 0;
271 for (row = 0; row < ia->file.line_cnt; row++)
273 struct string *line = &ia->file.lines[row];
274 struct substring text = ds_ss (line);
277 for (column_idx = 0; ; column_idx++)
279 struct substring field;
280 struct column *column;
283 ss_ltrim (&text, ss_cstr (" "));
284 if (ss_is_empty (text))
290 else if (!ds_is_empty (&s->quotes)
291 && ds_find_byte (&s->quotes, text.string[0]) != SIZE_MAX)
293 int quote = ss_get_byte (&text);
295 ss_get_until (&text, quote, &field);
302 while ((c = ss_get_byte (&text)) != EOF)
305 else if (ss_match_byte (&text, quote))
306 ds_put_byte (&s, quote);
313 ss_get_bytes (&text, ss_cspan (text, ds_ss (&s->separators)),
316 if (column_idx >= ia->column_cnt)
318 struct column *column;
320 if (ia->column_cnt >= columns_allocated)
321 ia->columns = x2nrealloc (ia->columns, &columns_allocated,
322 sizeof *ia->columns);
323 column = &ia->columns[ia->column_cnt++];
326 column->contents = xcalloc (ia->file.line_cnt,
327 sizeof *column->contents);
329 column = &ia->columns[column_idx];
330 column->contents[row] = field;
331 if (ss_length (field) > column->width)
332 column->width = ss_length (field);
335 ss_ltrim (&text, ss_cstr (" "));
336 if (ss_is_empty (text))
338 if (ss_find_byte (ds_ss (&s->separators), ss_first (text))
340 ss_advance (&text, 1);
345 /* Chooses a name for each column on the separators page */
347 choose_column_names (struct import_assistant *ia)
349 struct dictionary *dict;
350 unsigned long int generated_name_count = 0;
354 dict = dict_create (get_default_encoding ());
355 name_row = ia->variable_names && ia->skip_lines ? ia->skip_lines : 0;
356 for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
360 hint = name_row ? ss_xstrdup (col->contents[name_row - 1]) : NULL;
361 name = dict_make_unique_var_name (dict, hint, &generated_name_count);
365 dict_create_var_assert (dict, name, 0);
370 /* Picks the most likely separator and quote characters based on
373 choose_likely_separators (struct import_assistant *ia)
375 unsigned long int histogram[UCHAR_MAX + 1] = { 0 };
378 /* Construct a histogram of all the characters used in the
380 for (row = 0; row < ia->file.line_cnt; row++)
382 struct substring line = ds_ss (&ia->file.lines[row]);
383 size_t length = ss_length (line);
385 for (i = 0; i < length; i++)
386 histogram[(unsigned char) line.string[i]]++;
389 find_commonest_chars (histogram, "\"'", "", &ia->separators->quotes);
390 find_commonest_chars (histogram, ",;:/|!\t-", ",", &ia->separators->separators);
391 ia->separators->escape = true;
394 /* Chooses the most common character among those in TARGETS,
395 based on the frequency data in HISTOGRAM, and stores it in
396 RESULT. If there is a tie for the most common character among
397 those in TARGETS, the earliest character is chosen. If none
398 of the TARGETS appear at all, then DEF is used as a
401 find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
402 const char *targets, const char *def,
403 struct string *result)
405 unsigned char max = 0;
406 unsigned long int max_count = 0;
408 for (; *targets != '\0'; targets++)
410 unsigned char c = *targets;
411 unsigned long int count = histogram[c];
412 if (count > max_count)
421 ds_put_byte (result, max);
424 ds_assign_cstr (result, def);
427 /* Revises the contents of the fields tree view based on the
428 currently chosen set of separators. */
430 revise_fields_preview (struct import_assistant *ia)
434 push_watch_cursor (ia);
436 w = GTK_WIDGET (ia->separators->fields_tree_view);
437 gtk_widget_destroy (w);
440 choose_column_names (ia);
441 ia->separators->fields_tree_view = create_data_tree_view (
443 GTK_CONTAINER (get_widget_assert (ia->asst.builder, "fields-scroller")),
446 pop_watch_cursor (ia);
449 /* Sets the widgets to match IA's separators substructure. */
451 set_separators (struct import_assistant *ia)
453 struct separators_page *s = ia->separators;
455 struct string custom;
460 ds_init_empty (&custom);
462 for (i = 0; i < ds_length (&s->separators); i++)
464 unsigned char c = ds_at (&s->separators, i);
467 for (j = 0; j < SEPARATOR_CNT; j++)
469 const struct separator *s = &separators[j];
477 ds_put_byte (&custom, c);
481 for (i = 0; i < SEPARATOR_CNT; i++)
483 const struct separator *s = &separators[i];
484 GtkWidget *button = get_widget_assert (ia->asst.builder, s->name);
485 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
486 (seps & (1u << i)) != 0);
488 any_custom = !ds_is_empty (&custom);
489 gtk_entry_set_text (GTK_ENTRY (s->custom_entry), ds_cstr (&custom));
490 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->custom_cb),
492 gtk_widget_set_sensitive (s->custom_entry, any_custom);
493 ds_destroy (&custom);
495 any_quotes = !ds_is_empty (&s->quotes);
497 gtk_entry_set_text (s->quote_entry,
498 any_quotes ? ds_cstr (&s->quotes) : "\"");
499 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->quote_cb),
501 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->escape_cb),
503 gtk_widget_set_sensitive (s->quote_combo, any_quotes);
504 gtk_widget_set_sensitive (s->escape_cb, any_quotes);
507 /* Sets IA's separators substructure to match the widgets. */
509 get_separators (struct import_assistant *ia)
511 struct separators_page *s = ia->separators;
514 ds_clear (&s->separators);
515 for (i = 0; i < SEPARATOR_CNT; i++)
517 const struct separator *sep = &separators[i];
518 GtkWidget *button = get_widget_assert (ia->asst.builder, sep->name);
519 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
520 ds_put_byte (&s->separators, sep->c);
523 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->custom_cb)))
524 ds_put_cstr (&s->separators,
525 gtk_entry_get_text (GTK_ENTRY (s->custom_entry)));
527 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->quote_cb)))
529 const gchar *text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (s->quote_combo))));
530 ds_assign_cstr (&s->quotes, text);
533 ds_clear (&s->quotes);
534 s->escape = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->escape_cb));
537 /* Called when the user changes the entry field for custom
540 on_separators_custom_entry_notify (GObject *gobject UNUSED,
541 GParamSpec *arg1 UNUSED,
542 struct import_assistant *ia)
544 revise_fields_preview (ia);
547 /* Called when the user toggles the checkbox that enables custom
550 on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
551 struct import_assistant *ia)
553 bool is_active = gtk_toggle_button_get_active (custom_cb);
554 gtk_widget_set_sensitive (ia->separators->custom_entry, is_active);
555 revise_fields_preview (ia);
558 /* Called when the user changes the selection in the combo box
559 that selects a quote character. */
561 on_quote_combo_change (GtkComboBox *combo, struct import_assistant *ia)
563 revise_fields_preview (ia);
566 /* Called when the user toggles the checkbox that enables
569 on_quote_cb_toggle (GtkToggleButton *quote_cb, struct import_assistant *ia)
571 bool is_active = gtk_toggle_button_get_active (quote_cb);
572 gtk_widget_set_sensitive (ia->separators->quote_combo, is_active);
573 gtk_widget_set_sensitive (ia->separators->escape_cb, is_active);
574 revise_fields_preview (ia);
577 /* Called when the user toggles one of the separators
580 on_separator_toggle (GtkToggleButton *toggle UNUSED,
581 struct import_assistant *ia)
583 revise_fields_preview (ia);
589 separators_append_syntax (const struct import_assistant *ia, struct string *s)
592 ds_put_cstr (s, " /DELIMITERS=\"");
593 if (ds_find_byte (&ia->separators->separators, '\t') != SIZE_MAX)
594 ds_put_cstr (s, "\\t");
595 if (ds_find_byte (&ia->separators->separators, '\\') != SIZE_MAX)
596 ds_put_cstr (s, "\\\\");
597 for (i = 0; i < ds_length (&ia->separators->separators); i++)
599 char c = ds_at (&ia->separators->separators, i);
601 ds_put_cstr (s, "\"\"");
602 else if (c != '\t' && c != '\\')
605 ds_put_cstr (s, "\"\n");
606 if (!ds_is_empty (&ia->separators->quotes))
607 syntax_gen_pspp (s, " /QUALIFIER=%sq\n", ds_cstr (&ia->separators->quotes));
608 if (!ds_is_empty (&ia->separators->quotes) && ia->separators->escape)
609 ds_put_cstr (s, " /ESCAPE\n");