GnumericSheetPtr gnumeric_add_sheet(GnumericWorkbookPtr workbook, const char *name) { WorkbookView *wbv = (WorkbookView *)workbook; #ifndef OLD_GNUMERIC int cols = gnm_conf_get_core_workbook_n_cols (); int rows = gnm_conf_get_core_workbook_n_rows (); if (!gnm_sheet_valid_size (cols, rows)) { gnm_sheet_suggest_size (&cols, &rows); } Sheet *sheet = workbook_sheet_add (wb_view_get_workbook (wbv), -1, cols, rows); #else Sheet *sheet = workbook_sheet_add (wb_view_get_workbook (wbv), -1, TRUE); #endif if (name!=NULL) { int idx = gnumeric_get_sheet_count(workbook)-1; GSList *idxs = NULL; GSList *names = NULL; idxs = g_slist_append(idxs, GINT_TO_POINTER(idx)); names = g_slist_append(names, (gpointer)name); workbook_sheet_rename(wb_view_get_workbook (wbv), idxs, names, cc); g_slist_free(idxs); g_slist_free(names); } return (GnumericSheetPtr*)sheet; }
static void gnm_stf_file_saver_save (G_GNUC_UNUSED GOFileSaver const *fs, GOIOContext *context, GoView const *view, GsfOutput *output) { WorkbookView *wbv = GNM_WORKBOOK_VIEW (view); Workbook *wb = wb_view_get_workbook (wbv); GnmStfExport *stfe = gnm_stf_get_stfe (G_OBJECT (wb)); GsfOutput *dummy_sink; /* TODO: move this GUI dependent code out of this * filesaver into gui-file.c. After this, remove includes (see above). */ if (GNM_IS_WBC_GTK (context->impl)) { gboolean cancelled = stf_export_dialog (WBC_GTK (context->impl), stfe, wb); if (cancelled) { go_io_error_unknown (context); return; } } if (!stfe->sheet_list) gnm_stf_export_options_sheet_list_add (stfe, wb_view_cur_sheet (wbv)); g_object_set (G_OBJECT (stfe), "sink", output, NULL); if (gnm_stf_export (stfe) == FALSE) go_cmd_context_error_import (GO_CMD_CONTEXT (context), _("Error while trying to export file as text")); /* We're not allowed to set a NULL sink, so use a dummy. */ dummy_sink = gsf_output_memory_new (); g_object_set (G_OBJECT (stfe), "sink", dummy_sink, NULL); g_object_unref (dummy_sink); }
int gnumeric_free(GnumericWorkbookPtr workbook) { WorkbookView *wbv = (WorkbookView *)workbook; if (wbv!=NULL) { g_object_unref (wb_view_get_workbook (wbv)); } return 0; }
static void gplp_func_file_save (G_GNUC_UNUSED GOFileSaver const *fs, GOPluginService *service, GOIOContext *io_context, gconstpointer wb_view, GsfOutput *output) { ServiceLoaderDataFileSaver *saver_data; PyObject *py_workbook; PyObject *save_result = NULL; PyObject *output_wrapper; g_return_if_fail (GO_IS_PLUGIN_SERVICE_FILE_SAVER (service)); g_return_if_fail (output != NULL); g_return_if_fail (_PyGObject_API != NULL); saver_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); py_workbook = py_new_Workbook_object (wb_view_get_workbook (wb_view)); output_wrapper = pygobject_new (G_OBJECT (output)); if (output_wrapper != NULL) { /* wrapping adds a reference */ g_object_unref (output); save_result = PyObject_CallFunction (saver_data->python_func_file_save, (char *) "NO", py_workbook, output_wrapper); Py_DECREF (output_wrapper); } if (save_result != NULL) { Py_DECREF (save_result); } else { go_io_error_string (io_context, py_exc_to_string ()); gnm_python_clear_error_if_needed (SERVICE_GET_LOADER (service)->py_object); } }
static void stf_write_csv (G_GNUC_UNUSED GOFileSaver const *fs, GOIOContext *context, GoView const *view, GsfOutput *output) { Sheet *sheet; GnmRangeRef const *range; WorkbookView *wbv = GNM_WORKBOOK_VIEW (view); GnmStfExport *config = g_object_new (GNM_STF_EXPORT_TYPE, "sink", output, "quoting-triggers", ", \t\n\"", NULL); /* FIXME: this is crap in both branches of the "if". */ range = g_object_get_data (G_OBJECT (wb_view_get_workbook (wbv)), "ssconvert-range"); if (range && range->a.sheet) sheet = range->a.sheet; else sheet = wb_view_cur_sheet (wbv); gnm_stf_export_options_sheet_list_add (config, sheet); if (gnm_stf_export (config) == FALSE) go_cmd_context_error_import (GO_CMD_CONTEXT (context), _("Error while trying to write CSV file")); g_object_unref (config); }
/* * 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); }
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"); }
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)); }
void dif_file_open (GOFileOpener const *fo, GOIOContext *io_context, WorkbookView *wbv, GsfInput *input) { Workbook *wb = wb_view_get_workbook (wbv); DifInputContext *ctxt = dif_input_context_new (io_context, wb, input); workbook_set_saveinfo (wb, GO_FILE_FL_MANUAL_REMEMBER, go_file_saver_for_id ("Gnumeric_dif:dif")); if (ctxt != NULL) { dif_parse_sheet (ctxt); if (go_io_error_occurred (io_context)) go_io_error_push (io_context, go_error_info_new_str (_("Error while reading DIF file."))); dif_input_context_destroy (ctxt); } else if (!go_io_error_occurred (io_context)) go_io_error_unknown (io_context); }
void pwhtml_file_save (GOFileSaver const *fs, GOIOContext *io_context, WorkbookView const *wb_view, GsfOutput *output) { GSList *sheets, *ptr; Workbook *wb = wb_view_get_workbook (wb_view); GOFileSaveScope save_scope; g_return_if_fail (fs != NULL); g_return_if_fail (wb != NULL); g_return_if_fail (output != NULL); sheets = workbook_sheets (wb); save_scope = go_file_saver_get_save_scope (fs); for (ptr = sheets ; ptr != NULL ; ptr = ptr->next) { write_sheet (output, (Sheet *) ptr->data, save_scope); } g_slist_free (sheets); }
static void html_read_table (htmlNodePtr cur, htmlDocPtr doc, WorkbookView *wb_view, GnmHtmlTableCtxt *tc) { Workbook *wb; htmlNodePtr ptr, ptr2; g_return_if_fail (cur != NULL); g_return_if_fail (wb_view != NULL); wb = wb_view_get_workbook (wb_view); for (ptr = cur->children; ptr != NULL ; ptr = ptr->next) { if (ptr->type != XML_ELEMENT_NODE) continue; if (xmlStrEqual (ptr->name, CC2XML ("caption"))) { xmlBufferPtr buf; buf = xmlBufferCreate (); for (ptr2 = ptr->children; ptr2 != NULL ; ptr2 = ptr2->next) { htmlNodeDump (buf, doc, ptr2); } if (buf->use > 0) { char *name; name = g_strndup (CXML2C (buf->content), buf->use); tc->sheet = html_get_sheet (name, wb); g_free (name); } xmlBufferFree (buf); } else if (xmlStrEqual (ptr->name, CC2XML ("thead")) || xmlStrEqual (ptr->name, CC2XML ("tfoot")) || xmlStrEqual (ptr->name, CC2XML ("tbody"))) { html_read_rows (ptr, doc, wb, tc); } else if (xmlStrEqual (ptr->name, CC2XML ("tr"))) { html_read_rows (cur, doc, wb, tc); break; } } }
void sylk_file_save (GOFileSaver const *fs, GOIOContext *io_context, gconstpointer wb_view, GsfOutput *output) { GnmLocale *locale; SylkWriter state; state.wb = wb_view_get_workbook (wb_view); state.sheet = wb_view_cur_sheet (wb_view); state.output = output; state.convs = sylk_conventions_new (); if (NULL == state.sheet) { go_io_error_string (io_context, _("Cannot get default sheet.")); return; } locale = gnm_push_C_locale (); gsf_output_puts (output, "ID;PGnumeric;N;E\r\n"); sylk_write_sheet (&state); gsf_output_puts (output, "E\r\n"); gnm_pop_C_locale (locale); gnm_conventions_unref (state.convs); }
/* * write every sheet of the workbook to a roff file * * FIXME: Should roff quote sheet name (and everything else) */ void roff_file_save (GOFileSaver const *fs, GOIOContext *io_context, WorkbookView const *wb_view, GsfOutput *output) { GSList *sheets, *ptr; GnmCell *cell; int row, col, fontsize, v_size; Workbook *wb = wb_view_get_workbook (wb_view); g_return_if_fail (wb != NULL); gsf_output_printf (output, ".\\\" TROFF file\n"); gsf_output_printf (output, ".fo ''%%''\n"); sheets = workbook_sheets (wb); for (ptr = sheets ; ptr != NULL ; ptr = ptr->next) { Sheet *sheet = ptr->data; GnmRange r = sheet_get_extent (sheet, FALSE, TRUE); gsf_output_printf (output, "%s\n\n", sheet->name_unquoted); gsf_output_printf (output, ".TS H\n"); gsf_output_printf (output, "allbox;\n"); for (row = r.start.row; row <= r.end.row; row++) { ColRowInfo const * ri; ri = sheet_row_get_info (sheet, row); if (ri->needs_respan) row_calc_spans ((ColRowInfo *) ri, row, sheet); if (row > r.start.row) gsf_output_printf (output, ".T&\n"); /* define alignments, bold etc. per cell */ v_size = DEFSIZE; for (col = r.start.col; col <= r.end.col; col++) { cell = sheet_cell_get (sheet, col, row); if (col > r.start.col) gsf_output_printf (output, " "); if (!cell) { gsf_output_printf (output, "l"); } else { GnmStyle const *style = gnm_cell_get_style (cell); if (!style) break; if (gnm_style_get_align_h (style) & GNM_HALIGN_RIGHT) gsf_output_printf (output, "r"); else if (gnm_style_get_align_h (style) == GNM_HALIGN_CENTER || /* FIXME : center across selection is different */ gnm_style_get_align_h (style) == GNM_HALIGN_CENTER_ACROSS_SELECTION || gnm_style_get_align_h (style) == GNM_HALIGN_DISTRIBUTED) gsf_output_printf (output, "c"); else gsf_output_printf (output, "l"); if (font_is_monospaced (style)) { if (gnm_style_get_font_bold (style) && gnm_style_get_font_italic (style)) gsf_output_printf (output, "fCBI"); else if (gnm_style_get_font_bold (style)) gsf_output_printf (output, "fCB"); else if (gnm_style_get_font_italic (style)) gsf_output_printf (output, "fCI"); else gsf_output_printf (output, "fCR"); } else if (font_is_helvetica (style)) { if (gnm_style_get_font_bold (style) && gnm_style_get_font_italic (style)) gsf_output_printf (output, "fHBI"); else if (gnm_style_get_font_bold (style)) gsf_output_printf (output, "fHB"); else if (gnm_style_get_font_italic (style)) gsf_output_printf (output, "fHI"); else gsf_output_printf (output, "fHR"); } else { /* default is times */ if (gnm_style_get_font_bold (style) && gnm_style_get_font_italic (style)) gsf_output_printf (output, "fTBI"); else if (gnm_style_get_font_bold (style)) gsf_output_printf (output, "fTB"); else if (gnm_style_get_font_italic (style)) gsf_output_printf (output, "fTI"); } fontsize = gnm_style_get_font_size (style); if (fontsize) { gsf_output_printf (output, "p%d", fontsize); v_size = v_size > fontsize ? v_size : fontsize; } } } gsf_output_printf (output, ".\n"); gsf_output_printf (output, ".vs %.2fp\n", 2.5 + v_size); for (col = r.start.col; col <= r.end.col; col++) { if (col > r.start.col) gsf_output_printf (output, "\t"); cell = sheet_cell_get (sheet, col, row); if (!cell) { /* empty cell */ gsf_output_printf (output, " "); } else { roff_fprintf (output, cell); } } gsf_output_printf (output, "\n"); if (row == r.start.row) gsf_output_printf (output, ".TH\n"); } gsf_output_printf (output, ".TE\n\n"); } g_slist_free (sheets); }
GnumericSheetPtr gnumeric_get_sheet(GnumericWorkbookPtr workbook, int index) { WorkbookView *wbv = (WorkbookView *)workbook; Sheet *sheet = workbook_sheet_by_index (wb_view_get_workbook (wbv), index); return (GnumericSheetPtr*)sheet; }
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); }
int gnumeric_get_sheet_count(GnumericWorkbookPtr workbook) { WorkbookView *wbv = (WorkbookView *)workbook; return workbook_sheet_count(wb_view_get_workbook(wbv)); }
static gboolean feed(ChupaDecomposer *decomposer, ChupaFeeder *feeder, ChupaData *data, GError **error) { GOFileSaver *saver; GOFileOpener *opener = NULL; GOIOContext *io_context; WorkbookView *view = NULL; Workbook *workbook; GODoc *document; GsfInput *source; GsfOutput *output; GInputStream *input; ChupaData *next_data; const gchar *filename; GPrintFunc old_print_error_func; ChupaMetadata *metadata; filename = chupa_data_get_filename(data); source = chupa_data_input_new(data); io_context = go_io_context_new(command_context); old_print_error_func = g_set_printerr_handler(printerr_to_log_delegator); view = wb_view_new_from_input(source, filename, opener, io_context, NULL); g_object_unref(source); g_set_printerr_handler(old_print_error_func); if (go_io_error_occurred(io_context)) { go_io_error_display(io_context); } if (!view) { g_set_error(error, CHUPA_DECOMPOSER_ERROR, CHUPA_DECOMPOSER_ERROR_FEED, "[decomposer][excel][feed][error][%s]" ": failed to create workbook", filename); g_object_unref(io_context); return FALSE; } workbook = wb_view_get_workbook(view); document = wb_view_get_doc(view); saver = find_file_saver(filename, view, workbook, document, error); if (!saver) { g_object_unref(workbook); g_object_unref(io_context); return FALSE; } output = gsf_output_memory_new(); if (!output) { g_set_error(error, CHUPA_DECOMPOSER_ERROR, CHUPA_DECOMPOSER_ERROR_FEED, "[decomposer][excel][feed][%s][error]" ": failed to create output", filename); g_object_unref(workbook); g_object_unref(io_context); return FALSE; } wbv_save_to_output(view, saver, output, io_context); if (go_io_error_occurred(io_context)) { go_io_error_display(io_context); g_object_unref(workbook); g_object_unref(io_context); g_object_unref(output); return FALSE; } metadata = chupa_metadata_new(); chupa_metadata_merge_original_metadata(metadata, chupa_data_get_metadata(data)); collect_metadata(metadata, document); g_object_unref(workbook); g_object_unref(io_context); input = chupa_memory_input_stream_new(GSF_OUTPUT_MEMORY(output)); g_object_unref(output); chupa_metadata_set_content_length(metadata, gsf_output_size(output)); next_data = chupa_data_new(input, metadata); g_object_unref(metadata); g_object_unref(input); chupa_feeder_accepted(feeder, next_data); chupa_data_finished(next_data, NULL); g_object_unref(next_data); return TRUE; }
/* * 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); }
int main (int argc, char const **argv) { gboolean opened_workbook = FALSE; gboolean with_gui; GOIOContext *ioc; WorkbookView *wbv; GSList *wbcgs_to_kill = NULL; GOCmdContext *cc; #ifdef G_OS_WIN32 gboolean has_console; #endif /* No code before here, we need to init threads */ argv = gnm_pre_parse_init (argc, argv); /* * Attempt to disable Ubuntu's funky, non-working scroll * bars. This needs to be done before gtk starts loading * modules. Note: the following call will not replace * an existing setting, so you can run with =1 if you like. */ g_setenv ("LIBOVERLAY_SCROLLBAR", "0", FALSE); #ifdef G_OS_WIN32 has_console = FALSE; { typedef BOOL (CALLBACK* LPFNATTACHCONSOLE)(DWORD); LPFNATTACHCONSOLE MyAttachConsole; HMODULE hmod; if ((hmod = GetModuleHandle("kernel32.dll"))) { MyAttachConsole = (LPFNATTACHCONSOLE) GetProcAddress(hmod, "AttachConsole"); if (MyAttachConsole && MyAttachConsole(ATTACH_PARENT_PROCESS)) { freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); dup2(fileno(stdout), 1); dup2(fileno(stderr), 2); has_console = TRUE; } } } #endif gnumeric_arg_parse (argc, (char **)argv); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); bind_textdomain_codeset (GETTEXT_PACKAGE "-functions", "UTF-8"); with_gui = !func_def_file && !func_state_file && !split_funcdocs; if (with_gui) { gnm_session_init (argv[0]); } gnm_init (); /* These are useful for the build process only. */ if (func_state_file) return gnm_dump_func_defs (func_state_file, 0); if (func_def_file) return gnm_dump_func_defs (func_def_file, 1); if (split_funcdocs) return gnm_dump_func_defs (NULL, 2); if (ext_refs_file) return gnm_dump_func_defs (ext_refs_file, 4); if (with_gui) { go_component_set_default_command_context (cc = cmd_context_stderr_new ()); g_object_unref (cc); cc = g_object_new (GNM_TYPE_IO_CONTEXT_GTK, "show-splash", !gnumeric_no_splash, "show-warnings", !gnumeric_no_warnings, NULL); ioc = GO_IO_CONTEXT (g_object_ref (cc)); handle_paint_events (); pathetic_qt_workaround (); } else { /* TODO: Make this inconsistency go away */ cc = cmd_context_stderr_new (); ioc = go_io_context_new (cc); go_component_set_default_command_context (cc); } /* Keep in sync with .desktop file */ g_set_application_name (_("Gnumeric Spreadsheet")); gnm_plugins_init (GO_CMD_CONTEXT (ioc)); if (startup_files) { int i; for (i = 0; startup_files [i]; i++) ; go_io_context_set_num_files (ioc, i); for (i = 0; startup_files [i] && !initial_workbook_open_complete; i++) { char *uri = go_shell_arg_to_uri (startup_files[i]); if (uri == NULL) { g_warning ("Ignoring invalid URI."); continue; } go_io_context_processing_file (ioc, uri); wbv = workbook_view_new_from_uri (uri, NULL, ioc, NULL); g_free (uri); if (go_io_error_occurred (ioc) || go_io_warning_occurred (ioc)) { go_io_error_display (ioc); go_io_error_clear (ioc); } if (wbv != NULL) { WBCGtk *wbcg; workbook_update_history (wb_view_get_workbook (wbv), GNM_FILE_SAVE_AS_STYLE_SAVE); wbcg = wbc_gtk_new (wbv, NULL, NULL, geometry); geometry = NULL; sheet_update (wb_view_cur_sheet (wbv)); opened_workbook = TRUE; gnm_io_context_gtk_set_transient_for (GNM_IO_CONTEXT_GTK (ioc), wbcg_toplevel (wbcg)); if (immediate_exit_flag) wbcgs_to_kill = g_slist_prepend (wbcgs_to_kill, wbcg); } /* cheesy attempt to keep the ui from freezing during load */ handle_paint_events (); if (gnm_io_context_gtk_get_interrupted (GNM_IO_CONTEXT_GTK (ioc))) break; /* Don't load any more workbooks */ } } g_object_unref (cc); cc = NULL; /* FIXME: Maybe we should quit here if we were asked to open files and failed to do so. */ /* If we were intentionally short circuited exit now */ if (!initial_workbook_open_complete) { initial_workbook_open_complete = TRUE; if (!opened_workbook) { gint n_of_sheets = gnm_conf_get_core_workbook_n_sheet (); wbc_gtk_new (NULL, workbook_new_with_sheets (n_of_sheets), NULL, geometry); } if (immediate_exit_flag) { GSList *l; for (l = wbcgs_to_kill; l; l = l->next) g_idle_add ((GSourceFunc)cb_kill_wbcg, l->data); } g_signal_connect (gnm_app_get_app (), "workbook_removed", G_CALLBACK (cb_workbook_removed), NULL); gnm_io_context_gtk_discharge_splash (GNM_IO_CONTEXT_GTK (ioc)); g_object_unref (ioc); g_idle_add ((GSourceFunc)pathetic_qt_workaround, NULL); gtk_main (); } else { g_object_unref (ioc); g_slist_foreach (wbcgs_to_kill, (GFunc)cb_kill_wbcg, NULL); } g_slist_free (wbcgs_to_kill); gnumeric_arg_shutdown (); store_plugin_state (); gnm_shutdown (); #if defined(G_OS_WIN32) if (has_console) { close(1); close(2); FreeConsole(); } #endif gnm_pre_parse_shutdown (); go_component_set_default_command_context (NULL); /* * This helps finding leaks. We might want it in developent * only. */ if (with_gui && gnm_debug_flag ("close-displays")) { GSList *displays; gdk_flush(); while (g_main_context_iteration (NULL, FALSE)) ;/* nothing */ displays = gdk_display_manager_list_displays (gdk_display_manager_get ()); g_slist_foreach (displays, (GFunc)gdk_display_close, NULL); g_slist_free (displays); } return 0; }
Workbook * wb_control_get_workbook (WorkbookControl const *wbc) { g_return_val_if_fail (IS_WORKBOOK_CONTROL (wbc), NULL); return wbc->wb_view ? wb_view_get_workbook (wbc->wb_view) : NULL; }
/** * Other things we could index * - The names of external refernces * - functions used * - plugins used **/ static int ssindex (char const *file, GOIOContext *ioc) { int i, res = 0; GSList *objs, *ptr; char *str = go_shell_arg_to_uri (file); IndexerState state; GsfOutput *gsf_stdout; Workbook *wb; state.wb_view = workbook_view_new_from_uri (str, NULL, ioc, ssindex_import_encoding); g_free (str); if (state.wb_view == NULL) return 1; state.sheet = NULL; gsf_stdout = gsf_output_stdio_new_FILE ("<stdout>", stdout, TRUE); state.output = gsf_xml_out_new (gsf_stdout); gsf_xml_out_start_element (state.output, "gnumeric"); state.wb = wb = wb_view_get_workbook (state.wb_view); workbook_foreach_name (wb, TRUE, (GHFunc)cb_index_name, &state); for (i = 0; i < workbook_sheet_count (wb); i++) { state.sheet = workbook_sheet_by_index (wb, i); gsf_xml_out_simple_element (state.output, "data", state.sheet->name_unquoted); /* cell content */ sheet_cell_foreach (state.sheet, (GHFunc)&cb_index_cell, &state); /* now the objects */ objs = sheet_objects_get (state.sheet, NULL, G_TYPE_NONE); for (ptr = objs ; ptr != NULL ; ptr = ptr->next) { GObject *obj = ptr->data; char *str = NULL; if (gnm_object_has_readable_prop (obj, "text", G_TYPE_STRING, &str) && str) { gsf_xml_out_simple_element (state.output, "data", str); g_free (str); } else if (GNM_IS_SO_GRAPH (obj)) ssindex_chart (&state, (GogObject *)sheet_object_graph_get_gog (GNM_SO (obj))); } g_slist_free (objs); /* Various stuff in styles. */ sheet_style_foreach (state.sheet, (GFunc)cb_index_styles, &state); /* Local names. */ gnm_sheet_foreach_name (state.sheet, (GHFunc)cb_index_name, &state); } gsf_xml_out_end_element (state.output); /* </gnumeric> */ gsf_output_close (gsf_stdout); g_object_unref (gsf_stdout); g_object_unref (wb); return res; }
/* * html_file_save: * * write the html file (version of html according to version argument) */ static void html_file_save (GOFileSaver const *fs, GOIOContext *io_context, WorkbookView const *wb_view, GsfOutput *output, html_version_t version) { Workbook *wb = wb_view_get_workbook (wb_view); GOFileSaveScope save_scope; GPtrArray *sel; unsigned ui; g_return_if_fail (fs != NULL); g_return_if_fail (wb != NULL); g_return_if_fail (output != NULL); switch (version) { case HTML32: gsf_output_puts (output, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n" "<html>\n" "<head>\n\t<title>Tables</title>\n" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" "<meta name=\"generator\" content=\"Gnumeric " GNM_VERSION_FULL " via " G_PLUGIN_FOR_HTML "\">\n" "<style><!--\n" "tt {\n" "\tfont-family: courier;\n" "}\n" "td {\n" "\tfont-family: helvetica, sans-serif;\n" "}\n" "caption {\n" "\tfont-family: helvetica, sans-serif;\n" "\tfont-size: 14pt;\n" "\ttext-align: left;\n" "}\n" "--></style>\n" "</head>\n<body>\n"); break; case HTML40: gsf_output_puts (output, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" "\t\t\"http://www.w3.org/TR/html4/strict.dtd\">\n" "<html>\n" "<head>\n\t<title>Tables</title>\n" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" "<meta name=\"generator\" content=\"Gnumeric " GNM_VERSION_FULL " via " G_PLUGIN_FOR_HTML "\">\n" "<style type=\"text/css\">\n" "tt {\n" "\tfont-family: courier;\n" "}\n" "td {\n" "\tfont-family: helvetica, sans-serif;\n" "}\n" "caption {\n" "\tfont-family: helvetica, sans-serif;\n" "\tfont-size: 14pt;\n" "\ttext-align: left;\n" "}\n" "</style>\n" "</head>\n<body>\n"); break; case XHTML : gsf_output_puts (output, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" "\t\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" "<head>\n\t<title>Tables</title>\n" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" "<meta name=\"generator\" content=\"Gnumeric " GNM_VERSION_FULL " via " G_PLUGIN_FOR_HTML "\" />\n" "<style type=\"text/css\">\n" "tt {\n" "\tfont-family: courier;\n" "}\n" "td {\n" "\tfont-family: helvetica, sans-serif;\n" "}\n" "caption {\n" "\tfont-family: helvetica, sans-serif;\n" "\tfont-size: 14pt;\n" "\ttext-align: left;\n" "}\n" "</style>\n" "</head>\n<body>\n"); break; default: break; } save_scope = go_file_saver_get_save_scope (fs); sel = gnm_file_saver_get_sheets (fs, wb_view, TRUE); for (ui = 0; ui < sel->len; ui++) { Sheet *sheet = g_ptr_array_index (sel, ui); write_sheet (output, sheet, version, save_scope); } g_ptr_array_unref (sel); if (version == HTML32 || version == HTML40 || version == XHTML) gsf_output_puts (output, "</body>\n</html>\n"); }
GnumericSheetPtr gnumeric_get_sheet_by_name(GnumericWorkbookPtr workbook, const char *name) { WorkbookView *wbv = (WorkbookView *)workbook; Sheet *sheet = workbook_sheet_by_name (wb_view_get_workbook (wbv), name); return (GnumericSheetPtr*)sheet; }