-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_calc.c
153 lines (128 loc) · 4.36 KB
/
csv_calc.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
/*
* This file is part of libsysperf
*
* Copyright (C) 2001, 2004-2007 by Nokia Corporation.
*
* Contact: Eero Tamminen <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/* ========================================================================= *
* File: csv_calc.c
*
* Author: Simo Piiroinen
*
* -------------------------------------------------------------------------
*
* History:
*
* 25-Sep-2006 Simo Piiroinen
* - sync with calculator changes
*
* 17-May-2006 Simo Piiroinen
* - csv api updates
*
* 14-Sep-2005 Simo Piiroinen
* - initial version
* ========================================================================= */
/* ========================================================================= *
* Include Files
* ========================================================================= */
#include <stdio.h>
#include <assert.h>
#include "csv_calc.h"
#define EPSILON (1e-9)
/* ------------------------------------------------------------------------- *
* csv_calc_getsym_fn -- suitable callback for row operations
* ------------------------------------------------------------------------- */
static void csv_calc_getsym_fn(calc_t *calc, void *user, calctok_t *tok)
{
csv_calc_t *self = user;
if( tok->tok_col < 0 )
{
tok->tok_col = csv_addcol(self->table, calctok_getsymbol(tok));
}
tok->tok_val = *csv_getcell(self->table, self->row, tok->tok_col);
}
/* ------------------------------------------------------------------------- *
* csv_calc_setsym_fn -- suitable callback for row operations
* ------------------------------------------------------------------------- */
static void csv_calc_setsym_fn(calc_t *calc, void *user, calctok_t *tok)
{
csv_calc_t *self = user;
if( tok->tok_col < 0 )
{
tok->tok_col = csv_addcol(self->table, calctok_getsymbol(tok));
}
*csv_getcell(self->table, self->row, tok->tok_col) = tok->tok_val;
}
/* ------------------------------------------------------------------------- *
* csv_calc_delete
* ------------------------------------------------------------------------- */
void csv_calc_delete(csv_calc_t *self)
{
if( self != 0 )
{
calc_delete(self->calc);
free(self);
}
}
/* ------------------------------------------------------------------------- *
* csv_calc_create
* ------------------------------------------------------------------------- */
csv_calc_t *csv_calc_create(csv_t *table, const char *expr)
{
csv_calc_t *self = calloc(1, sizeof *self);
self->table = table;
self->row = 0;
self->calc = calc_create();
self->calc->calc_getvar = csv_calc_getsym_fn;
self->calc->calc_setvar = csv_calc_setsym_fn;
if( !calc_compile(self->calc, expr) )
{
csv_calc_delete(self);
self = 0;
}
return self;
}
/* ------------------------------------------------------------------------- *
* csv_calc_row_value
* ------------------------------------------------------------------------- */
double csv_calc_row_value(csv_calc_t *self, int row)
{
self->row = row;
return calc_evaluate(self->calc);
}
/* ------------------------------------------------------------------------- *
* csv_calc_row_true
* ------------------------------------------------------------------------- */
int csv_calc_row_true(csv_calc_t *self, int row)
{
double v = csv_calc_row_value(self, row);
return (v < -EPSILON) || (v > EPSILON);
}
/* ------------------------------------------------------------------------- *
* csv_calc_all
* ------------------------------------------------------------------------- */
void csv_calc_all_rows(csv_calc_t *self)
{
int rows = csv_rows(self->table);
// QUARANTINE for( self->row = 0; self->row < self->table->ct_rows; ++self->row )
for( self->row = 0; self->row < rows; ++self->row )
{
calc_evaluate(self->calc);
}
}