comparison src/audacious/tuple.c @ 3278:04df6bd984ad trunk

Tuple API try 1.
author William Pitcock <nenolod@atheme-project.org>
date Thu, 09 Aug 2007 03:13:01 -0500
parents
children a26138e391ee
comparison
equal deleted inserted replaced
3273:f24fe60cce1c 3278:04df6bd984ad
1 /*
2 * Audacious
3 * Copyright (c) 2006-2007 Audacious team
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; under version 3 of the License.
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 General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses>.
16 *
17 * The Audacious team does not consider modular code linking to
18 * Audacious or using our public API to be a derived work.
19 */
20
21 #include <glib.h>
22 #include <mowgli.h>
23
24 #include "tuple.h"
25
26 struct _Tuple {
27 mowgli_object_t parent;
28 mowgli_dictionary_t *dict;
29 };
30
31 typedef struct {
32 TupleValueType type;
33 union {
34 gchar *string;
35 gint integer;
36 } value;
37 } TupleValue;
38
39 static mowgli_heap_t *tuple_heap = NULL;
40 static mowgli_heap_t *tuple_value_heap = NULL;
41 static mowgli_object_class_t tuple_klass;
42
43 static void
44 tuple_destroy(Tuple *tuple)
45 {
46 mowgli_dictionary_destroy(tuple->dict);
47 mowgli_heap_free(tuple_heap, tuple);
48 }
49
50 Tuple *
51 tuple_new(void)
52 {
53 Tuple *tuple;
54
55 if (tuple_heap == NULL)
56 {
57 tuple_heap = mowgli_heap_create(sizeof(Tuple), 256);
58 tuple_value_heap = mowgli_heap_create(sizeof(TupleValue), 512);
59 mowgli_object_class_init(&tuple_klass, "audacious.tuple", tuple_destroy, FALSE);
60 }
61
62 /* FIXME: use mowgli_object_bless_from_class() in mowgli 0.4
63 when it is released --nenolod */
64 tuple = mowgli_heap_alloc(tuple_heap);
65 mowgli_object_init(mowgli_object(tuple), NULL, &tuple_klass, NULL);
66
67 tuple->dict = mowgli_dictionary_create(g_ascii_strcasecmp);
68
69 return tuple;
70 }
71
72 gboolean
73 tuple_associate_string(Tuple *tuple, const gchar *field, const gchar *string)
74 {
75 TupleValue *value;
76
77 g_return_val_if_fail(tuple != NULL, FALSE);
78 g_return_val_if_fail(field != NULL, FALSE);
79 g_return_val_if_fail(string != NULL, FALSE);
80
81 if (mowgli_dictionary_find(tuple->dict, field))
82 tuple_disassociate(tuple, field);
83
84 value = mowgli_heap_alloc(tuple_value_heap);
85 value->type = TUPLE_STRING;
86 value->value.string = g_strdup(string);
87
88 mowgli_dictionary_add(tuple->dict, field, value);
89
90 return TRUE;
91 }
92
93 gboolean
94 tuple_associate_int(Tuple *tuple, const gchar *field, gint integer)
95 {
96 TupleValue *value;
97
98 g_return_val_if_fail(tuple != NULL, FALSE);
99 g_return_val_if_fail(field != NULL, FALSE);
100
101 if (mowgli_dictionary_find(tuple->dict, field))
102 tuple_disassociate(tuple, field);
103
104 value = mowgli_heap_alloc(tuple_value_heap);
105 value->type = TUPLE_INT;
106 value->value.integer = integer;
107
108 mowgli_dictionary_add(tuple->dict, field, value);
109
110 return TRUE;
111 }
112
113 void
114 tuple_disassociate(Tuple *tuple, const gchar *field)
115 {
116 TupleValue *value;
117
118 g_return_if_fail(tuple != NULL);
119 g_return_if_fail(field != NULL);
120
121 /* why _delete()? because _delete() returns the dictnode's data on success */
122 if ((value = mowgli_dictionary_delete(tuple->dict, field)) == NULL)
123 return;
124
125 if (value->type == TUPLE_STRING)
126 g_free(value->value.string);
127
128 mowgli_heap_free(tuple_value_heap, value);
129 }
130
131 TupleValueType
132 tuple_get_value_type(Tuple *tuple, const gchar *field)
133 {
134 TupleValue *value;
135
136 g_return_if_fail(tuple != NULL);
137 g_return_if_fail(field != NULL);
138
139 if ((value = mowgli_dictionary_retrieve(tuple->dict, field)) == NULL)
140 return TUPLE_UNKNOWN;
141
142 return value->type;
143 }
144
145 const gchar *
146 tuple_get_string(Tuple *tuple, const gchar *field)
147 {
148 TupleValue *value;
149
150 g_return_if_fail(tuple != NULL);
151 g_return_if_fail(field != NULL);
152
153 if ((value = mowgli_dictionary_retrieve(tuple->dict, field)) == NULL)
154 return NULL;
155
156 if (value->type != TUPLE_STRING)
157 mowgli_throw_exception_val(audacious.tuple.invalid_type_request, NULL);
158
159 return value->value.string;
160 }
161
162 int
163 tuple_get_int(Tuple *tuple, const gchar *field)
164 {
165 TupleValue *value;
166
167 g_return_if_fail(tuple != NULL);
168 g_return_if_fail(field != NULL);
169
170 if ((value = mowgli_dictionary_retrieve(tuple->dict, field)) == NULL)
171 return NULL;
172
173 if (value->type != TUPLE_INT)
174 mowgli_throw_exception_val(audacious.tuple.invalid_type_request, NULL);
175
176 return value->value.integer;
177 }