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 | |
4e4d11b3 |
19 | #ifdef HAVE_CONFIG_H |
20 | #include <config.h> |
21 | #endif |
dc877563 |
22 | |
ffd54a90 |
23 | #include <lttv/iattribute.h> |
24 | |
dc877563 |
25 | static void |
ffd54a90 |
26 | lttv_iattribute_base_init (gpointer klass) |
dc877563 |
27 | { |
28 | static gboolean initialized = FALSE; |
29 | |
30 | if (!initialized) { |
31 | initialized = TRUE; |
32 | } |
33 | } |
34 | |
35 | |
36 | GType |
37 | lttv_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 | |
59 | unsigned int lttv_iattribute_get_number(LttvIAttribute *self) |
60 | { |
61 | return LTTV_IATTRIBUTE_GET_CLASS (self)->get_number (self); |
62 | } |
63 | |
64 | |
65 | gboolean lttv_iattribute_named(LttvIAttribute *self, gboolean *homogeneous) |
66 | { |
67 | return LTTV_IATTRIBUTE_GET_CLASS (self)->named (self, homogeneous); |
68 | } |
69 | |
70 | |
71 | LttvAttributeType lttv_iattribute_get(LttvIAttribute *self, unsigned i, |
c0cb4d12 |
72 | LttvAttributeName *name, LttvAttributeValue *v, gboolean *is_named) |
dc877563 |
73 | { |
c0cb4d12 |
74 | return LTTV_IATTRIBUTE_GET_CLASS (self)->get (self, i, name, v, is_named); |
dc877563 |
75 | } |
76 | |
77 | |
78 | LttvAttributeType 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 | |
ffd54a90 |
85 | LttvAttributeValue lttv_iattribute_add(LttvIAttribute *self, |
86 | LttvAttributeName name, LttvAttributeType t) |
dc877563 |
87 | { |
ffd54a90 |
88 | return LTTV_IATTRIBUTE_GET_CLASS (self)->add (self, name, t); |
dc877563 |
89 | } |
90 | |
c0cb4d12 |
91 | LttvAttributeValue 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 | } |
dc877563 |
96 | |
97 | void lttv_iattribute_remove(LttvIAttribute *self, unsigned i) |
98 | { |
99 | return LTTV_IATTRIBUTE_GET_CLASS (self)->remove (self, i); |
100 | } |
101 | |
102 | |
103 | void 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 | |
b445142a |
109 | LttvIAttribute* lttv_iattribute_find_subdir(LttvIAttribute *self, |
dc877563 |
110 | LttvAttributeName name) |
111 | { |
b445142a |
112 | return LTTV_IATTRIBUTE_GET_CLASS (self)->find_subdir (self, name); |
dc877563 |
113 | } |
114 | |
c0cb4d12 |
115 | LttvIAttribute* 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 | |
dc877563 |
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 | |
129 | gboolean 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) { |
ffd54a90 |
138 | *v = lttv_iattribute_add(self, name, t); |
dc877563 |
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 | |
149 | gboolean lttv_iattribute_find_by_path(LttvIAttribute *self, char *path, |
150 | LttvAttributeType t, LttvAttributeValue *v) |
151 | { |
dc877563 |
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 | |
ffd54a90 |
167 | for(cursor = components;;) { |
dc877563 |
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) { |
b445142a |
178 | node = lttv_iattribute_find_subdir(node, name); |
dc877563 |
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 | |
3e67c985 |
192 | |
dc877563 |
193 | /* Shallow and deep copies */ |
194 | |
195 | LttvIAttribute *lttv_iattribute_shallow_copy(LttvIAttribute *self) |
196 | { |
ffd54a90 |
197 | LttvIAttribute *copy; |
dc877563 |
198 | |
199 | LttvAttributeType t; |
200 | |
201 | LttvAttributeValue v, v_copy; |
202 | |
203 | LttvAttributeName name; |
204 | |
c0cb4d12 |
205 | gboolean is_named; |
206 | |
dc877563 |
207 | int i; |
208 | |
209 | int nb_attributes = lttv_iattribute_get_number(self); |
210 | |
3e67c985 |
211 | copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL); |
dc877563 |
212 | |
213 | for(i = 0 ; i < nb_attributes ; i++) { |
c0cb4d12 |
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); |
ffd54a90 |
219 | lttv_iattribute_copy_value(t, v_copy, v); |
dc877563 |
220 | } |
3e67c985 |
221 | return copy; |
dc877563 |
222 | } |
223 | |
224 | LttvIAttribute *lttv_iattribute_deep_copy(LttvIAttribute *self) |
225 | { |
ffd54a90 |
226 | LttvIAttribute *copy, *child; |
dc877563 |
227 | |
228 | LttvAttributeType t; |
229 | |
230 | LttvAttributeValue v, v_copy; |
231 | |
232 | LttvAttributeName name; |
233 | |
c0cb4d12 |
234 | gboolean is_named; |
235 | |
dc877563 |
236 | int i; |
237 | |
238 | int nb_attributes = lttv_iattribute_get_number(self); |
239 | |
3e67c985 |
240 | copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL); |
dc877563 |
241 | |
242 | for(i = 0 ; i < nb_attributes ; i++) { |
c0cb4d12 |
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); |
ffd54a90 |
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)); |
dc877563 |
251 | } |
252 | else lttv_iattribute_copy_value(t, v_copy, v); |
253 | } |
3e67c985 |
254 | return copy; |
dc877563 |
255 | } |
256 | |
257 | void lttv_iattribute_copy_value(LttvAttributeType t, LttvAttributeValue dest, |
258 | LttvAttributeValue src) |
259 | { |
260 | switch(t) { |
261 | case LTTV_INT: |
ffd54a90 |
262 | *(dest.v_int) = *(src.v_int); |
dc877563 |
263 | break; |
264 | |
265 | case LTTV_UINT: |
ffd54a90 |
266 | *(dest.v_uint) = *(src.v_uint); |
dc877563 |
267 | break; |
268 | |
269 | case LTTV_LONG: |
ffd54a90 |
270 | *(dest.v_long) = *(src.v_long); |
dc877563 |
271 | break; |
272 | |
273 | case LTTV_ULONG: |
ffd54a90 |
274 | *(dest.v_ulong) = *(src.v_ulong); |
dc877563 |
275 | break; |
276 | |
277 | case LTTV_FLOAT: |
ffd54a90 |
278 | *(dest.v_float) = *(src.v_float); |
dc877563 |
279 | break; |
280 | |
281 | case LTTV_DOUBLE: |
ffd54a90 |
282 | *(dest.v_double) = *(src.v_double); |
dc877563 |
283 | break; |
284 | |
285 | case LTTV_TIME: |
ffd54a90 |
286 | *(dest.v_time) = *(src.v_time); |
dc877563 |
287 | break; |
288 | |
289 | case LTTV_POINTER: |
ffd54a90 |
290 | *(dest.v_pointer) = *(src.v_pointer); |
dc877563 |
291 | break; |
292 | |
293 | case LTTV_STRING: |
ffd54a90 |
294 | *(dest.v_string) = *(src.v_string); |
dc877563 |
295 | break; |
296 | |
297 | case LTTV_GOBJECT: |
c47a6dc6 |
298 | *(dest.v_gobject) = *(src.v_gobject); |
299 | if(*(dest.v_gobject) != NULL) g_object_ref(*(dest.v_gobject)); |
dc877563 |
300 | break; |
301 | |
302 | case LTTV_NONE: |
303 | break; |
304 | } |
305 | } |
306 | |
307 | |