functions.c 7.57 KB
Newer Older
1
2
3
4
5
6
7
8
/*
 * fn-math.c:  Built in string functions.
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 */
#include <config.h>
#include <gnome.h>
9
#include <ctype.h>
10
11
12
13
14
15
#include "math.h"
#include "gnumeric.h"
#include "gnumeric-sheet.h"
#include "utils.h"
#include "func.h"

16
17
18
19
20
static char *help_char = {
	N_("@FUNCTION=CHAR\n"
	   "@SYNTAX=CHAR(x)\n"

	   "@DESCRIPTION="
21
	   "Returns the ascii character represented by the number x."
22
23
24
25
26
	   "\n"
	   
	   "@SEEALSO=CODE")
};

27
28
29
30
31
32
33
34
static Value *
gnumeric_char (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	char result [2];

	result [0] = value_get_as_int (argv [0]);
	result [1] = 0;

35
	return value_str (result);
36
37
}

38
39
40
41
42
43
44
45
46
47
48
static char *help_code = {
	N_("@FUNCTION=CODE\n"
	   "@SYNTAX=CODE(char)\n"

	   "@DESCRIPTION="
	   "Returns the ASCII number for the character char."
	   "\n"
	   
	   "@SEEALSO=CHAR")
};

49
50
51
52
53
54
55
56
57
58
59
static Value *
gnumeric_code (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	if (argv [0]->type != VALUE_STRING){
		*error_string = "Type mismatch";
		return NULL;
	}

	return value_int (argv [0]->v.str->str [0]);
}

60
61
62
63
64
static char *help_exact = {
	N_("@FUNCTION=EXACT\n"
	   "@SYNTAX=EXACT(string1, string2)\n"

	   "@DESCRIPTION="
65
	   "Returns true if string1 is exactly equal to string2 (this routine is case sensitive)."
66
67
68
69
70
	   "\n"
	   
	   "@SEEALSO=")
};

71
72
73
74
75
76
77
78
79
80
81
static Value *
gnumeric_exact (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	if (argv [0]->type != VALUE_STRING || argv [1]->type != VALUE_STRING){
		*error_string = "Type mismatch";
		return NULL;
	}

	return value_int (strcmp (argv [0]->v.str->str, argv [1]->v.str->str) == 0);
}

82
83
84
85
86
87
88
89
90
91
92
static char *help_len = {
	N_("@FUNCTION=LEN\n"
	   "@SYNTAX=LEN(string)\n"

	   "@DESCRIPTION="
	   "Returns the length in characters of the string @string."
	   "\n"
	   
	   "@SEEALSO=CHAR, CODE")
};

93
94
95
96
97
98
99
100
101
102
103
static Value *
gnumeric_len (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	if (argv [0]->type != VALUE_STRING){
		*error_string = "Type mismatch";
		return NULL;
	}

	return value_int (strlen (argv [0]->v.str->str));
}

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
static Value *
string_and_optional_int (Sheet *sheet, GList *l, int eval_col, int eval_row, char **error, int *count)
{
	int argc = g_list_length (l);
	Value *v, *vs;
	
	if (argc < 1 || argc > 2){
		*error = "Invalid number of arguments";
		return NULL;
	}

	vs = eval_expr (sheet, l->data, eval_col, eval_row, error);
	if (vs == NULL)
		return NULL;

	if (vs->type != VALUE_STRING){
		value_release (vs);
		*error = "Type mismatch";
		return NULL;
	}
	
	if (argc == 1){
		*count = 1;
	} else {
		v = eval_expr (sheet, l->next->data,
			       eval_col, eval_row, error);
		if (v == NULL)
			return NULL;
		*count = value_get_as_int (v);
		value_release (v);
	}

	return vs;
}

static char *help_left = {
	N_("@FUNCTION=LEFT\n"
	   "@SYNTAX=LEFT(text[,num_chars])\n"

	   "@DESCRIPTION="
	   "Returns the leftmost num_chars characters or the left"
	   " character if num_chars is not specified"
	   "\n"
	   "@SEEALSO=MID, RIGHT")
};

static Value *
gnumeric_left (void *sheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *vs, *v;
	int count;
	char *s;

	vs = string_and_optional_int (sheet, expr_node_list, eval_col, eval_row,
				      error_string, &count);

	if (vs == NULL)
		return NULL;
			
	s = g_malloc (count + 1);
	strncpy (s, vs->v.str->str, count);
	s [count] = 0;

	value_release (vs);
168
169

	v = value_str (s);
170
171
172
173
174
	g_free (s);
	
	return v;
}

175
176
177
178
179
180
181
182
183
184
static char *help_lower = {
	N_("@FUNCTION=LOWER\n"
	   "@SYNTAX=LOWER(text)\n"

	   "@DESCRIPTION="
	   "Returns a lower-case version of the string in @text"
	   "\n"
	   "@SEEALSO=UPPER")
};

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
static Value *
gnumeric_lower (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	Value *v;
	char *s, *p;
	
	if (argv [0]->type != VALUE_STRING){
		*error_string = "Type mismatch";
		return NULL;
	}

	v = g_new (Value, 1);
	p = s = strdup (argv [0]->v.str->str);
	while (*p)
		*p = tolower (*p);
	v->v.str = string_get (p);
	g_free (p);

	return v;
}

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
static char *help_mid = {
	N_("@FUNCTION=MID\n"
	   "@SYNTAX=MID(string, position, length)\n"

	   "@DESCRIPTION="
	   "Returns a substring from @string starting at @position for @lenght characters."
	   "\n"
	   
	   "@SEEALSO=LEFT, RIGHT")
};

static Value *
gnumeric_mid (struct FunctionDefinition *i, Value *argv [], char **error)
{
	Value *v;
	int pos, len;
	char *s, *source;

	if (argv [0]->type != VALUE_STRING ||
	    argv [1]->type != VALUE_INTEGER ||
	    argv [2]->type != VALUE_INTEGER){
		*error = "Type mismatch";
		return NULL;
	}

	len = value_get_as_int (argv [2]);
	pos = value_get_as_int (argv [1]);

	if (len < 0 || pos < 0){
		*error = "Invalid arguments";
		return NULL;
	}

	source = argv [0]->v.str->str;

	if (pos > strlen (source))
		return value_str ("");
	
	s = g_malloc0 (len);
	
	strncpy (s, source + pos, len);

	v = value_str (s);
	g_free (s);
	
	return v;
}

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
static char *help_right = {
	N_("@FUNCTION=RIGHT\n"
	   "@SYNTAX=RIGHT(text[,num_chars])\n"

	   "@DESCRIPTION="
	   "Returns the rightmost num_chars characters or the right"
	   " character if num_chars is not specified"
	   "\n"
	   "@SEEALSO=MID, LEFT")
};

static Value *
gnumeric_right (void *sheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string)
{
	Value *vs, *v;
	int count, len;
	char *s;

	vs = string_and_optional_int (sheet, expr_node_list, eval_col, eval_row,
				      error_string, &count);

	if (vs == NULL)
		return NULL;

	len = strlen (vs->v.str->str);
	if (count > len)
		count = len;
	
	s = g_malloc (count + 1);
	strncpy (s, vs->v.str->str+len-count, count);
	s [count] = 0;

	value_release (vs);
287
288

	v = value_str (s);
289
290
291
292
293
	g_free (s);
	
	return v;
}

294
295
296
297
298
299
300
301
302
303
static char *help_upper = {
	N_("@FUNCTION=UPPER\n"
	   "@SYNTAX=UPPER(text)\n"

	   "@DESCRIPTION="
	   "Returns a upper-case version of the string in @text"
	   "\n"
	   "@SEEALSO=LOWER")
};

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
static Value *
gnumeric_upper (struct FunctionDefinition *i, Value *argv [], char **error_string)
{
	Value *v;
	char *s, *p;
	
	if (argv [0]->type != VALUE_STRING){
		*error_string = "Type mismatch";
		return NULL;
	}

	v = g_new (Value, 1);
	p = s = strdup (argv [0]->v.str->str);
	while (*p)
		*p = toupper (*p);
	v->v.str = string_get (p);
	g_free (p);

	return v;
}
324
325

FunctionDefinition string_functions [] = {
326
327
328
329
330
331
332
	{ "char",     "f",  "number",            &help_char,  NULL, gnumeric_char },
	{ "code",     "s",  "text",              &help_code,  NULL, gnumeric_code },
	{ "exact",    "ss", "text1,text2",       &help_exact, NULL, gnumeric_exact },
	{ "left",     0,    "text,num_chars",    &help_left,  gnumeric_left, NULL },
	{ "len",      "s",  "text",              &help_len,   NULL, gnumeric_len },
	{ "lower",    "s",  "text",              &help_lower, NULL, gnumeric_lower },
        { "mid",      "sff","text,pos,num",      &help_mid,   NULL, gnumeric_mid },
333
	{ "right",    0,    "text,num_chars",    &help_right, gnumeric_right, NULL },
334
	{ "upper",    "s",  "text",              &help_upper, NULL, gnumeric_upper },
335
336
	{ NULL, NULL },
};
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

/*
 * Missing:
 *
 * DOLLAR(number [,decimals] formats number as currency.
 *
 * FIXED (number, decimals, no_comma)
 * formats number as text with a fixed number of decimals
 *
 * FIND (find_text, within_text [,start_at_num])
 *
 * PROPER(text) capitalizes the first letter in each word of a text value
 *
 * REPLACE(old_text, start_num, num_chars, new_text)
 *
 * REPT (text, number_of_times)
 *
 * SUBSTITUTE(text,old_text, new_text [,intenace_num])
 *
 * T(value) -> converts its argumnt to text
 *
 * TEXT (value, format_text)  converts value to text with format_text
 *
 * TRIM(text) removes spaces form text
 *
 * VALUE(text) converts a text argument to a number
 */