summaryrefslogtreecommitdiff
path: root/core/gmathlib.c
blob: 189ba0881df1dc51a7bc984ba350d9bc5167b0ad (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * gMathLib, mathematic expressions handling library
 *
 * Author : Guillaume Seguin
 * Email : guillaume@segu.in
 *
 * Copyright (c) 2007 Guillaume Seguin <guillaume@segu.in>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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 Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <gmathlib.h>

#define MODULES_PATH          PREFIX "/lib/gmathlib"
#define INPUT_MODULES_PATH    MODULES_PATH "/input"
#define OUTPUT_MODULES_PATH   MODULES_PATH "/output"

/* Create a new GMathContext and register templates */
GMathContext*
gmathcontext_new (void)
{
    GMathContext *context = g_new (GMathContext, 1);

    context->templates = NULL;

    g_datalist_init (&context->templates);
    g_datalist_init (&context->input_modules);
    g_datalist_init (&context->output_modules);

    gmath_register_templates (context);

    return context;
}

/* Free a context and its data */
void
gmathcontext_free (GMathContext *context)
{
    g_datalist_clear (&context->templates);
    g_datalist_clear (&context->input_modules);
    g_datalist_clear (&context->output_modules);
    g_free (context);
}

/* Register a new template */
bool
gmathcontext_register_template (GMathContext *context, gchar *name,
        			bool has_value, unsigned int children_count)
{
    GMathTemplate *template;

    if (g_datalist_get_data (&context->templates, name))
    {
        g_printerr ("Error: couldn't register %s template twice.\n", name);
        return false;
    }

    template = g_new (GMathTemplate, 1);

    template->has_value = has_value;
    template->children_count = children_count;

    g_datalist_set_data_full (&context->templates, name, template, g_free);

    return true;
}

/* Set current input module */
GMathModule*
gmathcontext_get_input_module (GMathContext *context, gchar *name)
{
    return g_datalist_get_data (&context->input_modules, name);
}

/* Set current output module */
GMathModule*
gmathcontext_get_output_module (GMathContext *context, gchar *name)
{
    return g_datalist_get_data (&context->output_modules, name);
}

/* Load an input module */
bool
gmathcontext_load_input_module (GMathContext *context, gchar *name)
{
    void *dl;
    GMathModule *module;
    gchar *path;

    if (gmathcontext_get_input_module (context, name))
    {
        g_printerr ("Error: couldn't register %s input module twice.\n",
        	    name);
        return false;
    }

    path = g_strdup_printf ("%s/lib%s.so", INPUT_MODULES_PATH, name);

    dl = dlopen (path, RTLD_NOW);
    if (!dl)
    {
        g_free (path);
        g_warning (dlerror ());
        return false;
    }
    g_free (path);

    module = g_new (GMathModule, 1);

    module->dl = dl;
    module->input_proc = dlsym (dl, "gmath_input_proc");
    module->output_proc = NULL;

    g_datalist_set_data_full (&context->input_modules, name, module, g_free);

    return true;
}

/* Load an output module */
bool
gmathcontext_load_output_module (GMathContext *context, gchar *name)
{
    void *dl;
    GMathModule *module;
    gchar *path;

    if (gmathcontext_get_output_module (context, name))
    {
        g_printerr ("Error: couldn't register %s output module twice.\n",
        	    name);
        return false;
    }

    path = g_strdup_printf ("%s/lib%s.so", OUTPUT_MODULES_PATH, name);

    dl = dlopen (path, RTLD_NOW);
    if (!dl)
    {
        g_free (path);
        g_warning (dlerror ());
        return false;
    }
    g_free (path);

    module = g_new (GMathModule, 1);

    module->dl = dl;
    module->input_proc = NULL;
    module->output_proc = dlsym (dl, "gmath_output_proc");

    g_datalist_set_data_full (&context->output_modules, name, module, g_free);

    return true;
}

/* Import a formula from raw data using the specified input module */
GMathFormula*
gmathcontext_import (GMathContext *context, gchar *module, gchar *data)
{
    GMathModule         *input_module;

    input_module = gmathcontext_get_input_module (context, module);
    if (!input_module)
    {
        g_printerr ("Error: input module %s not found.\n", module);
        return NULL;
    }

    return (*input_module->input_proc) (context, data);
}

/* Allocate a new formula */
GMathFormula*
gmathformula_new (GMathContext *context)
{
    GMathFormula *formula = g_new (GMathFormula, 1);

    formula->context = context;
    formula->root = NULL;

    return formula;
}

/* Free a formula and its symbols */
void
gmathformula_free (GMathFormula *formula)
{
    if (!formula)
        return;

    if (formula->root)
        gmathsymbol_free (formula->root);

    g_free (formula);
}

/* Set formula's root symbol */
void
gmathformula_set_root (GMathFormula *formula, GMathSymbol *symbol)
{
    if (!formula)
        return;

    formula->root = symbol;
}

/* Render a formula using the given output module */
size_t
gmathformula_export (GMathFormula *formula, gchar *module, gchar **data)
{
    GMathModule         *output_module;

    output_module = gmathcontext_get_output_module (formula->context, module);
    if (!output_module)
    {
        g_printerr ("Error: output module %s not found.\n", module);
        return 0;
    }

    return (*output_module->output_proc) (formula, data);
}

/* Allocate & prepare a new symbol */
GMathSymbol*
gmathsymbol_new (GMathContext *context, gchar *type)
{
    GMathTemplate *template = g_datalist_get_data (&context->templates, type);
    GMathSymbol *symbol;
    int i;

    if (!template)
    {
        g_printerr ("Unknown symbol type %s.\n", type);
        return NULL;
    }

    symbol = g_new (GMathSymbol, 1);

    symbol->type = g_strdup (type); 
    symbol->value = NULL;

    symbol->children = g_new0 (GMathSymbol*, template->children_count);

    symbol->parent = NULL;

    symbol->template = template;
    symbol->context = context;

    return symbol;
}

/* Free a symbol and its children */
void
gmathsymbol_free (GMathSymbol *symbol)
{
    int i;

    if (!symbol)
        return;

    for (i = 0; i < symbol->template->children_count; i++)
        gmathsymbol_free (symbol->children[i]);

    g_free (symbol->type);
    g_free (symbol->value);
    g_free (symbol);
}

/* Set symbol's value */
void
gmathsymbol_set_value (GMathSymbol *symbol, gchar *value)
{
    if (!symbol)
        return;

    symbol->value = g_strdup (value);
}

/* Set a Symbol's child */
void
gmathsymbol_set_child (GMathSymbol *symbol, GMathSymbol *child, int index)
{
    if (!symbol || child->parent || index >= symbol->template->children_count)
        return;

    symbol->children[index] = child;
    child->parent = symbol;
}

/* Get a Symbol's child */
GMathSymbol*
gmathsymbol_get_child (GMathSymbol *symbol, int index)
{
    if (!symbol || index >= symbol->template->children_count)
        return NULL;

    return symbol->children[index];
}