update compat
[lttv.git] / tags / lttv-0.10.0-pre15-12082008 / lttv / lttv / iattribute.c
CommitLineData
5750899b 1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <lttv/iattribute.h>
24
25static void
26lttv_iattribute_base_init (gpointer klass)
27{
28 static gboolean initialized = FALSE;
29
30 if (!initialized) {
31 initialized = TRUE;
32 }
33}
34
35
36GType
37lttv_iattribute_get_type (void)
38{
39 static GType type = 0;
40 if (type == 0) {
41 static const GTypeInfo info = {
42 sizeof (LttvIAttributeClass),
43 lttv_iattribute_base_init, /* base_init */
44 NULL, /* base_finalize */
45 NULL, /* class_init */
46 NULL, /* class_finalize */
47 NULL, /* class_data */
48 0,
49 0, /* n_preallocs */
50 NULL /* instance_init */
51 };
52 type = g_type_register_static (G_TYPE_INTERFACE, "LttvIAttribute",
53 &info, 0);
54 }
55 return type;
56}
57
58
59unsigned int lttv_iattribute_get_number(LttvIAttribute *self)
60{
61 return LTTV_IATTRIBUTE_GET_CLASS (self)->get_number (self);
62}
63
64
65gboolean lttv_iattribute_named(LttvIAttribute *self, gboolean *homogeneous)
66{
67 return LTTV_IATTRIBUTE_GET_CLASS (self)->named (self, homogeneous);
68}
69
70
71LttvAttributeType lttv_iattribute_get(LttvIAttribute *self, unsigned i,
72 LttvAttributeName *name, LttvAttributeValue *v, gboolean *is_named)
73{
74 return LTTV_IATTRIBUTE_GET_CLASS (self)->get (self, i, name, v, is_named);
75}
76
77
78LttvAttributeType lttv_iattribute_get_by_name(LttvIAttribute *self,
79 LttvAttributeName name, LttvAttributeValue *v)
80{
81 return LTTV_IATTRIBUTE_GET_CLASS (self)->get_by_name (self, name, v);
82}
83
84
85LttvAttributeValue lttv_iattribute_add(LttvIAttribute *self,
86 LttvAttributeName name, LttvAttributeType t)
87{
88 return LTTV_IATTRIBUTE_GET_CLASS (self)->add (self, name, t);
89}
90
91LttvAttributeValue lttv_iattribute_add_unnamed(LttvIAttribute *self,
92 LttvAttributeName name, LttvAttributeType t)
93{
94 return LTTV_IATTRIBUTE_GET_CLASS (self)->add_unnamed (self, name, t);
95}
96
97void lttv_iattribute_remove(LttvIAttribute *self, unsigned i)
98{
99 return LTTV_IATTRIBUTE_GET_CLASS (self)->remove (self, i);
100}
101
102
103void lttv_iattribute_remove_by_name(LttvIAttribute *self,
104 LttvAttributeName name)
105{
106 return LTTV_IATTRIBUTE_GET_CLASS (self)->remove_by_name (self, name);
107}
108
109LttvIAttribute* lttv_iattribute_find_subdir(LttvIAttribute *self,
110 LttvAttributeName name)
111{
112 return LTTV_IATTRIBUTE_GET_CLASS (self)->find_subdir (self, name);
113}
114
115LttvIAttribute* lttv_iattribute_find_subdir_unnamed(LttvIAttribute *self,
116 LttvAttributeName name)
117{
118 return LTTV_IATTRIBUTE_GET_CLASS (self)->find_subdir_unnamed (self, name);
119}
120
121
122
123/* Find the named attribute in the table, which must be of the specified type.
124 If it does not exist, it is created with a default value of 0 (NULL for
125 pointer types). Since the address of the value is obtained, it may be
126 changed easily afterwards. The function returns false when the attribute
127 exists but is of incorrect type. */
128
129gboolean lttv_iattribute_find(LttvIAttribute *self, LttvAttributeName name,
130 LttvAttributeType t, LttvAttributeValue *v)
131{
132 LttvAttributeType found_type;
133
134 found_type = lttv_iattribute_get_by_name(self, name, v);
135 if(found_type == t) return TRUE;
136
137 if(found_type == LTTV_NONE) {
138 *v = lttv_iattribute_add(self, name, t);
139 return TRUE;
140 }
141
142 return FALSE;
143}
144
145
146/* Trees of attribute tables may be accessed using a hierarchical path with
147 components separated by /, like in filesystems */
148
149gboolean lttv_iattribute_find_by_path(LttvIAttribute *self, char *path,
150 LttvAttributeType t, LttvAttributeValue *v)
151{
152 LttvIAttribute *node = self;
153
154 LttvAttributeType found_type;
155
156 LttvAttributeName name;
157
158 gchar **components, **cursor;
159
160 components = g_strsplit(path, "\"", G_MAXINT);
161
162 if(components == NULL || *components == NULL) {
163 g_strfreev(components);
164 return FALSE;
165 }
166
167 for(cursor = components;;) {
168 name = g_quark_from_string(*cursor);
169 cursor++;
170
171 if(*cursor == NULL) {
172 g_strfreev(components);
173 return lttv_iattribute_find(node, name, t, v);
174 }
175 else {
176 found_type = lttv_iattribute_get_by_name(node, name, v);
177 if(found_type == LTTV_NONE) {
178 node = lttv_iattribute_find_subdir(node, name);
179 }
180 else if(found_type == LTTV_GOBJECT &&
181 LTTV_IS_IATTRIBUTE(*(v->v_gobject))) {
182 node = LTTV_IATTRIBUTE(*(v->v_gobject));
183 }
184 else {
185 g_strfreev(components);
186 return FALSE;
187 }
188 }
189 }
190}
191
192
193/* Shallow and deep copies */
194
195LttvIAttribute *lttv_iattribute_shallow_copy(LttvIAttribute *self)
196{
197 LttvIAttribute *copy;
198
199 LttvAttributeType t;
200
201 LttvAttributeValue v, v_copy;
202
203 LttvAttributeName name;
204
205 gboolean is_named;
206
207 int i;
208
209 int nb_attributes = lttv_iattribute_get_number(self);
210
211 copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL);
212
213 for(i = 0 ; i < nb_attributes ; i++) {
214 t = lttv_iattribute_get(self, i, &name, &v, &is_named);
215 if(is_named)
216 v_copy = lttv_iattribute_add(copy, name, t);
217 else
218 v_copy = lttv_iattribute_add_unnamed(copy, name, t);
219 lttv_iattribute_copy_value(t, v_copy, v);
220 }
221 return copy;
222}
223
224LttvIAttribute *lttv_iattribute_deep_copy(LttvIAttribute *self)
225{
226 LttvIAttribute *copy, *child;
227
228 LttvAttributeType t;
229
230 LttvAttributeValue v, v_copy;
231
232 LttvAttributeName name;
233
234 gboolean is_named;
235
236 int i;
237
238 int nb_attributes = lttv_iattribute_get_number(self);
239
240 copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL);
241
242 for(i = 0 ; i < nb_attributes ; i++) {
243 t = lttv_iattribute_get(self, i, &name, &v, &is_named);
244 if(is_named)
245 v_copy = lttv_iattribute_add(copy, name, t);
246 else
247 v_copy = lttv_iattribute_add_unnamed(copy, name, t);
248 if(t == LTTV_GOBJECT && LTTV_IS_IATTRIBUTE(*(v.v_gobject))) {
249 child = LTTV_IATTRIBUTE(*(v.v_gobject));
250 *(v_copy.v_gobject) = G_OBJECT(lttv_iattribute_deep_copy(child));
251 }
252 else lttv_iattribute_copy_value(t, v_copy, v);
253 }
254 return copy;
255}
256
257void lttv_iattribute_copy_value(LttvAttributeType t, LttvAttributeValue dest,
258 LttvAttributeValue src)
259{
260 switch(t) {
261 case LTTV_INT:
262 *(dest.v_int) = *(src.v_int);
263 break;
264
265 case LTTV_UINT:
266 *(dest.v_uint) = *(src.v_uint);
267 break;
268
269 case LTTV_LONG:
270 *(dest.v_long) = *(src.v_long);
271 break;
272
273 case LTTV_ULONG:
274 *(dest.v_ulong) = *(src.v_ulong);
275 break;
276
277 case LTTV_FLOAT:
278 *(dest.v_float) = *(src.v_float);
279 break;
280
281 case LTTV_DOUBLE:
282 *(dest.v_double) = *(src.v_double);
283 break;
284
285 case LTTV_TIME:
286 *(dest.v_time) = *(src.v_time);
287 break;
288
289 case LTTV_POINTER:
290 *(dest.v_pointer) = *(src.v_pointer);
291 break;
292
293 case LTTV_STRING:
294 *(dest.v_string) = *(src.v_string);
295 break;
296
297 case LTTV_GOBJECT:
298 *(dest.v_gobject) = *(src.v_gobject);
299 if(*(dest.v_gobject) != NULL) g_object_ref(*(dest.v_gobject));
300 break;
301
302 case LTTV_NONE:
303 break;
304 }
305}
306
307
This page took 0.034066 seconds and 4 git commands to generate.