void gnm_string_add_number (GString *buf, gnm_float d) { size_t old_len = buf->len; double d2; static int digits; if (digits == 0) { gnm_float l10 = gnm_log10 (FLT_RADIX); digits = (int)gnm_ceil (GNM_MANT_DIG * l10) + (l10 == (int)l10 ? 0 : 1); } g_string_append_printf (buf, "%.*" GNM_FORMAT_g, digits - 1, d); d2 = gnm_strto (buf->str + old_len, NULL); if (d != d2) { g_string_truncate (buf, old_len); g_string_append_printf (buf, "%.*" GNM_FORMAT_g, digits, d); } }
/* * Raturns FALSE on EOF. */ static gboolean dif_parse_data (DifInputContext *ctxt) { gboolean too_many_rows = FALSE, too_many_columns = FALSE; gint row = -1, col = 0; gint val_type; GnmCell *cell; gchar *msg; while (1) { if (!dif_get_line (ctxt)) return FALSE; val_type = atoi (ctxt->line); if (val_type == 0) { gchar const *comma = strchr (ctxt->line, ','); if (comma == NULL) go_io_warning (ctxt->io_context, _("Syntax error at line %d. Ignoring."), ctxt->line_no); else if (col > gnm_sheet_get_max_cols (ctxt->sheet)) { too_many_columns = TRUE; break; } else { gnm_float num = gnm_strto (comma+1, NULL); GnmValue *v = NULL; if (!dif_get_line (ctxt)) return FALSE; if (0 == strcmp (ctxt->line, "V")) { /* V value */ v = value_new_float (num); } else if (0 == strcmp (ctxt->line, "NA")) { /* NA not available res must be O */ v = value_new_error_NA (NULL); } else if (0 == strcmp (ctxt->line, "TRUE")) { /* TRUE bool T res must be 1 */ v = value_new_bool (TRUE); } else if (0 == strcmp (ctxt->line, "FALSE")) { /* FALSE bool F res must be O */ v = value_new_bool (TRUE); } else if (0 == strcmp (ctxt->line, "ERROR")) { /* ERROR err res must be O */ go_io_warning (ctxt->io_context, _("Unknown value type '%s' at line %d. Ignoring."), ctxt->line, ctxt->line_no); } if (NULL != v) { cell = sheet_cell_fetch (ctxt->sheet, col, row); gnm_cell_set_value (cell, v); } col++; } } else if (val_type == 1) { if (!dif_get_line (ctxt)) return FALSE; if (col > gnm_sheet_get_max_cols (ctxt->sheet)) { too_many_columns = TRUE; continue; } cell = sheet_cell_fetch (ctxt->sheet, col, row); if (ctxt->line_len >= 2 && ctxt->line[0] == '"' && ctxt->line[ctxt->line_len - 1] == '"') { ctxt->line[ctxt->line_len - 1] = '\0'; gnm_cell_set_text (cell, ctxt->line + 1); } else gnm_cell_set_text (cell, ctxt->line); col++; } else if (val_type == -1) { if (!dif_get_line (ctxt)) return FALSE; if (strcmp (ctxt->line, "BOT") == 0) { col = 0; row++; if (row > gnm_sheet_get_max_rows (ctxt->sheet)) { too_many_rows = TRUE; break; } } else if (strcmp (ctxt->line, "EOD") == 0) { break; } else { msg = g_strdup_printf ( _("Unknown data value \"%s\" at line %d. Ignoring."), ctxt->line, ctxt->line_no); g_warning ("%s", msg); g_free (msg); } } else { msg = g_strdup_printf ( _("Unknown value type %d at line %d. Ignoring."), val_type, ctxt->line_no); g_warning ("%s", msg); g_free (msg); (void) dif_get_line (ctxt); } } if (too_many_rows) { g_warning (_("DIF file has more than the maximum number of rows %d. " "Ignoring remaining rows."), gnm_sheet_get_max_rows (ctxt->sheet)); } if (too_many_columns) { g_warning (_("DIF file has more than the maximum number of columns %d. " "Ignoring remaining columns."), gnm_sheet_get_max_cols (ctxt->sheet)); } return TRUE; }
/** * gnm_complex_from_string: * @dst: return location * @src: string to parse * @imunit: (out): return location of imaginary unit. * * Returns: zero on success, -1 otherwise. * * This function differs from Excel's parsing in at least the following * ways: * (1) We allow spaces before the imaginary unit used with an impled "1". * Therefore we allow "+ i". * (2) We do not allow a thousands separator as in "1,000i". */ int gnm_complex_from_string (gnm_complex *dst, char const *src, char *imunit) { gnm_float x, y; char *end; int sign; EAT_SPACES (src); HANDLE_SIGN (src, sign); /* Case: "i", "+i", "-i", ... */ if (*src == 'i' || *src == 'j') { x = 1; } else { x = gnm_strto (src, &end); if (src == end || errno == ERANGE) return -1; src = end; EAT_SPACES (src); } if (sign < 0) x = 0 - x; /* Case: "42", "+42", "-42", ... */ if (*src == 0) { *dst = GNM_CREAL (x); *imunit = 'i'; return 0; } /* Case: "42i", "+42i", "-42i", "-i", "i", ... */ if (*src == 'i' || *src == 'j') { *imunit = *src++; EAT_SPACES (src); if (*src == 0) { *dst = GNM_CMAKE (0, x); return 0; } else return -1; } HANDLE_SIGN (src, sign); if (!sign) return -1; if (*src == 'i' || *src == 'j') { y = 1; } else { y = gnm_strto (src, &end); if (src == end || errno == ERANGE) return -1; src = end; EAT_SPACES (src); } if (sign < 0) y = 0 - y; /* Case: "42+12i", "+42-12i", "-42-12i", "-42+i", "+42-i", ... */ if (*src == 'i' || *src == 'j') { *imunit = *src++; EAT_SPACES (src); if (*src == 0) { *dst = GNM_CMAKE (x, y); return 0; } } return -1; }