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