1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
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;
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.
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,
23 #include <lttv/iattribute.h>
25 static void lttv_iattribute_base_init (gpointer klass
)
27 static gboolean initialized
= FALSE
;
35 GType
lttv_iattribute_get_type (void)
37 static GType type
= 0;
39 static const GTypeInfo info
= {
40 sizeof (LttvIAttributeClass
),
41 lttv_iattribute_base_init
, /* base_init */
42 NULL
, /* base_finalize */
43 NULL
, /* class_init */
44 NULL
, /* class_finalize */
45 NULL
, /* class_data */
48 NULL
/* instance_init */
50 type
= g_type_register_static (G_TYPE_INTERFACE
, "LttvIAttribute",
57 unsigned int lttv_iattribute_get_number(LttvIAttribute
*self
)
59 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get_number (self
);
63 gboolean
lttv_iattribute_named(LttvIAttribute
*self
, gboolean
*homogeneous
)
65 return LTTV_IATTRIBUTE_GET_CLASS (self
)->named (self
, homogeneous
);
69 LttvAttributeType
lttv_iattribute_get(LttvIAttribute
*self
, unsigned i
,
70 LttvAttributeName
*name
, LttvAttributeValue
*v
, gboolean
*is_named
)
72 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get (self
, i
, name
, v
, is_named
);
76 LttvAttributeType
lttv_iattribute_get_by_name(LttvIAttribute
*self
,
77 LttvAttributeName name
, LttvAttributeValue
*v
)
79 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get_by_name (self
, name
, v
);
83 LttvAttributeValue
lttv_iattribute_add(LttvIAttribute
*self
,
84 LttvAttributeName name
, LttvAttributeType t
)
86 return LTTV_IATTRIBUTE_GET_CLASS (self
)->add (self
, name
, t
);
89 LttvAttributeValue
lttv_iattribute_add_unnamed(LttvIAttribute
*self
,
90 LttvAttributeName name
, LttvAttributeType t
)
92 return LTTV_IATTRIBUTE_GET_CLASS (self
)->add_unnamed (self
, name
, t
);
95 void lttv_iattribute_remove(LttvIAttribute
*self
, unsigned i
)
97 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove (self
, i
);
101 void lttv_iattribute_remove_by_name(LttvIAttribute
*self
,
102 LttvAttributeName name
)
104 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove_by_name (self
, name
);
107 LttvIAttribute
* lttv_iattribute_find_subdir(LttvIAttribute
*self
,
108 LttvAttributeName name
)
110 return LTTV_IATTRIBUTE_GET_CLASS (self
)->find_subdir (self
, name
);
113 LttvIAttribute
* lttv_iattribute_find_subdir_unnamed(LttvIAttribute
*self
,
114 LttvAttributeName name
)
116 return LTTV_IATTRIBUTE_GET_CLASS (self
)->find_subdir_unnamed (self
, name
);
121 /* Find the named attribute in the table, which must be of the specified type.
122 If it does not exist, it is created with a default value of 0 (NULL for
123 pointer types). Since the address of the value is obtained, it may be
124 changed easily afterwards. The function returns false when the attribute
125 exists but is of incorrect type. */
127 gboolean
lttv_iattribute_find(LttvIAttribute
*self
, LttvAttributeName name
,
128 LttvAttributeType t
, LttvAttributeValue
*v
)
130 LttvAttributeType found_type
;
132 found_type
= lttv_iattribute_get_by_name(self
, name
, v
);
133 if(found_type
== t
) return TRUE
;
135 if(found_type
== LTTV_NONE
) {
136 *v
= lttv_iattribute_add(self
, name
, t
);
144 /* Trees of attribute tables may be accessed using a hierarchical path with
145 components separated by /, like in filesystems */
147 gboolean
lttv_iattribute_find_by_path(LttvIAttribute
*self
, const char *path
,
148 LttvAttributeType t
, LttvAttributeValue
*v
)
150 LttvIAttribute
*node
= self
;
152 LttvAttributeType found_type
;
154 LttvAttributeName name
;
156 gchar
**components
, **cursor
;
158 components
= g_strsplit(path
, "\"", G_MAXINT
);
160 if(components
== NULL
|| *components
== NULL
) {
161 g_strfreev(components
);
165 for(cursor
= components
;;) {
166 name
= g_quark_from_string(*cursor
);
169 if(*cursor
== NULL
) {
170 g_strfreev(components
);
171 return lttv_iattribute_find(node
, name
, t
, v
);
173 found_type
= lttv_iattribute_get_by_name(node
, name
, v
);
174 if(found_type
== LTTV_NONE
) {
175 node
= lttv_iattribute_find_subdir(node
, name
);
177 else if(found_type
== LTTV_GOBJECT
&&
178 LTTV_IS_IATTRIBUTE(*(v
->v_gobject
))) {
179 node
= LTTV_IATTRIBUTE(*(v
->v_gobject
));
181 g_strfreev(components
);
189 /* Shallow and deep copies */
191 LttvIAttribute
*lttv_iattribute_shallow_copy(LttvIAttribute
*self
)
193 LttvIAttribute
*copy
;
197 LttvAttributeValue v
, v_copy
;
199 LttvAttributeName name
;
205 int nb_attributes
= lttv_iattribute_get_number(self
);
207 copy
= LTTV_IATTRIBUTE_GET_CLASS(self
)->new_attribute(NULL
);
209 for(i
= 0 ; i
< nb_attributes
; i
++) {
210 t
= lttv_iattribute_get(self
, i
, &name
, &v
, &is_named
);
212 v_copy
= lttv_iattribute_add(copy
, name
, t
);
214 v_copy
= lttv_iattribute_add_unnamed(copy
, name
, t
);
215 lttv_iattribute_copy_value(t
, v_copy
, v
);
220 LttvIAttribute
*lttv_iattribute_deep_copy(LttvIAttribute
*self
)
222 LttvIAttribute
*copy
, *child
;
226 LttvAttributeValue v
, v_copy
;
228 LttvAttributeName name
;
234 int nb_attributes
= lttv_iattribute_get_number(self
);
236 copy
= LTTV_IATTRIBUTE_GET_CLASS(self
)->new_attribute(NULL
);
238 for(i
= 0 ; i
< nb_attributes
; i
++) {
239 t
= lttv_iattribute_get(self
, i
, &name
, &v
, &is_named
);
241 v_copy
= lttv_iattribute_add(copy
, name
, t
);
243 v_copy
= lttv_iattribute_add_unnamed(copy
, name
, t
);
244 if(t
== LTTV_GOBJECT
&& LTTV_IS_IATTRIBUTE(*(v
.v_gobject
))) {
245 child
= LTTV_IATTRIBUTE(*(v
.v_gobject
));
246 *(v_copy
.v_gobject
) = G_OBJECT(lttv_iattribute_deep_copy(child
));
248 else lttv_iattribute_copy_value(t
, v_copy
, v
);
253 void lttv_iattribute_copy_value(LttvAttributeType t
, LttvAttributeValue dest
,
254 LttvAttributeValue src
)
258 *(dest
.v_int
) = *(src
.v_int
);
262 *(dest
.v_uint
) = *(src
.v_uint
);
266 *(dest
.v_long
) = *(src
.v_long
);
270 *(dest
.v_ulong
) = *(src
.v_ulong
);
274 *(dest
.v_float
) = *(src
.v_float
);
278 *(dest
.v_double
) = *(src
.v_double
);
282 *(dest
.v_time
) = *(src
.v_time
);
286 *(dest
.v_pointer
) = *(src
.v_pointer
);
290 *(dest
.v_string
) = *(src
.v_string
);
294 *(dest
.v_gobject
) = *(src
.v_gobject
);
295 if(*(dest
.v_gobject
) != NULL
) g_object_ref(*(dest
.v_gobject
));