+static struct casereader *
+textfile_create_reader (PsppireImportAssistant *ia)
+{
+ int n_vars = dict_get_var_cnt (ia->dict);
+
+ int i;
+
+ struct fmt_guesser **fg = xcalloc (sizeof *fg, n_vars);
+ for (i = 0 ; i < n_vars; ++i)
+ {
+ fg[i] = fmt_guesser_create ();
+ }
+
+ gint n_rows = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (ia->delimiters_model), NULL);
+
+ GtkTreeIter iter;
+ gboolean ok;
+ for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ia->delimiters_model), &iter);
+ ok;
+ ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ia->delimiters_model), &iter))
+ {
+ for (i = 0 ; i < n_vars; ++i)
+ {
+ gchar *s = NULL;
+ gtk_tree_model_get (GTK_TREE_MODEL (ia->delimiters_model), &iter, i+1, &s, -1);
+ if (s)
+ fmt_guesser_add (fg[i], ss_cstr (s));
+ free (s);
+ }
+ }
+
+ struct caseproto *proto = caseproto_create ();
+ for (i = 0 ; i < n_vars; ++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);
+
+ struct casereader *cr = casereader_create_random (proto, n_rows, &my_casereader_class, ia);
+ /* Store the dictionary at this point when the casereader is created.
+ my_read depends on the dictionary to interpret the strings in the treeview.
+ This guarantees that the union value is produced according to the
+ caseproto in the reader. */
+ ia->casereader_dict = dict_clone (ia->dict);
+ caseproto_unref (proto);
+ return cr;
+}
+
+/* When during import the variable type is changed, the reader is reinitialized
+ based on the new dictionary with a fresh caseprototype. The default behaviour
+ when a variable type is changed and the column is resized is that the union
+ value is interpreted with new variable type and an overlay for that column
+ is generated. Here we reinit to the original reader based on strings.
+ As a result you can switch from string to numeric to string without loosing
+ the string information. */
+static void
+ia_variable_changed_cb (GObject *obj, gint var_num, guint what,
+ const struct variable *oldvar, gpointer data)
+{
+ PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (data);
+
+ struct caseproto *proto = caseproto_create();
+ for (int i = 0; i < dict_get_var_cnt (ia->dict); i++)
+ {
+ const struct variable *var = dict_get_var (ia->dict, i);
+ int width = var_get_width (var);
+ proto = caseproto_add_width (proto, width);
+ }
+
+ gint n_rows = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (ia->delimiters_model), NULL);
+
+ PsppireDataStore *store = NULL;
+ g_object_get (ia->data_sheet, "data-model", &store, NULL);
+
+ struct casereader *cr = casereader_create_random (proto, n_rows,
+ &my_casereader_class, ia);
+ psppire_data_store_set_reader (store, cr);
+ dict_unref (ia->casereader_dict);
+ ia->casereader_dict = dict_clone (ia->dict);
+}
+