-
-
-
-/* Sets IA's separators substructure to match the widgets. */
-static void
-get_separators (PsppireImportAssistant *ia)
-{
- int i;
-
- ds_clear (&ia->separators);
- for (i = 0; i < SEPARATOR_CNT; i++)
- {
- const struct separator *sep = &separators[i];
- GtkWidget *button = get_widget_assert (ia->builder, sep->name);
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
- ds_put_byte (&ia->separators, sep->c);
- }
-
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->custom_cb)))
- ds_put_cstr (&ia->separators,
- gtk_entry_get_text (GTK_ENTRY (ia->custom_entry)));
-
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->quote_cb)))
- {
- const gchar *text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo))));
- ds_assign_cstr (&ia->quotes, text);
- }
- else
- ds_clear (&ia->quotes);
-}
-
-
-
-
-/* Frees and clears the column data in IA's separators
- substructure. */
-static void
-clear_fields (PsppireImportAssistant *ia)
-{
- if (ia->column_cnt > 0)
- {
- struct column *col;
- size_t row;
-
- for (row = 0; row < ia->line_cnt; row++)
- {
- const struct string *line = &ia->lines[row];
- const char *line_start = ds_data (line);
- const char *line_end = ds_end (line);
-
- for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
- {
- char *s = ss_data (col->contents[row]);
- if (!(s >= line_start && s <= line_end))
- ss_dealloc (&col->contents[row]);
- }
- }
-
- for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
- {
- free (col->name);
- free (col->contents);
- }
-
- free (ia->columns);
- ia->columns = NULL;
- ia->column_cnt = 0;
- }
-}
-
-
-/* Breaks the file data in IA into columns based on the
- separators set in IA's separators substructure. */
-static void
-split_fields (PsppireImportAssistant *ia)
-{
- size_t columns_allocated;
- bool space_sep;
- size_t row;
-
- clear_fields (ia);
-
- /* Is space in the set of separators? */
- space_sep = ss_find_byte (ds_ss (&ia->separators), ' ') != SIZE_MAX;
-
- /* Split all the lines, not just those from
- ia->first_line.skip_lines on, so that we split the line that
- contains variables names if ia->first_line.variable_names is
- TRUE. */
- columns_allocated = 0;
- for (row = 0; row < ia->line_cnt; row++)
- {
- struct string *line = &ia->lines[row];
- struct substring text = ds_ss (line);
- size_t column_idx;
-
- for (column_idx = 0; ; column_idx++)
- {
- struct substring field = SS_EMPTY_INITIALIZER;
- struct column *column;
-
- if (space_sep)
- {
- ss_ltrim (&text, ss_cstr (" "));
- }
- if (ss_is_empty (text))
- {
- if (column_idx != 0)
- break;
- field = text;
- }
- else if (!ds_is_empty (&ia->quotes)
- && ds_find_byte (&ia->quotes, text.string[0]) != SIZE_MAX)
- {
- int quote = ss_get_byte (&text);
- struct string s;
- int c;
-
- ds_init_empty (&s);
- while ((c = ss_get_byte (&text)) != EOF)
- if (c != quote)
- ds_put_byte (&s, c);
- else if (ss_match_byte (&text, quote))
- ds_put_byte (&s, quote);
- else
- break;
- field = ds_ss (&s);
- }
- else
- {
- ss_get_bytes (&text, ss_cspan (text, ds_ss (&ia->separators)),
- &field);
- }
-
- if (column_idx >= ia->column_cnt)
- {
- struct column *column;
-
- if (ia->column_cnt >= columns_allocated)
- {
- ia->columns = x2nrealloc (ia->columns, &columns_allocated,
- sizeof *ia->columns);
- }
- column = &ia->columns[ia->column_cnt++];
- column->name = NULL;
- column->width = 0;
- column->contents = xcalloc (ia->line_cnt,
- sizeof *column->contents);
- }
- column = &ia->columns[column_idx];
- column->contents[row] = field;
- if (ss_length (field) > column->width)
- column->width = ss_length (field);
-
- if (space_sep)
- ss_ltrim (&text, ss_cstr (" "));
- if (ss_is_empty (text))
- break;
- if (ss_find_byte (ds_ss (&ia->separators), ss_first (text))
- != SIZE_MAX)
- ss_advance (&text, 1);
- }
- }
-}
-
-static PsppSheetViewColumn *
-make_data_column (PsppireImportAssistant *ia, GtkWidget *tree_view,
- bool input, gint dict_idx)
-{
- struct variable *var = NULL;
- struct column *column = NULL;
- size_t char_cnt;
- gint content_width, header_width;
- PsppSheetViewColumn *tree_column;
- char *name;
-
- if (input)
- column = &ia->columns[dict_idx];
- else
- var = dict_get_var (ia->dict, dict_idx);
-
- name = escape_underscores (input ? column->name : var_get_name (var));
- char_cnt = input ? column->width : var_get_print_format (var)->w;
- content_width = get_monospace_width (tree_view, ia->fixed_renderer,
- char_cnt);
- header_width = get_string_width (tree_view, ia->prop_renderer,
- name);
-
- tree_column = pspp_sheet_view_column_new ();
- g_object_set_data (G_OBJECT (tree_column), "column-number",
- GINT_TO_POINTER (dict_idx));
- pspp_sheet_view_column_set_title (tree_column, name);
- pspp_sheet_view_column_pack_start (tree_column, ia->fixed_renderer,
- FALSE);
- pspp_sheet_view_column_set_cell_data_func (
- tree_column, ia->fixed_renderer,
- input ? render_input_cell : render_output_cell, ia, NULL);
- pspp_sheet_view_column_set_fixed_width (tree_column, MAX (content_width,
- header_width));
-
- free (name);
-
- return tree_column;
-}
-
-
-static GtkWidget *
-create_data_tree_view (gboolean input, GtkContainer *parent,
- PsppireImportAssistant *ia)
-{
- gint i;
- GtkWidget *tree_view = make_tree_view (ia);
-
- set_model_on_treeview (ia, tree_view, ia->skip_lines);
-
- pspp_sheet_selection_set_mode (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (tree_view)),
- PSPP_SHEET_SELECTION_NONE);
-
- for (i = 0; i < ia->column_cnt; i++)
- {
- PsppSheetViewColumn *w = make_data_column (ia, tree_view, input, i);
-
- pspp_sheet_view_append_column (PSPP_SHEET_VIEW (tree_view), w);
- }
-
- g_object_set (G_OBJECT (tree_view), "has-tooltip", TRUE, (void *) NULL);
- g_signal_connect (tree_view, "query-tooltip",
- G_CALLBACK (input ? on_query_input_tooltip
- : on_query_output_tooltip), ia);
-
- GtkWidget *child = gtk_bin_get_child (GTK_BIN (parent));
- if (child)
- {
- g_object_ref (child);
- gtk_container_remove (parent, child);
- }
- gtk_container_add (parent, tree_view);
- if (child)
- g_object_unref (child);
-
- gtk_widget_show (tree_view);
-
- return tree_view;
-}
-
-