#include "rbxs_domattribute.h" #include "rbxs_domnode.h" #include "rbxs_dom.h" //*********************************************************************************** // GC //*********************************************************************************** void rbxs_domattribute_free(rbxs_domattribute *prbxs_domattribute) { if (prbxs_domattribute != NULL) { free(prbxs_domattribute); } } void rbxs_domattribute_mark(rbxs_domattribute *prbxs_domattribute) { if (prbxs_domattribute == NULL) return; if (!NIL_P(prbxs_domattribute->doc)) rb_gc_mark(prbxs_domattribute->doc); } //*********************************************************************************** // Methods //*********************************************************************************** VALUE rbxs_domattribute_inspect(VALUE self) { rbxs_domattribute *prbxs_domattribute; xmlBufferPtr ret; VALUE val; Data_Get_Struct(self, rbxs_domattribute, prbxs_domattribute); ret = xmlBufferCreate(); xmlNodeDump(ret, prbxs_domattribute->attribute->doc, (xmlNodePtr)prbxs_domattribute->attribute, 0, 1); val = rb_str_new2(ret->content); val = rb_str_substr(val,1,RSTRING(val)->len); xmlBufferFree(ret); return(val); } VALUE rbxs_domattribute_value_get(VALUE self) { rbxs_domattribute *prbxs_domattribute; xmlChar *ret; VALUE val; Data_Get_Struct(self, rbxs_domattribute, prbxs_domattribute); ret = xmlGetProp(prbxs_domattribute->attribute->parent,prbxs_domattribute->attribute->name); val = rb_str_new2(ret); xmlFree(ret); return(val); } VALUE rbxs_domattribute_to_i(int argc, VALUE *argv, VALUE self) { VALUE b; int base; rb_scan_args(argc, argv, "01", &b); if (argc == 0) base = 10; else base = NUM2INT(b); if (base < 0) rb_raise(rb_eArgError, "illegal radix %d", base); return(rb_str_to_inum(rbxs_domattribute_value_get(self),base,Qfalse)); } VALUE rbxs_domattribute_to_f(VALUE self) { return rb_float_new(rb_str_to_dbl(rbxs_domattribute_value_get(self), Qfalse)); } VALUE rbxs_domattribute_name_get(VALUE self) { rbxs_domattribute *prbxs_domattribute; Data_Get_Struct(self, rbxs_domattribute, prbxs_domattribute); return(rb_str_new2(prbxs_domattribute->attribute->name)); } VALUE rbxs_domattribute_value_set(VALUE self, VALUE value) { rbxs_domattribute *prbxs_domattribute; Data_Get_Struct(self, rbxs_domattribute, prbxs_domattribute); if (xmlSetProp(prbxs_domattribute->attribute->parent,prbxs_domattribute->attribute->name,STR2CSTR(rb_obj_as_string(value))) == NULL) rb_raise(rb_eRuntimeError, "Couldn't set attribute"); return(value); } VALUE rbxs_domattribute_node(VALUE self) { rbxs_domattribute *prbxs_domattribute; Data_Get_Struct(self, rbxs_domattribute, prbxs_domattribute); return(rbxs_domnode_new(cSimpleDomNode,prbxs_domattribute->doc,prbxs_domattribute->attribute->parent)); } //*********************************************************************************** // Constructors //*********************************************************************************** VALUE rbxs_domattribute_new(VALUE class, VALUE doc, xmlAttrPtr attribute) { rbxs_domattribute *prbxs_domattribute; prbxs_domattribute = (rbxs_domattribute *)malloc(sizeof(rbxs_domattribute)); if (prbxs_domattribute == NULL ) rb_raise(rb_eNoMemError, "No memory left for XML::Simple::Dom::Attribute struct"); prbxs_domattribute->doc = doc; prbxs_domattribute->attribute = attribute; return(Data_Wrap_Struct(class, rbxs_domattribute_mark, rbxs_domattribute_free, prbxs_domattribute)); } //*********************************************************************************** // Initialize class Node //*********************************************************************************** VALUE cSimpleDomAttribute; void init_rbxs_domattribute(void) { cSimpleDomAttribute = rb_define_class_under( cSimpleDom, "Attribute", rb_cObject ); rb_define_method(cSimpleDomAttribute, "inspect", rbxs_domattribute_inspect, 0); rb_define_method(cSimpleDomAttribute, "to_s", rbxs_domattribute_value_get, 0); rb_define_method(cSimpleDomAttribute, "to_i", rbxs_domattribute_to_i, -1); rb_define_method(cSimpleDomAttribute, "to_f", rbxs_domattribute_to_f, 0); rb_define_method(cSimpleDomAttribute, "name", rbxs_domattribute_name_get, 0); rb_define_method(cSimpleDomAttribute, "value", rbxs_domattribute_value_get, 0); rb_define_method(cSimpleDomAttribute, "value=", rbxs_domattribute_value_set, 1); rb_define_method(cSimpleDomAttribute, "node", rbxs_domattribute_node, 0); }