Commit 20dc1984 authored by Miguel de Icaza's avatar Miguel de Icaza Committed by Arturo Espinosa

Fixed usage of errors with the non-broken setup.

1999-07-14  Miguel de Icaza  <miguel@gnu.org>

	* src/fn-misc.c (gnumeric_error): Fixed usage of errors with the
	non-broken setup.

	* src/expr.c (error_message_set): Rewrote the error message logic
	to be clean and clear.  Simplified and followed the Gnumeric
	coding conventions.
parent c9e199cb
1999-07-14 Miguel de Icaza <miguel@gnu.org>
* src/fn-misc.c (gnumeric_error): Fixed usage of errors with the
non-broken setup.
* src/expr.c (error_message_set): Rewrote the error message logic
to be clean and clear. Simplified and followed the Gnumeric
coding conventions.
1999-07-15 Michael Meeks <michael@edenproject.org>
* src/summary.c (summary_info_add): Add duplicate entry removal.
......
1999-07-14 Miguel de Icaza <miguel@gnu.org>
* src/fn-misc.c (gnumeric_error): Fixed usage of errors with the
non-broken setup.
* src/expr.c (error_message_set): Rewrote the error message logic
to be clean and clear. Simplified and followed the Gnumeric
coding conventions.
1999-07-15 Michael Meeks <michael@edenproject.org>
* src/summary.c (summary_info_add): Add duplicate entry removal.
......
......@@ -18,147 +18,124 @@
Value *value_zero = NULL;
EvalPosition *
eval_pos_init (EvalPosition *fp, Sheet *s, int col, int row)
eval_pos_init (EvalPosition *eval_pos, Sheet *sheet, int col, int row)
{
g_return_val_if_fail (s, NULL);
g_return_val_if_fail (fp, NULL);
g_return_val_if_fail (IS_SHEET (s), NULL);
g_return_val_if_fail (sheet != NULL, NULL);
g_return_val_if_fail (eval_pos != NULL, NULL);
g_return_val_if_fail (IS_SHEET (sheet), NULL);
fp->sheet = s;
fp->eval_col = col;
fp->eval_row = row;
return fp;
eval_pos->sheet = sheet;
eval_pos->eval_col = col;
eval_pos->eval_row = row;
return eval_pos;
}
EvalPosition *
eval_pos_cell (EvalPosition *fp, Cell *cell)
eval_pos_cell (EvalPosition *eval_pos, Cell *cell)
{
g_return_val_if_fail (fp, NULL);
g_return_val_if_fail (cell, NULL);
g_return_val_if_fail (cell->sheet, NULL);
g_return_val_if_fail (eval_pos != NULL, NULL);
g_return_val_if_fail (cell != NULL, NULL);
g_return_val_if_fail (cell->sheet != NULL, NULL);
g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);
return eval_pos_init (fp,
cell->sheet,
cell->col->pos,
cell->row->pos);
return eval_pos_init (
eval_pos,
cell->sheet,
cell->col->pos,
cell->row->pos);
}
FunctionEvalInfo *
func_eval_info_init (FunctionEvalInfo *s, Sheet *sheet, int col, int row)
func_eval_info_init (FunctionEvalInfo *eval_info, Sheet *sheet, int col, int row)
{
g_return_val_if_fail (s, NULL);
g_return_val_if_fail (eval_info != NULL, NULL);
eval_pos_init (&s->pos, sheet, col, row);
s->error = error_message_new ();
s->func_def = 0;
return s;
}
FunctionEvalInfo *
func_eval_info_cell (FunctionEvalInfo *s, Cell *cell)
{
g_return_val_if_fail (s, NULL);
g_return_val_if_fail (cell, NULL);
eval_pos_init (&eval_info->pos, sheet, col, row);
eval_info->error = error_message_new ();
eval_info->func_def = 0;
return func_eval_info_init (s,
cell->sheet,
cell->col->pos,
cell->row->pos);
return eval_info;
}
FunctionEvalInfo *
func_eval_info_pos (FunctionEvalInfo *s, const EvalPosition *fp)
func_eval_info_cell (FunctionEvalInfo *eval_info, Cell *cell)
{
g_return_val_if_fail (s, NULL);
g_return_val_if_fail (fp, NULL);
g_return_val_if_fail (eval_info != NULL, NULL);
g_return_val_if_fail (cell != NULL, NULL);
return func_eval_info_init (s,
fp->sheet,
fp->eval_col,
fp->eval_row);
return func_eval_info_init (
eval_info, cell->sheet,
cell->col->pos, cell->row->pos);
}
ErrorMessage *error_message_new (void)
FunctionEvalInfo *
func_eval_info_pos (FunctionEvalInfo *eval_info, const EvalPosition *eval_pos)
{
ErrorMessage *em = g_new (ErrorMessage, 1);
g_return_val_if_fail (eval_info != NULL, NULL);
g_return_val_if_fail (eval_pos != NULL, NULL);
em->err_msg = 0;
em->err_alloced = 0;
em->small_err[0] = '\0';
return em;
return func_eval_info_init (
eval_info,
eval_pos->sheet,
eval_pos->eval_col,
eval_pos->eval_row);
}
#define ERROR_MESSAGE_CLEAN(em) do { em->err_msg = NULL; \
if (em->err_alloced) { \
g_free (em->err_alloced); \
em->err_alloced = NULL; \
} \
em->small_err[0] = '\0'; \
} while (0)
void
error_message_set (ErrorMessage *em, const char *message)
ErrorMessage *
error_message_new (void)
{
g_return_if_fail (em);
ERROR_MESSAGE_CLEAN (em);
ErrorMessage *error_message = g_new (ErrorMessage, 1);
em->err_msg = message;
}
void
error_message_set_alloc (ErrorMessage *em, char *message)
{
g_return_if_fail (em);
ERROR_MESSAGE_CLEAN (em);
error_message->message = NULL;
error_message->alloc = NULL;
error_message->small [0] = '\0';
em->err_alloced = message;
return error_message;
}
void
error_message_set_small (ErrorMessage *em, const char *message)
error_message_set (ErrorMessage *error_message, const char *message)
{
g_return_if_fail (em);
g_return_if_fail (message);
g_return_if_fail (strlen(message) < 19);
ERROR_MESSAGE_CLEAN (em);
g_return_if_fail (error_message != NULL);
strcpy (em->small_err, message);
if (error_message->alloc){
g_free (error_message->alloc);
error_message->alloc = NULL;
}
if (strlen (message)+1 < sizeof (error_message->small)){
strcpy (error_message->small, message);
error_message->message = error_message->small;
return;
}
error_message->alloc = g_strdup (message);
error_message->message = error_message->alloc;
}
const char *
error_message_txt (ErrorMessage *em)
error_message_txt (ErrorMessage *error_message)
{
if (!em)
return _("Internal Error");
g_return_val_if_fail (error_message != NULL, NULL);
if (em->err_msg)
return em->err_msg;
if (em->err_alloced)
return em->err_msg;
return em->small_err;
return error_message->message;
}
/* Can be turned into a #define for speed later */
gboolean
error_message_is_set (ErrorMessage *em)
error_message_is_set (ErrorMessage *error_message)
{
g_return_val_if_fail (em, FALSE);
if (em->err_msg || em->err_alloced ||
em->small_err[0] != '\0')
return TRUE;
return FALSE;
g_return_val_if_fail (error_message, FALSE);
return error_message->message != NULL;
}
void
error_message_free (ErrorMessage *em)
error_message_free (ErrorMessage *error_message)
{
if (em->err_alloced) {
g_free (em->err_alloced);
em->err_alloced = 0;
}
g_free (em);
if (error_message->alloc)
g_free (error_message->alloc);
g_free (error_message);
}
ExprTree *
......@@ -239,16 +216,6 @@ function_error (FunctionEvalInfo *fe, char *error_string)
return NULL;
}
Value *
function_error_alloc (FunctionEvalInfo *fe, char *error_string)
{
g_return_val_if_fail (fe, NULL);
g_return_val_if_fail (error_string, NULL);
error_message_set_alloc (fe->error, error_string);
return NULL;
}
int
expr_tree_get_const_int (ExprTree const * const expr)
{
......@@ -1738,19 +1705,21 @@ eval_expr (FunctionEvalInfo *s, ExprTree *tree)
else
a = NULL;
}
if (a == NULL) /* Seems like errors return NULL */
if (a == NULL)
return NULL;
if (a->type == VALUE_CELLRANGE || a->type == VALUE_ARRAY){
int const num_x = value_area_get_width (&s->pos, a);
int const num_y = value_area_get_height (&s->pos, a);
if (x < num_x && y < num_y){
/* Evaluate relative to the upper left corner */
EvalPosition tmp_ep = s->pos; /* blit copy */
EvalPosition tmp_ep = s->pos;
tmp_ep.eval_col -= x;
tmp_ep.eval_row -= y;
a = (Value *)value_area_fetch_x_y (&tmp_ep,
a, x, y);
a = (Value *)value_area_fetch_x_y (
&tmp_ep,
a, x, y);
} else
return function_error (s, gnumeric_err_NA);
} else if (x >= 1 || y >= 1)
......
......@@ -169,15 +169,13 @@ typedef Value *(FunctionArgs) (FunctionEvalInfo *ei, Value **args);
typedef Value *(FunctionNodes) (FunctionEvalInfo *ei, GList *nodes);
struct _ErrorMessage {
const char *err_msg;
char *err_alloced;
char small_err[20];
const char *message;
char *alloc;
char small [20];
};
ErrorMessage *error_message_new (void);
void error_message_set (ErrorMessage *em, const char *message);
void error_message_set_alloc (ErrorMessage *em, char *message);
void error_message_set_small (ErrorMessage *em, const char *message);
gboolean error_message_is_set (ErrorMessage *em);
const char *error_message_txt (ErrorMessage *em);
void error_message_free (ErrorMessage *em);
......
......@@ -14,14 +14,15 @@
* directly
*/
static int
gnumeric_check_for_err (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_check_for_err (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
Value * tmp;
if (g_list_length (expr_node_list) != 1){
error_message_set (ei->error, _("Argument mismatch"));
error_message_set (eval_info->error, _("Argument mismatch"));
return -1;
}
tmp = eval_expr (ei, (ExprTree *) expr_node_list->data);
tmp = eval_expr (eval_info, (ExprTree *) expr_node_list->data);
if (tmp) {
value_release (tmp);
......@@ -43,15 +44,16 @@ static char *help_iserror = {
};
static Value *
gnumeric_iserror (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_iserror (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
int res;
res = gnumeric_check_for_err (ei, expr_node_list);
res = gnumeric_check_for_err (eval_info, expr_node_list);
if (res < 0)
return NULL;
if (res > 0) {
error_message_set_small (ei->error, "");
error_message_set (eval_info->error, "");
return value_new_bool (TRUE);
}
return value_new_bool (FALSE);
......@@ -73,16 +75,18 @@ static char *help_isna = {
* the error handling mechanism
*/
static Value *
gnumeric_isna (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_isna (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
int res;
res = gnumeric_check_for_err (ei, expr_node_list);
res = gnumeric_check_for_err (eval_info, expr_node_list);
if (res < 0)
return NULL;
if (res > 0) {
gboolean is_NA = (strcmp (gnumeric_err_NA,
error_message_txt(ei->error)) == 0);
error_message_set_small (ei->error, "");
error_message_txt(eval_info->error)) == 0);
error_message_set (eval_info->error, "");
return value_new_bool (is_NA);
}
return value_new_bool (FALSE);
......@@ -100,17 +104,17 @@ static char *help_iserr = {
};
static Value *
gnumeric_iserr (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_iserr (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
int res;
res = gnumeric_check_for_err (ei, expr_node_list);
res = gnumeric_check_for_err (eval_info, expr_node_list);
if (res < 0)
return NULL;
if (res > 0) {
gboolean is_NA = (strcmp (gnumeric_err_NA,
error_message_txt (ei->error)) == 0);
error_message_set_small (ei->error, "");
error_message_txt (eval_info->error)) == 0);
error_message_set (eval_info->error, "");
return value_new_bool (!is_NA);
}
return value_new_bool (FALSE);
......@@ -129,28 +133,28 @@ static char *help_error_type = {
};
static Value *
gnumeric_error_type (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_error_type (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
int retval = 0;
if (gnumeric_check_for_err (ei, expr_node_list)) {
if (!strcmp (gnumeric_err_NULL, error_message_txt (ei->error)))
if (gnumeric_check_for_err (eval_info, expr_node_list)) {
if (!strcmp (gnumeric_err_NULL, error_message_txt (eval_info->error)))
retval = 1;
else if (!strcmp (gnumeric_err_DIV0, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_DIV0, error_message_txt (eval_info->error)))
retval = 2;
else if (!strcmp (gnumeric_err_VALUE, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_VALUE, error_message_txt (eval_info->error)))
retval = 3;
else if (!strcmp (gnumeric_err_REF, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_REF, error_message_txt (eval_info->error)))
retval = 4;
else if (!strcmp (gnumeric_err_NAME, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_NAME, error_message_txt (eval_info->error)))
retval = 5;
else if (!strcmp (gnumeric_err_NUM, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_NUM, error_message_txt (eval_info->error)))
retval = 6;
else if (!strcmp (gnumeric_err_NA, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_NA, error_message_txt (eval_info->error)))
retval = 7;
else
return function_error (ei, gnumeric_err_NA);
error_message_set_small (ei->error, "");
return function_error (eval_info, gnumeric_err_NA);
error_message_set (eval_info->error, "");
}
return value_new_int (retval);
}
......@@ -167,9 +171,9 @@ static char *help_na = {
};
static Value *
gnumeric_na (FunctionEvalInfo *ei, Value **argv)
gnumeric_na (FunctionEvalInfo *eval_info, Value **argv)
{
return function_error (ei, gnumeric_err_NA);
return function_error (eval_info, gnumeric_err_NA);
}
static char *help_error = {
......@@ -184,15 +188,12 @@ static char *help_error = {
};
static Value *
gnumeric_error (FunctionEvalInfo *ei, Value *argv[])
gnumeric_error (FunctionEvalInfo *eval_info, Value *argv[])
{
if (argv [0]->type != VALUE_STRING)
return function_error (ei, _("Type mismatch"));
return function_error (eval_info, _("Type mismatch"));
/* The error signaling system is broken. We really cannot allocate a
dynamic error string. Let's hope the string stays around for long
enough... */
return function_error_alloc (ei, g_strdup (argv [0]->v.str->str));
return function_error (eval_info, argv [0]->v.str->str);
}
void misc_functions_init()
......
......@@ -27,7 +27,7 @@ gnumeric_selection (FunctionEvalInfo *ei, GList *expr_node_list)
{
Value *value;
GList *l;
int numrange,lp;
int numrange, i;
Sheet *sheet;
/* Type checking */
......@@ -35,16 +35,16 @@ gnumeric_selection (FunctionEvalInfo *ei, GList *expr_node_list)
return function_error (ei, _("Invalid number of arguments"));
sheet = ei->pos.sheet;
numrange=g_list_length (sheet->selections);
numrange = g_list_length (sheet->selections);
value = value_array_new (numrange, 1);
lp = 0;
i = 0;
for (l = sheet->selections; l; l = l->next){
SheetSelection *ss = (SheetSelection *) l->data;
Value *single_value;
CellRef *cell_ref;
single_value = value->v.array.vals [lp++][0];
single_value = value->v.array.vals [i++][0];
single_value->type = VALUE_CELLRANGE;
/* Fill it in */
......
......@@ -14,14 +14,15 @@
* directly
*/
static int
gnumeric_check_for_err (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_check_for_err (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
Value * tmp;
if (g_list_length (expr_node_list) != 1){
error_message_set (ei->error, _("Argument mismatch"));
error_message_set (eval_info->error, _("Argument mismatch"));
return -1;
}
tmp = eval_expr (ei, (ExprTree *) expr_node_list->data);
tmp = eval_expr (eval_info, (ExprTree *) expr_node_list->data);
if (tmp) {
value_release (tmp);
......@@ -43,15 +44,16 @@ static char *help_iserror = {
};
static Value *
gnumeric_iserror (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_iserror (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
int res;
res = gnumeric_check_for_err (ei, expr_node_list);
res = gnumeric_check_for_err (eval_info, expr_node_list);
if (res < 0)
return NULL;
if (res > 0) {
error_message_set_small (ei->error, "");
error_message_set (eval_info->error, "");
return value_new_bool (TRUE);
}
return value_new_bool (FALSE);
......@@ -73,16 +75,18 @@ static char *help_isna = {
* the error handling mechanism
*/
static Value *
gnumeric_isna (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_isna (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
int res;
res = gnumeric_check_for_err (ei, expr_node_list);
res = gnumeric_check_for_err (eval_info, expr_node_list);
if (res < 0)
return NULL;
if (res > 0) {
gboolean is_NA = (strcmp (gnumeric_err_NA,
error_message_txt(ei->error)) == 0);
error_message_set_small (ei->error, "");
error_message_txt(eval_info->error)) == 0);
error_message_set (eval_info->error, "");
return value_new_bool (is_NA);
}
return value_new_bool (FALSE);
......@@ -100,17 +104,17 @@ static char *help_iserr = {
};
static Value *
gnumeric_iserr (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_iserr (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
int res;
res = gnumeric_check_for_err (ei, expr_node_list);
res = gnumeric_check_for_err (eval_info, expr_node_list);
if (res < 0)
return NULL;
if (res > 0) {
gboolean is_NA = (strcmp (gnumeric_err_NA,
error_message_txt (ei->error)) == 0);
error_message_set_small (ei->error, "");
error_message_txt (eval_info->error)) == 0);
error_message_set (eval_info->error, "");
return value_new_bool (!is_NA);
}
return value_new_bool (FALSE);
......@@ -129,28 +133,28 @@ static char *help_error_type = {
};
static Value *
gnumeric_error_type (FunctionEvalInfo *ei, GList *expr_node_list)
gnumeric_error_type (FunctionEvalInfo *eval_info, GList *expr_node_list)
{
int retval = 0;
if (gnumeric_check_for_err (ei, expr_node_list)) {
if (!strcmp (gnumeric_err_NULL, error_message_txt (ei->error)))
if (gnumeric_check_for_err (eval_info, expr_node_list)) {
if (!strcmp (gnumeric_err_NULL, error_message_txt (eval_info->error)))
retval = 1;
else if (!strcmp (gnumeric_err_DIV0, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_DIV0, error_message_txt (eval_info->error)))
retval = 2;
else if (!strcmp (gnumeric_err_VALUE, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_VALUE, error_message_txt (eval_info->error)))
retval = 3;
else if (!strcmp (gnumeric_err_REF, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_REF, error_message_txt (eval_info->error)))
retval = 4;
else if (!strcmp (gnumeric_err_NAME, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_NAME, error_message_txt (eval_info->error)))
retval = 5;
else if (!strcmp (gnumeric_err_NUM, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_NUM, error_message_txt (eval_info->error)))
retval = 6;
else if (!strcmp (gnumeric_err_NA, error_message_txt (ei->error)))
else if (!strcmp (gnumeric_err_NA, error_message_txt (eval_info->error)))
retval = 7;
else
return function_error (ei, gnumeric_err_NA);
error_message_set_small (ei->error, "");
return function_error (eval_info, gnumeric_err_NA);
error_message_set (eval_info->error, "");
}
return value_new_int (retval);
}
......@@ -167,9 +171,9 @@ static char *help_na = {
};
static Value *
gnumeric_na (FunctionEvalInfo *ei, Value **argv)
gnumeric_na (FunctionEvalInfo *eval_info, Value **argv)
{
return function_error (ei, gnumeric_err_NA);
return function_error (eval_info, gnumeric_err_NA);
}
static char *help_error = {
......@@ -184,15 +188,12 @@ static char *help_error = {
};
static Value *
gnumeric_error (FunctionEvalInfo *ei, Value *argv[])
gnumeric_error (FunctionEvalInfo *eval_info, Value *argv[])
{
if (argv [0]->type != VALUE_STRING)
return function_error (ei, _("Type mismatch"));
return function_error (eval_info, _("Type mismatch"));
/* The error signaling system is broken. We really cannot allocate a
dynamic error string. Let's hope the string stays around for long
enough... */
return function_error_alloc (ei, g_strdup (argv [0]->v.str->str));
return function_error (eval_info, argv [0]->v.str->str);
}
void misc_functions_init()
......
......@@ -27,7 +27,7 @@ gnumeric_selection (FunctionEvalInfo *ei, GList *expr_node_list)
{
Value *value;
GList *l;