annotate src/rovascope/libcalc/function.c @ 901:08643d5994fe trunk

[svn] - now do_setpos() takes relative position as the argument and works even if cuesheets are included in large playlist. may close #869. - improve handling of end of cue. - suppress warnings.
author yaz
date Mon, 26 Mar 2007 14:44:46 -0700
parents 290588854a9d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
1 /* function.c --
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
2 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
3 * Copyright (C) 2001 Janusz Gregorczyk <jgregor@kki.net.pl>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
4 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
5 * This file is part of xvs.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
6 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
10 * (at your option) any later version.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
11 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
15 * GNU General Public License for more details.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
16 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
20 */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
21
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
22 #include <glib.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
23 #include <math.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
24 #include <string.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
25
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
26 #include "function.h"
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
27
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
28 /* Function pointer type. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
29 typedef struct {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
30 char *name;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
31 double (*funcptr)(ex_stack *stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
32 } func_t;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
33
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
34 /* */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
35
334
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
36 static double f_log (ex_stack *stack) {
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
37 return log (pop (stack));
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
38 }
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
39
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
40 static double f_sin (ex_stack *stack) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
41 return sin (pop (stack));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
42 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
43
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
44 static double f_cos (ex_stack *stack) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
45 return cos (pop (stack));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
46 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
47
334
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
48 static double f_tan (ex_stack *stack) {
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
49 return tan (pop (stack));
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
50 }
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
51
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
52 static double f_asin (ex_stack *stack) {
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
53 return asin (pop (stack));
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
54 }
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
55
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
56 static double f_acos (ex_stack *stack) {
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
57 return acos (pop (stack));
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
58 }
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
59
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
60 static double f_atan (ex_stack *stack) {
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
61 return atan (pop (stack));
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
62 }
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
63
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
64 static double f_if (ex_stack *stack) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
65 double a = pop (stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
66 double b = pop (stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
67 return (pop (stack) != 0.0) ? a : b;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
68 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
69
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
70 static double f_div (ex_stack *stack) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
71 int y = (int)pop (stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
72 int x = (int)pop (stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
73 return (y == 0) ? 0 : (x / y);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
74 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
75
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
76 /* */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
77
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
78 static const func_t init[] = {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
79 { "sin", f_sin },
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
80 { "cos", f_cos },
334
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
81 { "tan", f_tan },
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
82 { "asin", f_asin },
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
83 { "acos", f_acos },
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
84 { "atan", f_atan },
a9f1bd76a3e6 [svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents: 282
diff changeset
85 { "log", f_log },
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
86 { "if", f_if },
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
87 { "div", f_div }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
88 };
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
89
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
90 int function_lookup (const char *name) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
91 int i;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
92
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
93 for (i = 0; i < sizeof (init) / sizeof (init[0]); i++)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
94 if (strcmp (init[i].name, name) == 0)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
95 return i;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
96
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
97 g_warning ("Unknown function: %s\n", name);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
98 return -1;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
99 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
100
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
101 void function_call (int func_id, ex_stack *stack) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
102 g_assert (func_id >= 0);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
103 g_assert (func_id < sizeof (init) / sizeof (init[0]));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
104
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
105 push (stack, (*init[func_id].funcptr)(stack));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
106 }