]> git.draconx.ca Git - upkg.git/blob - src/module.c
build: Add uobject/package.h to Makefile.am
[upkg.git] / src / module.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright (C) 2009 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 2 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, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <glib-object.h>
25 #include <ltdl.h>
26
27 #include <uobject/module.h>
28 #include <uobject/package.h>
29 #include "avl.h"
30
31 static unsigned initialized;
32 static struct avl_table *package_tree;
33
34 static char *str_cpy_lower(char *dst, const char *src)
35 {
36         size_t i;
37
38         for (i = 0; src[i]; i++)
39                 dst[i] = tolower(src[i]);
40         dst[i] = 0;
41
42         return dst;
43 }
44
45 static int modcmp(const void *a, const void *b, void *_data)
46 {
47         const GTypeModule *ma = a;
48         const GTypeModule *mb = b;
49
50         return strcmp(ma->name, mb->name);
51 }
52
53 int u_object_module_init(void)
54 {
55         if (!initialized) {
56                 package_tree = avl_create(modcmp, NULL, NULL);
57                 if (!package_tree) {
58                         fprintf(stderr, "%s: failed to create package tree.\n", __func__);
59                         return -1;
60                 }
61
62                 g_type_init();
63         }
64
65         initialized++;
66         return 0;
67 }
68
69 int u_object_module_exit(void)
70 {
71         if (--initialized)
72                 return 0;
73
74         avl_destroy(package_tree, NULL);
75
76         return 0;
77 }
78
79 GType u_object_module_get_class(const char *package, const char *class)
80 {
81         char buf[strlen(package) + strlen(class) + 1];
82         GTypeModule search = { .name = str_cpy_lower(buf, package) };
83
84         GTypeModule *mod = avl_find(package_tree, &search);
85         if (!mod) {
86                 void **p;
87
88                 mod = u_pkg_new(package);
89                 if (!mod) {
90                         return 0;
91                 }
92
93                 p = avl_probe(package_tree, mod);
94                 if (!p) {
95                         g_object_unref(mod);
96                         return 0;
97                 }
98         }
99
100         if (!g_type_module_use(mod))
101                 return 0;
102
103         str_cpy_lower(buf+strlen(package), class);
104         buf[0] = toupper(buf[0]);
105         buf[strlen(package)] = toupper(buf[strlen(package)]);
106         return g_type_from_name(buf);
107 }