ox 2.10.0 → 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,22 +3,22 @@
3
3
  * All rights reserved.
4
4
  */
5
5
 
6
- #ifndef __OX_SAX_STACK_H__
7
- #define __OX_SAX_STACK_H__
6
+ #ifndef OX_SAX_STACK_H
7
+ #define OX_SAX_STACK_H
8
8
 
9
9
  #include "sax_hint.h"
10
10
 
11
11
  #define STACK_INC 32
12
12
 
13
- typedef struct _Nv {
13
+ typedef struct _nv {
14
14
  const char *name;
15
15
  VALUE val;
16
16
  int childCnt;
17
17
  Hint hint;
18
18
  } *Nv;
19
19
 
20
- typedef struct _NStack {
21
- struct _Nv base[STACK_INC];
20
+ typedef struct _nStack {
21
+ struct _nv base[STACK_INC];
22
22
  Nv head; /* current stack */
23
23
  Nv end; /* stack end */
24
24
  Nv tail; /* pointer to one past last element name on stack */
@@ -27,7 +27,7 @@ typedef struct _NStack {
27
27
  inline static void
28
28
  stack_init(NStack stack) {
29
29
  stack->head = stack->base;
30
- stack->end = stack->base + sizeof(stack->base) / sizeof(struct _Nv);
30
+ stack->end = stack->base + sizeof(stack->base) / sizeof(struct _nv);
31
31
  stack->tail = stack->head;
32
32
  }
33
33
 
@@ -50,10 +50,10 @@ stack_push(NStack stack, const char *name, VALUE val, Hint hint) {
50
50
  size_t toff = stack->tail - stack->head;
51
51
 
52
52
  if (stack->base == stack->head) {
53
- stack->head = ALLOC_N(struct _Nv, len + STACK_INC);
54
- memcpy(stack->head, stack->base, sizeof(struct _Nv) * len);
53
+ stack->head = ALLOC_N(struct _nv, len + STACK_INC);
54
+ memcpy(stack->head, stack->base, sizeof(struct _nv) * len);
55
55
  } else {
56
- REALLOC_N(stack->head, struct _Nv, len + STACK_INC);
56
+ REALLOC_N(stack->head, struct _nv, len + STACK_INC);
57
57
  }
58
58
  stack->tail = stack->head + toff;
59
59
  stack->end = stack->head + len + STACK_INC;
@@ -82,4 +82,4 @@ stack_pop(NStack stack) {
82
82
  return 0;
83
83
  }
84
84
 
85
- #endif /* __OX_SAX_STACK_H__ */
85
+ #endif /* OX_SAX_STACK_H */
@@ -3,11 +3,11 @@
3
3
  * All rights reserved.
4
4
  */
5
5
 
6
- #ifndef __OX_SPECIAL_H__
7
- #define __OX_SPECIAL_H__
6
+ #ifndef OX_SPECIAL_H
7
+ #define OX_SPECIAL_H
8
8
 
9
9
  #include <stdint.h>
10
10
 
11
11
  extern char* ox_ucs_to_utf8_chars(char *text, uint64_t u);
12
12
 
13
- #endif /* __OX_SPECIAL_H__ */
13
+ #endif /* OX_SPECIAL_H */
@@ -3,8 +3,8 @@
3
3
  * All rights reserved.
4
4
  */
5
5
 
6
- #ifndef __OX_TYPE_H__
7
- #define __OX_TYPE_H__
6
+ #ifndef OX_TYPE_H
7
+ #define OX_TYPE_H
8
8
 
9
9
  typedef enum {
10
10
  NoCode = 0,
@@ -36,4 +36,4 @@ typedef enum {
36
36
  NilClassCode = 'z',
37
37
  } Type;
38
38
 
39
- #endif /* __OX_TYPE_H__ */
39
+ #endif /* OX_TYPE_H */
@@ -182,6 +182,30 @@ module Ox
182
182
  found
183
183
  end
184
184
 
185
+ # Remove all the children matching the path provided
186
+ #
187
+ # Examples are:
188
+ # * <code>element.remove_children(Ox:Element)</code> removes the element passed as argument if child of the element.
189
+ # * <code>element.remove_children(Ox:Element, Ox:Element)</code> removes the list of elements passed as argument if children of the element.
190
+ #
191
+ # - +children+ [Array] array of OX
192
+ def remove_children(*children)
193
+ return self if children.compact.empty?
194
+ recursive_children_removal(children.compact.map { |c| c.object_id })
195
+ self
196
+ end
197
+
198
+ # Remove all the children matching the path provided
199
+ #
200
+ # Examples are:
201
+ # * <code>element.remove_children_by_path("*")</code> removes all children attributes.
202
+ # * <code>element.remove_children_by_path("Family/Kid[@age=32]")</code> removes the Kid elements with an age attribute equal to 32 in the Family Element.
203
+ #
204
+ # - +path+ [String] path to the Nodes to locate
205
+ def remove_children_by_path(path)
206
+ del_locate(path.split('/')) unless path.nil?
207
+ self
208
+ end
185
209
  # Handles the 'easy' API that allows navigating a simple XML by
186
210
  # referencing elements and attributes by name.
187
211
  # - +id+ [Symbol] element or attribute name
@@ -307,13 +331,105 @@ module Ox
307
331
  end
308
332
  end
309
333
 
334
+ # - +path+ [Array] array of steps in a path
335
+ def del_locate(path)
336
+ step = path[0]
337
+ if step.start_with?('@') # attribute
338
+ raise InvalidPath.new(path) unless 1 == path.size
339
+ if instance_variable_defined?(:@attributes)
340
+ step = step[1..-1]
341
+ sym_step = step.to_sym
342
+ @attributes.delete_if { |k,v| '?' == step || k.to_sym == sym_step }
343
+ end
344
+ else # element name
345
+ if (i = step.index('[')).nil? # just name
346
+ name = step
347
+ qual = nil
348
+ else
349
+ name = step[0..i-1]
350
+ raise InvalidPath.new(path) unless step.end_with?(']')
351
+ i += 1
352
+ qual = step[i..i] # step[i] would be better but some rubies (jruby, ree, rbx) take that as a Fixnum.
353
+ if '0' <= qual and qual <= '9'
354
+ qual = '+'
355
+ else
356
+ i += 1
357
+ end
358
+ index = step[i..-2].to_i
359
+ end
360
+ if '?' == name or '*' == name
361
+ match = nodes
362
+ elsif '^' == name[0..0] # 1.8.7 thinks name[0] is a fixnum
363
+ case name[1..-1]
364
+ when 'Element'
365
+ match = nodes.select { |e| e.is_a?(Element) }
366
+ when 'String', 'Text'
367
+ match = nodes.select { |e| e.is_a?(String) }
368
+ when 'Comment'
369
+ match = nodes.select { |e| e.is_a?(Comment) }
370
+ when 'CData'
371
+ match = nodes.select { |e| e.is_a?(CData) }
372
+ when 'DocType'
373
+ match = nodes.select { |e| e.is_a?(DocType) }
374
+ else
375
+ #puts "*** no match on #{name}"
376
+ match = []
377
+ end
378
+ else
379
+ match = nodes.select { |e| e.is_a?(Element) and name == e.name }
380
+ end
381
+ unless qual.nil? or match.empty?
382
+ case qual
383
+ when '+'
384
+ match = index < match.size ? [match[index]] : []
385
+ when '-'
386
+ match = index <= match.size ? [match[-index]] : []
387
+ when '<'
388
+ match = 0 < index ? match[0..index - 1] : []
389
+ when '>'
390
+ match = index <= match.size ? match[index + 1..-1] : []
391
+ when '@'
392
+ k,v = step[i..-2].split('=')
393
+ if v
394
+ match = match.select { |n| n.is_a?(Element) && (v == n.attributes[k.to_sym] || v == n.attributes[k]) }
395
+ else
396
+ match = match.select { |n| n.is_a?(Element) && (n.attributes[k.to_sym] || n.attributes[k]) }
397
+ end
398
+ else
399
+ raise InvalidPath.new(path)
400
+ end
401
+ end
402
+ if (1 == path.size)
403
+ nodes.delete_if { |n| match.include?(n) }
404
+ elsif '*' == name
405
+ match.each { |n| n.del_locate(path) if n.is_a?(Element) }
406
+ match.each { |n| n.del_locate(path[1..-1]) if n.is_a?(Element) }
407
+ else
408
+ match.each { |n| n.del_locate(path[1..-1]) if n.is_a?(Element) }
409
+ end
410
+ end
411
+ end
412
+
310
413
  private
311
414
 
415
+ # Removes recursively children for nodes and sub_nodes
416
+ #
417
+ # - +found+ [Array] An array of Ox::Element
418
+ def recursive_children_removal(found)
419
+ return if found.empty?
420
+ nodes.tap do |ns|
421
+ # found.delete(n.object_id) stops looking for an already found object_id
422
+ ns.delete_if { |n| found.include?(n.object_id) ? found.delete(n.object_id) : false }
423
+ nodes.each do |n|
424
+ n.send(:recursive_children_removal, found) if n.is_a?(Ox::Element)
425
+ end
426
+ end
427
+ end
428
+
312
429
  def name_matchs?(pat, id)
313
430
  return false unless pat.length == id.length
314
431
  pat.length.times { |i| return false unless '_' == id[i] || pat[i] == id[i] }
315
432
  true
316
433
  end
317
-
318
434
  end # Element
319
435
  end # Ox
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Ox
3
3
  # Current version of the module.
4
- VERSION = '2.10.0'
4
+ VERSION = '2.10.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ox
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.0
4
+ version: 2.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-03 00:00:00.000000000 Z
11
+ date: 2019-05-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: "A fast XML parser and object serializer that uses only standard C lib.\n
14
14
  \ \nOptimized XML (Ox), as the name implies was written to provide speed
@@ -35,6 +35,7 @@ files:
35
35
  - ext/ox/cache8.c
36
36
  - ext/ox/cache8.h
37
37
  - ext/ox/dump.c
38
+ - ext/ox/encode.h
38
39
  - ext/ox/err.c
39
40
  - ext/ox/err.h
40
41
  - ext/ox/extconf.rb
@@ -98,8 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
99
  - !ruby/object:Gem::Version
99
100
  version: '0'
100
101
  requirements: []
101
- rubyforge_project: ox
102
- rubygems_version: 2.7.6
102
+ rubygems_version: 3.0.3
103
103
  signing_key:
104
104
  specification_version: 4
105
105
  summary: A fast XML parser and object serializer.