Mercurial > audlegacy
annotate src/audacious/tuple.c @ 3395:df609e7e7bcf
updated romanian translation
| author | Cristi Magherusan <majeru@atheme-project.org> |
|---|---|
| date | Sun, 26 Aug 2007 03:06:40 +0300 |
| parents | 70149c3555f4 |
| children | 86dafe2300f7 |
| rev | line source |
|---|---|
| 3278 | 1 /* |
| 2 * Audacious | |
| 3 * Copyright (c) 2006-2007 Audacious team | |
| 4 * | |
| 5 * This program is free software; you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License as published by | |
| 7 * the Free Software Foundation; under version 3 of the License. | |
| 8 * | |
| 9 * This program is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with this program. If not, see <http://www.gnu.org/licenses>. | |
| 16 * | |
| 17 * The Audacious team does not consider modular code linking to | |
| 18 * Audacious or using our public API to be a derived work. | |
| 19 */ | |
| 20 | |
| 21 #include <glib.h> | |
| 22 #include <mowgli.h> | |
| 23 | |
| 24 #include "tuple.h" | |
| 25 | |
| 26 struct _Tuple { | |
| 27 mowgli_object_t parent; | |
| 28 mowgli_dictionary_t *dict; | |
| 29 }; | |
| 30 | |
| 31 typedef struct { | |
| 32 TupleValueType type; | |
| 33 union { | |
| 34 gchar *string; | |
| 35 gint integer; | |
| 36 } value; | |
| 37 } TupleValue; | |
| 38 | |
| 39 static mowgli_heap_t *tuple_heap = NULL; | |
| 40 static mowgli_heap_t *tuple_value_heap = NULL; | |
| 41 static mowgli_object_class_t tuple_klass; | |
| 42 | |
|
3280
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
43 /* iterative destructor of tuple values. */ |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
44 static void |
|
3282
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
45 tuple_value_destroy(mowgli_dictionary_elem_t *delem, gpointer privdata) |
|
3280
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
46 { |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
47 TupleValue *value = (TupleValue *) delem->data; |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
48 |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
49 if (value->type == TUPLE_STRING) |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
50 g_free(value->value.string); |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
51 |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
52 mowgli_heap_free(tuple_value_heap, value); |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
53 } |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
54 |
| 3278 | 55 static void |
|
3282
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
56 tuple_destroy(gpointer data) |
| 3278 | 57 { |
|
3282
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
58 Tuple *tuple = (Tuple *) data; |
|
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
59 |
|
3280
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
60 mowgli_dictionary_destroy(tuple->dict, tuple_value_destroy, NULL); |
| 3278 | 61 mowgli_heap_free(tuple_heap, tuple); |
| 62 } | |
| 63 | |
| 64 Tuple * | |
| 65 tuple_new(void) | |
| 66 { | |
| 67 Tuple *tuple; | |
| 68 | |
| 69 if (tuple_heap == NULL) | |
| 70 { | |
| 3281 | 71 tuple_heap = mowgli_heap_create(sizeof(Tuple), 256, BH_NOW); |
| 72 tuple_value_heap = mowgli_heap_create(sizeof(TupleValue), 512, BH_NOW); | |
| 3278 | 73 mowgli_object_class_init(&tuple_klass, "audacious.tuple", tuple_destroy, FALSE); |
| 74 } | |
| 75 | |
| 76 /* FIXME: use mowgli_object_bless_from_class() in mowgli 0.4 | |
| 77 when it is released --nenolod */ | |
| 78 tuple = mowgli_heap_alloc(tuple_heap); | |
| 79 mowgli_object_init(mowgli_object(tuple), NULL, &tuple_klass, NULL); | |
| 80 | |
| 81 tuple->dict = mowgli_dictionary_create(g_ascii_strcasecmp); | |
| 82 | |
| 83 return tuple; | |
| 84 } | |
| 85 | |
|
3301
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
86 Tuple * |
|
3304
00286cde2485
Make filename a const
Christian Birchinger <joker@netswarm.net>
parents:
3303
diff
changeset
|
87 tuple_new_from_filename(const gchar *filename) |
|
3301
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
88 { |
|
3303
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
89 gchar *scratch, *ext, *realfn; |
|
3301
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
90 Tuple *tuple; |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
91 |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
92 g_return_val_if_fail(filename != NULL, NULL); |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
93 |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
94 tuple = tuple_new(); |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
95 |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
96 g_return_val_if_fail(tuple != NULL, NULL); |
|
3303
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
97 |
|
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
98 realfn = g_filename_from_uri(filename, NULL, NULL); |
|
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
99 |
|
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
100 scratch = g_path_get_basename(realfn ? realfn : filename); |
|
3301
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
101 tuple_associate_string(tuple, "file-name", scratch); |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
102 g_free(scratch); |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
103 |
|
3303
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
104 scratch = g_path_get_dirname(realfn ? realfn : filename); |
|
3301
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
105 tuple_associate_string(tuple, "file-path", scratch); |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
106 g_free(scratch); |
|
3303
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
107 |
|
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
108 g_free(realfn); realfn = NULL; |
|
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
109 |
|
3301
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
110 ext = strrchr(filename, '.'); |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
111 if (ext != NULL) { |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
112 ++ext; |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
113 tuple_associate_string(tuple, "file-ext", scratch); |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
114 } |
|
3303
eaf68ed98166
Use real filenames inside tuples not URIs
Christian Birchinger <joker@netswarm.net>
parents:
3301
diff
changeset
|
115 |
|
3301
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
116 return tuple; |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
117 } |
|
008530664ba1
Add tuple_new_from_filename() which creates a new tuple with file-name, file-path and file-ext.
Christian Birchinger <joker@netswarm.net>
parents:
3282
diff
changeset
|
118 |
| 3278 | 119 gboolean |
| 120 tuple_associate_string(Tuple *tuple, const gchar *field, const gchar *string) | |
| 121 { | |
| 122 TupleValue *value; | |
| 123 | |
| 124 g_return_val_if_fail(tuple != NULL, FALSE); | |
| 125 g_return_val_if_fail(field != NULL, FALSE); | |
| 126 | |
| 127 if (mowgli_dictionary_find(tuple->dict, field)) | |
| 128 tuple_disassociate(tuple, field); | |
| 129 | |
|
3329
70149c3555f4
For interest of transparency, make associating NULL equivilant to deleting the field.
William Pitcock <nenolod@atheme-project.org>
parents:
3304
diff
changeset
|
130 if (string == NULL) |
|
70149c3555f4
For interest of transparency, make associating NULL equivilant to deleting the field.
William Pitcock <nenolod@atheme-project.org>
parents:
3304
diff
changeset
|
131 return TRUE; |
|
70149c3555f4
For interest of transparency, make associating NULL equivilant to deleting the field.
William Pitcock <nenolod@atheme-project.org>
parents:
3304
diff
changeset
|
132 |
| 3278 | 133 value = mowgli_heap_alloc(tuple_value_heap); |
| 134 value->type = TUPLE_STRING; | |
| 135 value->value.string = g_strdup(string); | |
| 136 | |
| 137 mowgli_dictionary_add(tuple->dict, field, value); | |
| 138 | |
| 139 return TRUE; | |
| 140 } | |
| 141 | |
| 142 gboolean | |
| 143 tuple_associate_int(Tuple *tuple, const gchar *field, gint integer) | |
| 144 { | |
| 145 TupleValue *value; | |
| 146 | |
| 147 g_return_val_if_fail(tuple != NULL, FALSE); | |
| 148 g_return_val_if_fail(field != NULL, FALSE); | |
| 149 | |
| 150 if (mowgli_dictionary_find(tuple->dict, field)) | |
| 151 tuple_disassociate(tuple, field); | |
| 152 | |
| 153 value = mowgli_heap_alloc(tuple_value_heap); | |
| 154 value->type = TUPLE_INT; | |
| 155 value->value.integer = integer; | |
| 156 | |
| 157 mowgli_dictionary_add(tuple->dict, field, value); | |
| 158 | |
| 159 return TRUE; | |
| 160 } | |
| 161 | |
| 162 void | |
| 163 tuple_disassociate(Tuple *tuple, const gchar *field) | |
| 164 { | |
| 165 TupleValue *value; | |
| 166 | |
| 167 g_return_if_fail(tuple != NULL); | |
| 168 g_return_if_fail(field != NULL); | |
| 169 | |
| 170 /* why _delete()? because _delete() returns the dictnode's data on success */ | |
| 171 if ((value = mowgli_dictionary_delete(tuple->dict, field)) == NULL) | |
| 172 return; | |
| 173 | |
| 174 if (value->type == TUPLE_STRING) | |
| 175 g_free(value->value.string); | |
| 176 | |
| 177 mowgli_heap_free(tuple_value_heap, value); | |
| 178 } | |
| 179 | |
| 180 TupleValueType | |
| 181 tuple_get_value_type(Tuple *tuple, const gchar *field) | |
| 182 { | |
| 183 TupleValue *value; | |
| 184 | |
|
3280
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
185 g_return_val_if_fail(tuple != NULL, TUPLE_UNKNOWN); |
|
a26138e391ee
Tuple engine cleanups.
William Pitcock <nenolod@atheme-project.org>
parents:
3278
diff
changeset
|
186 g_return_val_if_fail(field != NULL, TUPLE_UNKNOWN); |
| 3278 | 187 |
| 188 if ((value = mowgli_dictionary_retrieve(tuple->dict, field)) == NULL) | |
| 189 return TUPLE_UNKNOWN; | |
| 190 | |
| 191 return value->type; | |
| 192 } | |
| 193 | |
| 194 const gchar * | |
| 195 tuple_get_string(Tuple *tuple, const gchar *field) | |
| 196 { | |
| 197 TupleValue *value; | |
| 198 | |
|
3282
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
199 g_return_val_if_fail(tuple != NULL, NULL); |
|
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
200 g_return_val_if_fail(field != NULL, NULL); |
| 3278 | 201 |
| 202 if ((value = mowgli_dictionary_retrieve(tuple->dict, field)) == NULL) | |
| 203 return NULL; | |
| 204 | |
| 205 if (value->type != TUPLE_STRING) | |
| 206 mowgli_throw_exception_val(audacious.tuple.invalid_type_request, NULL); | |
| 207 | |
| 208 return value->value.string; | |
| 209 } | |
| 210 | |
| 211 int | |
| 212 tuple_get_int(Tuple *tuple, const gchar *field) | |
| 213 { | |
| 214 TupleValue *value; | |
| 215 | |
|
3282
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
216 g_return_val_if_fail(tuple != NULL, 0); |
|
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
217 g_return_val_if_fail(field != NULL, 0); |
| 3278 | 218 |
| 219 if ((value = mowgli_dictionary_retrieve(tuple->dict, field)) == NULL) | |
|
3282
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
220 return 0; |
| 3278 | 221 |
| 222 if (value->type != TUPLE_INT) | |
|
3282
b78d3197c70d
Tuple (final version)
William Pitcock <nenolod@atheme-project.org>
parents:
3281
diff
changeset
|
223 mowgli_throw_exception_val(audacious.tuple.invalid_type_request, 0); |
| 3278 | 224 |
| 225 return value->value.integer; | |
| 226 } |
