Merge remote branch 'origin/master' into import-gui
[pspp] / src / ui / gui / page-separators.c
index d16f0fe3d21c20992e481f7220b2dca889abed36..e147881730e710e6b70472806a2430c241071efa 100644 (file)
@@ -16,6 +16,8 @@
 
 #include <config.h>
 
+#include "page-separators.h"
+
 #include "ui/gui/text-data-import-dialog.h"
 
 #include <errno.h>
 #define _(msgid) gettext (msgid)
 #define N_(msgid) msgid
 
-\f
+/* Page where the user chooses field separators. */
+struct separators_page
+  {
+    /* How to break lines into columns. */
+    struct string separators;   /* Field separators. */
+    struct string quotes;       /* Quote characters. */
+    bool escape;                /* Doubled quotes yield a quote mark? */
+
+    GtkWidget *page;
+    GtkWidget *custom_cb;
+    GtkWidget *custom_entry;
+    GtkWidget *quote_cb;
+    GtkWidget *quote_combo;
+    GtkEntry *quote_entry;
+    GtkWidget *escape_cb;
+    GtkTreeView *fields_tree_view;
+  };
+
 /* The "separators" page of the assistant. */
 
 static void revise_fields_preview (struct import_assistant *ia);
@@ -130,17 +149,19 @@ set_quote_list (GtkComboBoxEntry *cb)
 }
 
 /* Initializes IA's separators substructure. */
-void
-init_separators_page (struct import_assistant *ia)
+
+struct separators_page *
+separators_page_create (struct import_assistant *ia)
 {
   GtkBuilder *builder = ia->asst.builder;
-  struct separators_page *p = ia->separators;
+
   size_t i;
 
-  choose_likely_separators (ia);
+  struct separators_page *p = xzalloc (sizeof *p);
 
   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Separators"),
                                    GTK_ASSISTANT_PAGE_CONTENT);
+
   p->custom_cb = get_widget_assert (builder, "custom-cb");
   p->custom_entry = get_widget_assert (builder, "custom-entry");
   p->quote_combo = get_widget_assert (builder, "quote-combo");
@@ -148,7 +169,6 @@ init_separators_page (struct import_assistant *ia)
   p->quote_cb = get_widget_assert (builder, "quote-cb");
   p->escape_cb = get_widget_assert (builder, "escape");
 
-  set_separators (ia);
   set_quote_list (GTK_COMBO_BOX_ENTRY (p->quote_combo));
   p->fields_tree_view = GTK_TREE_VIEW (get_widget_assert (builder, "fields"));
   g_signal_connect (p->quote_combo, "changed",
@@ -164,6 +184,8 @@ init_separators_page (struct import_assistant *ia)
                       "toggled", G_CALLBACK (on_separator_toggle), ia);
   g_signal_connect (p->escape_cb, "toggled",
                     G_CALLBACK (on_separator_toggle), ia);
+
+  return p;
 }
 
 /* Frees IA's separators substructure. */
@@ -328,15 +350,13 @@ split_fields (struct import_assistant *ia)
 static void
 choose_column_names (struct import_assistant *ia)
 {
-  const struct first_line_page *f = ia->first_line;
-  struct separators_page *s = ia->separators;
   struct dictionary *dict;
   unsigned long int generated_name_count = 0;
   struct column *col;
   size_t name_row;
 
   dict = dict_create (get_default_encoding ());
-  name_row = f->variable_names && f->skip_lines ? f->skip_lines : 0;
+  name_row = ia->variable_names && ia->skip_lines ? ia->skip_lines : 0;
   for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
     {
       char *hint, *name;
@@ -569,3 +589,28 @@ on_separator_toggle (GtkToggleButton *toggle UNUSED,
   revise_fields_preview (ia);
 }
 
+
+
+void 
+separators_append_syntax (const struct import_assistant *ia, struct string *s)
+{
+  int i;
+  ds_put_cstr (s, "  /DELIMITERS=\"");
+  if (ds_find_byte (&ia->separators->separators, '\t') != SIZE_MAX)
+    ds_put_cstr (s, "\\t");
+  if (ds_find_byte (&ia->separators->separators, '\\') != SIZE_MAX)
+    ds_put_cstr (s, "\\\\");
+  for (i = 0; i < ds_length (&ia->separators->separators); i++)
+    {
+      char c = ds_at (&ia->separators->separators, i);
+      if (c == '"')
+       ds_put_cstr (s, "\"\"");
+      else if (c != '\t' && c != '\\')
+       ds_put_byte (s, c);
+    }
+  ds_put_cstr (s, "\"\n");
+  if (!ds_is_empty (&ia->separators->quotes))
+    syntax_gen_pspp (s, "  /QUALIFIER=%sq\n", ds_cstr (&ia->separators->quotes));
+  if (!ds_is_empty (&ia->separators->quotes) && ia->separators->escape)
+    ds_put_cstr (s, "  /ESCAPE\n");
+}