static void choose_column_names (PsppireImportAssistant *ia);
/* Revises the contents of the fields tree view based on the
- currently chosen set of separators. */
+ currently chosen set of separators and quotes. */
static void
revise_fields_preview (PsppireImportAssistant *ia)
{
- choose_column_names (ia);
-}
-
-
-struct separator_count_node
-{
- struct hmap_node node;
- int occurance; /* The number of times the separator occurs in a line */
- int quantity; /* The number of lines with this occurance */
-};
-
-
-/* Picks the most likely separator and quote characters based on
- IA's file data. */
-static void
-choose_likely_separators (PsppireImportAssistant *ia)
-{
- gint first_line = 0;
- g_object_get (ia->delimiters_model, "first-line", &first_line, NULL);
-
- gboolean valid;
- GtkTreeIter iter;
-
- struct hmap count_map[SEPARATOR_CNT];
- for (int j = 0; j < SEPARATOR_CNT; ++j)
- hmap_init (count_map + j);
-
- GtkTreePath *p = gtk_tree_path_new_from_indices (first_line, -1);
-
- for (valid = gtk_tree_model_get_iter (GTK_TREE_MODEL (ia->text_file), &iter, p);
- valid;
- valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (ia->text_file), &iter))
+ GSList *delimiters = NULL;
+ for (int i = 0; i < N_SEPARATORS; i++)
{
- gchar *line_text = NULL;
- gtk_tree_model_get (GTK_TREE_MODEL (ia->text_file), &iter, 1, &line_text, -1);
-
- gint *counts = XCALLOC (SEPARATOR_CNT, gint);
-
- 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)
- {
- if (character == separators[s].c)
- counts[s]++;
- }
- }
-
- int j;
- for (j = 0; j < SEPARATOR_CNT; ++j)
+ const struct separator *s = &separators[i];
+ GtkWidget *button = get_widget_assert (ia->text_builder, s->name);
+ if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
{
- if (counts[j] > 0)
- {
- struct separator_count_node *cn = NULL;
- unsigned int hash = hash_int (counts[j], 0);
- HMAP_FOR_EACH_WITH_HASH (cn, struct separator_count_node, node, hash, &count_map[j])
- {
- if (cn->occurance == counts[j])
- break;
- }
-
- if (cn == NULL)
- {
- struct separator_count_node *new_cn = XZALLOC (struct separator_count_node);
- new_cn->occurance = counts[j];
- new_cn->quantity = 1;
- hmap_insert (&count_map[j], &new_cn->node, hash);
- }
- else
- cn->quantity++;
- }
+ delimiters = g_slist_prepend (delimiters, GINT_TO_POINTER (s->c));
}
-
- free (line_text);
- free (counts);
}
- gtk_tree_path_free (p);
- if (hmap_count (count_map) > 0)
- {
- int most_frequent = -1;
- int largest = 0;
- for (int j = 0; j < SEPARATOR_CNT; ++j)
- {
- struct separator_count_node *cn;
- struct separator_count_node *next;
- HMAP_FOR_EACH_SAFE (cn, next, struct separator_count_node, node, &count_map[j])
- {
- if (largest < cn->quantity)
- {
- largest = cn->quantity;
- most_frequent = j;
- }
- free (cn);
- }
- hmap_destroy (&count_map[j]);
- }
+ GtkComboBoxText *cbt = GTK_COMBO_BOX_TEXT (ia->quote_combo);
+ GtkToggleButton *quote_cb = GTK_TOGGLE_BUTTON (ia->quote_cb);
+ const gchar *quotes = (gtk_toggle_button_get_active (quote_cb)
+ ? gtk_combo_box_text_get_active_text (cbt)
+ : "");
- g_return_if_fail (most_frequent >= 0);
+ g_object_set (ia->delimiters_model,
+ "delimiters", delimiters,
+ "quotes", quotes,
+ NULL);
- GtkWidget *toggle =
- get_widget_assert (ia->text_builder, separators[most_frequent].name);
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), TRUE);
- }
+ choose_column_names (ia);
}
+
static void
repopulate_delimiter_columns (PsppireImportAssistant *ia)
{
on_separator_toggle (GtkToggleButton *toggle UNUSED,
PsppireImportAssistant *ia)
{
- int i;
- GSList *delimiters = NULL;
- for (i = 0; i < SEPARATOR_CNT; i++)
- {
- const struct separator *s = &separators[i];
- GtkWidget *button = get_widget_assert (ia->text_builder, s->name);
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
- {
- delimiters = g_slist_prepend (delimiters, GINT_TO_POINTER (s->c));
- }
- }
-
- g_object_set (ia->delimiters_model, "delimiters", delimiters, NULL);
-
revise_fields_preview (ia);
}
reset_separators_page (PsppireImportAssistant *ia)
{
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->custom_cb), FALSE);
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->quote_cb), FALSE);
gtk_entry_set_text (GTK_ENTRY (ia->custom_entry), "");
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->quote_cb), TRUE);
+ gtk_combo_box_set_active (GTK_COMBO_BOX (ia->quote_combo), 0);
- for (gint i = 0; i < SEPARATOR_CNT; i++)
+ for (gint i = 0; i < N_SEPARATORS; i++)
{
const struct separator *s = &separators[i];
GtkWidget *button = get_widget_assert (ia->text_builder, s->name);
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE);
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), s->c == ',');
}
- repopulate_delimiter_columns (ia);
+ if (ia->delimiters_model)
+ {
+ repopulate_delimiter_columns (ia);
- revise_fields_preview (ia);
- choose_likely_separators (ia);
+ revise_fields_preview (ia);
+ }
}
/* Called just before the separators page becomes visible in the
gtk_widget_set_sensitive (ia->custom_entry,
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->custom_cb)));
- gtk_combo_box_set_active (GTK_COMBO_BOX (ia->quote_combo), 0);
+ gtk_entry_set_max_length (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo))), 1);
if (ia->fields_tree_view == NULL)
{
G_CALLBACK (on_separators_custom_entry_notify), ia);
g_signal_connect (ia->custom_cb, "toggled",
G_CALLBACK (on_separators_custom_cb_toggle), ia);
- for (i = 0; i < SEPARATOR_CNT; i++)
+ for (i = 0; i < N_SEPARATORS; i++)
g_signal_connect (get_widget_assert (builder, separators[i].name),
"toggled", G_CALLBACK (on_separator_toggle), ia);
}
static void
-my_advance (struct casereader *reader, void *aux, casenumber cnt)
+my_advance (struct casereader *reader, void *aux, casenumber n)
{
g_print ("%s:%d\n", __FILE__, __LINE__);
}
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (get_widget_assert (ia->text_builder, "tab"))))
ds_put_cstr (s, "\\t");
- for (i = 0; i < SEPARATOR_CNT; i++)
+ for (i = 0; i < N_SEPARATORS; i++)
{
const struct separator *seps = &separators[i];
GtkWidget *button = get_widget_assert (ia->text_builder, seps->name);