Import Assistant: Fix generation of /DELIMITERS subcommand
[pspp] / src / ui / gui / psppire-import-assistant.c
index f7454c5b3e230a1683a7ce157a4bede2fcd4a264..51eae36241e49ebe6f1d164e13aad433a32d749c 100644 (file)
@@ -14,7 +14,6 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
-
 #include <config.h>
 
 #include <errno.h>
@@ -133,8 +132,6 @@ psppire_import_assistant_get_property (GObject    *object,
 static GObjectClass * parent_class = NULL;
 
 
-static void destroy_columns (PsppireImportAssistant *ia);
-
 static void
 psppire_import_assistant_finalize (GObject *object)
 {
@@ -144,9 +141,6 @@ psppire_import_assistant_finalize (GObject *object)
   if (ia->spreadsheet)
     spreadsheet_unref (ia->spreadsheet);
 
-  //  destroy_columns (ia);
-
-  ds_destroy (&ia->separators);
   ds_destroy (&ia->quotes);
 
   g_object_unref (ia->builder);
@@ -200,13 +194,16 @@ revise_fields_preview (PsppireImportAssistant *ia)
 {
   push_watch_cursor (ia);
 
-  get_separators (ia);
+  //  get_separators (ia);
   //  split_fields (ia);
   choose_column_names (ia);
 
   pop_watch_cursor (ia);
 }
 
+
+#if SHEET_MERGE
+
 /* Chooses the most common character among those in TARGETS,
    based on the frequency data in HISTOGRAM, and stores it in
    RESULT.  If there is a tie for the most common character among
@@ -269,6 +266,7 @@ choose_likely_separators (PsppireImportAssistant *ia)
   find_commonest_chars (histogram, ",;:/|!\t-", ",", &ia->separators);
 }
 
+#endif
 
 static void set_separators (PsppireImportAssistant *ia);
 
@@ -352,8 +350,8 @@ prepare_separators_page (PsppireImportAssistant *ia, GtkWidget *page)
   repopulate_delimiter_columns (ia);
 
   revise_fields_preview (ia);
-  choose_likely_separators (ia);
-  set_separators (ia);
+  //  choose_likely_separators (ia);
+  //  set_separators (ia);
 }
 
 struct separator
@@ -378,6 +376,7 @@ static const struct separator separators[] =
 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
 
 
+#if SHEET_MERGE
 
 /* Sets the widgets to match IA's separators substructure. */
 static void
@@ -433,6 +432,7 @@ set_separators (PsppireImportAssistant *ia)
   gtk_widget_set_sensitive (ia->quote_combo, any_quotes);
 }
 
+#endif
 
 /* Resets IA's intro page to its initial state. */
 static void
@@ -733,9 +733,9 @@ on_chosen (PsppireImportAssistant *ia, GtkWidget *page)
 static void
 on_map (PsppireImportAssistant *ia, GtkWidget *page)
 {
+#if TEXT_FILE
   GtkFileChooser *fc = GTK_FILE_CHOOSER (page);
 
-#if TEXT_FILE
   if (ia->file_name)
     gtk_file_chooser_set_filename (fc, ia->file_name);
 #endif
@@ -987,7 +987,6 @@ on_treeview_selection_change (PsppireImportAssistant *ia)
   if (gtk_tree_selection_get_selected (selection, &model, &iter))
     {
       int n;
-      PsppireTextFile *tf = PSPPIRE_TEXT_FILE (model);
       GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
       gint *index = gtk_tree_path_get_indices (path);
 
@@ -1232,21 +1231,6 @@ struct column
 
 #if SHEET_MERGE
 
-static void
-destroy_columns (PsppireImportAssistant *ia)
-{
-  struct column *col;
-  for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
-    {
-      free (col->name);
-      free (col->contents);
-    }
-
-  free (ia->columns);
-}
-
-
-
 /* Called to render one of the cells in the fields preview tree
    view. */
 static void
@@ -1487,7 +1471,7 @@ set_quote_list (GtkComboBox *cb)
 }
 
 
-
+#if SHEET_MERGE
 
 /* Sets IA's separators substructure to match the widgets. */
 static void
@@ -1520,7 +1504,7 @@ get_separators (PsppireImportAssistant *ia)
 
 
 
-#if SHEET_MERGE
+
 
 /* Breaks the file data in IA into columns based on the
    separators set in IA's separators substructure. */
@@ -1849,8 +1833,7 @@ my_read (struct casereader *reader, void *aux, casenumber idx)
 static void
 my_destroy (struct casereader *reader, void *aux)
 {
-  PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
-  g_print ("%s:%d\n", __FILE__, __LINE__);
+  g_print ("%s:%d %p\n", __FILE__, __LINE__, reader);
 }
 
 static void
@@ -1883,9 +1866,9 @@ foo (struct dictionary *dict, void *aux)
 
 
   PsppireDataStore *store = NULL;
-    
+
   g_object_get (ia->data_sheet, "data-model", &store, NULL);
-  
+
   psppire_data_store_set_reader (store, reader);
 }
 
@@ -1902,18 +1885,53 @@ prepare_formats_page (PsppireImportAssistant *ia)
   my_casereader_class.advance = my_advance;
 
   struct caseproto *proto = caseproto_create ();
-
-  dict_set_change_callback (ia->dict, foo, ia);
-
   int i;
+
+  struct fmt_guesser **fg = xcalloc (sizeof *fg, dict_get_var_cnt (ia->dict));
   for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
     {
-      const struct variable *var = dict_get_var (ia->dict, i);
-      proto = caseproto_add_width (proto, var_get_width (var));
+      fg[i] = fmt_guesser_create ();
     }
 
   gint n_rows = gtk_tree_model_iter_n_children (ia->delimiters_model, NULL);
 
+  GtkTreeIter iter;
+  gboolean ok;
+  for (ok = gtk_tree_model_get_iter_first (ia->delimiters_model, &iter);
+       ok;
+       ok = gtk_tree_model_iter_next (ia->delimiters_model, &iter))
+    {
+      for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
+       {
+         gchar *s = NULL;
+         gtk_tree_model_get (ia->delimiters_model, &iter, i+1, &s, -1);
+         fmt_guesser_add (fg[i], ss_cstr (s));
+         free (s);
+       }
+    }
+
+  for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
+    {
+      struct fmt_spec fs;
+      fmt_guesser_guess (fg[i], &fs);
+
+      fmt_fix (&fs, FMT_FOR_INPUT);
+
+      struct variable *var = dict_get_var (ia->dict, i);
+
+      int width = fmt_var_width (&fs);
+
+      var_set_width_and_formats (var, width,
+                                &fs, &fs);
+
+      proto = caseproto_add_width (proto, width);
+      fmt_guesser_destroy (fg[i]);
+    }
+
+  free (fg);
+
+  //  dict_set_change_callback (ia->dict, foo, ia);
+
   struct casereader *reader =
     casereader_create_random (proto, n_rows, &my_casereader_class,  ia);
 
@@ -1922,6 +1940,15 @@ prepare_formats_page (PsppireImportAssistant *ia)
 
   g_object_set (ia->data_sheet, "data-model", store, NULL);
 
+
+  gint pmax;
+  g_object_get (get_widget_assert (ia->builder, "vpaned1"),
+               "max-position", &pmax, NULL);
+
+
+  g_object_set (get_widget_assert (ia->builder, "vpaned1"),
+               "position", pmax / 2, NULL);
+
   gtk_widget_show (ia->paste_button);
 }
 
@@ -1969,25 +1996,28 @@ static void
 separators_append_syntax (const PsppireImportAssistant *ia, struct string *s)
 {
   int i;
+
   ds_put_cstr (s, "  /DELIMITERS=\"");
-  if (ds_find_byte (&ia->separators, '\t') != SIZE_MAX)
+
+  if (gtk_toggle_button_get_active (get_widget_assert (ia->builder, "tab")))
     ds_put_cstr (s, "\\t");
-  if (ds_find_byte (&ia->separators, '\\') != SIZE_MAX)
-    ds_put_cstr (s, "\\\\");
-  for (i = 0; i < ds_length (&ia->separators); i++)
+  for (i = 0; i < SEPARATOR_CNT; i++)
     {
-      char c = ds_at (&ia->separators, i);
-      if (c == '"')
-       ds_put_cstr (s, "\"\"");
-      else if (c != '\t' && c != '\\')
-       ds_put_byte (s, c);
+      const struct separator *seps = &separators[i];
+      GtkWidget *button = get_widget_assert (ia->builder, seps->name);
+      if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
+       {
+         if (seps->c == '\t')
+           continue;
+
+         ds_put_byte (s, seps->c);
+       }
     }
   ds_put_cstr (s, "\"\n");
   if (!ds_is_empty (&ia->quotes))
     syntax_gen_pspp (s, "  /QUALIFIER=%sq\n", ds_cstr (&ia->quotes));
 }
 
-
 static void
 formats_append_syntax (const PsppireImportAssistant *ia, struct string *s)
 {