]> git.draconx.ca Git - upkg.git/blob - src/uobject/module.c
88748a02f20f0e24eb440519a522fbb9fb8f35e3
[upkg.git] / src / uobject / module.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright © 2009-2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <glib-object.h>
24 #include <ltdl.h>
25
26 #include <uobject/module.h>
27 #include <uobject/package.h>
28 #include "avl.h"
29
30 static unsigned initialized;
31 static struct avl_table *package_tree;
32
33 static char *str_cpy_lower(char *dst, const char *src)
34 {
35         size_t i;
36
37         for (i = 0; src[i]; i++)
38                 dst[i] = tolower(src[i]);
39         dst[i] = 0;
40
41         return dst;
42 }
43
44 static int modcmp(const void *a, const void *b, void *_data)
45 {
46         const GTypeModule *ma = a;
47         const GTypeModule *mb = b;
48
49         return strcmp(ma->name, mb->name);
50 }
51
52 int u_object_module_init(void)
53 {
54         if (!initialized) {
55                 package_tree = avl_create(modcmp, NULL, NULL);
56                 if (!package_tree) {
57                         fprintf(stderr, "%s: failed to create package tree.\n", __func__);
58                         return -1;
59                 }
60
61                 g_type_init();
62         }
63
64         initialized++;
65         return 0;
66 }
67
68 int u_object_module_exit(void)
69 {
70         if (--initialized)
71                 return 0;
72
73         avl_destroy(package_tree, NULL);
74
75         return 0;
76 }
77
78 GType u_object_module_get_class(const char *package, const char *class)
79 {
80         char buf[strlen(package) + strlen(class) + 1];
81         GTypeModule search = { .name = str_cpy_lower(buf, package) };
82
83         GTypeModule *mod = avl_find(package_tree, &search);
84         if (!mod) {
85                 void **p;
86
87                 mod = u_pkg_open(package);
88                 if (!mod) {
89                         return 0;
90                 }
91
92                 p = avl_probe(package_tree, mod);
93                 if (!p) {
94                         g_object_unref(mod);
95                         return 0;
96                 }
97         }
98
99         if (!g_type_module_use(mod))
100                 return 0;
101
102         str_cpy_lower(buf+strlen(package), class);
103         buf[0] = toupper(buf[0]);
104         buf[strlen(package)] = toupper(buf[strlen(package)]);
105         return g_type_from_name(buf);
106 }