fn-misc.c 1.78 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1
2
3
4
5
6
7
8
9
/*
 * fn-misc.c:  Miscelaneous built-in functions
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 *
 */
#include <config.h>
#include <gnome.h>
10
#include <ctype.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
11
12
13
14
15
16
#include "math.h"
#include "gnumeric.h"
#include "gnumeric-sheet.h"
#include "utils.h"
#include "func.h"

17
18
19
static char *help_clean = {
	N_("@FUNCTION=CLEAN\n"
	   "@SYNTAX=CLEAN(string)\n"
Arturo Espinosa's avatar
Arturo Espinosa committed
20

21
22
23
24
25
26
	   "@DESCRIPTION="
	   "Cleans the string from any non-printable characters."
	   "\n"
	   
	   "@SEEALSO=")
};
Miguel de Icaza's avatar
Miguel de Icaza committed
27

Arturo Espinosa's avatar
Arturo Espinosa committed
28
static Value *
29
gnumeric_clean (FunctionDefinition *fn, Value *argv [], char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{
	Value *res;
	char *copy, *p, *q;
	
	if (argv [0]->type != VALUE_STRING){
		*error_string = _("Type mismatch");
		return NULL;
	}
	p = argv [0]->v.str->str;
	copy = q = g_malloc (strlen (p) + 1);
	
	while (p){
		if (isprint (*p))
			*q++ = *p;
		p++;
	}
	*q = 0;

	res = g_new (Value, 1);
	res->type = VALUE_STRING;
	res->v.str = string_get (copy);
	g_free (copy);

	return res;
}

Miguel de Icaza's avatar
Miguel de Icaza committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
static char *help_iserror = {
	N_("@FUNCTION=ISERROR\n"
	   "@SYNTAX=ISERROR(exp)\n"

	   "@DESCRIPTION="
	   "Returns a TRUE value if the expression has an error\n"
	   "\n"
	   
	   "@SEEALSO=")
};

static Value *
gnumeric_iserror (void *tsheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *v, *retval;
	
	if (g_list_length (expr_node_list) != 1){
		*error_string = _("Argument mismatch");
		return NULL;
	}
	v = eval_expr (tsheet, (ExprTree *) expr_node_list->data, eval_col, eval_row, error_string);
	if (v == NULL)
		retval = value_int (1);
	else {
		retval = value_int (0);
		value_release (v);
	}
	
	return retval;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
87
FunctionDefinition misc_functions [] = {
Miguel de Icaza's avatar
Miguel de Icaza committed
88
89
	{ "clean",   "s",  "text",             &help_clean, NULL, gnumeric_clean },
	{ "iserror", "",   "",                 &help_iserror, gnumeric_iserror, NULL },
Arturo Espinosa's avatar
Arturo Espinosa committed
90
91
	{ NULL, NULL }
};