File: ruby_xml_attribute.c

package info (click to toggle)
ruby-mkrf 0.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,688 kB
  • sloc: ansic: 12,494; ruby: 6,998; sh: 790; yacc: 374; makefile: 57; cpp: 10
file content (224 lines) | stat: -rw-r--r-- 5,660 bytes parent folder | download | duplicates (3)
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
/* $Id: ruby_xml_attribute.c,v 1.1 2006/02/21 20:40:16 roscopeco Exp $ */

/* Please see the LICENSE file for copyright and distribution information */

#include "libxml.h"
#include "ruby_xml_attribute.h"

VALUE cXMLAttribute;

// TODO Wtf is this about? It's not referenced outside this file AFAIK...

VALUE
ruby_xml_attribute_child_get(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);
  if (rxa->attribute->children == NULL)
    return(Qnil);
  else
    return(ruby_xml_node_new2(cXMLNode, rxa->xd, rxa->attribute->children));
}


VALUE
ruby_xml_attribute_children_q(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);
  if (rxa->attribute->children == NULL)
    return(Qfalse);
  else
    return(Qtrue);
}


VALUE
ruby_xml_attribute_default_get(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);

  if (rxa->attribute->defaultValue == NULL)
    return(Qnil);
  else
    return(rb_str_new2((const char*)rxa->attribute->defaultValue));
}


VALUE
ruby_xml_attribute_element_get(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);

  if (rxa->attribute->elem == NULL)
    return(Qnil);
  else
    return(rb_str_new2((const char*)rxa->attribute->elem));
}


void
ruby_xml_attribute_free(ruby_xml_attribute *rxa) {
 if (rxa->attribute != NULL && !rxa->is_ptr) {
    xmlUnlinkNode((xmlNodePtr)rxa->attribute);
    xmlFreeNode((xmlNodePtr)rxa->attribute);
    rxa->attribute = NULL;
  }

  free(rxa);
}


VALUE
ruby_xml_attribute_last_get(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);
  if (rxa->attribute->last == NULL)
    return(Qnil);
  else
    return(ruby_xml_node_new2(cXMLNode, rxa->xd, rxa->attribute->last));
}


VALUE
ruby_xml_attribute_last_q(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);
  if (rxa->attribute->last == NULL)
    return(Qfalse);
  else
    return(Qtrue);
}


static void
ruby_xml_attribute_mark(ruby_xml_attribute *rxa) {
  if (rxa == NULL) return;
  if (!NIL_P(rxa->xd)) rb_gc_mark(rxa->xd);
}


VALUE
ruby_xml_attribute_name_get(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);

  if (rxa->attribute->name == NULL)
    return(Qnil);
  else
    return(rb_str_new2((const char*)rxa->attribute->name));
}


VALUE
ruby_xml_attribute_new(VALUE class, VALUE xd, xmlAttributePtr attribute) {
  ruby_xml_attribute *rxa;

  rxa = ALLOC(ruby_xml_attribute);
  rxa->is_ptr = 0;
  rxa->attribute = attribute;
  if (NIL_P(xd))
    rxa->xd = Qnil;
  else
    rxa->xd = xd;
  return(Data_Wrap_Struct(class, ruby_xml_attribute_mark,
			  ruby_xml_attribute_free, rxa));
}


VALUE
ruby_xml_attribute_new2(VALUE class, VALUE xd, xmlAttributePtr attribute) {
  ruby_xml_attribute *rxa;

  rxa = ALLOC(ruby_xml_attribute);
  rxa->is_ptr = 1;
  rxa->attribute = attribute;
  if (NIL_P(xd))
    rxa->xd = Qnil;
  else
    rxa->xd = xd;
  return(Data_Wrap_Struct(class, ruby_xml_attribute_mark,
			  ruby_xml_attribute_free, rxa));
}


VALUE
ruby_xml_attribute_next_get(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);
  if (rxa->attribute->next == NULL)
    return(Qnil);
  else
    return(ruby_xml_node_new2(cXMLNode, rxa->xd, rxa->attribute->next));
}


VALUE
ruby_xml_attribute_next_q(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);
  if (rxa->attribute->next == NULL)
    return(Qfalse);
  else
    return(Qtrue);
}


VALUE
ruby_xml_attribute_node_type_name(VALUE self) {
  return(rb_str_new2("attribute"));
}


VALUE
ruby_xml_attribute_prefix_get(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);

  if (rxa->attribute->prefix == NULL)
    return(Qnil);
  else
    return(rb_str_new2((const char*)rxa->attribute->prefix));
}


VALUE
ruby_xml_attribute_prev_get(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);
  if (rxa->attribute->prev == NULL)
    return(Qnil);
  else
    return(ruby_xml_node_new2(cXMLNode, rxa->xd, rxa->attribute->prev));
}

VALUE
ruby_xml_attribute_prev_q(VALUE self) {
  ruby_xml_attribute *rxa;
  Data_Get_Struct(self, ruby_xml_attribute, rxa);
  if (rxa->attribute->prev == NULL)
    return(Qfalse);
  else
    return(Qtrue);
}

// Rdoc maybe doesn't need to know 
// #ifdef RDOC_NEVER_DEFINED
//   mXML = rb_define_module("XML");
// #endif

void
ruby_init_xml_attribute(void) {
  cXMLAttribute = rb_define_class_under(mXML, "Attribute", rb_cObject);
  rb_define_method(cXMLAttribute, "child", ruby_xml_attribute_child_get, 0);
  rb_define_method(cXMLAttribute, "children?", ruby_xml_attribute_children_q, 0);
  rb_define_method(cXMLAttribute, "default", ruby_xml_attribute_default_get, 0);
  rb_define_method(cXMLAttribute, "element", ruby_xml_attribute_element_get, 0);
  rb_define_method(cXMLAttribute, "last", ruby_xml_attribute_last_get, 0);
  rb_define_method(cXMLAttribute, "last?", ruby_xml_attribute_last_q, 0);
  rb_define_method(cXMLAttribute, "node_type_name", ruby_xml_attribute_node_type_name, 0);
  rb_define_method(cXMLAttribute, "name", ruby_xml_attribute_name_get, 0);
  rb_define_method(cXMLAttribute, "next", ruby_xml_attribute_next_get, 0);
  rb_define_method(cXMLAttribute, "next?", ruby_xml_attribute_next_q, 0);
  rb_define_method(cXMLAttribute, "prefix", ruby_xml_attribute_prefix_get, 0);
  rb_define_method(cXMLAttribute, "prev", ruby_xml_attribute_prev_get, 0);
  rb_define_method(cXMLAttribute, "prev?", ruby_xml_attribute_prev_q, 0);
}