/* * stf_read_workbook: * @fo: file opener * @enc: encoding of file * @context: command context * @book: workbook * @input: file to read from+convert * * Main routine, handles importing a file including all dialog mumbo-jumbo */ static void stf_read_workbook (G_GNUC_UNUSED GOFileOpener const *fo, gchar const *enc, GOIOContext *context, GoView *view, GsfInput *input) { DialogStfResult_t *dialogresult = NULL; char *name, *nameutf8 = NULL; char *data = NULL; size_t data_len; WorkbookView *wbv = GNM_WORKBOOK_VIEW (view); if (!GNM_IS_WBC_GTK (context->impl)) { go_io_error_string (context, _("This importer can only be used with a GUI.")); return; } name = g_path_get_basename (gsf_input_name (input)); nameutf8 = g_filename_to_utf8 (name, -1, NULL, NULL, NULL); g_free (name); if (!nameutf8) { g_warning ("Failed to convert filename to UTF-8. This shouldn't happen here."); goto out; } data = stf_preparse (context, input, &data_len); if (!data) goto out; dialogresult = stf_dialog (WBC_GTK (context->impl), enc, FALSE, NULL, FALSE, nameutf8, data, data_len); if (dialogresult != NULL) { Workbook *book = wb_view_get_workbook (wbv); int cols = dialogresult->colcount, rows = dialogresult->rowcount; Sheet *sheet; gnm_sheet_suggest_size (&cols, &rows); sheet = sheet_new (book, nameutf8, cols, rows); workbook_sheet_attach (book, sheet); if (stf_store_results (dialogresult, sheet, 0, 0)) { workbook_recalc_all (book); resize_columns (sheet); workbook_set_saveinfo (book, GO_FILE_FL_WRITE_ONLY, go_file_saver_for_id ("Gnumeric_stf:stf_assistant")); } else { /* the user has cancelled */ /* the caller should notice that we have no sheets */ workbook_sheet_delete (sheet); } } out: g_free (nameutf8); g_free (data); if (dialogresult != NULL) stf_dialog_result_free (dialogresult); }
void sylk_file_open (GOFileOpener const *fo, GOIOContext *io_context, WorkbookView *wb_view, GsfInput *input) { SylkReader state; char const *input_name; char *name = NULL; int i; GnmLocale *locale; memset (&state, 0, sizeof (state)); state.io_context = io_context; state.input = (GsfInputTextline *) gsf_input_textline_new (input); state.converter = g_iconv_open ("UTF-8", "ISO-8859-1"); state.finished = FALSE; state.line_no = 0; state.pp.wb = wb_view_get_workbook (wb_view); if (NULL == (input_name = gsf_input_name (input)) || NULL == (name = g_path_get_basename (input_name)) || '\0' == *name) { g_free (name); name = g_strdup ("Sheet"); } state.pp.sheet = sheet_new (state.pp.wb, name, 256, 65536); workbook_sheet_attach (state.pp.wb, state.pp.sheet); g_free (name); state.pp.eval.col = state.pp.eval.row = 1; state.convs = gnm_conventions_xls_r1c1; state.formats = g_ptr_array_new (); state.fonts = g_ptr_array_new (); locale = gnm_push_C_locale (); sylk_parse_sheet (&state); gnm_pop_C_locale (locale); workbook_set_saveinfo (state.pp.wb, GO_FILE_FL_AUTO, go_file_saver_for_id ("Gnumeric_sylk:sylk")); for (i = state.fonts->len ; i-- > 0 ; ) gnm_style_unref (g_ptr_array_index (state.fonts, i)); g_ptr_array_free (state.fonts, TRUE); for (i = state.formats->len ; i-- > 0 ; ) go_format_unref (g_ptr_array_index (state.formats, i)); g_ptr_array_free (state.formats, TRUE); gsf_iconv_close (state.converter); g_object_unref (G_OBJECT (state.input)); }
static Sheet * html_get_sheet (char const *name, Workbook *wb) { Sheet *sheet = NULL; if (name) { sheet = workbook_sheet_by_name (wb, name); if (sheet == NULL) { sheet = sheet_new (wb, name, GNM_DEFAULT_COLS, GNM_DEFAULT_ROWS); workbook_sheet_attach (wb, sheet); } } else sheet = workbook_sheet_add (wb, -1, GNM_DEFAULT_COLS, GNM_DEFAULT_ROWS); return sheet; }
static void gplp_func_file_open (GOFileOpener const *fo, GOPluginService *service, GOIOContext *io_context, gpointer wb_view, GsfInput *input) { ServiceLoaderDataFileOpener *loader_data; Sheet *sheet, *old_sheet; PyObject *open_result = NULL; PyObject *input_wrapper; printf("gplp_func_file_open(start)\n"); g_return_if_fail (GO_IS_PLUGIN_SERVICE_FILE_OPENER (service)); g_return_if_fail (input != NULL); g_return_if_fail (_PyGObject_API != NULL); old_sheet = wb_view_cur_sheet (wb_view); loader_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); sheet = sheet_new (wb_view_get_workbook (wb_view), _("Some name"), gnm_sheet_get_max_cols (old_sheet), gnm_sheet_get_max_rows (old_sheet)); input_wrapper = pygobject_new (G_OBJECT (input)); if (input_wrapper != NULL) { /* wrapping adds a reference */ g_object_unref (G_OBJECT (input)); open_result = PyObject_CallFunction (loader_data->python_func_file_open, (char *) "NO", py_new_Sheet_object (sheet), input_wrapper); Py_DECREF (input_wrapper); } if (open_result != NULL) { Py_DECREF (open_result); workbook_sheet_attach (wb_view_get_workbook (wb_view), sheet); } else { go_io_error_string (io_context, py_exc_to_string ()); gnm_python_clear_error_if_needed (SERVICE_GET_LOADER (service)->py_object); g_object_unref (sheet); } printf("gplp_func_file_open(end)\n"); }
static void add_worksheet(Workbook *wb, psiconv_sheet_worksheet psi_worksheet,int nr, psiconv_formula_list psi_formulas) { Sheet *sheet; char *sheet_name; GnmStyle *default_style; psiconv_sheet_grid_section grid; sheet_name = g_strdup_printf (_("Sheet%d"),nr); sheet = sheet_new (wb, sheet_name, 256, 65536); g_free (sheet_name); if (!sheet) return; /* Default layout */ default_style = gnm_style_new_default(); if (!default_style) { g_object_unref (sheet); return; } set_layout(default_style,psi_worksheet->default_layout); /* TODO: Add show_zeros */ grid = psi_worksheet->grid; if (grid) { sheet_row_set_default_size_pts (sheet, cm2pts (grid->default_row_height)); sheet_col_set_default_size_pts (sheet, cm2pts (grid->default_column_width)); if (grid->row_heights) set_row_heights (sheet, grid->row_heights); if (grid->column_heights) set_col_widths (sheet, grid->column_heights); } add_cells(sheet,psi_worksheet->cells,psi_formulas,default_style); /* TODO: What about the NULL? */ sheet_flag_recompute_spans(sheet); workbook_sheet_attach (wb, sheet); gnm_style_unref (default_style); }
G_MODULE_EXPORT void paradox_file_open (GOFileOpener const *fo, GOIOContext *io_context, WorkbookView *wb_view, GsfInput *input) { Workbook *wb; pxdoc_t *pxdoc; pxhead_t *pxh; pxfield_t *pxf; char *data; char *name; Sheet *sheet; GnmCell *cell; GnmValue *val = NULL; GOErrorInfo *open_error = NULL; guint row, i, j, offset; #ifdef PX_MEMORY_DEBUGGING PX_mp_init (); #endif #ifdef PX_MEMORY_DEBUGGING pxdoc = PX_new2 (gn_errorhandler, PX_mp_malloc, PX_mp_realloc, PX_mp_free); #else pxdoc = PX_new2 (gn_errorhandler, gn_malloc, gn_realloc, gn_free); #endif if (PX_open_gsf (pxdoc, input) < 0) { go_io_error_info_set (io_context, go_error_info_new_str_with_details ( _("Error while opening Paradox file."), open_error)); return; } pxh = pxdoc->px_head; PX_set_targetencoding (pxdoc, "UTF-8"); wb = wb_view_get_workbook (wb_view); name = workbook_sheet_get_free_name (wb, pxh->px_tablename, FALSE, TRUE); sheet = sheet_new (wb, name, 256, 65536); g_free (name); workbook_sheet_attach (wb, sheet); pxf = pxh->px_fields; for (i = 0 ; i < (guint) pxh->px_numfields; i++) { char str[30], *str2; char ctypes[26] = {'?', 'A', 'D', 'S', 'I', '$', 'N', '?', '?', 'L', '?', '?', 'M', 'B', 'F', 'O', 'G', '?', '?', '?', 'T', '@', '+', '#', 'Y', }; cell = sheet_cell_fetch (sheet, i, 0); if (pxf->px_ftype == pxfBCD) snprintf (str, 30, "%s,%c,%d", pxf->px_fname, ctypes[(int)pxf->px_ftype], pxf->px_fdc); else snprintf (str, 30, "%s,%c,%d", pxf->px_fname, ctypes[(int)pxf->px_ftype], pxf->px_flen); #if PXLIB_MAJOR_VERSION == 0 && (PXLIB_MINOR_VERION < 3 || (PXLIB_MAJOR_VERSION == 3 && PXLIB_MICRO_VERSION == 0)) /* Convert the field names to utf-8. This is actually in pxlib * responsibility, but hasn't been implemented yet. For the mean time * we *misuse* PX_get_data_alpha() */ PX_get_data_alpha (pxdoc, str, strlen (str), &str2); gnm_cell_set_text (cell, str2); pxdoc->free (pxdoc, str2); #else gnm_cell_set_text (cell, str); #endif pxf++; } { GnmRange r; GnmStyle *bold = gnm_style_new (); gnm_style_set_font_bold (bold, TRUE); sheet_style_apply_range (sheet, range_init (&r, 0, 0, pxh->px_numfields-1, 0), bold); } if ((data = (char *) pxdoc->malloc (pxdoc, pxh->px_recordsize, _("Could not allocate memory for record."))) == NULL) { go_io_error_info_set (io_context, go_error_info_new_str_with_details ( _("Error while opening Paradox file."), open_error)); return; } row = 1; for (j = 0; j < (guint)pxh->px_numrecords; j++) { pxdatablockinfo_t pxdbinfo; int isdeleted = 0; if (NULL != PX_get_record2 (pxdoc, j, data, &isdeleted, &pxdbinfo)) { offset = 0; pxf = pxh->px_fields; for (i = 0; i < (guint) pxh->px_numfields ; i++) { cell = sheet_cell_fetch (sheet, i, row); val = NULL; switch (pxf->px_ftype) { case pxfAlpha: { char *value; if (0 < PX_get_data_alpha (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_string_nocopy (value); /* value_set_fmt (val, field->fmt); */ } break; } case pxfShort: { short int value; if (0 < PX_get_data_short (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_int (value); } break; } case pxfAutoInc: case pxfLong: { long value; if (0 < PX_get_data_long (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_int (value); } break; } case pxfCurrency: case pxfNumber: { double value; if (0 < PX_get_data_double (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_float (value); if (pxf->px_ftype == pxfCurrency) value_set_fmt (val, go_format_default_money ()); } break; } case pxfTimestamp: { double value; if (0 < PX_get_data_double (pxdoc, &data[offset], pxf->px_flen, &value)) { value = value / 86400000.0; /* 693594 = number of days up to 31.12.1899 */ value -= 693594; val = value_new_float (value); value_set_fmt (val, go_format_default_date_time ()); } break; } case pxfLogical: { char value; if (0 < PX_get_data_byte (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_bool (value ? TRUE : FALSE); } break; } case pxfDate: { long value; int year, month, day; GDate *date; if (0 < PX_get_data_long (pxdoc, &data[offset], pxf->px_flen, &value)) { PX_SdnToGregorian (value+1721425, &year, &month, &day); date = g_date_new_dmy (day, month, year); val = value_new_int (go_date_g_to_serial (date, NULL)); value_set_fmt (val, go_format_default_date ()); g_date_free (date); } break; } case pxfTime: { long value; if (0 < PX_get_data_long (pxdoc, &data[offset], pxf->px_flen, &value)) { val = value_new_float (value/86400000.0); value_set_fmt (val, go_format_default_time ()); } break; } case pxfBCD: { char *value; if (0 < PX_get_data_bcd (pxdoc, &data[offset], pxf->px_fdc, &value)) { val = value_new_string_nocopy (value); } break; } case pxfMemoBLOb: { char *value; int size, mod_nr; if (0 < PX_get_data_blob (pxdoc, &data[offset], pxf->px_flen, &mod_nr, &size, &value)) { val = value_new_string_nocopy (value); } break; } default: val = value_new_string_nocopy ( g_strdup_printf (_("Field type %d is not supported."), pxf->px_ftype)); } if (val) gnm_cell_set_value (cell, val); offset += pxf->px_flen; pxf++; } if (pxh->px_filetype == pxfFileTypPrimIndex) { short int value; cell = sheet_cell_fetch (sheet, i++, row); if (0 < PX_get_data_short (pxdoc, &data[offset], 2, &value)) { val = value_new_int (value); gnm_cell_set_value (cell, val); } offset += 2; cell = sheet_cell_fetch (sheet, i++, row); if (0 < PX_get_data_short (pxdoc, &data[offset], 2, &value)) { val = value_new_int (value); gnm_cell_set_value (cell, val); } offset += 2; cell = sheet_cell_fetch (sheet, i++, row); if (0 < PX_get_data_short (pxdoc, &data[offset], 2, &value)) { val = value_new_int (value); gnm_cell_set_value (cell, val); } cell = sheet_cell_fetch (sheet, i++, row); val = value_new_int (pxdbinfo.number); gnm_cell_set_value (cell, val); } } row++; } pxdoc->free (pxdoc, data); PX_close (pxdoc); PX_delete (pxdoc); sheet_flag_recompute_spans (sheet); }
/* * 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); }