- syntax_gen_num_range (s, low, high, format);
- }
- ds_put_cstr (s, ").\n");
- }
- if (var_has_value_labels (var))
- {
- const struct val_labs *vls = var_get_value_labels (var);
- const struct val_lab **labels = val_labs_sorted (vls);
- size_t n_labels = val_labs_count (vls);
- size_t i;
-
- syntax_gen_pspp (s, "VALUE LABELS %ss", name);
- for (i = 0; i < n_labels; i++)
- {
- const struct val_lab *vl = labels[i];
- ds_put_cstr (s, "\n ");
- syntax_gen_value (s, &vl->value, width, format);
- ds_put_byte (s, ' ');
- syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
- }
- free (labels);
- ds_put_cstr (s, ".\n");
- }
- if (var_has_label (var))
- syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
- name, var_get_label (var));
- if (measure != var_default_measure (type))
- syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
- name,
- (measure == MEASURE_NOMINAL ? "NOMINAL"
- : measure == MEASURE_ORDINAL ? "ORDINAL"
- : "SCALE"));
- if (alignment != var_default_alignment (type))
- syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
- name,
- (alignment == ALIGN_LEFT ? "LEFT"
- : alignment == ALIGN_CENTRE ? "CENTER"
- : "RIGHT"));
- if (var_get_display_width (var) != var_default_display_width (width))
- syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
- name, var_get_display_width (var));
- }
-}
-
-/* Generates and returns PSPP syntax to execute the import
- operation described by IA. The caller must free the syntax
- with free(). */
-static char *
-generate_syntax (const struct import_assistant *ia)
-{
- struct string s = DS_EMPTY_INITIALIZER;
- size_t var_cnt;
- size_t i;
-
- syntax_gen_pspp (&s,
- "GET DATA\n"
- " /TYPE=TXT\n"
- " /FILE=%sq\n",
- ia->file.file_name);
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (
- ia->intro.n_cases_button)))
- ds_put_format (&s, " /IMPORTCASES=FIRST %d\n",
- gtk_spin_button_get_value_as_int (
- GTK_SPIN_BUTTON (ia->intro.n_cases_spin)));
- else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (
- ia->intro.percent_button)))
- ds_put_format (&s, " /IMPORTCASES=PERCENT %d\n",
- gtk_spin_button_get_value_as_int (
- GTK_SPIN_BUTTON (ia->intro.percent_spin)));
- else
- ds_put_cstr (&s, " /IMPORTCASES=ALL\n");
- ds_put_cstr (&s,
- " /ARRANGEMENT=DELIMITED\n"
- " /DELCASE=LINE\n");
- if (ia->first_line.skip_lines > 0)
- ds_put_format (&s, " /FIRSTCASE=%d\n", ia->first_line.skip_lines + 1);
- ds_put_cstr (&s, " /DELIMITERS=\"");
- if (ds_find_byte (&ia->separators.separators, '\t') != SIZE_MAX)
- ds_put_cstr (&s, "\\t");
- if (ds_find_byte (&ia->separators.separators, '\\') != SIZE_MAX)
- ds_put_cstr (&s, "\\\\");
- for (i = 0; i < ds_length (&ia->separators.separators); i++)
- {
- char c = ds_at (&ia->separators.separators, i);
- if (c == '"')
- ds_put_cstr (&s, "\"\"");
- else if (c != '\t' && c != '\\')
- ds_put_byte (&s, c);
- }
- ds_put_cstr (&s, "\"\n");
- if (!ds_is_empty (&ia->separators.quotes))
- syntax_gen_pspp (&s, " /QUALIFIER=%sq\n", ds_cstr (&ia->separators.quotes));
- if (!ds_is_empty (&ia->separators.quotes) && ia->separators.escape)
- ds_put_cstr (&s, " /ESCAPE\n");
- ds_put_cstr (&s, " /VARIABLES=\n");
-
- var_cnt = dict_get_var_cnt (ia->formats.dict);
- for (i = 0; i < var_cnt; i++)
- {
- struct variable *var = dict_get_var (ia->formats.dict, i);
- char format_string[FMT_STRING_LEN_MAX + 1];
- fmt_to_string (var_get_print_format (var), format_string);
- ds_put_format (&s, " %s %s%s\n",
- var_get_name (var), format_string,
- i == var_cnt - 1 ? "." : "");
- }
-
- apply_dict (ia->formats.dict, &s);
-
- return ds_cstr (&s);
-}
-\f
-/* Choosing a file and reading it. */
-
-static char *choose_file (GtkWindow *parent_window);
-
-/* Obtains the file to import from the user and initializes IA's
- file substructure. PARENT_WINDOW must be the window to use
- as the file chooser window's parent.
-
- Returns true if successful, false if the file name could not
- be obtained or the file could not be read. */
-static bool
-init_file (struct import_assistant *ia, GtkWindow *parent_window)
-{
- struct file *file = &ia->file;
- enum { MAX_PREVIEW_LINES = 1000 }; /* Max number of lines to read. */
- enum { MAX_LINE_LEN = 16384 }; /* Max length of an acceptable line. */
- FILE *stream;
-
- file->file_name = choose_file (parent_window);
- if (file->file_name == NULL)
- return false;
-
- stream = fopen (file->file_name, "r");
- if (stream == NULL)
- {
- msg (ME, _("Could not open `%s': %s"),
- file->file_name, strerror (errno));
- return false;
- }
-
- file->lines = xnmalloc (MAX_PREVIEW_LINES, sizeof *file->lines);
- for (; file->line_cnt < MAX_PREVIEW_LINES; file->line_cnt++)
- {
- struct string *line = &file->lines[file->line_cnt];
-
- ds_init_empty (line);
- if (!ds_read_line (line, stream, MAX_LINE_LEN))
- {
- if (feof (stream))
- break;
- else if (ferror (stream))
- msg (ME, _("Error reading `%s': %s"),
- file->file_name, strerror (errno));
- else
- msg (ME, _("Failed to read `%s', because it contains a line "
- "over %d bytes long and therefore appears not to be "
- "a text file."),
- file->file_name, MAX_LINE_LEN);
- fclose (stream);
- destroy_file (ia);
- return false;
- }
- ds_chomp_byte (line, '\n');
- ds_chomp_byte (line, '\r');
- }
-
- if (file->line_cnt == 0)
- {
- msg (ME, _("`%s' is empty."), file->file_name);
- fclose (stream);
- destroy_file (ia);
- return false;
- }
-
- /* Estimate the number of lines in the file. */
- if (file->line_cnt < MAX_PREVIEW_LINES)
- file->total_lines = file->line_cnt;
- else
- {
- struct stat s;
- off_t position = ftello (stream);
- if (fstat (fileno (stream), &s) == 0 && position > 0)
- file->total_lines = (double) file->line_cnt / position * s.st_size;
- else
- file->total_lines = 0;
- }
-
- return true;
-}
-
-/* Frees IA's file substructure. */
-static void
-destroy_file (struct import_assistant *ia)
-{
- struct file *f = &ia->file;
- size_t i;
-
- for (i = 0; i < f->line_cnt; i++)
- ds_destroy (&f->lines[i]);
- free (f->lines);
- g_free (f->file_name);
-}
-
-/* Obtains the file to read from the user and returns the name of
- the file as a string that must be freed with g_free if
- successful, otherwise a null pointer. PARENT_WINDOW must be
- the window to use as the file chooser window's parent. */
-static char *
-choose_file (GtkWindow *parent_window)
-{
- GtkWidget *dialog;
- char *file_name;
-
- dialog = gtk_file_chooser_dialog_new (_("Import Delimited Text Data"),
- parent_window,
- GTK_FILE_CHOOSER_ACTION_OPEN,
- GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
- GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
- NULL);
-
- switch (gtk_dialog_run (GTK_DIALOG (dialog)))
- {
- case GTK_RESPONSE_ACCEPT:
- file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
- break;
- default:
- file_name = NULL;
- break;
- }
- gtk_widget_destroy (dialog);
-
- return file_name;
-}
-\f
-/* Assistant. */
-
-static void close_assistant (struct import_assistant *, int response);
-static void on_prepare (GtkAssistant *assistant, GtkWidget *page,
- struct import_assistant *);
-static void on_cancel (GtkAssistant *assistant, struct import_assistant *);
-static void on_close (GtkAssistant *assistant, struct import_assistant *);
-static void on_paste (GtkButton *button, struct import_assistant *);
-static void on_reset (GtkButton *button, struct import_assistant *);
-static void close_assistant (struct import_assistant *, int response);
-
-/* Initializes IA's asst substructure. PARENT_WINDOW must be the
- window to use as the assistant window's parent. */
-static void
-init_assistant (struct import_assistant *ia, GtkWindow *parent_window)
-{
- struct assistant *a = &ia->asst;
-
- a->builder = builder_new ("text-data-import.ui");
- a->assistant = GTK_ASSISTANT (gtk_assistant_new ());
- g_signal_connect (a->assistant, "prepare", G_CALLBACK (on_prepare), ia);
- g_signal_connect (a->assistant, "cancel", G_CALLBACK (on_cancel), ia);
- g_signal_connect (a->assistant, "close", G_CALLBACK (on_close), ia);
- a->paste_button = gtk_button_new_from_stock (GTK_STOCK_PASTE);
- gtk_assistant_add_action_widget (a->assistant, a->paste_button);
- g_signal_connect (a->paste_button, "clicked", G_CALLBACK (on_paste), ia);
- a->reset_button = gtk_button_new_from_stock ("pspp-stock-reset");
- gtk_assistant_add_action_widget (a->assistant, a->reset_button);
- g_signal_connect (a->reset_button, "clicked", G_CALLBACK (on_reset), ia);
- gtk_window_set_title (GTK_WINDOW (a->assistant),
- _("Importing Delimited Text Data"));
- gtk_window_set_transient_for (GTK_WINDOW (a->assistant), parent_window);
- gtk_window_set_icon_name (GTK_WINDOW (a->assistant), "psppicon");
-
- a->prop_renderer = gtk_cell_renderer_text_new ();
- g_object_ref_sink (a->prop_renderer);
- a->fixed_renderer = gtk_cell_renderer_text_new ();
- g_object_ref_sink (a->fixed_renderer);
- g_object_set (G_OBJECT (a->fixed_renderer),
- "family", "Monospace",
- (void *) NULL);
-}
-
-/* Frees IA's asst substructure. */
-static void
-destroy_assistant (struct import_assistant *ia)
-{
- struct assistant *a = &ia->asst;
-
- g_object_unref (a->prop_renderer);
- g_object_unref (a->fixed_renderer);
- g_object_unref (a->builder);
-}
-
-/* Appends a page of the given TYPE, with PAGE as its content, to
- the GtkAssistant encapsulated by IA. Returns the GtkWidget
- that represents the page. */
-static GtkWidget *
-add_page_to_assistant (struct import_assistant *ia,
- GtkWidget *page, GtkAssistantPageType type)
-{
- const char *title;
- char *title_copy;
- GtkWidget *content;
-
- title = gtk_window_get_title (GTK_WINDOW (page));
- title_copy = xstrdup (title ? title : "");
-
- content = gtk_bin_get_child (GTK_BIN (page));
- assert (content);
- g_object_ref (content);
- gtk_container_remove (GTK_CONTAINER (page), content);
-
- gtk_widget_destroy (page);
-
- gtk_assistant_append_page (ia->asst.assistant, content);
- gtk_assistant_set_page_type (ia->asst.assistant, content, type);
- gtk_assistant_set_page_title (ia->asst.assistant, content, title_copy);
- gtk_assistant_set_page_complete (ia->asst.assistant, content, true);
-
- free (title_copy);
-
- return content;
-}
-
-/* Called just before PAGE is displayed as the current page of
- ASSISTANT, this updates IA content according to the new
- page. */
-static void
-on_prepare (GtkAssistant *assistant, GtkWidget *page,
- struct import_assistant *ia)
-{
-
- if (gtk_assistant_get_page_type (assistant, page)
- == GTK_ASSISTANT_PAGE_CONFIRM)
- gtk_widget_grab_focus (assistant->apply);
- else
- gtk_widget_grab_focus (assistant->forward);
-
- if (page == ia->separators.page)
- prepare_separators_page (ia);
- else if (page == ia->formats.page)
- prepare_formats_page (ia);
-
- gtk_widget_show (ia->asst.reset_button);
- if (page == ia->formats.page)
- gtk_widget_show (ia->asst.paste_button);
- else
- gtk_widget_hide (ia->asst.paste_button);
-}
-
-/* Called when the Cancel button in the assistant is clicked. */
-static void
-on_cancel (GtkAssistant *assistant, struct import_assistant *ia)
-{
- close_assistant (ia, GTK_RESPONSE_CANCEL);
-}
-
-/* Called when the Apply button on the last page of the assistant
- is clicked. */
-static void
-on_close (GtkAssistant *assistant, struct import_assistant *ia)
-{
- close_assistant (ia, GTK_RESPONSE_APPLY);
-}
-
-/* Called when the Paste button on the last page of the assistant
- is clicked. */
-static void
-on_paste (GtkButton *button, struct import_assistant *ia)
-{
- close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
-}
-
-/* Called when the Reset button is clicked. */
-static void
-on_reset (GtkButton *button, struct import_assistant *ia)
-{
- gint page_num = gtk_assistant_get_current_page (ia->asst.assistant);
- GtkWidget *page = gtk_assistant_get_nth_page (ia->asst.assistant, page_num);
-
- if (page == ia->intro.page)
- reset_intro_page (ia);
- else if (page == ia->first_line.page)
- reset_first_line_page (ia);
- else if (page == ia->separators.page)
- reset_separators_page (ia);
- else if (page == ia->formats.page)
- reset_formats_page (ia);
-}
-
-/* Causes the assistant to close, returning RESPONSE for
- interpretation by text_data_import_assistant. */
-static void
-close_assistant (struct import_assistant *ia, int response)
-{
- ia->asst.response = response;
- g_main_loop_quit (ia->asst.main_loop);
- gtk_widget_hide (GTK_WIDGET (ia->asst.assistant));
-}
-\f
-/* The "intro" page of the assistant. */
-
-static void on_intro_amount_changed (struct import_assistant *);
-
-/* Initializes IA's intro substructure. */
-static void
-init_intro_page (struct import_assistant *ia)
-{
- GtkBuilder *builder = ia->asst.builder;
- struct intro_page *p = &ia->intro;
- struct string s;
- GtkWidget *hbox_n_cases ;
- GtkWidget *hbox_percent ;
- GtkWidget *table ;
-
-
- p->n_cases_spin = gtk_spin_button_new_with_range (0, INT_MAX, 100);
-
- hbox_n_cases = widget_scanf (_("Only the first %4d cases"), &p->n_cases_spin);
-
- table = get_widget_assert (builder, "button-table");
-
- gtk_table_attach_defaults (GTK_TABLE (table), hbox_n_cases,
- 1, 2,
- 1, 2);
-
- p->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
-
- hbox_percent = widget_scanf (_("Only the first %3d %% of file (approximately)"), &p->percent_spin);
-
- gtk_table_attach_defaults (GTK_TABLE (table), hbox_percent,
- 1, 2,
- 2, 3);
-
- p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Intro"),
- GTK_ASSISTANT_PAGE_INTRO);
-
- p->all_cases_button = get_widget_assert (builder, "import-all-cases");
-
- p->n_cases_button = get_widget_assert (builder, "import-n-cases");
-
- p->percent_button = get_widget_assert (builder, "import-percent");
-
- g_signal_connect_swapped (p->all_cases_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
- g_signal_connect_swapped (p->n_cases_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
- g_signal_connect_swapped (p->percent_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
-
- on_intro_amount_changed (ia);
-
- ds_init_empty (&s);
- ds_put_cstr (&s, _("This assistant will guide you through the process of "
- "importing data into PSPP from a text file with one line "
- "per case, in which fields are separated by tabs, "
- "commas, or other delimiters.\n\n"));
- if (ia->file.total_is_exact)
- ds_put_format (
- &s, ngettext ("The selected file contains %zu line of text. ",
- "The selected file contains %zu lines of text. ",
- ia->file.line_cnt),
- ia->file.line_cnt);
- else if (ia->file.total_lines > 0)
- {
- ds_put_format (
- &s, ngettext (
- "The selected file contains approximately %lu line of text. ",
- "The selected file contains approximately %lu lines of text. ",
- ia->file.total_lines),
- ia->file.total_lines);
- ds_put_format (
- &s, ngettext (
- "Only the first %zu line of the file will be shown for "
- "preview purposes in the following screens. ",
- "Only the first %zu lines of the file will be shown for "
- "preview purposes in the following screens. ",
- ia->file.line_cnt),
- ia->file.line_cnt);
- }
- ds_put_cstr (&s, _("You may choose below how much of the file should "
- "actually be imported."));
- gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
- ds_cstr (&s));
- ds_destroy (&s);
-}
-
-/* Resets IA's intro page to its initial state. */
-static void
-reset_intro_page (struct import_assistant *ia)
-{
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->intro.all_cases_button),
- true);
-}
-
-/* Called when one of the radio buttons is clicked. */
-static void
-on_intro_amount_changed (struct import_assistant *ia)
-{
- struct intro_page *p = &ia->intro;
-
- gtk_widget_set_sensitive (p->n_cases_spin,
- 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)));
-}
-\f
-/* The "first line" page of the assistant. */
-
-static GtkTreeView *create_lines_tree_view (GtkContainer *parent_window,
- struct import_assistant *);
-static void on_first_line_change (GtkTreeSelection *,
- struct import_assistant *);
-static void on_variable_names_cb_toggle (GtkToggleButton *,
- struct import_assistant *);
-static void set_first_line (struct import_assistant *);
-static void get_first_line (struct import_assistant *);
-
-/* Initializes IA's first_line substructure. */
-static void
-init_first_line_page (struct import_assistant *ia)
-{
- struct first_line_page *p = &ia->first_line;
- GtkBuilder *builder = ia->asst.builder;
-
- p->page = add_page_to_assistant (ia, get_widget_assert (builder, "FirstLine"),
- GTK_ASSISTANT_PAGE_CONTENT);
- gtk_widget_destroy (get_widget_assert (builder, "first-line"));
- p->tree_view = create_lines_tree_view (
- GTK_CONTAINER (get_widget_assert (builder, "first-line-scroller")), ia);
- p->variable_names_cb = get_widget_assert (builder, "variable-names");
- gtk_tree_selection_set_mode (
- gtk_tree_view_get_selection (GTK_TREE_VIEW (p->tree_view)),
- GTK_SELECTION_BROWSE);
- set_first_line (ia);
- g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (p->tree_view)),
- "changed", G_CALLBACK (on_first_line_change), ia);
- g_signal_connect (p->variable_names_cb, "toggled",
- G_CALLBACK (on_variable_names_cb_toggle), ia);
-}
-
-/* Resets the first_line page to its initial content. */
-static void
-reset_first_line_page (struct import_assistant *ia)
-{
- ia->first_line.skip_lines = 0;
- ia->first_line.variable_names = false;
- set_first_line (ia);
-}
-
-/* Creates and returns a tree view that contains each of the
- lines in IA's file as a row. */
-static GtkTreeView *
-create_lines_tree_view (GtkContainer *parent, struct import_assistant *ia)
-{
- GtkTreeView *tree_view;
- GtkTreeViewColumn *column;
- size_t max_line_length;
- gint content_width, header_width;
- size_t i;
- const gchar *title = _("Text");
-
- make_tree_view (ia, 0, &tree_view);
-
- column = gtk_tree_view_column_new_with_attributes
- (
- title, ia->asst.fixed_renderer,
- "text", TEXT_IMPORT_MODEL_COLUMN_LINE,
- (void *) NULL
- );
- gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
-
- max_line_length = 0;
- for (i = 0; i < ia->file.line_cnt; i++)
- {
- size_t w = ds_length (&ia->file.lines[i]);
- max_line_length = MAX (max_line_length, w);
- }
-
- content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
- max_line_length);
- header_width = get_string_width (tree_view, ia->asst.prop_renderer, title);
- gtk_tree_view_column_set_fixed_width (column, MAX (content_width,
- header_width));
- gtk_tree_view_append_column (tree_view, column);
-
- gtk_tree_view_set_fixed_height_mode (tree_view, true);
-
- gtk_container_add (parent, GTK_WIDGET (tree_view));
- gtk_widget_show (GTK_WIDGET (tree_view));
-
- return tree_view;
-}
-
-/* Called when the line selected in the first_line tree view
- changes. */
-static void
-on_first_line_change (GtkTreeSelection *selection UNUSED,
- struct import_assistant *ia)
-{
- get_first_line (ia);
-}
-
-/* Called when the checkbox that indicates whether variable
- names are in the row above the first line is toggled. */
-static void
-on_variable_names_cb_toggle (GtkToggleButton *variable_names_cb UNUSED,
- struct import_assistant *ia)
-{
- get_first_line (ia);
-}
-
-/* Sets the widgets to match IA's first_line substructure. */
-static void
-set_first_line (struct import_assistant *ia)
-{
- GtkTreePath *path;
-
- path = gtk_tree_path_new_from_indices (ia->first_line.skip_lines, -1);
- gtk_tree_view_set_cursor (GTK_TREE_VIEW (ia->first_line.tree_view),
- path, NULL, false);
- gtk_tree_path_free (path);
-
- gtk_toggle_button_set_active (
- GTK_TOGGLE_BUTTON (ia->first_line.variable_names_cb),
- ia->first_line.variable_names);
- gtk_widget_set_sensitive (ia->first_line.variable_names_cb,
- ia->first_line.skip_lines > 0);
-}
-
-/* Sets IA's first_line substructure to match the widgets. */
-static void
-get_first_line (struct import_assistant *ia)
-{
- GtkTreeSelection *selection;
- GtkTreeIter iter;
- GtkTreeModel *model;
-
- selection = gtk_tree_view_get_selection (ia->first_line.tree_view);
- if (gtk_tree_selection_get_selected (selection, &model, &iter))
- {
- GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
- int row = gtk_tree_path_get_indices (path)[0];
- gtk_tree_path_free (path);
-
- ia->first_line.skip_lines = row;
- ia->first_line.variable_names =
- (ia->first_line.skip_lines > 0
- && gtk_toggle_button_get_active (
- GTK_TOGGLE_BUTTON (ia->first_line.variable_names_cb)));
- }
- gtk_widget_set_sensitive (ia->first_line.variable_names_cb,
- ia->first_line.skip_lines > 0);
-}
-\f
-/* The "separators" page of the assistant. */
-
-static void revise_fields_preview (struct import_assistant *ia);
-static void choose_likely_separators (struct import_assistant *ia);
-static void find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
- const char *targets, const char *def,
- struct string *result);
-static void clear_fields (struct import_assistant *ia);
-static void revise_fields_preview (struct import_assistant *);
-static void set_separators (struct import_assistant *);
-static void get_separators (struct import_assistant *);
-static void on_separators_custom_entry_notify (GObject *UNUSED,
- GParamSpec *UNUSED,
- struct import_assistant *);
-static void on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
- struct import_assistant *);
-static void on_quote_combo_change (GtkComboBox *combo,
- struct import_assistant *);
-static void on_quote_cb_toggle (GtkToggleButton *quote_cb,
- struct import_assistant *);
-static void on_separator_toggle (GtkToggleButton *, struct import_assistant *);
-static void render_input_cell (GtkTreeViewColumn *tree_column,
- GtkCellRenderer *cell,
- GtkTreeModel *model, GtkTreeIter *iter,
- gpointer ia);
-static gboolean on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
- gboolean keyboard_mode UNUSED,
- GtkTooltip *tooltip,
- struct import_assistant *);
-
-/* A common field separator and its identifying name. */
-struct separator
- {
- const char *name; /* Name (for use with get_widget_assert). */
- int c; /* Separator character. */
- };
-
-/* All the separators in the dialog box. */
-static const struct separator separators[] =
- {
- {"space", ' '},
- {"tab", '\t'},
- {"bang", '!'},
- {"colon", ':'},
- {"comma", ','},
- {"hyphen", '-'},
- {"pipe", '|'},
- {"semicolon", ';'},
- {"slash", '/'},
- };
-#define SEPARATOR_CNT (sizeof separators / sizeof *separators)
-
-static void
-set_quote_list (GtkComboBoxEntry *cb)
-{
- GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
- GtkTreeIter iter;
- gint i;
- const gchar *seperator[3] = {"'\"", "\'", "\""};
-
- for (i = 0; i < 3; i++)
- {
- const gchar *s = seperator[i];
-
- /* Add a new row to the model */
- gtk_list_store_append (list, &iter);
- gtk_list_store_set (list, &iter,
- 0, s,
- -1);
-
- }
-
- gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
-
- gtk_combo_box_entry_set_text_column (cb, 0);
-}
-
-/* Initializes IA's separators substructure. */
-static void
-init_separators_page (struct import_assistant *ia)
-{
- GtkBuilder *builder = ia->asst.builder;
- struct separators_page *p = &ia->separators;
- size_t i;
-
- choose_likely_separators (ia);
-
- p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Separators"),
- GTK_ASSISTANT_PAGE_CONTENT);
- p->custom_cb = get_widget_assert (builder, "custom-cb");
- p->custom_entry = get_widget_assert (builder, "custom-entry");
- p->quote_combo = get_widget_assert (builder, "quote-combo");
- p->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (p->quote_combo)));
- p->quote_cb = get_widget_assert (builder, "quote-cb");
- p->escape_cb = get_widget_assert (builder, "escape");
-
- set_separators (ia);
- set_quote_list (GTK_COMBO_BOX_ENTRY (p->quote_combo));
- p->fields_tree_view = GTK_TREE_VIEW (get_widget_assert (builder, "fields"));
- g_signal_connect (p->quote_combo, "changed",
- G_CALLBACK (on_quote_combo_change), ia);
- g_signal_connect (p->quote_cb, "toggled",
- G_CALLBACK (on_quote_cb_toggle), ia);
- g_signal_connect (p->custom_entry, "notify::text",
- G_CALLBACK (on_separators_custom_entry_notify), ia);
- g_signal_connect (p->custom_cb, "toggled",
- G_CALLBACK (on_separators_custom_cb_toggle), ia);
- for (i = 0; i < SEPARATOR_CNT; i++)
- g_signal_connect (get_widget_assert (builder, separators[i].name),
- "toggled", G_CALLBACK (on_separator_toggle), ia);
- g_signal_connect (p->escape_cb, "toggled",
- G_CALLBACK (on_separator_toggle), ia);
-}
-
-/* Frees IA's separators substructure. */
-static void
-destroy_separators_page (struct import_assistant *ia)
-{
- struct separators_page *s = &ia->separators;
-
- ds_destroy (&s->separators);
- ds_destroy (&s->quotes);
- clear_fields (ia);
-}
-
-/* Called just before the separators page becomes visible in the
- assistant. */
-static void
-prepare_separators_page (struct import_assistant *ia)
-{
- revise_fields_preview (ia);
-}
-
-/* Called when the Reset button is clicked on the separators
- page, resets the separators to the defaults. */
-static void
-reset_separators_page (struct import_assistant *ia)
-{
- choose_likely_separators (ia);
- set_separators (ia);
-}
-
-/* Frees and clears the column data in IA's separators
- substructure. */
-static void
-clear_fields (struct import_assistant *ia)
-{
- struct separators_page *s = &ia->separators;
-
- if (s->column_cnt > 0)
- {
- struct column *col;
- size_t row;
-
- for (row = 0; row < ia->file.line_cnt; row++)
- {
- const struct string *line = &ia->file.lines[row];
- const char *line_start = ds_data (line);
- const char *line_end = ds_end (line);
-
- for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
- {
- char *s = ss_data (col->contents[row]);
- if (!(s >= line_start && s <= line_end))
- ss_dealloc (&col->contents[row]);
- }
- }
-
- for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
- {
- free (col->name);
- free (col->contents);
- }
-
- free (s->columns);
- s->columns = NULL;
- s->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 (struct import_assistant *ia)
-{
- struct separators_page *s = &ia->separators;
- 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 (&s->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->file.line_cnt; row++)
- {
- struct string *line = &ia->file.lines[row];
- struct substring text = ds_ss (line);
- size_t column_idx;
-
- for (column_idx = 0; ; column_idx++)
- {
- struct substring field;
- 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 (&s->quotes)
- && ds_find_byte (&s->quotes, text.string[0]) != SIZE_MAX)
- {
- int quote = ss_get_byte (&text);
- if (!s->escape)
- ss_get_until (&text, quote, &field);
- else
- {
- 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 (&s->separators)),
- &field);
-
- if (column_idx >= s->column_cnt)
- {
- struct column *column;
-
- if (s->column_cnt >= columns_allocated)
- s->columns = x2nrealloc (s->columns, &columns_allocated,
- sizeof *s->columns);
- column = &s->columns[s->column_cnt++];
- column->name = NULL;
- column->width = 0;
- column->contents = xcalloc (ia->file.line_cnt,
- sizeof *column->contents);
- }
- column = &s->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 (&s->separators), ss_first (text))
- != SIZE_MAX)
- ss_advance (&text, 1);
- }
- }
-}
-
-/* Chooses a name for each column on the separators page */
-static void
-choose_column_names (struct import_assistant *ia)
-{
- const struct first_line_page *f = &ia->first_line;
- struct separators_page *s = &ia->separators;
- struct dictionary *dict;
- unsigned long int generated_name_count = 0;
- struct column *col;
- size_t name_row;
-
- dict = dict_create (get_default_encoding ());
- name_row = f->variable_names && f->skip_lines ? f->skip_lines : 0;
- for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
- {
- char *hint, *name;
-
- hint = name_row ? ss_xstrdup (col->contents[name_row - 1]) : NULL;
- name = dict_make_unique_var_name (dict, hint, &generated_name_count);
- free (hint);
-
- col->name = name;
- dict_create_var_assert (dict, name, 0);
- }
- dict_destroy (dict);
-}
-
-/* Picks the most likely separator and quote characters based on
- IA's file data. */
-static void
-choose_likely_separators (struct import_assistant *ia)
-{
- unsigned long int histogram[UCHAR_MAX + 1] = { 0 };
- size_t row;
-
- /* Construct a histogram of all the characters used in the
- file. */
- for (row = 0; row < ia->file.line_cnt; row++)
- {
- struct substring line = ds_ss (&ia->file.lines[row]);
- size_t length = ss_length (line);
- size_t i;
- for (i = 0; i < length; i++)
- histogram[(unsigned char) line.string[i]]++;
- }
-
- find_commonest_chars (histogram, "\"'", "", &ia->separators.quotes);
- find_commonest_chars (histogram, ",;:/|!\t-", ",",
- &ia->separators.separators);
- ia->separators.escape = true;
-}
-
-/* Chooses the most common character among those in TARGETS,
- based on the frequency data in HISTOGRAM, and stores it in
- RESULT. If there is a tie for the most common character among
- those in TARGETS, the earliest character is chosen. If none
- of the TARGETS appear at all, then DEF is used as a
- fallback. */
-static void
-find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
- const char *targets, const char *def,
- struct string *result)
-{
- unsigned char max = 0;
- unsigned long int max_count = 0;
-
- for (; *targets != '\0'; targets++)
- {
- unsigned char c = *targets;
- unsigned long int count = histogram[c];
- if (count > max_count)
- {
- max = c;
- max_count = count;
- }
- }
- if (max_count > 0)
- {
- ds_clear (result);
- ds_put_byte (result, max);
- }
- else
- ds_assign_cstr (result, def);
-}
-
-/* Revises the contents of the fields tree view based on the
- currently chosen set of separators. */
-static void
-revise_fields_preview (struct import_assistant *ia)
-{
- GtkWidget *w;
-
- push_watch_cursor (ia);
-
- w = GTK_WIDGET (ia->separators.fields_tree_view);
- gtk_widget_destroy (w);
- get_separators (ia);
- split_fields (ia);
- choose_column_names (ia);
- ia->separators.fields_tree_view = create_data_tree_view (
- true,
- GTK_CONTAINER (get_widget_assert (ia->asst.builder, "fields-scroller")),
- ia);
-
- pop_watch_cursor (ia);
-}
-
-/* Sets the widgets to match IA's separators substructure. */
-static void
-set_separators (struct import_assistant *ia)
-{
- struct separators_page *s = &ia->separators;
- unsigned int seps;
- struct string custom;
- bool any_custom;
- bool any_quotes;
- size_t i;
-
- ds_init_empty (&custom);
- seps = 0;
- for (i = 0; i < ds_length (&s->separators); i++)
- {
- unsigned char c = ds_at (&s->separators, i);
- int j;
-
- for (j = 0; j < SEPARATOR_CNT; j++)