1 /* This file is part of GMetaDOM
2  * a generic bind package for the Document Object Model API.
3  * Copyright (C) 2001-2002 Luca Padovani <luca.padovani@cs.unibo.it>
4  *               2002 Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * For more information, please visit the project home page
21  * http://gmetadom.sourceforge.net
22  * or send an email to <luca.padovani@cs.unibo.it>
23  */
24 
25 #include <assert.h>
26 #include <gdome.h>
27 #include <caml/memory.h>
28 #include <caml/alloc.h>
29 #include <caml/custom.h>
30 #include "mlgdomevalue.h"
31 
32 static void
ml_gdome_str_finalize(value v)33 ml_gdome_str_finalize(value v)
34 {
35   GdomeDOMString* obj_ = DOMString_val(v);
36   g_assert(obj_ != NULL);
37   gdome_str_unref(obj_);
38 }
39 
40 static int
ml_gdome_str_compare(value v1,value v2)41 ml_gdome_str_compare(value v1, value v2)
42 {
43   CAMLparam2(v1,v2);
44   GdomeDOMString* str1 = DOMString_val(v1);
45   GdomeDOMString* str2 = DOMString_val(v2);
46   CAMLreturn(Val_int(strcmp(str1->str,str2->str)));
47 }
48 
49 value
Val_DOMString(GdomeDOMString * str)50 Val_DOMString(GdomeDOMString* str)
51 {
52   static struct custom_operations ops = {
53     "http://gmetadom.sourceforge.net/gdome_caml/DOMString",
54     ml_gdome_str_finalize,
55     ml_gdome_str_compare,
56     custom_hash_default,
57     custom_serialize_default,
58     custom_deserialize_default
59   };
60 
61   value v = alloc_custom(&ops, sizeof(GdomeDOMString*), 0, 1);
62   g_assert(str != NULL);
63   *((GdomeDOMString**) Data_custom_val(v)) = str;
64 
65   return v;
66 }
67 
68 GdomeDOMString*
DOMString_val(value v)69 DOMString_val(value v)
70 {
71   GdomeDOMString* res_ = *((GdomeDOMString**) Data_custom_val(v));
72   g_assert(res_ != NULL);
73   return res_;
74 }
75 
76 value
ml_gdome_DOMString_of_string(value str)77 ml_gdome_DOMString_of_string(value str)
78 {
79   return Val_DOMString(gdome_str_mkref_dup(String_val(str)));
80 }
81 
82 value
ml_gdome_DOMString_to_string(value self)83 ml_gdome_DOMString_to_string(value self)
84 {
85   CAMLparam1(self);
86   CAMLreturn(copy_string(DOMString_val(self)->str));
87 }
88 
89 value
ml_gdome_DOMString_equals(value v1,value v2)90 ml_gdome_DOMString_equals(value v1, value v2)
91 {
92   CAMLparam2(v1,v2);
93   GdomeDOMString* str1 = DOMString_val(v1);
94   GdomeDOMString* str2 = DOMString_val(v2);
95   if (v1 == v2) CAMLreturn(Val_bool(1));
96   else
97      CAMLreturn(Val_bool(gdome_str_equal(str1,str2)));
98 }
99