Commit 87eb4c0b authored by Morten Welinder's avatar Morten Welinder Committed by Morten Welinder

Make public.

2001-05-04  Morten Welinder  <terra@diku.dk>

	* src/analysis-tools.c (set_cell_value): Make public.

	* src/solver-lp.c (solver_sensitivity_report,
 	solver_answer_report): Use set_cell_value.

	* src/value.c (value_peek_string): New function.

	* src/expr.c (eval_expr_real): Simplify CONCAT case using
 	value_peek_string.

	* src/datetime.c (datetime_value_to_serial_raw): Simplify using
 	value_peek_string.

	* src/workbook-view.c (wb_view_auto_expr_recalc): Ditto.

	* src/graph-vector.c (graph_vector_seq_string): Ditto.

	* src/solver-lp.c (find_name): Ditto.

2001-05-04  Morten Welinder  <terra@diku.dk>

	* fn-string.c (gnumeric_exact, gnumeric_len, gnumeric_rept,
 	gnumeric_find): Ditto.

	* fn-eng.c (value_get_as_complex): Simplify using
 	value_peek_string.
parent 6557b2b7
2001-05-04 Morten Welinder <terra@diku.dk>
* src/analysis-tools.c (set_cell_value): Make public.
* src/solver-lp.c (solver_sensitivity_report,
solver_answer_report): Use set_cell_value.
* src/value.c (value_peek_string): New function.
* src/expr.c (eval_expr_real): Simplify CONCAT case using
value_peek_string.
* src/datetime.c (datetime_value_to_serial_raw): Simplify using
value_peek_string.
* src/workbook-view.c (wb_view_auto_expr_recalc): Ditto.
* src/graph-vector.c (graph_vector_seq_string): Ditto.
* src/solver-lp.c (find_name): Ditto.
2001-05-04 Almer S. Tigelaar <almer@gnome.org>
* src/GNOME_Gnumeric.xml: Correct InsertComment
......
2001-05-04 Morten Welinder <terra@diku.dk>
* src/analysis-tools.c (set_cell_value): Make public.
* src/solver-lp.c (solver_sensitivity_report,
solver_answer_report): Use set_cell_value.
* src/value.c (value_peek_string): New function.
* src/expr.c (eval_expr_real): Simplify CONCAT case using
value_peek_string.
* src/datetime.c (datetime_value_to_serial_raw): Simplify using
value_peek_string.
* src/workbook-view.c (wb_view_auto_expr_recalc): Ditto.
* src/graph-vector.c (graph_vector_seq_string): Ditto.
* src/solver-lp.c (find_name): Ditto.
2001-05-04 Almer S. Tigelaar <almer@gnome.org>
* src/GNOME_Gnumeric.xml: Correct InsertComment
......
2001-05-04 Morten Welinder <terra@diku.dk>
* src/analysis-tools.c (set_cell_value): Make public.
* src/solver-lp.c (solver_sensitivity_report,
solver_answer_report): Use set_cell_value.
* src/value.c (value_peek_string): New function.
* src/expr.c (eval_expr_real): Simplify CONCAT case using
value_peek_string.
* src/datetime.c (datetime_value_to_serial_raw): Simplify using
value_peek_string.
* src/workbook-view.c (wb_view_auto_expr_recalc): Ditto.
* src/graph-vector.c (graph_vector_seq_string): Ditto.
* src/solver-lp.c (find_name): Ditto.
2001-05-04 Almer S. Tigelaar <almer@gnome.org>
* src/GNOME_Gnumeric.xml: Correct InsertComment
......
2001-05-04 Morten Welinder <terra@diku.dk>
* src/analysis-tools.c (set_cell_value): Make public.
* src/solver-lp.c (solver_sensitivity_report,
solver_answer_report): Use set_cell_value.
* src/value.c (value_peek_string): New function.
* src/expr.c (eval_expr_real): Simplify CONCAT case using
value_peek_string.
* src/datetime.c (datetime_value_to_serial_raw): Simplify using
value_peek_string.
* src/workbook-view.c (wb_view_auto_expr_recalc): Ditto.
* src/graph-vector.c (graph_vector_seq_string): Ditto.
* src/solver-lp.c (find_name): Ditto.
2001-05-04 Almer S. Tigelaar <almer@gnome.org>
* src/GNOME_Gnumeric.xml: Correct InsertComment
......
2001-05-04 Morten Welinder <terra@diku.dk>
* src/analysis-tools.c (set_cell_value): Make public.
* src/solver-lp.c (solver_sensitivity_report,
solver_answer_report): Use set_cell_value.
* src/value.c (value_peek_string): New function.
* src/expr.c (eval_expr_real): Simplify CONCAT case using
value_peek_string.
* src/datetime.c (datetime_value_to_serial_raw): Simplify using
value_peek_string.
* src/workbook-view.c (wb_view_auto_expr_recalc): Ditto.
* src/graph-vector.c (graph_vector_seq_string): Ditto.
* src/solver-lp.c (find_name): Ditto.
2001-05-04 Almer S. Tigelaar <almer@gnome.org>
* src/GNOME_Gnumeric.xml: Correct InsertComment
......
......@@ -601,13 +601,9 @@ value_get_as_complex (Value *val, complex_t *res, char *imunit)
*imunit = 'i';
return 0;
} else {
char *s;
int err;
s = value_get_as_string (val);
err = complex_from_string (res, s, imunit);
g_free (s);
return err;
return complex_from_string (res,
value_peek_string (val),
imunit);
}
}
......
......@@ -91,16 +91,8 @@ static char *help_exact = {
static Value *
gnumeric_exact (FunctionEvalInfo *ei, Value **argv)
{
char *s0, *s1;
Value *res;
s0 = value_get_as_string (argv[0]);
s1 = value_get_as_string (argv[1]);
res = value_new_bool (strcmp (s0, s1) == 0);
g_free (s0);
g_free (s1);
return res;
return value_new_bool (strcmp (value_peek_string (argv[0]),
value_peek_string (argv[1])) == 0);
}
/***************************************************************************/
......@@ -121,14 +113,7 @@ static char *help_len = {
static Value *
gnumeric_len (FunctionEvalInfo *ei, Value **argv)
{
char *s;
Value *res;
s = value_get_as_string (argv[0]);
res = value_new_int (strlen (s));
g_free (s);
return res;
return value_new_int (strlen (value_peek_string (argv[0])));
}
/***************************************************************************/
......@@ -382,35 +367,30 @@ static Value *
gnumeric_rept (FunctionEvalInfo *ei, Value **argv)
{
Value *v;
char *s, *p, *source;
char *s, *p;
const char *source;
int num;
int len;
num = value_get_as_int (argv[1]);
if (num < 0)
return value_new_error (ei->pos, gnumeric_err_VALUE);
source = value_get_as_string (argv[0]);
source = value_peek_string (argv[0]);
len = strlen (source);
/* Fast special case. =REPT ("",2^30) should not take long. */
if (len == 0 || num == 0) {
g_free (source);
if (len == 0 || num == 0)
return value_new_string ("");
}
/* Check if the length would overflow. */
if (num >= INT_MAX / len) {
g_free (source);
if (num >= INT_MAX / len)
return value_new_error (ei->pos, gnumeric_err_VALUE);
}
p = s = g_new (gchar, 1 + len * num);
if (!p) {
g_free (source);
if (!p)
/* FIXME: this and above case should probably have the
same error message. */
return value_new_error (ei->pos, _("Out of memory"));
}
while (num--) {
memcpy (p, source, len);
......@@ -419,7 +399,6 @@ gnumeric_rept (FunctionEvalInfo *ei, Value **argv)
*p = '\0';
v = value_new_string (s);
g_free (s);
g_free (source);
return v;
}
......@@ -475,31 +454,25 @@ static Value *
gnumeric_find (FunctionEvalInfo *ei, Value **argv)
{
int count, haystacksize;
char *haystack, *needle;
Value *res;
const char *haystack, *needle;
needle = value_get_as_string (argv[0]);
haystack = value_get_as_string (argv[1]);
needle = value_peek_string (argv[0]);
haystack = value_peek_string (argv[1]);
count = argv[2] ? value_get_as_int (argv[2]) : 1;
haystacksize = strlen (haystack);
if (count <= 0 || count > haystacksize) {
res = value_new_error (ei->pos, gnumeric_err_VALUE);
return value_new_error (ei->pos, gnumeric_err_VALUE);
} else {
const char *haystart = haystack + (count - 1);
const char *p = strstr (haystart, needle);
if (p)
res = value_new_int (count + (p - haystart));
return value_new_int (count + (p - haystart));
else
/* Really? */
res = value_new_error (ei->pos, gnumeric_err_VALUE);
return value_new_error (ei->pos, gnumeric_err_VALUE);
}
g_free (needle);
g_free (haystack);
return res;
}
/***************************************************************************/
......
......@@ -75,7 +75,7 @@ float_compare_desc (const gnum_float *a, const gnum_float *b)
return -1;
}
static void
void
set_cell_value (data_analysis_output_t *dao, int col, int row, Value *v)
{
Cell *cell;
......
......@@ -94,6 +94,7 @@ void set_cell (data_analysis_output_t *dao, int col, int row, const char
void set_cell_printf (data_analysis_output_t *dao,
int col, int row, const char *fmt, ...)
G_GNUC_PRINTF (4, 5);
void set_cell_value (data_analysis_output_t *dao, int col, int row, Value *v);
void set_cell_float (data_analysis_output_t *dao,
int col, int row, gnum_float v);
void set_cell_int (data_analysis_output_t *dao,
......
......@@ -77,7 +77,7 @@ datetime_value_to_serial_raw (const Value *v)
if (VALUE_IS_NUMBER (v))
serial = value_get_as_float (v);
else {
char *str = value_get_as_string (v);
const char *str = value_peek_string (v);
Value *conversion = format_match (str, NULL, NULL);
if (conversion) {
......@@ -88,7 +88,6 @@ datetime_value_to_serial_raw (const Value *v)
value_release (conversion);
} else
serial = 0;
g_free (str);
}
return serial;
}
......
......@@ -773,7 +773,7 @@ eval_expr_real (EvalPos const *pos, ExprTree const *tree,
return res;
case OPER_CONCAT: {
char *sa, *sb, *tmp;
char *tmp;
a = eval_expr_real (pos, tree->binary.value_a, flags);
if (a != NULL && a->type == VALUE_ERROR)
......@@ -785,19 +785,15 @@ eval_expr_real (EvalPos const *pos, ExprTree const *tree,
return b;
}
sa = value_get_as_string (a);
sb = value_get_as_string (b);
tmp = g_strconcat (sa, sb, NULL);
tmp = g_strconcat (value_peek_string (a),
value_peek_string (b), NULL);
res = value_new_string (tmp);
g_free (sa);
g_free (sb);
g_free (tmp);
if (a != NULL)
value_release (a);
value_release (a);
if (b != NULL)
value_release (b);
value_release (b);
return res;
}
......
2001-05-04 Morten Welinder <terra@diku.dk>
* fn-string.c (gnumeric_exact, gnumeric_len, gnumeric_rept,
gnumeric_find): Ditto.
* fn-eng.c (value_get_as_complex): Simplify using
value_peek_string.
* fn-string.c (gnumeric_len, gnumeric_exact, gnumeric_trim):
Cleanup.
(gnumeric_rept): Un-suckify.
......
......@@ -601,13 +601,9 @@ value_get_as_complex (Value *val, complex_t *res, char *imunit)
*imunit = 'i';
return 0;
} else {
char *s;
int err;
s = value_get_as_string (val);
err = complex_from_string (res, s, imunit);
g_free (s);
return err;
return complex_from_string (res,
value_peek_string (val),
imunit);
}
}
......
......@@ -91,16 +91,8 @@ static char *help_exact = {
static Value *
gnumeric_exact (FunctionEvalInfo *ei, Value **argv)
{
char *s0, *s1;
Value *res;
s0 = value_get_as_string (argv[0]);
s1 = value_get_as_string (argv[1]);
res = value_new_bool (strcmp (s0, s1) == 0);
g_free (s0);
g_free (s1);
return res;
return value_new_bool (strcmp (value_peek_string (argv[0]),
value_peek_string (argv[1])) == 0);
}
/***************************************************************************/
......@@ -121,14 +113,7 @@ static char *help_len = {
static Value *
gnumeric_len (FunctionEvalInfo *ei, Value **argv)
{
char *s;
Value *res;
s = value_get_as_string (argv[0]);
res = value_new_int (strlen (s));
g_free (s);
return res;
return value_new_int (strlen (value_peek_string (argv[0])));
}
/***************************************************************************/
......@@ -382,35 +367,30 @@ static Value *
gnumeric_rept (FunctionEvalInfo *ei, Value **argv)
{
Value *v;
char *s, *p, *source;
char *s, *p;
const char *source;
int num;
int len;
num = value_get_as_int (argv[1]);
if (num < 0)
return value_new_error (ei->pos, gnumeric_err_VALUE);
source = value_get_as_string (argv[0]);
source = value_peek_string (argv[0]);
len = strlen (source);
/* Fast special case. =REPT ("",2^30) should not take long. */
if (len == 0 || num == 0) {
g_free (source);
if (len == 0 || num == 0)
return value_new_string ("");
}
/* Check if the length would overflow. */
if (num >= INT_MAX / len) {
g_free (source);
if (num >= INT_MAX / len)
return value_new_error (ei->pos, gnumeric_err_VALUE);
}
p = s = g_new (gchar, 1 + len * num);
if (!p) {
g_free (source);
if (!p)
/* FIXME: this and above case should probably have the
same error message. */
return value_new_error (ei->pos, _("Out of memory"));
}
while (num--) {
memcpy (p, source, len);
......@@ -419,7 +399,6 @@ gnumeric_rept (FunctionEvalInfo *ei, Value **argv)
*p = '\0';
v = value_new_string (s);
g_free (s);
g_free (source);
return v;
}
......@@ -475,31 +454,25 @@ static Value *
gnumeric_find (FunctionEvalInfo *ei, Value **argv)
{
int count, haystacksize;
char *haystack, *needle;
Value *res;
const char *haystack, *needle;
needle = value_get_as_string (argv[0]);
haystack = value_get_as_string (argv[1]);
needle = value_peek_string (argv[0]);
haystack = value_peek_string (argv[1]);
count = argv[2] ? value_get_as_int (argv[2]) : 1;
haystacksize = strlen (haystack);
if (count <= 0 || count > haystacksize) {
res = value_new_error (ei->pos, gnumeric_err_VALUE);
return value_new_error (ei->pos, gnumeric_err_VALUE);
} else {
const char *haystart = haystack + (count - 1);
const char *p = strstr (haystart, needle);
if (p)
res = value_new_int (count + (p - haystart));
return value_new_int (count + (p - haystart));
else
/* Really? */
res = value_new_error (ei->pos, gnumeric_err_VALUE);
return value_new_error (ei->pos, gnumeric_err_VALUE);
}
g_free (needle);
g_free (haystack);
return res;
}
/***************************************************************************/
......
......@@ -154,9 +154,8 @@ graph_vector_seq_string (GraphVector *vector)
Value const *elem = vector->is_column
? value_area_get_x_y (&pos, v, 0, i)
: value_area_get_x_y (&pos, v, i, 0);
char * tmp = value_get_as_string (elem);
values->_buffer [i] = CORBA_string_dup (tmp);
g_free (tmp);
const char *tmp = value_peek_string (elem);
values->_buffer[i] = CORBA_string_dup (tmp);
}
return values;
......
......@@ -154,9 +154,8 @@ graph_vector_seq_string (GraphVector *vector)
Value const *elem = vector->is_column
? value_area_get_x_y (&pos, v, 0, i)
: value_area_get_x_y (&pos, v, i, 0);
char * tmp = value_get_as_string (elem);
values->_buffer [i] = CORBA_string_dup (tmp);
g_free (tmp);
const char *tmp = value_peek_string (elem);
values->_buffer[i] = CORBA_string_dup (tmp);
}
return values;
......
......@@ -841,31 +841,26 @@ static char *
find_name (Sheet *sheet, int col, int row)
{
static char *str = NULL;
Cell *cell = NULL;
char *col_str, *row_str;
const char *col_str = "";
const char *row_str = "";
int col_n, row_n;
for (col_n = col - 1; col_n >= 0; col_n--) {
cell = sheet_cell_get (sheet, col_n, row);
if (cell && !VALUE_IS_NUMBER (cell->value))
Cell *cell = sheet_cell_get (sheet, col_n, row);
if (cell && !VALUE_IS_NUMBER (cell->value)) {
col_str = value_peek_string (cell->value);
break;
}
}
if (col_n >= 0)
col_str = value_get_as_string (cell->value);
else
col_str = g_strdup ("");
for (row_n = row - 1; row_n >= 0; row_n--) {
cell = sheet_cell_get (sheet, col, row_n);
if (cell && !VALUE_IS_NUMBER (cell->value))
Cell *cell = sheet_cell_get (sheet, col, row_n);
if (cell && !VALUE_IS_NUMBER (cell->value)) {
row_str = value_peek_string (cell->value);
break;
}
}
if (row_n >= 0)
row_str = value_get_as_string (cell->value);
else
row_str = g_strdup ("");
if (str)
g_free (str);
str = g_new (char, strlen (col_str) + strlen (row_str) + 2);
......@@ -875,9 +870,6 @@ find_name (Sheet *sheet, int col, int row)
else
sprintf (str, "%s", row_str);
g_free (col_str);
g_free (row_str);
return str;
}
......@@ -891,7 +883,6 @@ solver_answer_report (WorkbookControl *wbc, Sheet *sheet, GSList *ov,
CellList *cell_list = param->input_cells;
Cell *cell;
char *str;
int row, i;
dao.type = NewSheetOutput;
......@@ -923,9 +914,7 @@ solver_answer_report (WorkbookControl *wbc, Sheet *sheet, GSList *ov,
/* Set `Final Value' field */
cell = sheet_cell_fetch (sheet, param->target_cell->pos.col,
param->target_cell->pos.row);
str = value_get_as_string (cell->value);
set_cell (&dao, 3, 3, str);
g_free (str);
set_cell_value (&dao, 3, 3, value_duplicate (cell->value));
row = 4;
set_cell (&dao, 0, row++, _("Adjustable Cells"));
......@@ -953,9 +942,7 @@ solver_answer_report (WorkbookControl *wbc, Sheet *sheet, GSList *ov,
/* Set `Final Value' column */
cell = sheet_cell_fetch (sheet, cell->pos.col,
cell->pos.row);
str = value_get_as_string (cell->value);
set_cell (&dao, 3, row, str);
g_free (str);
set_cell_value (&dao, 3, row, value_duplicate (cell->value));
/* Go to next row */
cell_list = cell_list->next;
......@@ -1004,10 +991,8 @@ solver_answer_report (WorkbookControl *wbc, Sheet *sheet, GSList *ov,
/* Set `Cell Value' column */
cell = sheet_cell_fetch (sheet, sc, sr);
str = value_get_as_string (cell->value);
rhs = value_get_as_float (cell->value);
set_cell (&dao, 2, row, str);
g_free (str);
set_cell_value (&dao, 2, row, value_duplicate (cell->value));
/* Set `Formula' column */
set_cell (&dao, 3, row, c->str);
......@@ -1022,7 +1007,7 @@ solver_answer_report (WorkbookControl *wbc, Sheet *sheet, GSList *ov,
set_cell (&dao, 4, row, _("Not Binding"));
/* Set `Slack' column */
set_cell_float (&dao, 5, row, fabs (lhs-rhs));
set_cell_float (&dao, 5, row, fabs (lhs - rhs));
/* Go to next row */
++row;
......@@ -1082,9 +1067,7 @@ solver_sensitivity_report (WorkbookControl *wbc, Sheet *sheet, gnum_float *x,
/* Set `Final Value' column */
cell = sheet_cell_fetch (sheet, cell->pos.col, cell->pos.row);
str = value_get_as_string (cell->value);
set_cell (&dao, 2, row, str);
g_free (str);
set_cell_value (&dao, 2, row, value_duplicate (cell->value));
/* Set `Reduced Cost' column */
set_cell_float (&dao, 3, row, x[i]);
......@@ -1131,9 +1114,7 @@ solver_sensitivity_report (WorkbookControl *wbc, Sheet *sheet, gnum_float *x,
/* Set `Final Value' column */
cell = sheet_cell_fetch (sheet, c->lhs.col, c->lhs.row);
str = value_get_as_string (cell->value);
set_cell (&dao, 2, row, str);
g_free (str);
set_cell_value (&dao, 2, row, value_duplicate (cell->value));
#if 0
/* Set `Shadow Prize' column */
......@@ -1142,9 +1123,7 @@ solver_sensitivity_report (WorkbookControl *wbc, Sheet *sheet, gnum_float *x,
/* Set `R.H. Side Value' column */
cell = sheet_cell_fetch (sheet, c->rhs.col, c->rhs.row);
str = value_get_as_string (cell->value);
set_cell (&dao, 4, row, str);
g_free (str);
set_cell_value (&dao, 4, row, value_duplicate (cell->value));
/* Go to next row */
++row;
......
......@@ -521,6 +521,31 @@ value_get_as_string (const Value *value)
return g_strdup ("Internal problem");
}
/*
* Result will stay valid until (a) the value is disposed of, or (b) two
* further calls to this function are made.
*/
const char *
value_peek_string (const Value *v)
{
g_return_val_if_fail (v, "");
if (v->type == VALUE_STRING)
return v->v_str.val->str;
else {
static char *cache[2] = { 0 };
static int next = 0;
const char *s;
g_free (cache[next]);
s = cache[next] = value_get_as_string (v);
next = (next + 1) % (sizeof (cache) / sizeof (cache[0]));
return s;
}
}
/*
* FIXME FIXME FIXME : Support errors
*/
......
......@@ -107,6 +107,7 @@ ValueCompare value_compare (const Value *a, const Value *b,
gboolean value_get_as_bool (Value const *v, gboolean *err);
gboolean value_get_as_checked_bool (Value const *v);
char *value_get_as_string (const Value *value);
const char *value_peek_string (const Value *v);
int value_get_as_int (const Value *v);
gnum_float value_get_as_float (const Value *v);
char *value_cellrange_get_as_string (const Value *value,
......
......@@ -371,11 +371,10 @@ wb_view_auto_expr_recalc (WorkbookView *wbv, gboolean display)
if (wbv->auto_expr_value_as_string)
g_free (wbv->auto_expr_value_as_string);
if (v) {
char *val_str = value_get_as_string (v);
const char *val_str = value_peek_string (v);
wbv->auto_expr_value_as_string =
g_strconcat (wbv->auto_expr_desc, "=", val_str, NULL);
value_release (v);
g_free (val_str);
} else
wbv->auto_expr_value_as_string = g_strdup (_("Internal ERROR"));
......
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