On Mon, 2009-12-07 at 21:35 +0100, Michael 'Mickey' Lauer wrote:
> Am Sonntag, den 06.12.2009, 17:18 +0100 schrieb Jürg Billeter:
> > On Sat, 2009-12-05 at 21:10 +0100, Michael 'Mickey' Lauer wrote:
> > > How can we fix the MarkupParser binding?
> > > 
> > > Apart from the anonymous callbacks, which needs fixing upstream, it
> > > still doesn't quite work, as the delegates complain about their missing
> > > target. How can we fix that?
> > 
> > The MarkupParser binding should be usable, IIRC, although possibly not
> > in the most convenient way and it's easy to get it wrong in a way that
> > valac can't detect. If you have an issue that goes beyond convenience
> > and error detection, please paste a test case and I'll take a look.
> 
> Ok, for a start the attached example which fails with
> 
> mic...@andromeda:/tmp$ valac parse.vala 
> /tmp/parse.vala.c: In function ‘foo_construct’:
> /tmp/parse.vala.c:108: warning: assignment from incompatible pointer
> type
> /tmp/parse.vala.c:108: error: ‘GMarkupParser’ has no member named
> ‘start_element_target’
> /tmp/parse.vala.c:109: error: ‘GMarkupParser’ has no member named
> ‘end_element_target’
> /tmp/parse.vala.c:110: error: ‘GMarkupParser’ has no member named
> ‘text_target’
> /tmp/parse.vala.c:111: error: ‘GMarkupParser’ has no member named
> ‘passthrough_target’
> /tmp/parse.vala.c:112: error: ‘GMarkupParser’ has no member named
> ‘error_target’
> error: cc exited with status 256
> Compilation failed: 1 error(s), 0 warning(s)
> 
> Making the functions in question static doesn't improve it.
> 
> Thanks,
> 
> :M:

Hi Michael,

today I had to resolve the same problem and I think that the
MarkupParser bindings are wrong.

This problem was already reported in the past as you can see here:

http://mail.gnome.org/archives/vala-list/2009-June/msg00030.html

and the proposed solution seems to "partially" work as stated here:

http://mail.gnome.org/archives/vala-list/2009-June/msg00031.html


There is also another problem about the user_data field that in the
current bindings is wrongly assumed to be the MarkupParser instance
pointer but instead it is the user_data parameter passed to the
MarkupParserContext class.

Attached a patch to glib-2.0.vapi and your updated example.

HTH,
        Andrea

using GLib;

class Foo
{
    public MarkupParser parser;
    string _name;
    
    public Foo(string name)
    {
        parser.start_element = start_element_func;
        parser.end_element = end_element_func;
        parser.text = text_func;
        parser.passthrough = passthrough_func;
        parser.error = error_func;
        _name = name;
    }

    private static void start_element_func (MarkupParseContext context, string element_name, string[] attribute_names, string[] attribute_values, void* user_data) throws MarkupError
    {
    	var foo = (Foo) user_data;
        debug( "%s: start element %s", foo._name, element_name);
    }

    private static void end_element_func (MarkupParseContext context, string element_name, void* user_data) throws MarkupError
    {
    	var foo = (Foo) user_data;
        debug( "%s: end element %s", foo._name, element_name);
    }

    private static void text_func (MarkupParseContext context, string text, size_t text_len, void* user_data) throws MarkupError
    {
    	var foo = (Foo) user_data;
        debug( "%s: text %s", foo._name, text);

    }

    private static void passthrough_func (MarkupParseContext context, string passthrough_text, size_t text_len, void* user_data) throws MarkupError
    {
    	var foo = (Foo) user_data;
        debug( "%s: passthrough %s", foo._name, passthrough_text);
    }

    private static void error_func (MarkupParseContext context, Error error, void* user_data)
    {
    	var foo = (Foo) user_data;
        debug( "%s: error %s", foo._name, error.message);
    }
}


void main()
{
    string xml_data = 
"""<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- http://www.w3schools.com/XML/note.xml -->
<note>
	<to>Tove</to>
	<from>Jani</from>
	<heading>Reminder</heading>
	<body>Don't forget me this weekend!</body>
</note>""";

    var foo = new Foo("example markup");
    var context = new MarkupParseContext (foo.parser, MarkupParseFlags.TREAT_CDATA_AS_TEXT, foo, null);
    
    try {
        context.parse (xml_data, xml_data.length);
    } catch (Error err)  {
        print ("error %s", err.message);
    }
    
}
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index 65da48d..a562ab1 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -2803,15 +2803,20 @@ namespace GLib {
 		public void* pop ();
 	}
 	
-	public delegate void MarkupParserStartElementFunc (MarkupParseContext context, string element_name, [CCode (array_length = false, array_null_terminated = true)] string[] attribute_names, [CCode (array_length = false, array_null_terminated = true)] string[] attribute_values) throws MarkupError;
+	[CCode (has_target = false)]
+	public delegate void MarkupParserStartElementFunc (MarkupParseContext context, string element_name, [CCode (array_length = false, array_null_terminated = true)] string[] attribute_names, [CCode (array_length = false, array_null_terminated = true)] string[] attribute_values, void* user_data) throws MarkupError;
 	
-	public delegate void MarkupParserEndElementFunc (MarkupParseContext context, string element_name) throws MarkupError;
+	[CCode (has_target = false)]
+	public delegate void MarkupParserEndElementFunc (MarkupParseContext context, string element_name, void* user_data) throws MarkupError;
 	
-	public delegate void MarkupParserTextFunc (MarkupParseContext context, string text, size_t text_len) throws MarkupError;
+	[CCode (has_target = false)]
+	public delegate void MarkupParserTextFunc (MarkupParseContext context, string text, size_t text_len, void* user_data) throws MarkupError;
 	
-	public delegate void MarkupParserPassthroughFunc (MarkupParseContext context, string passthrough_text, size_t text_len) throws MarkupError;
+	[CCode (has_target = false)]
+	public delegate void MarkupParserPassthroughFunc (MarkupParseContext context, string passthrough_text, size_t text_len, void* user_data) throws MarkupError;
 	
-	public delegate void MarkupParserErrorFunc (MarkupParseContext context, Error error);
+	[CCode (has_target = false)]
+	public delegate void MarkupParserErrorFunc (MarkupParseContext context, Error error, void* user_data);
 	
 	public struct MarkupParser {
 		public unowned MarkupParserStartElementFunc start_element;
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to