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 |
22 | static void |
ffd54a90 |
23 | lttv_iattribute_base_init (gpointer klass) |
dc877563 |
24 | { |
25 | static gboolean initialized = FALSE; |
26 | |
27 | if (!initialized) { |
28 | initialized = TRUE; |
29 | } |
30 | } |
31 | |
32 | |
33 | GType |
34 | lttv_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 | |
56 | unsigned int lttv_iattribute_get_number(LttvIAttribute *self) |
57 | { |
58 | return LTTV_IATTRIBUTE_GET_CLASS (self)->get_number (self); |
59 | } |
60 | |
61 | |
62 | gboolean lttv_iattribute_named(LttvIAttribute *self, gboolean *homogeneous) |
63 | { |
64 | return LTTV_IATTRIBUTE_GET_CLASS (self)->named (self, homogeneous); |
65 | } |
66 | |
67 | |
68 | LttvAttributeType 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 | |
75 | LttvAttributeType 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 |
82 | LttvAttributeValue 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 | |
89 | void lttv_iattribute_remove(LttvIAttribute *self, unsigned i) |
90 | { |
91 | return LTTV_IATTRIBUTE_GET_CLASS (self)->remove (self, i); |
92 | } |
93 | |
94 | |
95 | void 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 |
101 | LttvIAttribute* 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 | |
114 | gboolean 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 | |
134 | gboolean 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 | |
177 | /* Shallow and deep copies */ |
178 | |
179 | LttvIAttribute *lttv_iattribute_shallow_copy(LttvIAttribute *self) |
180 | { |
ffd54a90 |
181 | LttvIAttribute *copy; |
dc877563 |
182 | |
183 | LttvAttributeType t; |
184 | |
185 | LttvAttributeValue v, v_copy; |
186 | |
187 | LttvAttributeName name; |
188 | |
189 | int i; |
190 | |
191 | int nb_attributes = lttv_iattribute_get_number(self); |
192 | |
ffd54a90 |
193 | copy = LTTV_IATTRIBUTE(g_object_new(G_OBJECT_TYPE(self),NULL)); |
dc877563 |
194 | |
195 | for(i = 0 ; i < nb_attributes ; i++) { |
196 | t = lttv_iattribute_get(self, i, &name, &v); |
197 | v_copy = lttv_iattribute_add(copy, name, t); |
ffd54a90 |
198 | lttv_iattribute_copy_value(t, v_copy, v); |
dc877563 |
199 | } |
200 | } |
201 | |
202 | LttvIAttribute *lttv_iattribute_deep_copy(LttvIAttribute *self) |
203 | { |
ffd54a90 |
204 | LttvIAttribute *copy, *child; |
dc877563 |
205 | |
206 | LttvAttributeType t; |
207 | |
208 | LttvAttributeValue v, v_copy; |
209 | |
210 | LttvAttributeName name; |
211 | |
212 | int i; |
213 | |
214 | int nb_attributes = lttv_iattribute_get_number(self); |
215 | |
ffd54a90 |
216 | copy = LTTV_IATTRIBUTE(g_object_new(G_OBJECT_TYPE(self), NULL)); |
dc877563 |
217 | |
218 | for(i = 0 ; i < nb_attributes ; i++) { |
219 | t = lttv_iattribute_get(self, i, &name, &v); |
220 | v_copy = lttv_iattribute_add(copy, name, t); |
ffd54a90 |
221 | if(t == LTTV_GOBJECT && LTTV_IS_IATTRIBUTE(*(v.v_gobject))) { |
222 | child = LTTV_IATTRIBUTE(*(v.v_gobject)); |
223 | *(v_copy.v_gobject) = G_OBJECT(lttv_iattribute_deep_copy(child)); |
dc877563 |
224 | } |
225 | else lttv_iattribute_copy_value(t, v_copy, v); |
226 | } |
227 | } |
228 | |
229 | void lttv_iattribute_copy_value(LttvAttributeType t, LttvAttributeValue dest, |
230 | LttvAttributeValue src) |
231 | { |
232 | switch(t) { |
233 | case LTTV_INT: |
ffd54a90 |
234 | *(dest.v_int) = *(src.v_int); |
dc877563 |
235 | break; |
236 | |
237 | case LTTV_UINT: |
ffd54a90 |
238 | *(dest.v_uint) = *(src.v_uint); |
dc877563 |
239 | break; |
240 | |
241 | case LTTV_LONG: |
ffd54a90 |
242 | *(dest.v_long) = *(src.v_long); |
dc877563 |
243 | break; |
244 | |
245 | case LTTV_ULONG: |
ffd54a90 |
246 | *(dest.v_ulong) = *(src.v_ulong); |
dc877563 |
247 | break; |
248 | |
249 | case LTTV_FLOAT: |
ffd54a90 |
250 | *(dest.v_float) = *(src.v_float); |
dc877563 |
251 | break; |
252 | |
253 | case LTTV_DOUBLE: |
ffd54a90 |
254 | *(dest.v_double) = *(src.v_double); |
dc877563 |
255 | break; |
256 | |
257 | case LTTV_TIME: |
ffd54a90 |
258 | *(dest.v_time) = *(src.v_time); |
dc877563 |
259 | break; |
260 | |
261 | case LTTV_POINTER: |
ffd54a90 |
262 | *(dest.v_pointer) = *(src.v_pointer); |
dc877563 |
263 | break; |
264 | |
265 | case LTTV_STRING: |
ffd54a90 |
266 | *(dest.v_string) = *(src.v_string); |
dc877563 |
267 | break; |
268 | |
269 | case LTTV_GOBJECT: |
ffd54a90 |
270 | *(dest.v_gobject) = *(src.v_gobject); |
dc877563 |
271 | break; |
272 | |
273 | case LTTV_NONE: |
274 | break; |
275 | } |
276 | } |
277 | |
278 | |