dc877563 |
1 | |
2 | static void |
3 | lttv_iattribute_base_init (gpointer g_class) |
4 | { |
5 | static gboolean initialized = FALSE; |
6 | |
7 | if (!initialized) { |
8 | initialized = TRUE; |
9 | } |
10 | } |
11 | |
12 | |
13 | GType |
14 | lttv_iattribute_get_type (void) |
15 | { |
16 | static GType type = 0; |
17 | if (type == 0) { |
18 | static const GTypeInfo info = { |
19 | sizeof (LttvIAttributeClass), |
20 | lttv_iattribute_base_init, /* base_init */ |
21 | NULL, /* base_finalize */ |
22 | NULL, /* class_init */ |
23 | NULL, /* class_finalize */ |
24 | NULL, /* class_data */ |
25 | 0, |
26 | 0, /* n_preallocs */ |
27 | NULL /* instance_init */ |
28 | }; |
29 | type = g_type_register_static (G_TYPE_INTERFACE, "LttvIAttribute", |
30 | &info, 0); |
31 | } |
32 | return type; |
33 | } |
34 | |
35 | |
36 | unsigned int lttv_iattribute_get_number(LttvIAttribute *self) |
37 | { |
38 | return LTTV_IATTRIBUTE_GET_CLASS (self)->get_number (self); |
39 | } |
40 | |
41 | |
42 | gboolean lttv_iattribute_named(LttvIAttribute *self, gboolean *homogeneous) |
43 | { |
44 | return LTTV_IATTRIBUTE_GET_CLASS (self)->named (self, homogeneous); |
45 | } |
46 | |
47 | |
48 | LttvAttributeType lttv_iattribute_get(LttvIAttribute *self, unsigned i, |
49 | LttvAttributeName *name, LttvAttributeValue *v) |
50 | { |
51 | return LTTV_IATTRIBUTE_GET_CLASS (self)->get (self, i, name, v); |
52 | } |
53 | |
54 | |
55 | LttvAttributeType lttv_iattribute_get_by_name(LttvIAttribute *self, |
56 | LttvAttributeName name, LttvAttributeValue *v) |
57 | { |
58 | return LTTV_IATTRIBUTE_GET_CLASS (self)->get_by_name (self, name, v); |
59 | } |
60 | |
61 | |
62 | void lttv_iattribute_add(LttvIAttribute *self, LttvAttributeName name, |
63 | LttvAttributeType t, LttvAttributeValue *v) |
64 | { |
65 | return LTTV_IATTRIBUTE_GET_CLASS (self)->add (self, name, t, v); |
66 | } |
67 | |
68 | |
69 | void lttv_iattribute_remove(LttvIAttribute *self, unsigned i) |
70 | { |
71 | return LTTV_IATTRIBUTE_GET_CLASS (self)->remove (self, i); |
72 | } |
73 | |
74 | |
75 | void lttv_iattribute_remove_by_name(LttvIAttribute *self, |
76 | LttvAttributeName name) |
77 | { |
78 | return LTTV_IATTRIBUTE_GET_CLASS (self)->remove_by_name (self, name); |
79 | } |
80 | |
81 | LttvIAttribute* lttv_iattribute_create_subdir(LttvIAttribute *self, |
82 | LttvAttributeName name) |
83 | { |
84 | return LTTV_IATTRIBUTE_GET_CLASS (self)->get_number (self, name); |
85 | } |
86 | |
87 | |
88 | /* Find the named attribute in the table, which must be of the specified type. |
89 | If it does not exist, it is created with a default value of 0 (NULL for |
90 | pointer types). Since the address of the value is obtained, it may be |
91 | changed easily afterwards. The function returns false when the attribute |
92 | exists but is of incorrect type. */ |
93 | |
94 | gboolean lttv_iattribute_find(LttvIAttribute *self, LttvAttributeName name, |
95 | LttvAttributeType t, LttvAttributeValue *v) |
96 | { |
97 | LttvAttributeType found_type; |
98 | |
99 | found_type = lttv_iattribute_get_by_name(self, name, v); |
100 | if(found_type == t) return TRUE; |
101 | |
102 | if(found_type == LTTV_NONE) { |
103 | v = lttv_iattribute_add(self, name, t); |
104 | return TRUE; |
105 | } |
106 | |
107 | return FALSE; |
108 | } |
109 | |
110 | |
111 | /* Trees of attribute tables may be accessed using a hierarchical path with |
112 | components separated by /, like in filesystems */ |
113 | |
114 | gboolean lttv_iattribute_find_by_path(LttvIAttribute *self, char *path, |
115 | LttvAttributeType t, LttvAttributeValue *v) |
116 | { |
117 | char *cursor; |
118 | |
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 | |
134 | while(cursor = components;;) { |
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 | { |
163 | LttvIAttribute copy; |
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 | |
175 | copy = LTTV_IATTRIBUTE(g_object_new(G_OBJECT_TYPE(self))); |
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); |
180 | lttv_iattribute_copy_value(v_copy, v); |
181 | } |
182 | } |
183 | |
184 | LttvIAttribute *lttv_iattribute_deep_copy(LttvIAttribute *self) |
185 | { |
186 | LttvIAttribute copy, child; |
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 | |
198 | copy = LTTV_IATTRIBUTE(g_object_new(G_OBJECT_TYPE(self))); |
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); |
203 | if(t == LTTV_GOBJECT && LTTV_IS_IATTRIBUTE(*(v->v_gobject))) { |
204 | child = LTTV_IATTRIBUTE(*(v->v_gobject)); |
205 | *(v_copy->v_gobject) = lttv_iattribute_deep_copy(child); |
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: |
216 | *(dest->v_int) = *(src->v_int); |
217 | break; |
218 | |
219 | case LTTV_UINT: |
220 | *(dest->v_uint) = *(src->v_uint); |
221 | break; |
222 | |
223 | case LTTV_LONG: |
224 | *(dest->v_long) = *(src->v_long); |
225 | break; |
226 | |
227 | case LTTV_ULONG: |
228 | *(dest->v_ulong) = *(src->v_ulong); |
229 | break; |
230 | |
231 | case LTTV_FLOAT: |
232 | *(dest->v_float) = *(src->v_float); |
233 | break; |
234 | |
235 | case LTTV_DOUBLE: |
236 | *(dest->v_double) = *(src->v_double); |
237 | break; |
238 | |
239 | case LTTV_TIME: |
240 | *(dest->v_time) = *(src->v_time); |
241 | break; |
242 | |
243 | case LTTV_POINTER: |
244 | *(dest->v_pointer) = *(src->v_pointer); |
245 | break; |
246 | |
247 | case LTTV_STRING: |
248 | *(dest->v_string) = *(src->v_string); |
249 | break; |
250 | |
251 | case LTTV_GOBJECT: |
252 | *(dest->v_gobject) = *(src->v_gobject); |
253 | break; |
254 | |
255 | case LTTV_NONE: |
256 | break; |
257 | } |
258 | } |
259 | |
260 | |