File: ruby_xml_state.c

package info (click to toggle)
libxml-ruby 0.5.2.0-3%2Blenny1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 652 kB
  • ctags: 875
  • sloc: ansic: 5,874; ruby: 1,524; xml: 144; makefile: 9
file content (114 lines) | stat: -rw-r--r-- 2,130 bytes parent folder | download
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
/* $Id$ */

#include "libxml.h"

VALUE cXMLState;
static VALUE weak_holder;
static int id_inst;

ID id_state;

#undef DEBUG

void
ruby_xml_state_free(int * dummy) {
  if ( dummy==NULL ) return;
  xmlCleanupParser();
  free(dummy);
  dummy=NULL;
  weak_holder=Qnil;
  rb_ivar_set(cXMLState,id_state,Qnil);
}

void
ruby_xml_state_mark(void * a) {
#ifdef DEBUG
  fprintf(stderr,"marked 0x%x\n",NUM2INT(rb_ivar_get(cXMLState,id_state)));
#endif
}

VALUE
ruby_xml_state_object() {
  VALUE obj,id;
  int *dummy;

  id=rb_ivar_get(cXMLState,id_state);
  if (NIL_P(id)) {
    obj = Data_Make_Struct(cXMLState, int,
			   ruby_xml_state_mark,
			   ruby_xml_state_free,
			   dummy);
    rb_ivar_set(cXMLState,id_state,id=rb_obj_id(obj));
    *dummy=0;
    weak_holder=obj;
    return obj;
  }

#ifdef DEBUG
  fprintf(stderr,"getting 0x%x\n",NUM2INT(id));
#endif
  return weak_holder;

  return rb_funcall(rb_const_get(rb_cModule,rb_intern("ObjectSpace")),
		    rb_intern("_id2ref"),
		    1,
		    id);
}

VALUE
ruby_xml_state_object_find_aux(VALUE id) {
  rb_funcall(rb_const_get(rb_cModule,rb_intern("ObjectSpace")),
	     rb_intern("_id2ref"),
	     1,
	     id);
}


static VALUE
find_failed() {
  return Qnil;
}

VALUE
ruby_xml_state_object_find(VALUE id) {
  return rb_rescue(ruby_xml_state_object_find_aux, id, find_failed, 0);
}
/*
 * This will establish a use and mark it or mark an existing use.
 * should be called by parser.mark and document.mark
 */
void
ruby_xml_state_marker(void) {
  ruby_xml_state_object();
  rb_gc_mark(weak_holder);
}

VALUE
ruby_xml_state_used_p(VALUE klass)
{
  return rb_ivar_get(cXMLState,id_state);
}

VALUE
ruby_xml_state_use(VALUE klass)
{
  return ruby_xml_state_object();
}

// Rdoc needs to know 
#ifdef RDOC_NEVER_DEFINED
  mXML = rb_define_module("XML");
#endif


void
ruby_init_state(void) {	
  cXMLState = rb_define_class_under(mXML, "State", rb_cObject);

  rb_define_singleton_method(cXMLState, "used?",
			     ruby_xml_state_used_p, 0);
  rb_define_singleton_method(cXMLState, "use",
			     ruby_xml_state_use, 0);

  rb_ivar_set(cXMLState, id_state=rb_intern("@stateid"), Qnil);
}