header missing
[lttv.git] / ltt / branches / poly / lttv / lttv / iattribute.c
CommitLineData
9c312311 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
dc877563 19
ffd54a90 20#include <lttv/iattribute.h>
21
dc877563 22static void
ffd54a90 23lttv_iattribute_base_init (gpointer klass)
dc877563 24{
25 static gboolean initialized = FALSE;
26
27 if (!initialized) {
28 initialized = TRUE;
29 }
30}
31
32
33GType
34lttv_iattribute_get_type (void)
35{
36 static GType type = 0;
37 if (type == 0) {
38 static const GTypeInfo info = {
39 sizeof (LttvIAttributeClass),
40 lttv_iattribute_base_init, /* base_init */
41 NULL, /* base_finalize */
42 NULL, /* class_init */
43 NULL, /* class_finalize */
44 NULL, /* class_data */
45 0,
46 0, /* n_preallocs */
47 NULL /* instance_init */
48 };
49 type = g_type_register_static (G_TYPE_INTERFACE, "LttvIAttribute",
50 &info, 0);
51 }
52 return type;
53}
54
55
56unsigned int lttv_iattribute_get_number(LttvIAttribute *self)
57{
58 return LTTV_IATTRIBUTE_GET_CLASS (self)->get_number (self);
59}
60
61
62gboolean lttv_iattribute_named(LttvIAttribute *self, gboolean *homogeneous)
63{
64 return LTTV_IATTRIBUTE_GET_CLASS (self)->named (self, homogeneous);
65}
66
67
68LttvAttributeType lttv_iattribute_get(LttvIAttribute *self, unsigned i,
69 LttvAttributeName *name, LttvAttributeValue *v)
70{
71 return LTTV_IATTRIBUTE_GET_CLASS (self)->get (self, i, name, v);
72}
73
74
75LttvAttributeType lttv_iattribute_get_by_name(LttvIAttribute *self,
76 LttvAttributeName name, LttvAttributeValue *v)
77{
78 return LTTV_IATTRIBUTE_GET_CLASS (self)->get_by_name (self, name, v);
79}
80
81
ffd54a90 82LttvAttributeValue lttv_iattribute_add(LttvIAttribute *self,
83 LttvAttributeName name, LttvAttributeType t)
dc877563 84{
ffd54a90 85 return LTTV_IATTRIBUTE_GET_CLASS (self)->add (self, name, t);
dc877563 86}
87
88
89void lttv_iattribute_remove(LttvIAttribute *self, unsigned i)
90{
91 return LTTV_IATTRIBUTE_GET_CLASS (self)->remove (self, i);
92}
93
94
95void lttv_iattribute_remove_by_name(LttvIAttribute *self,
96 LttvAttributeName name)
97{
98 return LTTV_IATTRIBUTE_GET_CLASS (self)->remove_by_name (self, name);
99}
100
b445142a 101LttvIAttribute* lttv_iattribute_find_subdir(LttvIAttribute *self,
dc877563 102 LttvAttributeName name)
103{
b445142a 104 return LTTV_IATTRIBUTE_GET_CLASS (self)->find_subdir (self, name);
dc877563 105}
106
107
108/* Find the named attribute in the table, which must be of the specified type.
109 If it does not exist, it is created with a default value of 0 (NULL for
110 pointer types). Since the address of the value is obtained, it may be
111 changed easily afterwards. The function returns false when the attribute
112 exists but is of incorrect type. */
113
114gboolean lttv_iattribute_find(LttvIAttribute *self, LttvAttributeName name,
115 LttvAttributeType t, LttvAttributeValue *v)
116{
117 LttvAttributeType found_type;
118
119 found_type = lttv_iattribute_get_by_name(self, name, v);
120 if(found_type == t) return TRUE;
121
122 if(found_type == LTTV_NONE) {
ffd54a90 123 *v = lttv_iattribute_add(self, name, t);
dc877563 124 return TRUE;
125 }
126
127 return FALSE;
128}
129
130
131/* Trees of attribute tables may be accessed using a hierarchical path with
132 components separated by /, like in filesystems */
133
134gboolean lttv_iattribute_find_by_path(LttvIAttribute *self, char *path,
135 LttvAttributeType t, LttvAttributeValue *v)
136{
dc877563 137 LttvIAttribute *node = self;
138
139 LttvAttributeType found_type;
140
141 LttvAttributeName name;
142
143 gchar **components, **cursor;
144
145 components = g_strsplit(path, "\"", G_MAXINT);
146
147 if(components == NULL || *components == NULL) {
148 g_strfreev(components);
149 return FALSE;
150 }
151
ffd54a90 152 for(cursor = components;;) {
dc877563 153 name = g_quark_from_string(*cursor);
154 cursor++;
155
156 if(*cursor == NULL) {
157 g_strfreev(components);
158 return lttv_iattribute_find(node, name, t, v);
159 }
160 else {
161 found_type = lttv_iattribute_get_by_name(node, name, v);
162 if(found_type == LTTV_NONE) {
b445142a 163 node = lttv_iattribute_find_subdir(node, name);
dc877563 164 }
165 else if(found_type == LTTV_GOBJECT &&
166 LTTV_IS_IATTRIBUTE(*(v->v_gobject))) {
167 node = LTTV_IATTRIBUTE(*(v->v_gobject));
168 }
169 else {
170 g_strfreev(components);
171 return FALSE;
172 }
173 }
174 }
175}
176
3e67c985 177
dc877563 178/* Shallow and deep copies */
179
180LttvIAttribute *lttv_iattribute_shallow_copy(LttvIAttribute *self)
181{
ffd54a90 182 LttvIAttribute *copy;
dc877563 183
184 LttvAttributeType t;
185
186 LttvAttributeValue v, v_copy;
187
188 LttvAttributeName name;
189
190 int i;
191
192 int nb_attributes = lttv_iattribute_get_number(self);
193
3e67c985 194 copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL);
dc877563 195
196 for(i = 0 ; i < nb_attributes ; i++) {
197 t = lttv_iattribute_get(self, i, &name, &v);
198 v_copy = lttv_iattribute_add(copy, name, t);
ffd54a90 199 lttv_iattribute_copy_value(t, v_copy, v);
dc877563 200 }
3e67c985 201 return copy;
dc877563 202}
203
204LttvIAttribute *lttv_iattribute_deep_copy(LttvIAttribute *self)
205{
ffd54a90 206 LttvIAttribute *copy, *child;
dc877563 207
208 LttvAttributeType t;
209
210 LttvAttributeValue v, v_copy;
211
212 LttvAttributeName name;
213
214 int i;
215
216 int nb_attributes = lttv_iattribute_get_number(self);
217
3e67c985 218 copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL);
dc877563 219
220 for(i = 0 ; i < nb_attributes ; i++) {
221 t = lttv_iattribute_get(self, i, &name, &v);
222 v_copy = lttv_iattribute_add(copy, name, t);
ffd54a90 223 if(t == LTTV_GOBJECT && LTTV_IS_IATTRIBUTE(*(v.v_gobject))) {
224 child = LTTV_IATTRIBUTE(*(v.v_gobject));
225 *(v_copy.v_gobject) = G_OBJECT(lttv_iattribute_deep_copy(child));
dc877563 226 }
227 else lttv_iattribute_copy_value(t, v_copy, v);
228 }
3e67c985 229 return copy;
dc877563 230}
231
232void lttv_iattribute_copy_value(LttvAttributeType t, LttvAttributeValue dest,
233 LttvAttributeValue src)
234{
235 switch(t) {
236 case LTTV_INT:
ffd54a90 237 *(dest.v_int) = *(src.v_int);
dc877563 238 break;
239
240 case LTTV_UINT:
ffd54a90 241 *(dest.v_uint) = *(src.v_uint);
dc877563 242 break;
243
244 case LTTV_LONG:
ffd54a90 245 *(dest.v_long) = *(src.v_long);
dc877563 246 break;
247
248 case LTTV_ULONG:
ffd54a90 249 *(dest.v_ulong) = *(src.v_ulong);
dc877563 250 break;
251
252 case LTTV_FLOAT:
ffd54a90 253 *(dest.v_float) = *(src.v_float);
dc877563 254 break;
255
256 case LTTV_DOUBLE:
ffd54a90 257 *(dest.v_double) = *(src.v_double);
dc877563 258 break;
259
260 case LTTV_TIME:
ffd54a90 261 *(dest.v_time) = *(src.v_time);
dc877563 262 break;
263
264 case LTTV_POINTER:
ffd54a90 265 *(dest.v_pointer) = *(src.v_pointer);
dc877563 266 break;
267
268 case LTTV_STRING:
ffd54a90 269 *(dest.v_string) = *(src.v_string);
dc877563 270 break;
271
272 case LTTV_GOBJECT:
c47a6dc6 273 *(dest.v_gobject) = *(src.v_gobject);
274 if(*(dest.v_gobject) != NULL) g_object_ref(*(dest.v_gobject));
dc877563 275 break;
276
277 case LTTV_NONE:
278 break;
279 }
280}
281
282
This page took 0.041445 seconds and 4 git commands to generate.