| 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 | |
| 19 | |
| 20 | #include <lttv/iattribute.h> |
| 21 | |
| 22 | static void |
| 23 | lttv_iattribute_base_init (gpointer klass) |
| 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 | |
| 82 | LttvAttributeValue lttv_iattribute_add(LttvIAttribute *self, |
| 83 | LttvAttributeName name, LttvAttributeType t) |
| 84 | { |
| 85 | return LTTV_IATTRIBUTE_GET_CLASS (self)->add (self, name, t); |
| 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 | |
| 101 | LttvIAttribute* lttv_iattribute_find_subdir(LttvIAttribute *self, |
| 102 | LttvAttributeName name) |
| 103 | { |
| 104 | return LTTV_IATTRIBUTE_GET_CLASS (self)->find_subdir (self, name); |
| 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) { |
| 123 | *v = lttv_iattribute_add(self, name, t); |
| 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 | { |
| 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 | |
| 152 | for(cursor = components;;) { |
| 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) { |
| 163 | node = lttv_iattribute_find_subdir(node, name); |
| 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 | |
| 178 | /* Shallow and deep copies */ |
| 179 | |
| 180 | LttvIAttribute *lttv_iattribute_shallow_copy(LttvIAttribute *self) |
| 181 | { |
| 182 | LttvIAttribute *copy; |
| 183 | |
| 184 | LttvAttributeType t; |
| 185 | |
| 186 | LttvAttributeValue v, v_copy; |
| 187 | |
| 188 | LttvAttributeName name; |
| 189 | |
| 190 | int i; |
| 191 | |
| 192 | int nb_attributes = lttv_iattribute_get_number(self); |
| 193 | |
| 194 | copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL); |
| 195 | |
| 196 | for(i = 0 ; i < nb_attributes ; i++) { |
| 197 | t = lttv_iattribute_get(self, i, &name, &v); |
| 198 | v_copy = lttv_iattribute_add(copy, name, t); |
| 199 | lttv_iattribute_copy_value(t, v_copy, v); |
| 200 | } |
| 201 | return copy; |
| 202 | } |
| 203 | |
| 204 | LttvIAttribute *lttv_iattribute_deep_copy(LttvIAttribute *self) |
| 205 | { |
| 206 | LttvIAttribute *copy, *child; |
| 207 | |
| 208 | LttvAttributeType t; |
| 209 | |
| 210 | LttvAttributeValue v, v_copy; |
| 211 | |
| 212 | LttvAttributeName name; |
| 213 | |
| 214 | int i; |
| 215 | |
| 216 | int nb_attributes = lttv_iattribute_get_number(self); |
| 217 | |
| 218 | copy = LTTV_IATTRIBUTE_GET_CLASS(self)->new_attribute(NULL); |
| 219 | |
| 220 | for(i = 0 ; i < nb_attributes ; i++) { |
| 221 | t = lttv_iattribute_get(self, i, &name, &v); |
| 222 | v_copy = lttv_iattribute_add(copy, name, t); |
| 223 | if(t == LTTV_GOBJECT && LTTV_IS_IATTRIBUTE(*(v.v_gobject))) { |
| 224 | child = LTTV_IATTRIBUTE(*(v.v_gobject)); |
| 225 | *(v_copy.v_gobject) = G_OBJECT(lttv_iattribute_deep_copy(child)); |
| 226 | } |
| 227 | else lttv_iattribute_copy_value(t, v_copy, v); |
| 228 | } |
| 229 | return copy; |
| 230 | } |
| 231 | |
| 232 | void lttv_iattribute_copy_value(LttvAttributeType t, LttvAttributeValue dest, |
| 233 | LttvAttributeValue src) |
| 234 | { |
| 235 | switch(t) { |
| 236 | case LTTV_INT: |
| 237 | *(dest.v_int) = *(src.v_int); |
| 238 | break; |
| 239 | |
| 240 | case LTTV_UINT: |
| 241 | *(dest.v_uint) = *(src.v_uint); |
| 242 | break; |
| 243 | |
| 244 | case LTTV_LONG: |
| 245 | *(dest.v_long) = *(src.v_long); |
| 246 | break; |
| 247 | |
| 248 | case LTTV_ULONG: |
| 249 | *(dest.v_ulong) = *(src.v_ulong); |
| 250 | break; |
| 251 | |
| 252 | case LTTV_FLOAT: |
| 253 | *(dest.v_float) = *(src.v_float); |
| 254 | break; |
| 255 | |
| 256 | case LTTV_DOUBLE: |
| 257 | *(dest.v_double) = *(src.v_double); |
| 258 | break; |
| 259 | |
| 260 | case LTTV_TIME: |
| 261 | *(dest.v_time) = *(src.v_time); |
| 262 | break; |
| 263 | |
| 264 | case LTTV_POINTER: |
| 265 | *(dest.v_pointer) = *(src.v_pointer); |
| 266 | break; |
| 267 | |
| 268 | case LTTV_STRING: |
| 269 | *(dest.v_string) = *(src.v_string); |
| 270 | break; |
| 271 | |
| 272 | case LTTV_GOBJECT: |
| 273 | *(dest.v_gobject) = *(src.v_gobject); |
| 274 | if(*(dest.v_gobject) != NULL) g_object_ref(*(dest.v_gobject)); |
| 275 | break; |
| 276 | |
| 277 | case LTTV_NONE: |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | |