static void dump_child (GsfInfile *infile, char const *childname) { GsfInput *child = gsf_infile_child_by_name (infile, childname); GsfInputTextline *textinput; unsigned char *res; int len = 0; if (child == NULL) { printf ("not an OpenOffice document\n"); return; } textinput = (GsfInputTextline *)gsf_input_textline_new (child); if (textinput == NULL) { printf ("Could not read lines from %s", gsf_input_name (child)); return; } do { res = gsf_input_textline_ascii_gets (textinput); if (res) { printf ("'%s'\n", res); len += strlen (res) + 1; } } while (res); printf ("Finished reading - %d bytes\n", len); g_object_unref (G_OBJECT (textinput)); g_object_unref (G_OBJECT (child)); }
GOImage * go_spectre_new_from_file (char const *filename, GError **error) { GOSpectre *spectre = g_object_new (GO_TYPE_SPECTRE, NULL); guint8 *data; GsfInput *input = gsf_input_stdio_new (filename, error); #ifdef GOFFICE_WITH_EPS int width, height; #endif GOImage *image; if (!input) return NULL; image = GO_IMAGE (spectre); image->data_length = gsf_input_size (input); data = g_malloc (image->data_length); if (!data || !gsf_input_read (input, image->data_length, data)) { g_object_unref (spectre); g_free (data); return NULL; } image->data = data; #ifdef GOFFICE_WITH_EPS spectre->doc = spectre_document_new (); if (spectre->doc == NULL) { g_object_unref (spectre); return NULL; } spectre_document_load (spectre->doc, filename); if (spectre_document_status (spectre->doc) != SPECTRE_STATUS_SUCCESS) { g_object_unref (spectre); return NULL; } spectre_document_get_page_size (spectre->doc, &width, &height); image->width = width; image->height = height; #else { GsfInput *input = gsf_input_memory_new (image->data, image->data_length, FALSE); GsfInputTextline *text = GSF_INPUT_TEXTLINE (gsf_input_textline_new (input)); guint8 *line; while ((line = gsf_input_textline_ascii_gets (text))) if (!strncmp (line, "%%BoundingBox: ", 15)) { unsigned x0, x1, y0, y1; if (sscanf (line + 15, "%u %u %u %u", &x0, &y0, &x1, &y1) == 4) { image->width = x1 - x0; image->height = y1 - y0; } else { image->width = 100; image->height = 100; } break; } g_object_unref (text); g_object_unref (input); } #endif return image; }
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 DifInputContext * dif_input_context_new (GOIOContext *io_context, Workbook *wb, GsfInput *input) { DifInputContext *ctxt = NULL; ctxt = g_new (DifInputContext, 1); ctxt->io_context = io_context; ctxt->input = (GsfInputTextline *) gsf_input_textline_new (input); ctxt->line_no = 1; ctxt->line = NULL; ctxt->sheet = workbook_sheet_add (wb, -1, GNM_DEFAULT_COLS, GNM_DEFAULT_ROWS); ctxt->converter = g_iconv_open ("UTF-8", "ISO-8859-1"); go_io_progress_message (io_context, _("Reading file...")); return ctxt; }
static int test (int argc, char *argv[]) { GsfInput *input; GsfInputTextline *textline; GError *err = NULL; unsigned char *line; if (argc < 2) { fprintf (stderr, "Usage : %s <text_filename>\n", argv[0]); return 1; } fprintf (stderr, "%s\n", argv[1]); input = gsf_input_stdio_new (argv[1], &err); if (input == NULL) { g_return_val_if_fail (err != NULL, 1); g_warning ("'%s' error: %s", argv[1], err->message); g_error_free (err); return 1; } textline = (GsfInputTextline *)gsf_input_textline_new (input); if (textline == NULL) { g_warning ("unable to create a textline"); return 2; } while (NULL != (line = gsf_input_textline_ascii_gets (textline))) puts (line); g_object_unref (G_OBJECT (input)); return 0; }
static void go_help_display (CBHelpPaths const *paths) { #ifdef GOFFICE_WITH_GNOME gnome_help_display (paths->app, paths->link, NULL); #elif defined(G_OS_WIN32) static GHashTable* context_help_map = NULL; guint id; if (!context_help_map) { GsfInput *input; GsfInputTextline *textline; GError *err = NULL; gchar *mapfile = g_strconcat (paths->app, ".hhmap", NULL); gchar *path = g_build_filename (paths->data_dir, "doc", "C", mapfile, NULL); g_free (mapfile); if (!(input = gsf_input_stdio_new (path, &err)) || !(textline = (GsfInputTextline *) gsf_input_textline_new (input))) go_gtk_notice_dialog (NULL, GTK_MESSAGE_ERROR, "Cannot open '%s': %s", path, err ? err->message : "failed to create gsf-input-textline"); else { gchar *line, *topic; gulong id, i = 1; context_help_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); while ((line = gsf_input_textline_utf8_gets (textline)) != NULL) { if (!(id = strtoul (line, &topic, 10))) { go_gtk_notice_dialog (NULL, GTK_MESSAGE_ERROR, "Invalid topic id at line %lu in %s: '%s'", i, path, line); continue; } for (; *topic == ' '; ++topic); g_hash_table_insert (context_help_map, g_strdup (topic), (gpointer)id); } g_object_unref (G_OBJECT (textline)); } if (input) g_object_unref (G_OBJECT (input)); g_free (path); } if (!(id = (guint) g_hash_table_lookup (context_help_map, paths->link))) go_gtk_notice_dialog (NULL, GTK_MESSAGE_ERROR, "Topic '%s' not found.", paths->link); else { gchar *chmfile = g_strconcat (paths->app, ".chm", NULL); gchar *path = g_build_filename (paths->data_dir, "doc", "C", chmfile, NULL); g_free (chmfile); if (!HtmlHelp (GetDesktopWindow (), path, HH_HELP_CONTEXT, id)) go_gtk_notice_dialog (NULL, GTK_MESSAGE_ERROR, "Failed to spawn HtmlHelp"); g_free (path); } #else go_gtk_notice_dialog (NULL, GTK_MESSAGE_ERROR, "TODO : launch help browser for %s", paths->link); #endif }
static void gnm_glpk_read_solution (GnmGlpk *lp) { GnmSubSolver *subsol = lp->parent; GnmSolver *sol = GNM_SOLVER (subsol); GsfInput *input; GsfInputTextline *tl = NULL; const char *line; GnmSolverResult *result = NULL; GnmSolverSensitivity *sensitivity = NULL; enum { SEC_UNKNOWN, SEC_ROWS, SEC_COLUMNS } state; gboolean has_integer; GSList *l; input = gsf_input_stdio_new (lp->result_filename, NULL); if (!input) goto fail; tl = GSF_INPUT_TEXTLINE (gsf_input_textline_new (input)); g_object_unref (input); result = g_object_new (GNM_SOLVER_RESULT_TYPE, NULL); result->solution = g_new0 (gnm_float, sol->input_cells->len); sensitivity = gnm_solver_sensitivity_new (sol); /* * glpsol's output format is different if there are any integer * constraint. Go figure. */ has_integer = sol->params->options.assume_discrete; for (l = sol->params->constraints; !has_integer && l; l = l->next) { GnmSolverConstraint *c = l->data; has_integer = (c->type == GNM_SOLVER_INTEGER || c->type == GNM_SOLVER_BOOLEAN); } switch (gnm_glpk_detect_version (lp, tl)) { case GLPK_457: if (gnm_glpk_read_solution_457 (lp, tl, result, sensitivity, has_integer)) goto fail; break; case GLPK_458: if (gnm_glpk_read_solution_458 (lp, tl, result, sensitivity, has_integer)) goto fail; break; default: goto fail; } g_object_unref (tl); tl = NULL; // ---------------------------------------- if (!lp->ranges_filename) goto done; input = gsf_input_stdio_new (lp->ranges_filename, NULL); if (!input) goto fail; tl = GSF_INPUT_TEXTLINE (gsf_input_textline_new (input)); g_object_unref (input); state = SEC_UNKNOWN; // We are reading a file intended for human consumption. // That is unfortunately because it implies rounding, for example. // The information does not appear to be available elsewhere. while ((line = gsf_input_textline_utf8_gets (tl)) != NULL) { gchar **items, **items2 = NULL; int len, len2 = 0; if (g_str_has_prefix (line, " No. Row name")) { state = SEC_ROWS; continue; } else if (g_str_has_prefix (line, " No. Column name")) { state = SEC_COLUMNS; continue; } else if (g_ascii_isalpha (line[0])) { state = SEC_UNKNOWN; continue; } if (state == SEC_UNKNOWN) continue; items = my_strsplit (line); len = g_strv_length (items); if (len == 10 && g_ascii_isdigit (items[0][0])) { line = gsf_input_textline_utf8_gets (tl); if (line) { items2 = my_strsplit (line); len2 = g_strv_length (items2); } } if (len == 10 && len2 == 6 && state == SEC_COLUMNS) { gnm_float low = parse_number (items[7]); gnm_float high = parse_number (items2[3]); GnmCell const *cell = gnm_sub_solver_find_cell (lp->parent, items[1]); int idx = gnm_solver_cell_index (sol, cell); if (idx >= 0) { sensitivity->vars[idx].low = low; sensitivity->vars[idx].high = high; } } if (len == 10 && len2 == 6 && state == SEC_ROWS) { gnm_float low = parse_number (items[6]); gnm_float high = parse_number (items2[2]); int cidx = gnm_sub_solver_find_constraint (lp->parent, items[1]); if (cidx >= 0) { sensitivity->constraints[cidx].low = low; sensitivity->constraints[cidx].high = high; } } g_strfreev (items); g_strfreev (items2); } g_object_unref (tl); // ---------------------------------------- done: gnm_solver_set_status (sol, GNM_SOLVER_STATUS_DONE); g_object_set (subsol, "result", result, NULL); g_object_unref (result); g_object_set (subsol, "sensitivity", sensitivity, NULL); g_object_unref (sensitivity); return; fail: if (tl) g_object_unref (tl); if (result) g_object_unref (result); if (sensitivity) g_object_unref (sensitivity); gnm_solver_set_status (sol, GNM_SOLVER_STATUS_ERROR); }