fn-misc.c 1.6 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
/*
 * fn-misc.c:  Miscelaneous built-in functions
 * (C) 1998 The Free Software Foundation
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 *
 */
#include <config.h>
#include <gnome.h>
#include "math.h"
#include "gnumeric.h"
#include "gnumeric-sheet.h"
#include "utils.h"
#include "func.h"

static Value *
gnumeric_char (Value *argv [], char **error_string)
{
	Value *v = g_new (Value, 1);
	int i;
	char buffer [2];
	
	v->type = VALUE_STRING;
	i = value_get_as_double (argv [0]);
	buffer [0] = i;
	buffer [1] = 0;
	v->v.str = string_get (buffer);
	
	return v;
}

static Value *
gnumeric_clean (Value *argv [], char **error_string)
{
	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;
}

static Value *
gnumeric_exact (Value *argv [], char **error_string)
{
	Value *res;
	
	if (argv [0]->type != VALUE_STRING || argv [1]->type != VALUE_STRING){
		*error_string = _("Type mismatch");
		return NULL;
	}

	res = g_new (Value, 1);
	res->type = VALUE_INTEGER;
	res->v.v_int = !strcmp (argv [0]->v.str->str, argv [1]->v.str->str);
	return res;
}

FunctionDefinition misc_functions [] = {
	{ "char",  "f",  "number",           NULL, NULL, gnumeric_char },
	{ "clean", "s",  "text",             NULL, NULL, gnumeric_clean },
	{ "exact", "ss", "text1,text2",      NULL, NULL, gnumeric_exact },
	{ NULL, NULL }
};