Commit b169366d authored by Morten Welinder's avatar Morten Welinder Committed by Morten Welinder

New function.

2002-12-15  Morten Welinder  <terra@diku.dk>

	* src/expr.c (gnm_expr_as_gstring): New function.

	* src/rendered-value.c (rendered_value_new): Use
	gnm_expr_as_gstring to avoid extra copy of result.  Use
	string_get_nocopy to avoid yet another copy.
	(cell_get_entered_text): Use gnm_expr_as_gstring to avoid extra
	copy of result.


plus parts of this one:

2002-12-13  Morten Welinder  <terra@diku.dk>

	* src/value.c (value_get_as_gstring): New function.
	(value_get_as_string): Use that.

	* src/expr.c (do_expr_as_string): Use value_get_as_gstring.

	* src/xml-io.c (gnumeric_xml_read_workbook): Grammar fix.

	* src/parse-util.h (struct _GnmExprConventions): New members
 	expr_name_handler, output_sheet_name_sep, output_argument_sep,
 	output_array_col_sep, and output_translated.  Eliminate
 	use_locale_C.

	* src/expr.c (do_expr_as_string): Use new expr_name_handler in
	conventions.

	* src/parse-util.c (def_expr_name_handler): New function, mostly
	extracted from do_expr_as_string.
	(gnm_expr_conventions_new): Attach def_expr_name_handler.
parent bd37dc18
2002-12-15 Morten Welinder <terra@diku.dk>
* src/expr.c (gnm_expr_as_gstring): New function.
* src/rendered-value.c (rendered_value_new): Use
gnm_expr_as_gstring to avoid extra copy of result. Use
string_get_nocopy to avoid yet another copy.
(cell_get_entered_text): Use gnm_expr_as_gstring to avoid extra
copy of result.
2002-12-14 Jody Goldberg <jody@gnome.org>
* src/workbook-control-gui.c (cb_auto_filter) : It makes no sense to
......@@ -27,12 +37,19 @@
2002-12-13 Morten Welinder <terra@diku.dk>
* src/value.c (value_get_as_gstring): New function.
(value_get_as_string): Use that.
* src/expr.c (do_expr_as_string): Use value_get_as_gstring.
* src/xml-io.c (gnumeric_xml_read_workbook): Grammar fix.
* src/parse-util.h (struct _GnmExprConventions): New members
expr_name_handler, output_sheet_name_sep, and output_argument_sep.
expr_name_handler, output_sheet_name_sep, output_argument_sep,
output_array_col_sep, and output_translated. Eliminate
use_locale_C.
* src/expr.c (do_expr_as_string): Use new expr_name_handler is
* src/expr.c (do_expr_as_string): Use new expr_name_handler in
conventions.
* src/parse-util.c (def_expr_name_handler): New function, mostly
......
2002-12-15 Morten Welinder <terra@diku.dk>
* src/expr.c (gnm_expr_as_gstring): New function.
* src/rendered-value.c (rendered_value_new): Use
gnm_expr_as_gstring to avoid extra copy of result. Use
string_get_nocopy to avoid yet another copy.
(cell_get_entered_text): Use gnm_expr_as_gstring to avoid extra
copy of result.
2002-12-14 Jody Goldberg <jody@gnome.org>
* src/workbook-control-gui.c (cb_auto_filter) : It makes no sense to
......@@ -27,12 +37,19 @@
2002-12-13 Morten Welinder <terra@diku.dk>
* src/value.c (value_get_as_gstring): New function.
(value_get_as_string): Use that.
* src/expr.c (do_expr_as_string): Use value_get_as_gstring.
* src/xml-io.c (gnumeric_xml_read_workbook): Grammar fix.
* src/parse-util.h (struct _GnmExprConventions): New members
expr_name_handler, output_sheet_name_sep, and output_argument_sep.
expr_name_handler, output_sheet_name_sep, output_argument_sep,
output_array_col_sep, and output_translated. Eliminate
use_locale_C.
* src/expr.c (do_expr_as_string): Use new expr_name_handler is
* src/expr.c (do_expr_as_string): Use new expr_name_handler in
conventions.
* src/parse-util.c (def_expr_name_handler): New function, mostly
......
2002-12-15 Morten Welinder <terra@diku.dk>
* src/expr.c (gnm_expr_as_gstring): New function.
* src/rendered-value.c (rendered_value_new): Use
gnm_expr_as_gstring to avoid extra copy of result. Use
string_get_nocopy to avoid yet another copy.
(cell_get_entered_text): Use gnm_expr_as_gstring to avoid extra
copy of result.
2002-12-14 Jody Goldberg <jody@gnome.org>
* src/workbook-control-gui.c (cb_auto_filter) : It makes no sense to
......@@ -27,12 +37,19 @@
2002-12-13 Morten Welinder <terra@diku.dk>
* src/value.c (value_get_as_gstring): New function.
(value_get_as_string): Use that.
* src/expr.c (do_expr_as_string): Use value_get_as_gstring.
* src/xml-io.c (gnumeric_xml_read_workbook): Grammar fix.
* src/parse-util.h (struct _GnmExprConventions): New members
expr_name_handler, output_sheet_name_sep, and output_argument_sep.
expr_name_handler, output_sheet_name_sep, output_argument_sep,
output_array_col_sep, and output_translated. Eliminate
use_locale_C.
* src/expr.c (do_expr_as_string): Use new expr_name_handler is
* src/expr.c (do_expr_as_string): Use new expr_name_handler in
conventions.
* src/parse-util.c (def_expr_name_handler): New function, mostly
......
......@@ -826,7 +826,11 @@ oo_conventions (void)
GnmExprConventions *res = gnm_expr_conventions_new ();
res->decode_ampersands = TRUE;
res->use_locale_C = TRUE;
res->decimal_sep_dot = TRUE;
res->argument_sep_semicolon = TRUE;
res->array_col_sep_comma = TRUE;
res->output_argument_sep = ";";
res->output_array_col_sep = ",";
res->unknown_function_handler = gnm_func_placeholder_factory;
res->ref_parser = oo_rangeref_parse;
......
......@@ -1214,9 +1214,8 @@ do_expr_as_string (GString *target, GnmExpr const *expr, ParsePos const *pp,
}
case GNM_EXPR_OP_CONSTANT: {
const char *res;
Value const *v = expr->constant.value;
gboolean need_par;
size_t prelen = target->len;
if (v->type == VALUE_STRING) {
gnm_strescape (target, v->v_str.val->str);
......@@ -1230,19 +1229,22 @@ do_expr_as_string (GString *target, GnmExpr const *expr, ParsePos const *pp,
return;
}
res = value_peek_string (v);
value_get_as_gstring (target, v, conv);
/* If the number has a sign, pretend that it is the result of
* OPER_UNARY_{NEG,PLUS}. It is not clear how we would
* currently get negative numbers here, but some loader might
* do it.
*/
need_par = (res[0] == '-' || res[0] == '+') &&
operations[GNM_EXPR_OP_UNARY_NEG].prec <= paren_level;
if (need_par) g_string_append_c (target, '(');
g_string_append (target, res);
if (need_par) g_string_append_c (target, ')');
if ((target->str[prelen] == '-' || target->str[prelen] == '+') &&
operations[GNM_EXPR_OP_UNARY_NEG].prec <= paren_level) {
/* Add ")" and make room for "(". */
g_string_append (target, ")X");
g_memmove (target->str + prelen,
target->str + prelen + 1,
target->len - prelen - 1);
target->str[prelen] = '(';
}
return;
}
......@@ -1293,6 +1295,17 @@ gnm_expr_as_string (GnmExpr const *expr, ParsePos const *pp,
return g_string_free (res, FALSE);
}
void
gnm_expr_as_gstring (GString *target,
GnmExpr const *expr, ParsePos const *pp,
const GnmExprConventions *fmt)
{
g_return_if_fail (expr != NULL);
g_return_if_fail (pp != NULL);
do_expr_as_string (target, expr, pp, 0, fmt);
}
typedef enum {
CELLREF_NO_RELOCATE,
CELLREF_RELOCATE_FROM_IN,
......
......@@ -66,7 +66,10 @@ gboolean gnm_expr_is_shared (GnmExpr const *expr);
gboolean gnm_expr_is_rangeref (GnmExpr const *expr);
gboolean gnm_expr_is_err (GnmExpr const *expr, char const *err);
gboolean gnm_expr_equal (GnmExpr const *a, GnmExpr const *b);
char *gnm_expr_as_string (GnmExpr const *expr, ParsePos const *fp,
char *gnm_expr_as_string (GnmExpr const *expr, ParsePos const *pp,
const GnmExprConventions *fmt);
void gnm_expr_as_gstring (GString *target,
GnmExpr const *expr, ParsePos const *pp,
const GnmExprConventions *fmt);
void gnm_expr_get_boundingbox (GnmExpr const *expr, Range *bound);
GSList *gnm_expr_referenced_sheets (GnmExpr const *expr);
......
......@@ -978,6 +978,7 @@ def_expr_name_handler (GString *target,
const GnmNamedExpr *thename = name->name;
if (!thename->active) {
/* FIXME! Always saved translated. */
g_string_append (target, gnumeric_err_REF);
return;
}
......@@ -997,7 +998,7 @@ def_expr_name_handler (GString *target,
g_string_append (target, thename->pos.sheet->name_quoted);
g_string_append (target, conv->output_sheet_name_sep);
}
g_string_append (target, thename->name->str);
}
......@@ -1010,6 +1011,7 @@ gnm_expr_conventions_new (void)
res->expr_name_handler = def_expr_name_handler;
res->output_sheet_name_sep = "!";
res->output_translated = TRUE;
return res;
}
......
......@@ -120,8 +120,14 @@ struct _GnmExprConventions {
/* Formerly part of USE_OPENCALC_CONVENTIONS. */
gboolean decode_ampersands;
/* Formerly part of USE_OPENCALC_CONVENTIONS. */
gboolean use_locale_C;
/* Is the decimal separator "." (as opposed to locale's)? */
gboolean decimal_sep_dot;
/* Is the argument separator ";" (as opposed to locale's)? */
gboolean argument_sep_semicolon;
/* Is the array column separator "," (as opposed to locale's)? */
gboolean array_col_sep_comma;
/* Accept prefix #NOT# and infixs #AND# and #OR#. */
gboolean accept_hash_logicals;
......@@ -149,6 +155,11 @@ struct _GnmExprConventions {
/* If non-null, used to separate elements in lists. */
const char *output_argument_sep;
/* If non-null, used to separate array columns. */
const char *output_array_col_sep;
gboolean output_translated;
};
GnmExprConventions *gnm_expr_conventions_new (void);
......
......@@ -1238,15 +1238,15 @@ gnm_expr_parse_str (char const *expr_text, ParsePos const *pos,
pstate.unknown_names_are_strings = flags & GNM_EXPR_PARSE_UNKNOWN_NAMES_ARE_STRINGS;
pstate.convs = convs;
if (convs->use_locale_C) {
pstate.decimal_point = '.';
pstate.separator = ';';
pstate.array_col_separator = ',';
} else {
pstate.decimal_point = format_get_decimal ();
pstate.separator = format_get_arg_sep ();
pstate.array_col_separator = format_get_col_sep ();
}
pstate.decimal_point = convs->decimal_sep_dot
? '.'
: format_get_decimal ();
pstate.separator = convs->argument_sep_semicolon
? ';'
: format_get_arg_sep ();
pstate.array_col_separator = convs->array_col_sep_comma
? ','
: format_get_col_sep ();
pstate.result = NULL;
pstate.error = error;
......
......@@ -83,11 +83,12 @@ rendered_value_new (Cell *cell, MStyle const *mstyle, gboolean dynamic_width)
if (cell_has_expr (cell) && sheet != NULL && sheet->display_formulas) {
ParsePos pp;
char *tmpstr = gnm_expr_as_string (cell->base.expression,
parse_pos_init_cell (&pp, cell),
gnm_expr_conventions_default);
str = g_strconcat ("=", tmpstr, NULL);
g_free (tmpstr);
GString *gstr = g_string_new ("=");
gnm_expr_as_gstring (gstr, cell->base.expression,
parse_pos_init_cell (&pp, cell),
gnm_expr_conventions_default);
str = g_string_free (gstr, FALSE);
color = NULL;
dynamic_width = FALSE;
} else if (mstyle_is_element_set (mstyle, MSTYLE_FORMAT)) {
......@@ -141,11 +142,10 @@ rendered_value_new (Cell *cell, MStyle const *mstyle, gboolean dynamic_width)
g_return_val_if_fail (str != NULL, NULL);
res = CHUNK_ALLOC (RenderedValue, rendered_value_pool);
res->rendered_text = string_get (str);
res->rendered_text = string_get_nocopy (str);
res->render_color = color;
res->width_pixel = res->height_pixel = res->offset_pixel = 0;
res->dynamic_width = dynamic_width;
g_free (str);
return res;
}
......@@ -363,16 +363,13 @@ cell_get_entered_text (Cell const *cell)
g_return_val_if_fail (cell != NULL, NULL);
if (cell_has_expr (cell)) {
char *func, *ret;
ParsePos pp;
GString *res = g_string_new ("=");
func = gnm_expr_as_string (cell->base.expression,
parse_pos_init_cell (&pp, cell),
gnm_expr_conventions_default);
ret = g_strconcat ("=", func, NULL);
g_free (func);
return ret;
gnm_expr_as_gstring (res, cell->base.expression,
parse_pos_init_cell (&pp, cell),
gnm_expr_conventions_default);
return g_string_free (res, FALSE);
}
if (cell->value != NULL) {
......
......@@ -625,83 +625,119 @@ value_get_as_checked_bool (Value const *v)
return result;
}
/**
* value_get_as_string :
* @v :
*
* simplistic value rendering
*
* Returns a string that must be freed.
*/
char *
value_get_as_string (Value const *v)
void
value_get_as_gstring (GString *target, Value const *v,
const GnmExprConventions *conv)
{
if (v == NULL)
return g_strdup ("");
return;
switch (v->type){
case VALUE_EMPTY:
return g_strdup ("");
return;
case VALUE_ERROR:
return g_strdup (v->v_err.mesg->str);
/* FIXME: conv */
g_string_append (target, v->v_err.mesg->str);
return;
case VALUE_BOOLEAN:
return g_strdup (v->v_bool.val ? _("TRUE") : _("FALSE"));
case VALUE_BOOLEAN: {
const char *cval = v->v_bool.val ? "TRUE" : "FALSE";
g_string_append (target, conv->output_translated ? _(cval) : cval);
return;
}
case VALUE_STRING:
return g_strdup (v->v_str.val->str);
g_string_append (target, v->v_str.val->str);
return;
case VALUE_INTEGER:
return g_strdup_printf ("%d", v->v_int.val);
g_string_append_printf (target, "%d", v->v_int.val);
return;
case VALUE_FLOAT:
return g_strdup_printf ("%.*" GNUM_FORMAT_g, GNUM_DIG, v->v_float.val);
g_string_append_printf (target, "%.*" GNUM_FORMAT_g, GNUM_DIG,
v->v_float.val);
return;
case VALUE_ARRAY: {
char const row_sep = format_get_arg_sep ();
char const col_sep = format_get_col_sep ();
GString *str = g_string_new ("{");
const char *row_sep, *col_sep;
char locale_arg_sep[2], locale_col_sep[2];
int x, y;
char *ans;
if (conv->output_argument_sep)
row_sep = conv->output_argument_sep;
else {
locale_arg_sep[0] = format_get_arg_sep ();
locale_arg_sep[1] = 0;
row_sep = locale_arg_sep;
}
if (conv->output_array_col_sep)
col_sep = conv->output_array_col_sep;
else {
locale_col_sep[0] = format_get_col_sep ();
locale_col_sep[1] = 0;
col_sep = locale_col_sep;
}
g_string_append_c (target, '{');
for (y = 0; y < v->v_array.y; y++){
if (y)
g_string_append (target, col_sep);
for (x = 0; x < v->v_array.x; x++){
Value const *val = v->v_array.vals[x][y];
if (x)
g_string_append_c (str, row_sep);
g_string_append (target, row_sep);
/* quote strings */
if (val->type == VALUE_STRING)
gnm_strescape (str, val->v_str.val->str);
gnm_strescape (target, val->v_str.val->str);
else
g_string_append (str, value_peek_string (val));
value_get_as_gstring (target, val, conv);
}
if (y < v->v_array.y-1)
g_string_append_c (str, col_sep);
}
g_string_append_printf (str, "}");
ans = str->str;
g_string_free (str, FALSE);
return ans;
g_string_append_c (target, '}');
return;
}
case VALUE_CELLRANGE: {
char *tmp;
/* Note: this makes only sense for absolute references or
* references relative to A1
*/
Range range;
range_init_value (&range, v);
return global_range_name (v->v_range.cell.a.sheet, &range);;
tmp = global_range_name (v->v_range.cell.a.sheet, &range);;
g_string_append (target, tmp);
g_free (tmp);
return;
}
default:
g_warning ("value_get_as_string problem.");
break;
}
return g_strdup ("Internal problem.");
g_assert_not_reached ();
}
/**
* value_get_as_string :
* @v :
*
* simplistic value rendering
*
* Returns a string that must be freed.
*/
char *
value_get_as_string (Value const *v)
{
GString *res = g_string_sized_new (10);
value_get_as_gstring (res, v, gnm_expr_conventions_default);
return g_string_free (res, FALSE);
}
/*
......
......@@ -126,6 +126,8 @@ guint value_hash (Value const *v);
gboolean value_get_as_bool (Value const *v, gboolean *err);
gboolean value_get_as_checked_bool (Value const *v);
void value_get_as_gstring (GString *target, Value const *v,
const GnmExprConventions *conv);
char *value_get_as_string (Value const *v);
char const *value_peek_string (Value const *v);
int value_get_as_int (Value const *v);
......
......@@ -1635,11 +1635,10 @@ xml_write_cell_and_position (XmlParseContext *ctxt, Cell const *cell, ParsePos c
if (write_contents) {
if (cell_has_expr (cell)) {
char *tmp;
tmp = gnm_expr_as_string (cell->base.expression, pp, gnm_expr_conventions_default_1_0);
text = C2XML (g_strconcat ("=", tmp, NULL));
g_free (tmp);
GString *str = g_string_sized_new (1000);
g_string_append_c (str, '=');
gnm_expr_as_gstring (str, cell->base.expression, pp, gnm_expr_conventions_default_1_0);
text = C2XML (g_string_free (str, FALSE));
} else
text = C2XML (value_get_as_string (cell->value));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment