ovirt-engine-sdk 4.0.1 → 4.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright (c) 2015-2016 Red Hat, Inc.
2
+ Copyright (c) 2015-2017 Red Hat, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -30,63 +30,79 @@ limitations under the License.
30
30
  /* Class: */
31
31
  VALUE ov_xml_reader_class;
32
32
 
33
- // Method identifiers:
33
+ /* Method identifiers: */
34
34
  static ID READ_ID;
35
35
  static ID STRING_IO_ID;
36
36
 
37
- typedef struct {
38
- VALUE io;
39
- xmlTextReaderPtr reader;
40
- bool closed;
41
- } ov_xml_reader_object;
42
-
43
- static void ov_xml_reader_check_closed(ov_xml_reader_object* object) {
44
- if (object->closed) {
37
+ static void ov_xml_reader_check_closed(ov_xml_reader_object* ptr) {
38
+ if (ptr->closed) {
45
39
  rb_raise(ov_error_class, "The reader is already closed");
46
40
  }
47
41
  }
48
42
 
49
- static void ov_xml_reader_mark(ov_xml_reader_object *object) {
50
- /* Mark the IO object as reachable: */
51
- if (!NIL_P(object->io)) {
52
- rb_gc_mark(object->io);
53
- }
43
+ static void ov_xml_reader_mark(void* vptr) {
44
+ ov_xml_reader_object* ptr;
45
+
46
+ ptr = vptr;
47
+ rb_gc_mark(ptr->io);
54
48
  }
55
49
 
56
- static void ov_xml_reader_free(ov_xml_reader_object *object) {
50
+ static void ov_xml_reader_free(void* vptr) {
51
+ ov_xml_reader_object* ptr;
52
+
53
+ /* Get the pointer: */
54
+ ptr = vptr;
55
+
57
56
  /* Free the libxml reader: */
58
- if (!object->closed) {
59
- if (object->reader != NULL) {
60
- xmlFreeTextReader(object->reader);
57
+ if (!ptr->closed) {
58
+ if (ptr->reader != NULL) {
59
+ xmlFreeTextReader(ptr->reader);
61
60
  }
62
- object->reader = NULL;
63
- object->closed = true;
61
+ ptr->reader = NULL;
62
+ ptr->closed = true;
64
63
  }
65
64
 
66
65
  /* Free this object: */
67
- xfree(object);
66
+ xfree(ptr);
68
67
  }
69
68
 
69
+ rb_data_type_t ov_xml_reader_type = {
70
+ .wrap_struct_name = "OVXMLREADER",
71
+ .function = {
72
+ .dmark = ov_xml_reader_mark,
73
+ .dfree = ov_xml_reader_free,
74
+ .dsize = NULL,
75
+ .reserved = { NULL, NULL }
76
+ },
77
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
78
+ .parent = NULL,
79
+ .data = NULL,
80
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
81
+ #endif
82
+ };
83
+
70
84
  static VALUE ov_xml_reader_alloc(VALUE klass) {
71
- ov_xml_reader_object* object = NULL;
85
+ ov_xml_reader_object* ptr;
72
86
 
73
- object = ALLOC(ov_xml_reader_object);
74
- memset(object, 0, sizeof(ov_xml_reader_object));
75
- return Data_Wrap_Struct(klass, ov_xml_reader_mark, ov_xml_reader_free, object);
87
+ ptr = ALLOC(ov_xml_reader_object);
88
+ ptr->io = Qnil;
89
+ ptr->reader = NULL;
90
+ ptr->closed = false;
91
+ return TypedData_Wrap_Struct(klass, &ov_xml_reader_type, ptr);
76
92
  }
77
93
 
78
94
  static int ov_xml_reader_callback(void *context, char *buffer, int length) {
79
95
  VALUE data;
80
- ov_xml_reader_object* object = NULL;
96
+ ov_xml_reader_object* ptr;
81
97
 
82
98
  /* Do nothing if the reader is already closed: */
83
- object = (ov_xml_reader_object*) context;
84
- if (object->closed) {
99
+ ptr = (ov_xml_reader_object*) context;
100
+ if (ptr->closed) {
85
101
  return -1;
86
102
  }
87
103
 
88
104
  /* Read from the Ruby IO object, and copy the result to the buffer: */
89
- data = rb_funcall(object->io, READ_ID, 1, INT2NUM(length));
105
+ data = rb_funcall(ptr->io, READ_ID, 1, INT2NUM(length));
90
106
  if (NIL_P(data)) {
91
107
  return 0;
92
108
  }
@@ -107,20 +123,20 @@ static VALUE ov_xml_reader_create_string_io(VALUE text) {
107
123
 
108
124
  static VALUE ov_xml_reader_initialize(VALUE self, VALUE io) {
109
125
  VALUE io_class;
110
- int rc = 0;
111
- ov_xml_reader_object* object = NULL;
126
+ int rc;
127
+ ov_xml_reader_object* ptr;
112
128
 
113
129
  /* Get the pointer to the object: */
114
- Data_Get_Struct(self, ov_xml_reader_object, object);
130
+ ov_xml_reader_ptr(self, ptr);
115
131
 
116
132
  /* The parameter of the constructor can be a string or an IO object. If it is a string then we need to create aa
117
133
  IO object to read from it. */
118
134
  io_class = rb_class_of(io);
119
135
  if (io_class == rb_cString) {
120
- object->io = ov_xml_reader_create_string_io(io);
136
+ ptr->io = ov_xml_reader_create_string_io(io);
121
137
  }
122
138
  else if (io_class == rb_cIO) {
123
- object->io = io;
139
+ ptr->io = io;
124
140
  }
125
141
  else {
126
142
  rb_raise(
@@ -131,16 +147,16 @@ static VALUE ov_xml_reader_initialize(VALUE self, VALUE io) {
131
147
  }
132
148
 
133
149
  /* Clear the closed flag: */
134
- object->closed = false;
150
+ ptr->closed = false;
135
151
 
136
152
  /* Create the libxml reader: */
137
- object->reader = xmlReaderForIO(ov_xml_reader_callback, NULL, object, NULL, NULL, 0);
138
- if (object->reader == NULL) {
153
+ ptr->reader = xmlReaderForIO(ov_xml_reader_callback, NULL, ptr, NULL, NULL, 0);
154
+ if (ptr->reader == NULL) {
139
155
  rb_raise(ov_error_class, "Can't create reader");
140
156
  }
141
157
 
142
158
  /* Move the cursor to the first node: */
143
- rc = xmlTextReaderRead(object->reader);
159
+ rc = xmlTextReaderRead(ptr->reader);
144
160
  if (rc == -1) {
145
161
  rb_raise(ov_error_class, "Can't read first node");
146
162
  }
@@ -150,11 +166,11 @@ static VALUE ov_xml_reader_initialize(VALUE self, VALUE io) {
150
166
 
151
167
  static VALUE ov_xml_reader_read(VALUE self) {
152
168
  int rc = 0;
153
- ov_xml_reader_object* object = NULL;
169
+ ov_xml_reader_object* ptr;
154
170
 
155
- Data_Get_Struct(self, ov_xml_reader_object, object);
156
- ov_xml_reader_check_closed(object);
157
- rc = xmlTextReaderRead(object->reader);
171
+ ov_xml_reader_ptr(self, ptr);
172
+ ov_xml_reader_check_closed(ptr);
173
+ rc = xmlTextReaderRead(ptr->reader);
158
174
  if (rc == 0) {
159
175
  return Qfalse;
160
176
  }
@@ -168,13 +184,13 @@ static VALUE ov_xml_reader_read(VALUE self) {
168
184
  static VALUE ov_xml_reader_forward(VALUE self) {
169
185
  int c_type = 0;
170
186
  int rc = 0;
171
- ov_xml_reader_object *object = NULL;
187
+ ov_xml_reader_object* ptr;
172
188
 
173
- Data_Get_Struct(self, ov_xml_reader_object, object);
174
- ov_xml_reader_check_closed(object);
189
+ ov_xml_reader_ptr(self, ptr);
190
+ ov_xml_reader_check_closed(ptr);
175
191
 
176
192
  for (;;) {
177
- c_type = xmlTextReaderNodeType(object->reader);
193
+ c_type = xmlTextReaderNodeType(ptr->reader);
178
194
  if (c_type == -1) {
179
195
  rb_raise(ov_error_class, "Can't get current node type");
180
196
  }
@@ -185,7 +201,7 @@ static VALUE ov_xml_reader_forward(VALUE self) {
185
201
  return Qfalse;
186
202
  }
187
203
  else {
188
- rc = xmlTextReaderRead(object->reader);
204
+ rc = xmlTextReaderRead(ptr->reader);
189
205
  if (rc == -1) {
190
206
  rb_raise(ov_error_class, "Can't move to next node");
191
207
  }
@@ -195,12 +211,12 @@ static VALUE ov_xml_reader_forward(VALUE self) {
195
211
 
196
212
  static VALUE ov_xml_reader_node_name(VALUE self) {
197
213
  VALUE name;
198
- const xmlChar* c_name = NULL;
199
- ov_xml_reader_object* object = NULL;
214
+ const xmlChar* c_name;
215
+ ov_xml_reader_object* ptr;
200
216
 
201
- Data_Get_Struct(self, ov_xml_reader_object, object);
202
- ov_xml_reader_check_closed(object);
203
- c_name = xmlTextReaderConstName(object->reader);
217
+ ov_xml_reader_ptr(self, ptr);
218
+ ov_xml_reader_check_closed(ptr);
219
+ c_name = xmlTextReaderConstName(ptr->reader);
204
220
  if (c_name == NULL) {
205
221
  return Qnil;
206
222
  }
@@ -209,12 +225,12 @@ static VALUE ov_xml_reader_node_name(VALUE self) {
209
225
  }
210
226
 
211
227
  static VALUE ov_xml_reader_empty_element(VALUE self) {
212
- int c_empty = 0;
213
- ov_xml_reader_object* object = NULL;
228
+ int c_empty;
229
+ ov_xml_reader_object* ptr;
214
230
 
215
- Data_Get_Struct(self, ov_xml_reader_object, object);
216
- ov_xml_reader_check_closed(object);
217
- c_empty = xmlTextReaderIsEmptyElement(object->reader);
231
+ ov_xml_reader_ptr(self, ptr);
232
+ ov_xml_reader_check_closed(ptr);
233
+ c_empty = xmlTextReaderIsEmptyElement(ptr->reader);
218
234
  if (c_empty == -1) {
219
235
  rb_raise(ov_error_class, "Can't check if current element is empty");
220
236
  }
@@ -223,14 +239,14 @@ static VALUE ov_xml_reader_empty_element(VALUE self) {
223
239
 
224
240
  static VALUE ov_xml_reader_get_attribute(VALUE self, VALUE name) {
225
241
  VALUE value;
226
- ov_xml_reader_object* object = NULL;
227
- xmlChar* c_name = NULL;
228
- xmlChar* c_value = NULL;
242
+ ov_xml_reader_object* ptr;
243
+ xmlChar* c_name;
244
+ xmlChar* c_value;
229
245
 
230
- Data_Get_Struct(self, ov_xml_reader_object, object);
231
- ov_xml_reader_check_closed(object);
246
+ ov_xml_reader_ptr(self, ptr);
247
+ ov_xml_reader_check_closed(ptr);
232
248
  c_name = (xmlChar*) StringValueCStr(name);
233
- c_value = xmlTextReaderGetAttribute(object->reader, c_name);
249
+ c_value = xmlTextReaderGetAttribute(ptr->reader, c_name);
234
250
  if (c_value == NULL) {
235
251
  return Qnil;
236
252
  }
@@ -241,17 +257,17 @@ static VALUE ov_xml_reader_get_attribute(VALUE self, VALUE name) {
241
257
 
242
258
  static VALUE ov_xml_reader_read_element(VALUE self) {
243
259
  VALUE value;
244
- int c_empty = 0;
245
- int c_type = 0;
246
- int rc = 0;
247
- ov_xml_reader_object *object = NULL;
248
- xmlChar *c_value = NULL;
260
+ int c_empty;
261
+ int c_type;
262
+ int rc;
263
+ ov_xml_reader_object* ptr;
264
+ xmlChar* c_value;
249
265
 
250
- Data_Get_Struct(self, ov_xml_reader_object, object);
251
- ov_xml_reader_check_closed(object);
266
+ ov_xml_reader_ptr(self, ptr);
267
+ ov_xml_reader_check_closed(ptr);
252
268
 
253
269
  /* Check the type of the current node: */
254
- c_type = xmlTextReaderNodeType(object->reader);
270
+ c_type = xmlTextReaderNodeType(ptr->reader);
255
271
  if (c_type == -1) {
256
272
  rb_raise(ov_error_class, "Can't get current node type");
257
273
  }
@@ -260,7 +276,7 @@ static VALUE ov_xml_reader_read_element(VALUE self) {
260
276
  }
261
277
 
262
278
  /* Check if the current node is empty: */
263
- c_empty = xmlTextReaderIsEmptyElement(object->reader);
279
+ c_empty = xmlTextReaderIsEmptyElement(ptr->reader);
264
280
  if (c_empty == -1) {
265
281
  rb_raise(ov_error_class, "Can't check if current element is empty");
266
282
  }
@@ -271,7 +287,7 @@ static VALUE ov_xml_reader_read_element(VALUE self) {
271
287
  c_value = NULL;
272
288
  }
273
289
  else {
274
- c_value = xmlTextReaderReadString(object->reader);
290
+ c_value = xmlTextReaderReadString(ptr->reader);
275
291
  if (c_value == NULL) {
276
292
  c_value = xmlCharStrdup("");
277
293
  if (c_value == NULL) {
@@ -281,7 +297,7 @@ static VALUE ov_xml_reader_read_element(VALUE self) {
281
297
  }
282
298
 
283
299
  /* Move to the next element: */
284
- rc = xmlTextReaderNext(object->reader);
300
+ rc = xmlTextReaderNext(ptr->reader);
285
301
  if (rc == -1) {
286
302
  if (c_value != NULL) {
287
303
  xmlFree(c_value);
@@ -301,14 +317,14 @@ static VALUE ov_xml_reader_read_element(VALUE self) {
301
317
  static VALUE ov_xml_reader_read_elements(VALUE self) {
302
318
  VALUE element;
303
319
  VALUE list;
304
- int c_empty = 0;
305
- int c_type = 0;
306
- int rc = 0;
307
- ov_xml_reader_object* object = NULL;
320
+ int c_empty;
321
+ int c_type;
322
+ int rc;
323
+ ov_xml_reader_object* ptr;
308
324
 
309
325
  /* Get the pointer to the object and check that it isn't closed: */
310
- Data_Get_Struct(self, ov_xml_reader_object, object);
311
- ov_xml_reader_check_closed(object);
326
+ ov_xml_reader_ptr(self, ptr);
327
+ ov_xml_reader_check_closed(ptr);
312
328
 
313
329
  /* This method assumes that the reader is positioned at the element that contains the values to read. For example
314
330
  if the XML document is the following:
@@ -319,7 +335,7 @@ static VALUE ov_xml_reader_read_elements(VALUE self) {
319
335
  </list>
320
336
 
321
337
  The reader should be positioned at the <list> element. The first thing we need to do is to check: */
322
- c_type = xmlTextReaderNodeType(object->reader);
338
+ c_type = xmlTextReaderNodeType(ptr->reader);
323
339
  if (c_type == -1) {
324
340
  rb_raise(ov_error_class, "Can't get current node type");
325
341
  }
@@ -329,14 +345,14 @@ static VALUE ov_xml_reader_read_elements(VALUE self) {
329
345
 
330
346
  /* If we are indeed positioned at the first element, then we need to check if it is empty, <list/>, as we will
331
347
  need this lter, after discarding the element: */
332
- c_empty = xmlTextReaderIsEmptyElement(object->reader);
348
+ c_empty = xmlTextReaderIsEmptyElement(ptr->reader);
333
349
  if (c_empty == -1) {
334
350
  rb_raise(ov_error_class, "Can't check if current element is empty");
335
351
  }
336
352
 
337
353
  /* Now we need to discard the current element, as we are interested only in the nested <value>...</value>
338
354
  elements: */
339
- rc = xmlTextReaderRead(object->reader);
355
+ rc = xmlTextReaderRead(ptr->reader);
340
356
  if (rc == -1) {
341
357
  rb_raise(ov_error_class, "Can't move to next node");
342
358
  }
@@ -351,7 +367,7 @@ static VALUE ov_xml_reader_read_elements(VALUE self) {
351
367
 
352
368
  /* Process the nested elements: */
353
369
  for (;;) {
354
- c_type = xmlTextReaderNodeType(object->reader);
370
+ c_type = xmlTextReaderNodeType(ptr->reader);
355
371
  if (c_type == -1) {
356
372
  rb_raise(ov_error_class, "Can't get current node type");
357
373
  }
@@ -363,7 +379,7 @@ static VALUE ov_xml_reader_read_elements(VALUE self) {
363
379
  break;
364
380
  }
365
381
  else {
366
- rc = xmlTextReaderNext(object->reader);
382
+ rc = xmlTextReaderNext(ptr->reader);
367
383
  if (rc == -1) {
368
384
  rb_raise(ov_error_class, "Can't move to the next node");
369
385
  }
@@ -373,7 +389,7 @@ static VALUE ov_xml_reader_read_elements(VALUE self) {
373
389
  /* Once all the nested <value>...</value> elements are processed the reader will be positioned at the closing
374
390
  </list> element, or at the end of the document. If it is the closing element then we need to discard it. */
375
391
  if (c_type == XML_READER_TYPE_END_ELEMENT) {
376
- rc = xmlTextReaderRead(object->reader);
392
+ rc = xmlTextReaderRead(ptr->reader);
377
393
  if (rc == -1) {
378
394
  rb_raise(ov_error_class, "Can't move to next node");
379
395
  }
@@ -383,12 +399,12 @@ static VALUE ov_xml_reader_read_elements(VALUE self) {
383
399
  }
384
400
 
385
401
  static VALUE ov_xml_reader_next_element(VALUE self) {
386
- int rc = 0;
387
- ov_xml_reader_object* object = NULL;
402
+ int rc;
403
+ ov_xml_reader_object* ptr;
388
404
 
389
- Data_Get_Struct(self, ov_xml_reader_object, object);
390
- ov_xml_reader_check_closed(object);
391
- rc = xmlTextReaderNext(object->reader);
405
+ ov_xml_reader_ptr(self, ptr);
406
+ ov_xml_reader_check_closed(ptr);
407
+ rc = xmlTextReaderNext(ptr->reader);
392
408
  if (rc == 0) {
393
409
  return Qfalse;
394
410
  }
@@ -399,13 +415,13 @@ static VALUE ov_xml_reader_next_element(VALUE self) {
399
415
  }
400
416
 
401
417
  static VALUE ov_xml_reader_close(VALUE self) {
402
- ov_xml_reader_object* object = NULL;
418
+ ov_xml_reader_object* ptr;
403
419
 
404
- Data_Get_Struct(self, ov_xml_reader_object, object);
405
- ov_xml_reader_check_closed(object);
406
- xmlFreeTextReader(object->reader);
407
- object->reader = NULL;
408
- object->closed = true;
420
+ ov_xml_reader_ptr(self, ptr);
421
+ ov_xml_reader_check_closed(ptr);
422
+ xmlFreeTextReader(ptr->reader);
423
+ ptr->reader = NULL;
424
+ ptr->closed = true;
409
425
  return Qnil;
410
426
  }
411
427
 
@@ -414,7 +430,7 @@ void ov_xml_reader_define(void) {
414
430
  rb_require("stringio");
415
431
 
416
432
  /* Define the class: */
417
- ov_xml_reader_class = rb_define_class_under(ov_module, "XmlReader", rb_cObject);
433
+ ov_xml_reader_class = rb_define_class_under(ov_module, "XmlReader", rb_cData);
418
434
 
419
435
  /* Define the constructor: */
420
436
  rb_define_alloc_func(ov_xml_reader_class, ov_xml_reader_alloc);
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright (c) 2015-2016 Red Hat, Inc.
2
+ Copyright (c) 2015-2017 Red Hat, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -17,10 +17,27 @@ limitations under the License.
17
17
  #ifndef __OV_XML_READER_H__
18
18
  #define __OV_XML_READER_H__
19
19
 
20
- // Classes:
20
+ #include <ruby.h>
21
+
22
+ #include <libxml/xmlreader.h>
23
+ #include <stdbool.h>
24
+
25
+ /* Data type and class: */
26
+ extern rb_data_type_t ov_xml_reader_type;
21
27
  extern VALUE ov_xml_reader_class;
22
28
 
23
- // Initialization function:
29
+ /* Content: */
30
+ typedef struct {
31
+ VALUE io;
32
+ xmlTextReaderPtr reader;
33
+ bool closed;
34
+ } ov_xml_reader_object;
35
+
36
+ /* Macro to get the pointer: */
37
+ #define ov_xml_reader_ptr(object, ptr) \
38
+ TypedData_Get_Struct((object), ov_xml_reader_object, &ov_xml_reader_type, (ptr))
39
+
40
+ /* Initialization function: */
24
41
  extern void ov_xml_reader_define(void);
25
42
 
26
43
  #endif