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