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, |
72 | LttvAttributeName *name, LttvAttributeValue *v) |
73 | { |
74 | return LTTV_IATTRIBUTE_GET_CLASS (self)->get (self, i, name, v); |
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 | |
91 | |
92 | void lttv_iattribute_remove(LttvIAttribute *self, unsigned i) |
93 | { |
94 | return LTTV_IATTRIBUTE_GET_CLASS (self)->remove (self, i); |
95 | } |
96 | |
97 | |
98 | void lttv_iattribute_remove_by_name(LttvIAttribute *self, |
99 | LttvAttributeName name) |
100 | { |
101 | return LTTV_IATTRIBUTE_GET_CLASS (self)->remove_by_name (self, name); |
102 | } |
103 | |
b445142a |
104 | LttvIAttribute* lttv_iattribute_find_subdir(LttvIAttribute *self, |
dc877563 |
105 | LttvAttributeName name) |
106 | { |
b445142a |
107 | return LTTV_IATTRIBUTE_GET_CLASS (self)->find_subdir (self, name); |
dc877563 |
108 | } |
109 | |
110 | |
111 | /* Find the named attribute in the table, which must be of the specified type. |
112 | If it does not exist, it is created with a default value of 0 (NULL for |
113 | pointer types). Since the address of the value is obtained, it may be |
114 | changed easily afterwards. The function returns false when the attribute |
115 | exists but is of incorrect type. */ |
116 | |
117 | gboolean lttv_iattribute_find(LttvIAttribute *self, LttvAttributeName name, |
118 | LttvAttributeType t, LttvAttributeValue *v) |
119 | { |
120 | LttvAttributeType found_type; |
121 | |
122 | found_type = lttv_iattribute_get_by_name(self, name, v); |
123 | if(found_type == t) return TRUE; |
124 | |
125 | if(found_type == LTTV_NONE) { |
ffd54a90 |
126 | *v = lttv_iattribute_add(self, name, t); |
dc877563 |
127 | return TRUE; |
128 | } |
129 | |
130 | return FALSE; |
131 | } |
132 | |
133 | |
134 | /* Trees of attribute tables may be accessed using a hierarchical path with |
135 | components separated by /, like in filesystems */ |
136 | |
137 | gboolean lttv_iattribute_find_by_path(LttvIAttribute *self, char *path, |
138 | LttvAttributeType t, LttvAttributeValue *v) |
139 | { |
dc877563 |
140 | LttvIAttribute *node = self; |
141 | |
142 | LttvAttributeType found_type; |
143 | |
144 | LttvAttributeName name; |
145 | |
146 | gchar **components, **cursor; |
147 | |
148 | components = g_strsplit(path, "\"", G_MAXINT); |
149 | |
150 | if(components == NULL || *components == NULL) { |
151 | g_strfreev(components); |
152 | return FALSE; |
153 | } |
154 | |
ffd54a90 |
155 | for(cursor = components;;) { |
dc877563 |
156 | name = g_quark_from_string(*cursor); |
157 | cursor++; |
158 | |
159 | if(*cursor == NULL) { |
160 | g_strfreev(components); |
161 | return lttv_iattribute_find(node, name, t, v); |
162 | } |
163 | else { |
164 | found_type = lttv_iattribute_get_by_name(node, name, v); |
165 | if(found_type == LTTV_NONE) { |
b445142a |
166 | node = lttv_iattribute_find_subdir(node, name); |
dc877563 |
167 | } |
168 | else if(found_type == LTTV_GOBJECT && |
169 | LTTV_IS_IATTRIBUTE(*(v->v_gobject))) { |
170 | node = LTTV_IATTRIBUTE(*(v->v_gobject)); |
171 | } |
172 | else { |
173 | g_strfreev(components); |
174 | return FALSE; |
175 | } |
176 | } |
177 | } |
178 | } |
179 | |
3e67c985 |
180 | |
dc877563 |
181 | /* Shallow and deep copies */ |
182 | |
183 | LttvIAttribute *lttv_iattribute_shallow_copy(LttvIAttribute *self) |
184 | { |
ffd54a90 |
185 | LttvIAttribute *copy; |
dc877563 |
186 | |
187 | LttvAttributeType t; |
188 | |
189 | LttvAttributeValue v, v_copy; |
190 | |
191 | LttvAttributeName name; |
192 | |
193 | int i; |
194 | |
195 | int nb_attributes = lttv_iattribute_get_number(self); |
196 | |
3e67c985 |
197 | copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL); |
dc877563 |
198 | |
199 | for(i = 0 ; i < nb_attributes ; i++) { |
200 | t = lttv_iattribute_get(self, i, &name, &v); |
201 | v_copy = lttv_iattribute_add(copy, name, t); |
ffd54a90 |
202 | lttv_iattribute_copy_value(t, v_copy, v); |
dc877563 |
203 | } |
3e67c985 |
204 | return copy; |
dc877563 |
205 | } |
206 | |
207 | LttvIAttribute *lttv_iattribute_deep_copy(LttvIAttribute *self) |
208 | { |
ffd54a90 |
209 | LttvIAttribute *copy, *child; |
dc877563 |
210 | |
211 | LttvAttributeType t; |
212 | |
213 | LttvAttributeValue v, v_copy; |
214 | |
215 | LttvAttributeName name; |
216 | |
217 | int i; |
218 | |
219 | int nb_attributes = lttv_iattribute_get_number(self); |
220 | |
3e67c985 |
221 | copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL); |
dc877563 |
222 | |
223 | for(i = 0 ; i < nb_attributes ; i++) { |
224 | t = lttv_iattribute_get(self, i, &name, &v); |
225 | v_copy = lttv_iattribute_add(copy, name, t); |
ffd54a90 |
226 | if(t == LTTV_GOBJECT && LTTV_IS_IATTRIBUTE(*(v.v_gobject))) { |
227 | child = LTTV_IATTRIBUTE(*(v.v_gobject)); |
228 | *(v_copy.v_gobject) = G_OBJECT(lttv_iattribute_deep_copy(child)); |
dc877563 |
229 | } |
230 | else lttv_iattribute_copy_value(t, v_copy, v); |
231 | } |
3e67c985 |
232 | return copy; |
dc877563 |
233 | } |
234 | |
235 | void lttv_iattribute_copy_value(LttvAttributeType t, LttvAttributeValue dest, |
236 | LttvAttributeValue src) |
237 | { |
238 | switch(t) { |
239 | case LTTV_INT: |
ffd54a90 |
240 | *(dest.v_int) = *(src.v_int); |
dc877563 |
241 | break; |
242 | |
243 | case LTTV_UINT: |
ffd54a90 |
244 | *(dest.v_uint) = *(src.v_uint); |
dc877563 |
245 | break; |
246 | |
247 | case LTTV_LONG: |
ffd54a90 |
248 | *(dest.v_long) = *(src.v_long); |
dc877563 |
249 | break; |
250 | |
251 | case LTTV_ULONG: |
ffd54a90 |
252 | *(dest.v_ulong) = *(src.v_ulong); |
dc877563 |
253 | break; |
254 | |
255 | case LTTV_FLOAT: |
ffd54a90 |
256 | *(dest.v_float) = *(src.v_float); |
dc877563 |
257 | break; |
258 | |
259 | case LTTV_DOUBLE: |
ffd54a90 |
260 | *(dest.v_double) = *(src.v_double); |
dc877563 |
261 | break; |
262 | |
263 | case LTTV_TIME: |
ffd54a90 |
264 | *(dest.v_time) = *(src.v_time); |
dc877563 |
265 | break; |
266 | |
267 | case LTTV_POINTER: |
ffd54a90 |
268 | *(dest.v_pointer) = *(src.v_pointer); |
dc877563 |
269 | break; |
270 | |
271 | case LTTV_STRING: |
ffd54a90 |
272 | *(dest.v_string) = *(src.v_string); |
dc877563 |
273 | break; |
274 | |
275 | case LTTV_GOBJECT: |
c47a6dc6 |
276 | *(dest.v_gobject) = *(src.v_gobject); |
277 | if(*(dest.v_gobject) != NULL) g_object_ref(*(dest.v_gobject)); |
dc877563 |
278 | break; |
279 | |
280 | case LTTV_NONE: |
281 | break; |
282 | } |
283 | } |
284 | |
285 | |