Merge remote-tracking branch 'origin/master' into gtk3
[pspp] / src / ui / gui / page-separators.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "page-separators.h"
20
21 #include "ui/gui/text-data-import-dialog.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <gtk/gtk.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29
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/checkbox-treeview.h"
41 #include "ui/gui/dialog-common.h"
42 #include "ui/gui/executor.h"
43 #include "ui/gui/helper.h"
44 #include "ui/gui/builder-wrapper.h"
45 #include "ui/gui/psppire-data-window.h"
46 #include "ui/gui/psppire-dialog.h"
47 #include "ui/gui/psppire-encoding-selector.h"
48 #include "ui/gui/psppire-empty-list-store.h"
49 #include "ui/gui/psppire-var-sheet.h"
50 #include "ui/gui/psppire-scanf.h"
51 #include "ui/syntax-gen.h"
52
53 #include "gl/error.h"
54 #include "gl/intprops.h"
55 #include "gl/xalloc.h"
56
57 #include "gettext.h"
58 #define _(msgid) gettext (msgid)
59 #define N_(msgid) msgid
60
61 /* Page where the user chooses field separators. */
62 struct separators_page
63   {
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? */
68
69     GtkWidget *page;
70     GtkWidget *custom_cb;
71     GtkWidget *custom_entry;
72     GtkWidget *quote_cb;
73     GtkWidget *quote_combo;
74     GtkEntry *quote_entry;
75     GtkWidget *escape_cb;
76     PsppSheetView *fields_tree_view;
77   };
78
79 /* The "separators" page of the assistant. */
80
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,
91                                                GParamSpec *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 *);
100
101 /* A common field separator and its identifying name. */
102 struct separator
103   {
104     const char *name;           /* Name (for use with get_widget_assert). */
105     int c;                      /* Separator character. */
106   };
107
108 /* All the separators in the dialog box. */
109 static const struct separator separators[] =
110   {
111     {"space", ' '},
112     {"tab", '\t'},
113     {"bang", '!'},
114     {"colon", ':'},
115     {"comma", ','},
116     {"hyphen", '-'},
117     {"pipe", '|'},
118     {"semicolon", ';'},
119     {"slash", '/'},
120   };
121 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
122
123 static void
124 set_quote_list (GtkComboBox *cb)
125 {
126   GtkListStore *list =  gtk_list_store_new (1, G_TYPE_STRING);
127   GtkTreeIter iter;
128   gint i;
129   const gchar *seperator[3] = {"'\"", "\'", "\""};
130
131   for (i = 0; i < 3; i++)
132     {
133       const gchar *s = seperator[i];
134
135       /* Add a new row to the model */
136       gtk_list_store_append (list, &iter);
137       gtk_list_store_set (list, &iter,
138                           0, s,
139                           -1);
140
141     }
142
143   gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
144   g_object_unref (list);
145
146   gtk_combo_box_set_entry_text_column (cb, 0);
147 }
148
149 /* Initializes IA's separators substructure. */
150
151 struct separators_page *
152 separators_page_create (struct import_assistant *ia)
153 {
154   GtkBuilder *builder = ia->asst.builder;
155
156   size_t i;
157
158   struct separators_page *p = xzalloc (sizeof *p);
159
160   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Separators"),
161                                    GTK_ASSISTANT_PAGE_CONTENT);
162
163   p->custom_cb = get_widget_assert (builder, "custom-cb");
164   p->custom_entry = get_widget_assert (builder, "custom-entry");
165   p->quote_combo = get_widget_assert (builder, "quote-combo");
166   p->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (p->quote_combo)));
167   p->quote_cb = get_widget_assert (builder, "quote-cb");
168   p->escape_cb = get_widget_assert (builder, "escape");
169
170   set_quote_list (GTK_COMBO_BOX (p->quote_combo));
171   p->fields_tree_view = PSPP_SHEET_VIEW (get_widget_assert (builder, "fields"));
172   g_signal_connect (p->quote_combo, "changed",
173                     G_CALLBACK (on_quote_combo_change), ia);
174   g_signal_connect (p->quote_cb, "toggled",
175                     G_CALLBACK (on_quote_cb_toggle), ia);
176   g_signal_connect (p->custom_entry, "notify::text",
177                     G_CALLBACK (on_separators_custom_entry_notify), ia);
178   g_signal_connect (p->custom_cb, "toggled",
179                     G_CALLBACK (on_separators_custom_cb_toggle), ia);
180   for (i = 0; i < SEPARATOR_CNT; i++)
181     g_signal_connect (get_widget_assert (builder, separators[i].name),
182                       "toggled", G_CALLBACK (on_separator_toggle), ia);
183   g_signal_connect (p->escape_cb, "toggled",
184                     G_CALLBACK (on_separator_toggle), ia);
185
186   return p;
187 }
188
189 /* Frees IA's separators substructure. */
190 void
191 destroy_separators_page (struct import_assistant *ia)
192 {
193   struct separators_page *s = ia->separators;
194
195   ds_destroy (&s->separators);
196   ds_destroy (&s->quotes);
197   clear_fields (ia);
198 }
199
200 /* Called just before the separators page becomes visible in the
201    assistant. */
202 void
203 prepare_separators_page (struct import_assistant *ia)
204 {
205   revise_fields_preview (ia);
206 }
207
208 /* Called when the Reset button is clicked on the separators
209    page, resets the separators to the defaults. */
210 void
211 reset_separators_page (struct import_assistant *ia)
212 {
213   choose_likely_separators (ia);
214   set_separators (ia);
215 }
216
217 /* Frees and clears the column data in IA's separators
218    substructure. */
219 static void
220 clear_fields (struct import_assistant *ia)
221 {
222   if (ia->column_cnt > 0)
223     {
224       struct column *col;
225       size_t row;
226
227       for (row = 0; row < ia->file.line_cnt; row++)
228         {
229           const struct string *line = &ia->file.lines[row];
230           const char *line_start = ds_data (line);
231           const char *line_end = ds_end (line);
232
233           for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
234             {
235               char *s = ss_data (col->contents[row]);
236               if (!(s >= line_start && s <= line_end))
237                 ss_dealloc (&col->contents[row]);
238             }
239         }
240
241       for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
242         {
243           free (col->name);
244           free (col->contents);
245         }
246
247       free (ia->columns);
248       ia->columns = NULL;
249       ia->column_cnt = 0;
250     }
251 }
252
253 /* Breaks the file data in IA into columns based on the
254    separators set in IA's separators substructure. */
255 static void
256 split_fields (struct import_assistant *ia)
257 {
258   struct separators_page *s = ia->separators;
259   size_t columns_allocated;
260   bool space_sep;
261   size_t row;
262
263   clear_fields (ia);
264
265   /* Is space in the set of separators? */
266   space_sep = ss_find_byte (ds_ss (&s->separators), ' ') != SIZE_MAX;
267
268   /* Split all the lines, not just those from
269      ia->first_line.skip_lines on, so that we split the line that
270      contains variables names if ia->first_line.variable_names is
271      true. */
272   columns_allocated = 0;
273   for (row = 0; row < ia->file.line_cnt; row++)
274     {
275       struct string *line = &ia->file.lines[row];
276       struct substring text = ds_ss (line);
277       size_t column_idx;
278
279       for (column_idx = 0; ; column_idx++)
280         {
281           struct substring field;
282           struct column *column;
283
284           if (space_sep)
285             ss_ltrim (&text, ss_cstr (" "));
286           if (ss_is_empty (text))
287             {
288               if (column_idx != 0)
289                 break;
290               field = text;
291             }
292           else if (!ds_is_empty (&s->quotes)
293                    && ds_find_byte (&s->quotes, text.string[0]) != SIZE_MAX)
294             {
295               int quote = ss_get_byte (&text);
296               if (!s->escape)
297                 ss_get_until (&text, quote, &field);
298               else
299                 {
300                   struct string s;
301                   int c;
302
303                   ds_init_empty (&s);
304                   while ((c = ss_get_byte (&text)) != EOF)
305                     if (c != quote)
306                       ds_put_byte (&s, c);
307                     else if (ss_match_byte (&text, quote))
308                       ds_put_byte (&s, quote);
309                     else
310                       break;
311                   field = ds_ss (&s);
312                 }
313             }
314           else
315             ss_get_bytes (&text, ss_cspan (text, ds_ss (&s->separators)),
316                           &field);
317
318           if (column_idx >= ia->column_cnt)
319             {
320               struct column *column;
321
322               if (ia->column_cnt >= columns_allocated)
323                 ia->columns = x2nrealloc (ia->columns, &columns_allocated,
324                                          sizeof *ia->columns);
325               column = &ia->columns[ia->column_cnt++];
326               column->name = NULL;
327               column->width = 0;
328               column->contents = xcalloc (ia->file.line_cnt,
329                                           sizeof *column->contents);
330             }
331           column = &ia->columns[column_idx];
332           column->contents[row] = field;
333           if (ss_length (field) > column->width)
334             column->width = ss_length (field);
335
336           if (space_sep)
337             ss_ltrim (&text, ss_cstr (" "));
338           if (ss_is_empty (text))
339             break;
340           if (ss_find_byte (ds_ss (&s->separators), ss_first (text))
341               != SIZE_MAX)
342             ss_advance (&text, 1);
343         }
344     }
345 }
346
347 /* Chooses a name for each column on the separators page */
348 static void
349 choose_column_names (struct import_assistant *ia)
350 {
351   struct dictionary *dict;
352   unsigned long int generated_name_count = 0;
353   struct column *col;
354   size_t name_row;
355
356   dict = dict_create (get_default_encoding ());
357   name_row = ia->variable_names && ia->skip_lines ? ia->skip_lines : 0;
358   for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
359     {
360       char *hint, *name;
361
362       hint = name_row ? ss_xstrdup (col->contents[name_row - 1]) : NULL;
363       name = dict_make_unique_var_name (dict, hint, &generated_name_count);
364       free (hint);
365
366       col->name = name;
367       dict_create_var_assert (dict, name, 0);
368     }
369   dict_destroy (dict);
370 }
371
372 /* Picks the most likely separator and quote characters based on
373    IA's file data. */
374 static void
375 choose_likely_separators (struct import_assistant *ia)
376 {
377   unsigned long int histogram[UCHAR_MAX + 1] = { 0 };
378   size_t row;
379
380   /* Construct a histogram of all the characters used in the
381      file. */
382   for (row = 0; row < ia->file.line_cnt; row++)
383     {
384       struct substring line = ds_ss (&ia->file.lines[row]);
385       size_t length = ss_length (line);
386       size_t i;
387       for (i = 0; i < length; i++)
388         histogram[(unsigned char) line.string[i]]++;
389     }
390
391   find_commonest_chars (histogram, "\"'", "", &ia->separators->quotes);
392   find_commonest_chars (histogram, ",;:/|!\t-", ",", &ia->separators->separators);
393   ia->separators->escape = true;
394 }
395
396 /* Chooses the most common character among those in TARGETS,
397    based on the frequency data in HISTOGRAM, and stores it in
398    RESULT.  If there is a tie for the most common character among
399    those in TARGETS, the earliest character is chosen.  If none
400    of the TARGETS appear at all, then DEF is used as a
401    fallback. */
402 static void
403 find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
404                       const char *targets, const char *def,
405                       struct string *result)
406 {
407   unsigned char max = 0;
408   unsigned long int max_count = 0;
409
410   for (; *targets != '\0'; targets++)
411     {
412       unsigned char c = *targets;
413       unsigned long int count = histogram[c];
414       if (count > max_count)
415         {
416           max = c;
417           max_count = count;
418         }
419     }
420   if (max_count > 0)
421     {
422       ds_clear (result);
423       ds_put_byte (result, max);
424     }
425   else
426     ds_assign_cstr (result, def);
427 }
428
429 /* Revises the contents of the fields tree view based on the
430    currently chosen set of separators. */
431 static void
432 revise_fields_preview (struct import_assistant *ia)
433 {
434   GtkWidget *w;
435
436   push_watch_cursor (ia);
437
438   w = GTK_WIDGET (ia->separators->fields_tree_view);
439   gtk_widget_destroy (w);
440   get_separators (ia);
441   split_fields (ia);
442   choose_column_names (ia);
443   ia->separators->fields_tree_view = create_data_tree_view (
444     true,
445     GTK_CONTAINER (get_widget_assert (ia->asst.builder, "fields-scroller")),
446     ia);
447
448   pop_watch_cursor (ia);
449 }
450
451 /* Sets the widgets to match IA's separators substructure. */
452 static void
453 set_separators (struct import_assistant *ia)
454 {
455   struct separators_page *s = ia->separators;
456   unsigned int seps;
457   struct string custom;
458   bool any_custom;
459   bool any_quotes;
460   size_t i;
461
462   ds_init_empty (&custom);
463   seps = 0;
464   for (i = 0; i < ds_length (&s->separators); i++)
465     {
466       unsigned char c = ds_at (&s->separators, i);
467       int j;
468
469       for (j = 0; j < SEPARATOR_CNT; j++)
470         {
471           const struct separator *s = &separators[j];
472           if (s->c == c)
473             {
474               seps += 1u << j;
475               goto next;
476             }
477         }
478
479       ds_put_byte (&custom, c);
480     next:;
481     }
482
483   for (i = 0; i < SEPARATOR_CNT; i++)
484     {
485       const struct separator *s = &separators[i];
486       GtkWidget *button = get_widget_assert (ia->asst.builder, s->name);
487       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
488                                     (seps & (1u << i)) != 0);
489     }
490   any_custom = !ds_is_empty (&custom);
491   gtk_entry_set_text (GTK_ENTRY (s->custom_entry), ds_cstr (&custom));
492   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->custom_cb),
493                                 any_custom);
494   gtk_widget_set_sensitive (s->custom_entry, any_custom);
495   ds_destroy (&custom);
496
497   any_quotes = !ds_is_empty (&s->quotes);
498
499   gtk_entry_set_text (s->quote_entry,
500                       any_quotes ? ds_cstr (&s->quotes) : "\"");
501   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->quote_cb),
502                                 any_quotes);
503   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->escape_cb),
504                                 s->escape);
505   gtk_widget_set_sensitive (s->quote_combo, any_quotes);
506   gtk_widget_set_sensitive (s->escape_cb, any_quotes);
507 }
508
509 /* Sets IA's separators substructure to match the widgets. */
510 static void
511 get_separators (struct import_assistant *ia)
512 {
513   struct separators_page *s = ia->separators;
514   int i;
515
516   ds_clear (&s->separators);
517   for (i = 0; i < SEPARATOR_CNT; i++)
518     {
519       const struct separator *sep = &separators[i];
520       GtkWidget *button = get_widget_assert (ia->asst.builder, sep->name);
521       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
522         ds_put_byte (&s->separators, sep->c);
523     }
524
525   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->custom_cb)))
526     ds_put_cstr (&s->separators,
527                  gtk_entry_get_text (GTK_ENTRY (s->custom_entry)));
528
529   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->quote_cb)))
530     {
531       const gchar *text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (s->quote_combo))));
532       ds_assign_cstr (&s->quotes, text);
533     }
534   else
535     ds_clear (&s->quotes);
536   s->escape = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->escape_cb));
537 }
538
539 /* Called when the user changes the entry field for custom
540    separators. */
541 static void
542 on_separators_custom_entry_notify (GObject *gobject UNUSED,
543                                    GParamSpec *arg1 UNUSED,
544                                    struct import_assistant *ia)
545 {
546   revise_fields_preview (ia);
547 }
548
549 /* Called when the user toggles the checkbox that enables custom
550    separators. */
551 static void
552 on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
553                                 struct import_assistant *ia)
554 {
555   bool is_active = gtk_toggle_button_get_active (custom_cb);
556   gtk_widget_set_sensitive (ia->separators->custom_entry, is_active);
557   revise_fields_preview (ia);
558 }
559
560 /* Called when the user changes the selection in the combo box
561    that selects a quote character. */
562 static void
563 on_quote_combo_change (GtkComboBox *combo, struct import_assistant *ia)
564 {
565   revise_fields_preview (ia);
566 }
567
568 /* Called when the user toggles the checkbox that enables
569    quoting. */
570 static void
571 on_quote_cb_toggle (GtkToggleButton *quote_cb, struct import_assistant *ia)
572 {
573   bool is_active = gtk_toggle_button_get_active (quote_cb);
574   gtk_widget_set_sensitive (ia->separators->quote_combo, is_active);
575   gtk_widget_set_sensitive (ia->separators->escape_cb, is_active);
576   revise_fields_preview (ia);
577 }
578
579 /* Called when the user toggles one of the separators
580    checkboxes. */
581 static void
582 on_separator_toggle (GtkToggleButton *toggle UNUSED,
583                      struct import_assistant *ia)
584 {
585   revise_fields_preview (ia);
586 }
587
588
589
590 void 
591 separators_append_syntax (const struct import_assistant *ia, struct string *s)
592 {
593   int i;
594   ds_put_cstr (s, "  /DELIMITERS=\"");
595   if (ds_find_byte (&ia->separators->separators, '\t') != SIZE_MAX)
596     ds_put_cstr (s, "\\t");
597   if (ds_find_byte (&ia->separators->separators, '\\') != SIZE_MAX)
598     ds_put_cstr (s, "\\\\");
599   for (i = 0; i < ds_length (&ia->separators->separators); i++)
600     {
601       char c = ds_at (&ia->separators->separators, i);
602       if (c == '"')
603         ds_put_cstr (s, "\"\"");
604       else if (c != '\t' && c != '\\')
605         ds_put_byte (s, c);
606     }
607   ds_put_cstr (s, "\"\n");
608   if (!ds_is_empty (&ia->separators->quotes))
609     syntax_gen_pspp (s, "  /QUALIFIER=%sq\n", ds_cstr (&ia->separators->quotes));
610   if (!ds_is_empty (&ia->separators->quotes) && ia->separators->escape)
611     ds_put_cstr (s, "  /ESCAPE\n");
612 }