PsppireImportAssistant: Avoid possible null pointer dereferences
[pspp] / src / ui / gui / psppire-import-assistant.c
index b5d8bc713b43c426e56ab5fcdd7edd58ad86e99c..bd69629d84e803cc12e5519048e7d2e6dbb9c7d8 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)
@@ -570,7 +558,7 @@ on_chosen (PsppireImportAssistant *ia, GtkWidget *page)
 
   gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), FALSE);
 
-  if (f && !g_file_test (f, G_FILE_TEST_IS_DIR))
+  if (f && g_file_test (f, G_FILE_TEST_IS_REGULAR))
     {
       gtk_assistant_set_page_complete (GTK_ASSISTANT(ia), GTK_WIDGET (fc), TRUE);
 
@@ -582,15 +570,15 @@ on_chosen (PsppireImportAssistant *ia, GtkWidget *page)
       if (!ia->spreadsheet)
        ia->spreadsheet = ods_probe (f, FALSE);
 
-      if (!ia->spreadsheet)
+      if (ia->spreadsheet)
        {
-         intro_page_create (ia);
-         first_line_page_create (ia);
-         separators_page_create (ia);
+         sheet_spec_page_create (ia);
        }
       else
        {
-         sheet_spec_page_create (ia);
+         intro_page_create (ia);
+         first_line_page_create (ia);
+         separators_page_create (ia);
        }
 
       formats_page_create (ia);
@@ -624,7 +612,6 @@ chooser_page_enter (PsppireImportAssistant *ia, GtkWidget *page)
 static void
 chooser_page_leave (PsppireImportAssistant *ia, GtkWidget *page)
 {
-  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
   g_free (ia->file_name);
   ia->file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (page));
   gchar *encoding = psppire_encoding_selector_get_encoding (ia->encoding_selector);
@@ -738,6 +725,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);
@@ -788,16 +777,14 @@ static void
 on_intro_amount_changed (PsppireImportAssistant *p)
 {
   gtk_widget_set_sensitive (p->n_cases_spin,
-                            gtk_toggle_button_get_active (
-                                                         GTK_TOGGLE_BUTTON (p->n_cases_button)));
+                           gtk_toggle_button_get_active
+                           (GTK_TOGGLE_BUTTON (p->n_cases_button)));
 
   gtk_widget_set_sensitive (p->percent_spin,
-                            gtk_toggle_button_get_active (
-                                                         GTK_TOGGLE_BUTTON (p->percent_button)));
+                           gtk_toggle_button_get_active
+                           (GTK_TOGGLE_BUTTON (p->percent_button)));
 }
 
-
-
 static void
 on_treeview_selection_change (PsppireImportAssistant *ia)
 {
@@ -807,30 +794,45 @@ on_treeview_selection_change (PsppireImportAssistant *ia)
   GtkTreeIter iter;
   if (gtk_tree_selection_get_selected (selection, &model, &iter))
     {
+      gint max_lines;
       int n;
       GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
       gint *index = gtk_tree_path_get_indices (path);
-
       n = *index;
-
       gtk_tree_path_free (path);
-
-      gtk_widget_set_sensitive (ia->variable_names_cb, n > 0);
-
-      ia->delimiters_model
-       psppire_delimited_text_new (GTK_TREE_MODEL (ia->text_file));
+      g_object_get (model, "maximum-lines", &max_lines, NULL);
+      gtk_widget_set_sensitive (ia->variable_names_cb,
+                               (n > 0 && n < max_lines));
+      ia->delimiters_model =
+       psppire_delimited_text_new (GTK_TREE_MODEL (ia->text_file));
       g_object_set (ia->delimiters_model, "first-line", n, NULL);
-
-      g_print ("%s:%d DT %p first line %d\n", __FILE__, __LINE__, ia->delimiters_model, n);
     }
 }
 
+static void
+render_text_preview_line (GtkTreeViewColumn *tree_column,
+               GtkCellRenderer *cell,
+               GtkTreeModel *tree_model,
+               GtkTreeIter *iter,
+               gpointer data)
+{
+  /*
+     Set the text  to a "insensitive" state if the row
+     is greater than what the user declared to be the maximum.
+  */
+  PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (data);
+  GtkTreePath *path = gtk_tree_model_get_path (tree_model, iter);
+  gint *ii = gtk_tree_path_get_indices (path);
+  gint max_lines;
+  g_object_get (tree_model, "maximum-lines", &max_lines, NULL);
+  g_object_set (cell, "sensitive", (*ii < max_lines), NULL);
+  gtk_tree_path_free (path);
+}
 
 /* Initializes IA's first_line substructure. */
 static void
 first_line_page_create (PsppireImportAssistant *ia)
 {
-  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
   GtkWidget *w =  get_widget_assert (ia->builder, "FirstLine");
 
   g_object_set_data (G_OBJECT (w), "on-entering", on_treeview_selection_change);
@@ -850,24 +852,46 @@ first_line_page_create (PsppireImportAssistant *ia)
       GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes (_("Line"), renderer,
                                                                            "text", 0,
                                                                            NULL);
+
+      gtk_tree_view_column_set_cell_data_func (column, renderer, render_text_preview_line, ia, 0);
       gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
 
       renderer = gtk_cell_renderer_text_new ();
       column = gtk_tree_view_column_new_with_attributes (_("Text"), renderer, "text", 1, NULL);
-      gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
+      gtk_tree_view_column_set_cell_data_func (column, renderer, render_text_preview_line, ia, 0);
 
-      gtk_container_add (GTK_CONTAINER (scrolled_window), ia->first_line_tree_view);
+      gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
 
       g_signal_connect_swapped (ia->first_line_tree_view, "cursor-changed",
-                       G_CALLBACK (on_treeview_selection_change), ia);
+                               G_CALLBACK (on_treeview_selection_change), ia);
+      gtk_container_add (GTK_CONTAINER (scrolled_window), ia->first_line_tree_view);
     }
+
   gtk_widget_show_all (scrolled_window);
 
   ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
-
 }
 
-
+static void
+intro_on_leave (PsppireImportAssistant *ia)
+{
+  gint lc = 0;
+  g_object_get (ia->text_file, "line-count", &lc, NULL);
+  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
+    {
+      gint max_lines = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin));
+      g_object_set (ia->text_file, "maximum-lines", max_lines, NULL);
+    }
+  else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
+    {
+      gdouble percent = gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin));
+      g_object_set (ia->text_file, "maximum-lines", (gint) (lc * percent / 100.0), NULL);
+    }
+  else
+    {
+      g_object_set (ia->text_file, "maximum-lines", lc, NULL);
+    }
+}
 
 
 static void
@@ -920,43 +944,26 @@ intro_on_enter (PsppireImportAssistant *ia)
                       ds_cstr (&s));
   ds_destroy (&s);
 
-  GtkWidget *w  =  gtk_grid_get_child_at (GTK_GRID (table), 1, 1);
-  int old_value = w ? gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)) : 1;
-  if (w)
-    gtk_container_remove (GTK_CONTAINER (table), w);
-
-  w  =  gtk_grid_get_child_at (GTK_GRID (table), 1, 2);
-  if (w)
-    gtk_container_remove (GTK_CONTAINER (table), w);
-
-
-  GtkWidget *hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &ia->n_cases_spin);
+  if (gtk_grid_get_child_at (GTK_GRID (table), 1, 1) == NULL)
+    {
+      GtkWidget *hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &ia->n_cases_spin);
+      gtk_grid_attach (GTK_GRID (table), hbox_n_cases,
+                      1, 1,
+                      1, 1);
+    }
 
   GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (ia->n_cases_spin));
   gtk_adjustment_set_lower (adj, 1.0);
 
-  if (ia->text_file)
+  if (gtk_grid_get_child_at (GTK_GRID (table), 1, 2) == NULL)
     {
-      if (psppire_text_file_get_total_exact (ia->text_file))
-       {
-         gulong total_lines = psppire_text_file_get_n_lines (ia->text_file);
-         gtk_adjustment_set_upper (adj, total_lines);
-         gtk_adjustment_set_value (adj, old_value);
-       }
-      else
-       gtk_adjustment_set_upper (adj, DBL_MAX);
-    }
-  gtk_grid_attach (GTK_GRID (table), hbox_n_cases,
-                  1, 1,
-                  1, 1);
-
-
-  GtkWidget *hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"),
-                                              &ia->percent_spin);
+      GtkWidget *hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"),
+                                                  &ia->percent_spin);
 
-  gtk_grid_attach (GTK_GRID (table), hbox_percent,
-                  1, 2,
-                  1, 1);
+      gtk_grid_attach (GTK_GRID (table), hbox_percent,
+                      1, 2,
+                      1, 1);
+    }
 
   gtk_widget_show_all (table);
 
@@ -990,6 +997,7 @@ intro_page_create (PsppireImportAssistant *ia)
                            G_CALLBACK (on_intro_amount_changed), ia);
 
 
+  g_object_set_data (G_OBJECT (w), "on-forward", intro_on_leave);
   g_object_set_data (G_OBJECT (w), "on-entering", intro_on_enter);
   g_object_set_data (G_OBJECT (w), "on-reset", reset_intro_page);
 }
@@ -1043,29 +1051,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);
     }
 }
 
@@ -1210,16 +1214,18 @@ my_read (struct casereader *reader, void *aux, casenumber idx)
          const struct variable *var = dict_get_var (ia->dict, i);
 
          const gchar *ss = g_value_get_string (&value);
-
-         union value *v = case_data_rw (c, var);
-         char *xx = data_in (ss_cstr (ss),
-                             "UTF-8",
-                             var_get_write_format (var)->type,
-                             v, var_get_width (var), "UTF-8");
-
-         /* if (xx) */
-         /*   g_print ("%s:%d Err %s\n", __FILE__, __LINE__, xx); */
-         free (xx);
+         if (ss)
+           {
+             union value *v = case_data_rw (c, var);
+             char *xx = data_in (ss_cstr (ss),
+                                 "UTF-8",
+                                 var_get_write_format (var)->type,
+                                 v, var_get_width (var), "UTF-8");
+
+             /* if (xx) */
+             /*   g_print ("%s:%d Err %s\n", __FILE__, __LINE__, xx); */
+             free (xx);
+           }
          g_value_unset (&value);
        }
     }
@@ -1303,7 +1309,8 @@ prepare_formats_page (PsppireImportAssistant *ia)
        {
          gchar *s = NULL;
          gtk_tree_model_get (ia->delimiters_model, &iter, i+1, &s, -1);
-         fmt_guesser_add (fg[i], ss_cstr (s));
+         if (s)
+           fmt_guesser_add (fg[i], ss_cstr (s));
          free (s);
        }
     }
@@ -1448,9 +1455,12 @@ first_line_append_syntax (const PsppireImportAssistant *ia, struct string *s)
 static void
 intro_append_syntax (const PsppireImportAssistant *ia, struct string *s)
 {
+  gint first_line = 0;
+  g_object_get (ia->delimiters_model, "first-line", &first_line, NULL);
+
   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
-    ds_put_format (s, "N OF CASES %d.\n",
-                  gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)));
+    ds_put_format (s, "SELECT IF ($CASENUM <= %d).\n",
+                  gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)) - first_line);
   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
     ds_put_format (s, "SAMPLE %.4g.\n",
                   gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin)) / 100.0);
@@ -1627,3 +1637,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;
+}
+