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