Merge remote-tracking branch 'origin/master' into sheet
[pspp] / src / ui / gui / psppire-import-assistant.c
index 59c34b8b85d285fc76bfe089e97cda8fc72b95a0..2eecd7a10589180cb819700b0bee0e9e436cf745 100644 (file)
 
 #include <config.h>
 
-#include <errno.h>
-#include <fcntl.h>
 #include <gtk/gtk.h>
-#include <sys/stat.h>
 
 #include "data/casereader.h"
 #include "data/data-in.h"
@@ -33,8 +30,6 @@
 #include "data/value-labels.h"
 #include "data/casereader-provider.h"
 
-#include "gl/intprops.h"
-
 #include "libpspp/i18n.h"
 #include "libpspp/line-reader.h"
 #include "libpspp/message.h"
 #include "libpspp/str.h"
 
 #include "builder-wrapper.h"
-#include "helper.h"
-#include "psppire-import-assistant.h"
-#include "psppire-scanf.h"
+
+#include "psppire-data-sheet.h"
+#include "psppire-data-store.h"
 #include "psppire-dialog.h"
-#include "psppire-empty-list-store.h"
+#include "psppire-delimited-text.h"
+#include "psppire-dict.h"
 #include "psppire-encoding-selector.h"
+#include "psppire-import-assistant.h"
+#include "psppire-scanf.h"
 #include "psppire-spreadsheet-model.h"
 #include "psppire-text-file.h"
-#include "psppire-delimited-text.h"
-#include "psppire-data-sheet.h"
-#include "psppire-data-store.h"
-#include "psppire-dict.h"
 #include "psppire-variable-sheet.h"
 
 #include "ui/syntax-gen.h"
 enum { MAX_LINE_LEN = 16384 }; /* Max length of an acceptable line. */
 
 
-/* Sets IA's separators substructure to match the widgets. */
-static void split_fields (PsppireImportAssistant *ia);
-
 /* Chooses a name for each column on the separators page */
 static void choose_column_names (PsppireImportAssistant *ia);
 
-
-
 static void intro_page_create (PsppireImportAssistant *ia);
 static void first_line_page_create (PsppireImportAssistant *ia);
 
 static void separators_page_create (PsppireImportAssistant *ia);
 static void formats_page_create (PsppireImportAssistant *ia);
 
-static void push_watch_cursor (PsppireImportAssistant *ia);
-static void pop_watch_cursor (PsppireImportAssistant *ia);
-
-
-
 static void psppire_import_assistant_init            (PsppireImportAssistant      *act);
 static void psppire_import_assistant_class_init      (PsppireImportAssistantClass *class);
 
@@ -138,7 +122,6 @@ psppire_import_assistant_finalize (GObject *object)
 {
   PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (object);
 
-
   if (ia->spreadsheet)
     spreadsheet_unref (ia->spreadsheet);
 
@@ -146,6 +129,9 @@ psppire_import_assistant_finalize (GObject *object)
 
   g_object_unref (ia->builder);
 
+  ia->response = -1;
+  g_main_loop_unref (ia->main_loop);
+
   if (G_OBJECT_CLASS (parent_class)->finalize)
     G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -233,8 +219,6 @@ choose_likely_separators (PsppireImportAssistant *ia)
 
   gboolean valid;
   GtkTreeIter iter;
-  int i = 0;
-
   int j;
 
   struct hmap count_map[SEPARATOR_CNT];
@@ -247,23 +231,27 @@ choose_likely_separators (PsppireImportAssistant *ia)
        valid;
        valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (ia->text_file), &iter))
     {
-      int j;
-
       gchar *line_text = NULL;
       gtk_tree_model_get (GTK_TREE_MODEL (ia->text_file), &iter, 1, &line_text, -1);
 
       gint *counts = xzalloc (sizeof *counts * SEPARATOR_CNT);
-      for (j = 0; j < strlen (line_text); ++j)
+
+      struct substring cs = ss_cstr (line_text);
+      for (;
+          UINT32_MAX != ss_first_mb (cs);
+          ss_get_mb (&cs))
        {
+         ucs4_t character = ss_first_mb (cs);
+
          int s;
          for (s = 0; s < SEPARATOR_CNT; ++s)
            {
-             // FIXME do this in UTF8 encoding
-             if (line_text[j] == separators[s].c)
+             if (character == separators[s].c)
                counts[s]++;
            }
        }
 
+      int j;
       for (j = 0; j < SEPARATOR_CNT; ++j)
        {
          if (counts[j] > 0)
@@ -738,6 +726,8 @@ psppire_import_assistant_init (PsppireImportAssistant *ia)
 
   ia->spreadsheet = NULL;
 
+  ia->main_loop = g_main_loop_new (NULL, TRUE);
+
   g_signal_connect (ia, "prepare", G_CALLBACK (on_prepare), ia);
   g_signal_connect (ia, "cancel", G_CALLBACK (on_cancel), ia);
   g_signal_connect (ia, "close", G_CALLBACK (on_close), ia);
@@ -1011,31 +1001,6 @@ psppire_import_assistant_new (GtkWindow *toplevel)
 
 \f
 
-struct column
-{
-  /* Variable name for this column.  This is the variable name
-     used on the separators page; it can be overridden by the
-     user on the formats page. */
-  char *name;
-
-  /* Maximum length of any row in this column. */
-  size_t width;
-
-  /* Contents of this column: contents[row] is the contents for
-     the given row.
-
-     A null substring indicates a missing column for that row
-     (because the line contains an insufficient number of
-     separators).
-
-     contents[] elements may be substrings of the lines[]
-     strings that represent the whole lines of the file, to
-     save memory.  Other elements are dynamically allocated
-     with ss_alloc_substring. */
-  struct substring *contents;
-};
-
-
 static void
 set_quote_list (GtkComboBox *cb)
 {
@@ -1068,29 +1033,25 @@ set_quote_list (GtkComboBox *cb)
 static void
 choose_column_names (PsppireImportAssistant *ia)
 {
-  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
+  int i;
+  unsigned long int generated_name_count = 0;
+  dict_clear (ia->dict);
+
+  for (i = 0; i < gtk_tree_model_get_n_columns (ia->delimiters_model) - 1; ++i)
     {
-      int i;
-      unsigned long int generated_name_count = 0;
-      dict_clear (ia->dict);
+      const gchar *candidate_name = NULL;
 
-      g_print ("%s:%d XXX %d\n", __FILE__, __LINE__, gtk_tree_model_get_n_columns (ia->delimiters_model));
-      
-      for (i = 0; i < gtk_tree_model_get_n_columns (ia->delimiters_model) - 1; ++i)
+      if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
        {
-         const gchar *candidate_name =
-           psppire_delimited_text_get_header_title
-           (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), i);
-
-         g_print ("%s:%d CN is %s\n", __FILE__, __LINE__, candidate_name);
-
-         char *name = dict_make_unique_var_name (ia->dict,
-                                                 candidate_name,
-                                                 &generated_name_count);
-         
-         dict_create_var_assert (ia->dict, name, 0);
-         free (name);
+         candidate_name = psppire_delimited_text_get_header_title (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), i);
        }
+
+      char *name = dict_make_unique_var_name (ia->dict,
+                                             candidate_name,
+                                             &generated_name_count);
+
+      dict_create_var_assert (ia->dict, name, 0);
+      free (name);
     }
 }
 
@@ -1652,3 +1613,12 @@ psppire_import_assistant_generate_syntax (PsppireImportAssistant *ia)
 
   return ds_cstr (&s);
 }
+
+
+int
+psppire_import_assistant_run (PsppireImportAssistant *asst)
+{
+  g_main_loop_run (asst->main_loop);
+  return asst->response;
+}
+