]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mlminidom/ml_minidom.c
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / DEVEL / mlminidom / ml_minidom.c
1 /* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
2  *
3  * This file is part of mlminidom, the Ocaml binding for minidom.
4  * 
5  * mlminidom is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * mlminidom is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with mlminidom; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  * 
19  * For details, send a mail to the author.
20  */
21
22 #include <assert.h>
23 #include <mlvalues.h>
24 #include <memory.h>
25 #include <custom.h>
26
27 #include "minidom.h"
28
29 #define Val_ptr(p)        ((value) (p))
30 #define Val_option(p,f)   ((p != NULL) ? ml_some(f(p)) : Val_unit)
31 #define mDOMString_val(v) ((mDOMStringRef) String_val(v))
32 #define mDOMDocRef_val(r) (*((mDOMDocRef *)Data_custom_val(r)))
33
34 static value
35 Val_mDOMConstString(mDOMConstStringRef s)
36 {
37   return copy_string((char *) s);
38 }
39
40 static value
41 Val_mDOMString(mDOMStringRef s)
42 {
43   value r = copy_string((char *) s);
44   mdom_string_free(s);
45   return r;
46 }
47
48 static value
49 ml_some(value v)
50 {
51   CAMLparam1(v);
52   value ret = alloc_small(1,0);
53   Field(ret,0) = v;
54   CAMLreturn(ret);
55 }
56
57 value
58 ml_string_of_mDOMString(value s)
59 {
60   CAMLparam1(s);
61   CAMLreturn(s);
62 }
63
64 value
65 ml_mDOMString_of_string(value s)
66 {
67   CAMLparam1(s);
68   CAMLreturn(s);
69 }
70
71 static void
72 ml_doc_free(value doc)
73 {
74         mdom_doc_free(mDOMDocRef_val(doc));
75 }
76
77 static struct custom_operations ops =
78   {"it.unibo.cs.helm.gtkmathview.mDOMDocRef",
79    ml_doc_free,
80    custom_compare_default,
81    custom_hash_default,
82    custom_serialize_default,
83    custom_deserialize_default
84   };
85
86 value
87 ml_doc_load(value file_name)
88 {
89   mDOMDocRef doc_ref;
90
91   CAMLparam1(file_name);
92   CAMLlocal1(val_doc_ref);
93
94   doc_ref = mdom_load(String_val(file_name), FALSE, NULL);
95   if (doc_ref == NULL) failwith("minidom: could not load document");
96   val_doc_ref = alloc_custom(&ops, sizeof(mDOMDocRef), 1, 1);
97
98   *((mDOMDocRef *)Data_custom_val(val_doc_ref)) = doc_ref;
99
100   CAMLreturn(val_doc_ref);
101 }
102
103 value
104 ml_doc_new(value s)
105 {
106   mDOMDocRef doc_ref;
107
108   CAMLparam1(s);
109   CAMLlocal1(val_doc_ref);
110
111   doc_ref = mdom_doc_new(mDOMString_val(s));
112   if (doc_ref == NULL) failwith("minidom: could not create new document");
113   val_doc_ref = alloc_custom(&ops, sizeof(mDOMDocRef), 1, 1);
114
115   *((mDOMDocRef *)Data_custom_val(val_doc_ref)) = doc_ref;
116
117   CAMLreturn(val_doc_ref);
118 }
119
120
121 value
122 ml_doc_get_root_node(value doc)
123 {
124   mDOMNodeRef root;
125
126   CAMLparam1(doc);
127   root = mdom_doc_get_root_node(mDOMDocRef_val(doc));
128   if (root == NULL) failwith("minidom: document has no root node!");
129
130   CAMLreturn((value) root);
131 }
132
133 value
134 ml_doc_add_entity(value doc, value name, value content)
135 {
136   mDOMEntityRef ent;
137
138   CAMLparam3(doc, name, content);
139   ent = mdom_doc_add_entity(mDOMDocRef_val(doc), mDOMString_val(name), mDOMString_val(content));
140   if (ent == NULL) failwith("minidom: could not add entity");
141
142   CAMLreturn((value) ent);
143 }
144
145 value
146 ml_doc_get_entity(value doc, value name)
147 {
148   mDOMEntityRef ent;
149
150   CAMLparam2(doc, name);
151   ent = mdom_doc_get_entity(mDOMDocRef_val(doc), mDOMString_val(name));
152
153   CAMLreturn(Val_option(ent, Val_ptr));
154 }
155
156 value
157 ml_doc_get_predefined_entity(value name)
158 {
159   mDOMEntityRef ent;
160
161   CAMLparam1(name);
162   ent = mdom_get_predefined_entity(mDOMString_val(name));
163
164   CAMLreturn(Val_option(ent, Val_ptr));
165 }
166
167 value
168 ml_entity_get_content(value ent)
169 {
170   CAMLparam1(ent);
171   CAMLreturn(Val_mDOMConstString(mdom_entity_get_content((mDOMEntityRef) ent)));
172 }
173
174 value
175 ml_node_is_text(value node)
176 {
177   CAMLparam1(node);
178   CAMLreturn(Val_bool(mdom_node_is_text((mDOMNodeRef) node)));
179 }
180
181 value
182 ml_node_is_element(value node)
183 {
184   CAMLparam1(node);
185   CAMLreturn(Val_bool(mdom_node_is_element((mDOMNodeRef) node)));
186 }
187
188 value
189 ml_node_is_blank(value node)
190 {
191   CAMLparam1(node);
192   CAMLreturn(Val_bool(mdom_node_is_blank((mDOMNodeRef) node)));
193 }
194
195 value
196 ml_node_is_entity_ref(value node)
197 {
198   CAMLparam1(node);
199   CAMLreturn(Val_bool(mdom_node_is_entity_ref((mDOMNodeRef) node)));
200 }
201
202 value
203 ml_node_get_type(value node)
204 {
205   CAMLparam1(node);
206   CAMLreturn(Val_int(mdom_node_get_type((mDOMNodeRef) node)));
207 }
208
209 value
210 ml_node_get_name(value node)
211 {
212   CAMLparam1(node);
213   CAMLreturn(Val_option(mdom_node_get_name((mDOMNodeRef) node), Val_mDOMConstString));
214 }
215
216 value
217 ml_node_has_attribute(value node, value name)
218 {
219   CAMLparam2(node,name);
220   CAMLreturn(Val_bool(mdom_node_has_attribute((mDOMNodeRef) node, String_val(name))));
221 }
222
223 value
224 ml_node_has_attribute_ns(value node, value name, value uri)
225 {
226   CAMLparam3(node,name,uri);
227   CAMLreturn(Val_bool(mdom_node_has_attribute_ns((mDOMNodeRef) node, String_val(name), String_val(uri))));
228 }
229
230 value
231 ml_node_get_content(value node)
232 {
233   CAMLparam1(node);
234   CAMLreturn(Val_option(mdom_node_get_content((mDOMNodeRef) node), Val_mDOMString));
235 }
236
237 value
238 ml_node_get_ns_uri(value node)
239 {
240   CAMLparam1(node);
241   CAMLreturn(Val_option(mdom_node_get_ns_uri((mDOMNodeRef) node), Val_mDOMConstString));
242 }
243
244 value
245 ml_node_get_attribute(value node, value name)
246 {
247   CAMLparam2(node,name);
248   CAMLreturn(Val_option(mdom_node_get_attribute((mDOMNodeRef) node, String_val(name)), Val_mDOMString));
249 }
250
251 value
252 ml_node_get_attribute_ns(value node, value name, value ns_uri)
253 {
254   CAMLparam2(node,name);
255   CAMLreturn(Val_option(mdom_node_get_attribute_ns((mDOMNodeRef) node,
256                                                    String_val(name),
257                                                    String_val(ns_uri)), Val_mDOMString));
258 }
259
260 value
261 ml_node_get_parent(value node)
262 {
263   CAMLparam1(node);
264   CAMLreturn(Val_option(mdom_node_get_parent((mDOMNodeRef) node), Val_ptr));
265 }
266
267 value
268 ml_node_get_prev_sibling(value node)
269 {
270   CAMLparam1(node);
271   CAMLreturn(Val_option(mdom_node_get_prev_sibling((mDOMNodeRef) node), Val_ptr));
272 }
273
274 value
275 ml_node_get_next_sibling(value node)
276 {
277   CAMLparam1(node);
278   CAMLreturn(Val_option(mdom_node_get_next_sibling((mDOMNodeRef) node), Val_ptr));
279 }
280
281 value
282 ml_node_get_first_child(value node)
283 {
284   CAMLparam1(node);
285   CAMLreturn(Val_option(mdom_node_get_first_child((mDOMNodeRef) node), Val_ptr));
286 }
287
288 value
289 ml_node_get_first_attribute(value node)
290 {
291   CAMLparam1(node);
292   CAMLreturn(Val_option(mdom_node_get_first_attribute((mDOMNodeRef) node), Val_ptr));
293 }
294
295 value
296 ml_node_is_first(value node)
297 {
298   CAMLparam1(node);
299   CAMLreturn(Val_bool(mdom_node_is_first((mDOMNodeRef) node)));
300 }
301
302 value
303 ml_node_is_last(value node)
304 {
305   CAMLparam1(node);
306   CAMLreturn(Val_bool(mdom_node_is_last((mDOMNodeRef) node)));
307 }
308
309 value
310 ml_attr_get_name(value attr)
311 {
312   CAMLparam1(attr);
313   CAMLreturn(Val_option(mdom_attr_get_name((mDOMAttrRef) attr), Val_mDOMConstString));
314 }
315
316 value
317 ml_attr_get_ns_uri(value attr)
318 {
319   CAMLparam1(attr);
320   CAMLreturn(Val_option(mdom_attr_get_ns_uri((mDOMAttrRef) attr), Val_mDOMConstString));
321 }
322
323 value
324 ml_attr_get_value(value attr)
325 {
326   CAMLparam1(attr);
327   CAMLreturn(Val_option(mdom_attr_get_value((mDOMAttrRef) attr), Val_mDOMString));
328 }
329
330 value
331 ml_attr_get_prev_sibling(value attr)
332 {
333   CAMLparam1(attr);
334   CAMLreturn(Val_option(mdom_attr_get_prev_sibling((mDOMAttrRef) attr), Val_ptr));
335 }
336
337 value
338 ml_attr_get_next_sibling(value attr)
339 {
340   CAMLparam1(attr);
341   CAMLreturn(Val_option(mdom_attr_get_next_sibling((mDOMAttrRef) attr), Val_ptr));
342 }
343
344 value
345 ml_attr_get_parent(value attr)
346 {
347   CAMLparam1(attr);
348   CAMLreturn(Val_option(mdom_attr_get_parent((mDOMAttrRef) attr), Val_ptr));
349 }
350