/* Rwv2 -- Microsoft Word Parser extension
   Copyright (C) 2003 Hannes Wyss, ywesee

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   ywesee - intellectual capital connected
	 Winterthurerstrasse 52, CH-8006 Zürich, Switzerland
   hwyss@ywesee.com
*/

#include "ruby.h"
#include "rwv2_parser.h"

extern "C" {

VALUE rwv2_parser_is_ok(VALUE self) {
	Rwv2Parser * stub;
	Data_Get_Struct(self, Rwv2Parser, stub);
	if(stub->parser->isOk())
		return Qtrue;
	else
		return Qfalse;
}
VALUE rwv2_parser_parse(VALUE self) {
	Rwv2Parser * stub;
	Data_Get_Struct(self, Rwv2Parser, stub);
	stub->parser->parse();
	return Qnil;
}

#define SET_HANDLER(t_rb, t_wv) \
	VALUE rwv2_parser_set_ ## t_rb ## _handler(VALUE self, VALUE rb_handler) { \
		Rwv2 ## t_wv ## Handler * handler = new Rwv2 ## t_wv ## Handler(rb_handler); \
		Rwv2Parser * stub; \
		Data_Get_Struct(self, Rwv2Parser, stub); \
		stub->rb_ ## t_rb ## _handler = rb_handler; \
		stub->wv_ ## t_rb ## _handler = handler; \
		stub->parser->set ## t_wv ## Handler(handler); \
		return Qnil; \
	}

SET_HANDLER(inline_replacement, InlineReplacement)
SET_HANDLER(subdocument, SubDocument)
SET_HANDLER(table, Table)
SET_HANDLER(text, Text)

/*
VALUE rwv2_parser_set_text_handler(VALUE self, VALUE rb_handler) {
	Rwv2TextHandler * handler = new Rwv2TextHandler(rb_handler);
	Rwv2Parser * stub;
	Data_Get_Struct(self, Rwv2Parser, stub);
	stub->rb_text_handler = rb_handler;
	stub->text_handler = handler;
	stub->parser->setTextHandler(handler);
	return Qnil;
}
VALUE rwv2_parser_set_inline_replacement_handler(VALUE self, VALUE rb_handler) {
	Rwv2InlineReplacementHandler * handler;
	handler = new Rwv2InlineReplacementHandler(rb_handler);
	Rwv2Parser * stub;
	Data_Get_Struct(self, Rwv2Parser, stub);
	stub->rb_inline_replacement_handler = rb_handler;
	stub->inline_replacement_handler = handler;
	stub->parser->setInlineReplacementHandler(handler);
	return Qnil;
}
*/

void rwv2_parser_mark(Rwv2Parser * stub) {
	rb_gc_mark(stub->rb_inline_replacement_handler);
	rb_gc_mark(stub->rb_subdocument_handler);
	rb_gc_mark(stub->rb_table_handler);
	rb_gc_mark(stub->rb_text_handler);
}
void rwv2_parser_free(Rwv2Parser * stub) {
	// delete stub->parser; // messes up the save/restore stack!?
	delete stub->wv_inline_replacement_handler;
	delete stub->wv_subdocument_handler;
	delete stub->wv_table_handler;
	delete stub->wv_text_handler;
	free(stub);
}

} // extern "C"
