announce project is moving outside of garage.maemo.org
[lms] / ruby-lightmediascanner / lightmediascanner_parser.c
1 /**
2  * Copyright (C) 2007 by Levi Bard
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * @author Levi Bard <taktaktaktaktaktaktaktaktaktak@gmail.com>
19  */
20
21 #include "lightmediascanner_parser.h"
22
23 typedef struct {
24         lms_plugin_t *plugin;
25         lms_t *parent;
26 } ParserRuby;
27
28 /**
29  *
30  * Native memory deletion function 
31  *
32  * * return nil
33  *
34  */
35 static VALUE parser_free(ParserRuby *parser) {
36         if(parser) {
37                 if(parser->plugin) {
38                         lms_parser_del(parser->parent, parser->plugin);
39                 }// delete plugin
40
41                 free(parser);
42         }
43
44         return Qnil;
45 }// parser_free
46
47 #if 0
48 // Obsolete
49 /**
50  *
51  * Add parser plugin, given its shared object path.
52  *
53  * * p1 (name) path to shared object (usable by dlopen(3)).
54  *
55  * * return A new Parser
56  *
57  */
58 static VALUE scanner_add_parser(VALUE self, VALUE name) {
59         lms_t *lms;
60         lms_plugin_t *plugin = NULL;
61         char *cname = NULL;
62
63         Check_Type(name, T_STRING);
64
65         Data_Get_Struct(self, lms_t, lms);
66         cname = StringValuePtr(name);
67
68         if(!(plugin = lms_parser_add(lms, cname))) {
69                 plugin = lms_parser_find_and_add(lms, cname);
70                 if(!plugin) {
71                         rb_raise(rb_eRuntimeError, "Unable to load plugin '%s'", cname);
72                 }// throw!
73         }// search
74
75         //wrap and return plugin
76         return lightmediascanner_parser_new(lms, plugin);
77 }// scanner_add_parser
78 #endif
79
80 /**
81  *
82  * Parser#new
83  *
84  * constructor
85  *
86  * * p1 (parent) The Scanner to which the parser belongs
87  *
88  * * p2 (name) path to shared object (usable by dlopen(3)).
89  *
90  * * return A new Parser
91  *
92  */
93 static VALUE parser_new(VALUE obj, VALUE parent, VALUE name) {
94         lms_t *lms = NULL;
95         char *cname = NULL;
96         ParserRuby *parser = NULL;
97         lms_plugin_t *plugin = NULL;
98
99         Check_Type(name, T_STRING);
100
101         Data_Get_Struct(parent, lms_t, lms);
102         cname = StringValuePtr(name);
103
104         if(!(plugin = lms_parser_add(lms, cname))) {
105                 plugin = lms_parser_find_and_add(lms, cname);
106                 if(!plugin) {
107                         rb_raise(rb_eRuntimeError, "Unable to load plugin '%s'", cname);
108                 }// throw!
109         }// search
110
111         parser = (ParserRuby*)malloc(sizeof(ParserRuby));
112         parser->parent = lms;
113         parser->plugin = plugin;
114
115         return Data_Wrap_Struct(obj, NULL, parser_free, parser);
116 }// scanner_new
117
118 /**
119  * LightMediaScanner::Parser is a class to represent 
120  * a LightMediaScanner plugin.
121  */
122 void Init_lightmediascanner_parser() {
123         lightmediascanner_parser = rb_define_class_under(lightmediascanner_module, "Parser", rb_cObject);
124         rb_define_singleton_method(lightmediascanner_parser, "new", parser_new, 2);
125 }// Init_lightmediaparser_parser