/** * stf_dialog_result_free * @dialogresult: a dialogresult struct * * This routine will properly free the members of @dialogresult and * @dialogresult itself * * returns: nothing **/ void stf_dialog_result_free (DialogStfResult_t *dialogresult) { g_return_if_fail (dialogresult != NULL); stf_parse_options_free (dialogresult->parseoptions); g_free (dialogresult->text); g_free (dialogresult->encoding); g_free (dialogresult); }
int gnumeric_overlay_csv(GnumericWorkbookPtr workbook, const char *start, const char *stop) { WorkbookView *wbv = (WorkbookView *)workbook; printf("For testing, try pasting some data into workbook\n"); //Workbook *wb = wb_view_get_workbook (wbv); Sheet *sheet = wb_view_cur_sheet (wbv); if (sheet==NULL) { printf("no sheet!\n"); return 1; } StfParseOptions_t *options = stf_parse_options_guess(start); stf_parse_sheet(options,start,stop,sheet,0,0); stf_parse_options_free(options); options = NULL; return 0; }
/** Destructor for GncCsvParseData. * @param parse_data Parse data whose memory will be freed */ void gnc_csv_parse_data_free (GncCsvParseData* parse_data) { /* All non-NULL pointers have been initialized and must be freed. */ if (parse_data->raw_mapping != NULL) { g_mapped_file_unref (parse_data->raw_mapping); } if (parse_data->file_str.begin != NULL) g_free (parse_data->file_str.begin); if (parse_data->orig_lines != NULL) stf_parse_general_free (parse_data->orig_lines); if (parse_data->orig_row_lengths != NULL) g_array_free (parse_data->orig_row_lengths, FALSE); if (parse_data->options != NULL) stf_parse_options_free (parse_data->options); if (parse_data->column_types != NULL) g_array_free (parse_data->column_types, TRUE); if (parse_data->error_lines != NULL) g_list_free (parse_data->error_lines); if (parse_data->transactions != NULL) { GList* transactions = parse_data->transactions; /* We have to free the GncCsvTransLine's that are at each node in * the list before freeing the entire list. */ do { g_free (transactions->data); transactions = g_list_next (transactions); } while (transactions != NULL); g_list_free (parse_data->transactions); } g_free (parse_data->chunk); g_free (parse_data); }
/** * stf_dialog * @wbcg: a Commandcontext (can be NULL) * @source: name of the file we are importing (or data) in UTF-8 * @data: the data itself * * This will start the import. * (NOTE: you have to free the DialogStfResult_t that this function returns yourself) * * returns: A DialogStfResult_t struct on success, NULL otherwise. **/ DialogStfResult_t* stf_dialog (WBCGtk *wbcg, const char *opt_encoding, gboolean fixed_encoding, const char *opt_locale, gboolean fixed_locale, const char *source, const char *data, int data_len) { GladeXML *gui; DialogStfResult_t *dialogresult; StfDialogData pagedata; GtkResponseType resp; g_return_val_if_fail (opt_encoding != NULL || !fixed_encoding, NULL); g_return_val_if_fail (opt_locale != NULL || !fixed_locale, NULL); g_return_val_if_fail (source != NULL, NULL); g_return_val_if_fail (data != NULL, NULL); gui = gnm_glade_xml_new (GO_CMD_CONTEXT (wbcg), "dialog-stf.glade", NULL, NULL); if (gui == NULL) return NULL; pagedata.encoding = g_strdup (opt_encoding); pagedata.fixed_encoding = fixed_encoding; pagedata.locale = g_strdup (opt_locale); pagedata.fixed_locale = fixed_locale; pagedata.wbcg = wbcg; pagedata.source = source; pagedata.raw_data = data; pagedata.raw_data_len = data_len < 0 ? (int)strlen (data) : data_len; pagedata.utf8_data = NULL; pagedata.cur = NULL; pagedata.dialog = GTK_DIALOG (glade_xml_get_widget (gui, "stf_dialog")); pagedata.notebook = GTK_NOTEBOOK (glade_xml_get_widget (gui, "stf_notebook")); pagedata.next_button = glade_xml_get_widget (gui, "forward_button"); pagedata.back_button = glade_xml_get_widget (gui, "back_button"); pagedata.cancel_button = glade_xml_get_widget (gui, "cancel_button"); pagedata.help_button = glade_xml_get_widget (gui, "help_button"); pagedata.finish_button = glade_xml_get_widget (gui, "finish_button"); pagedata.parseoptions = NULL; stf_dialog_main_page_init (gui, &pagedata); stf_dialog_csv_page_init (gui, &pagedata); stf_dialog_fixed_page_init (gui, &pagedata); stf_dialog_format_page_init (gui, &pagedata); stf_dialog_attach_page_signals (gui, &pagedata); stf_dialog_editables_enter (&pagedata); stf_dialog_set_initial_keyboard_focus (&pagedata); prepare_page (&pagedata); frob_buttons (&pagedata); resp = go_gtk_dialog_run (pagedata.dialog, wbcg_toplevel (wbcg)); if (resp == GTK_RESPONSE_OK) { dialogresult = g_new (DialogStfResult_t, 1); dialogresult->text = pagedata.utf8_data; *((char *)pagedata.cur_end) = 0; if (dialogresult->text != pagedata.cur) strcpy (dialogresult->text, pagedata.cur); pagedata.cur = pagedata.utf8_data = NULL; dialogresult->encoding = pagedata.encoding; pagedata.encoding = NULL; dialogresult->colcount = pagedata.format.col_import_count; dialogresult->rowcount = pagedata.rowcount; dialogresult->parseoptions = pagedata.parseoptions; pagedata.parseoptions = NULL; g_free (dialogresult->parseoptions->locale); dialogresult->parseoptions->locale = pagedata.locale; pagedata.locale = NULL; dialogresult->parseoptions->formats = pagedata.format.formats; pagedata.format.formats = NULL; dialogresult->parseoptions->col_import_array = pagedata.format.col_import_array; dialogresult->parseoptions->col_import_array_len = pagedata.format.col_import_array_len; pagedata.format.col_import_array = NULL; pagedata.format.col_import_count = 0; pagedata.format.col_import_array_len = 0; } else { dialogresult = NULL; } stf_dialog_main_page_cleanup (&pagedata); stf_dialog_csv_page_cleanup (&pagedata); stf_dialog_fixed_page_cleanup (&pagedata); stf_dialog_format_page_cleanup (&pagedata); g_object_unref (gui); g_free (pagedata.encoding); g_free (pagedata.locale); g_free (pagedata.utf8_data); if (pagedata.parseoptions) stf_parse_options_free (pagedata.parseoptions); return dialogresult; }
/* * stf_read_workbook_auto_csvtab: * @fo: file opener * @enc: optional encoding * @context: command context * @book: workbook * @input: file to read from+convert * * Attempt to auto-detect CSV or tab-delimited file */ static void stf_read_workbook_auto_csvtab (G_GNUC_UNUSED GOFileOpener const *fo, gchar const *enc, GOIOContext *context, GoView *view, GsfInput *input) { Sheet *sheet; Workbook *book; char *name; char *data; GString *utf8data; size_t data_len; StfParseOptions_t *po; const char *gsfname; int cols, rows, i; GStringChunk *lines_chunk; GPtrArray *lines; WorkbookView *wbv = GNM_WORKBOOK_VIEW (view); g_return_if_fail (context != NULL); g_return_if_fail (wbv != NULL); book = wb_view_get_workbook (wbv); data = stf_preparse (context, input, &data_len); if (!data) return; enc = go_guess_encoding (data, data_len, enc, &utf8data, NULL); g_free (data); if (!enc) { go_cmd_context_error_import (GO_CMD_CONTEXT (context), _("That file is not in the given encoding.")); return; } clear_stray_NULs (context, utf8data); /* * Try to get the filename we're reading from. This is not a * great way. */ gsfname = gsf_input_name (input); { const char *ext = gsf_extension_pointer (gsfname); gboolean iscsv = ext && strcasecmp (ext, "csv") == 0; if (iscsv) po = stf_parse_options_guess_csv (utf8data->str); else po = stf_parse_options_guess (utf8data->str); } lines_chunk = g_string_chunk_new (100 * 1024); lines = stf_parse_general (po, lines_chunk, utf8data->str, utf8data->str + utf8data->len); rows = lines->len; cols = 0; for (i = 0; i < rows; i++) { GPtrArray *line = g_ptr_array_index (lines, i); cols = MAX (cols, (int)line->len); } gnm_sheet_suggest_size (&cols, &rows); stf_parse_general_free (lines); g_string_chunk_free (lines_chunk); name = g_path_get_basename (gsfname); sheet = sheet_new (book, name, cols, rows); g_free (name); workbook_sheet_attach (book, sheet); if (stf_parse_sheet (po, utf8data->str, NULL, sheet, 0, 0)) { gboolean is_csv; workbook_recalc_all (book); resize_columns (sheet); if (po->cols_exceeded || po->rows_exceeded) { stf_warning (context, _("Some data did not fit on the " "sheet and was dropped.")); } is_csv = po->sep.chr && po->sep.chr[0] == ','; workbook_set_saveinfo (book, GO_FILE_FL_WRITE_ONLY, go_file_saver_for_id (is_csv ? "Gnumeric_stf:stf_csv" : "Gnumeric_stf:stf_assistant")); } else { workbook_sheet_delete (sheet); go_cmd_context_error_import (GO_CMD_CONTEXT (context), _("Parse error while trying to parse data into sheet")); } stf_parse_options_free (po); g_string_free (utf8data, TRUE); }