be paranoid about empty buffers
[swfdec.git] / test / swfedit_list.c
blob45e0dd59259fbc7e27c2019a19e71eabd9b617cd
1 /* Swfedit
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "swfedit_list.h"
26 G_DEFINE_TYPE (SwfeditList, swfedit_list, SWFEDIT_TYPE_TOKEN)
28 static void
29 swfedit_list_dispose (GObject *object)
31 //SwfeditList *list = SWFEDIT_LIST (object);
33 G_OBJECT_CLASS (swfedit_list_parent_class)->dispose (object);
36 static void
37 swfedit_list_changed (SwfeditToken *token, guint i)
39 guint j;
40 SwfeditList *list = SWFEDIT_LIST (token);
41 SwfeditTokenEntry *entry = &g_array_index (token->tokens, SwfeditTokenEntry, i);
43 /* update visibility */
44 for (j = i + 1; j % list->n_defs != 0; j++) {
45 if (list->def[j].n_items == (j % list->n_defs) + 1) {
46 swfedit_token_set_visible (token, j, entry->value != NULL);
49 /* update length */
50 j = list->def[i % list->n_defs].n_items;
51 if (j != 0) {
52 entry = &g_array_index (token->tokens,
53 SwfeditTokenEntry, j - 1);
54 if (entry->type == SWFEDIT_TOKEN_UINT32) {
55 SwfdecOut *out = swfdec_out_open ();
56 SwfdecBuffer *buffer;
57 swfedit_tag_write_token (token, out, i);
58 buffer = swfdec_out_close (out);
59 if (entry->value != GUINT_TO_POINTER (buffer->length)) {
60 swfedit_token_set (token, i / list->n_defs * list->n_defs + j - 1,
61 GUINT_TO_POINTER (buffer->length));
63 swfdec_buffer_unref (buffer);
66 /* maybe add items */
67 if (i == token->tokens->len - 1) {
68 for (j = 0; j < list->n_defs; j++) {
69 const SwfeditTagDefinition *def = &list->def[(j + 1) % list->n_defs];
70 swfedit_tag_add_token (SWFEDIT_TOKEN (list), def->name, def->type, def->hint);
75 static void
76 swfedit_list_class_init (SwfeditListClass *klass)
78 GObjectClass *object_class = G_OBJECT_CLASS (klass);
79 SwfeditTokenClass *token_class = SWFEDIT_TOKEN_CLASS (klass);
81 object_class->dispose = swfedit_list_dispose;
83 token_class->changed = swfedit_list_changed;
86 static void
87 swfedit_list_init (SwfeditList *list)
91 static SwfeditList *
92 swfedit_list_new_internal (const SwfeditTagDefinition *def)
94 SwfeditList *list;
96 list = g_object_new (SWFEDIT_TYPE_LIST, NULL);
97 list->def = def;
98 for (; def->name; def++)
99 list->n_defs++;
101 return list;
104 SwfeditList *
105 swfedit_list_new (const SwfeditTagDefinition *def)
107 SwfeditList *list;
109 g_return_val_if_fail (def != NULL, NULL);
111 list = swfedit_list_new_internal (def);
112 swfedit_tag_add_token (SWFEDIT_TOKEN (list), def->name, def->type, def->hint);
114 return list;
117 SwfeditList *
118 swfedit_list_new_read (SwfeditToken *parent, SwfdecBits *bits, const SwfeditTagDefinition *def)
120 SwfeditList *list;
121 SwfeditTokenEntry *entry;
122 guint offset;
124 g_return_val_if_fail (bits != NULL, NULL);
125 g_return_val_if_fail (def != NULL, NULL);
127 list = swfedit_list_new_internal (def);
128 SWFEDIT_TOKEN (list)->parent = parent;
129 offset = 0;
130 while (TRUE) {
131 def = list->def;
132 swfedit_tag_read_tag (SWFEDIT_TOKEN (list), bits, def);
133 entry = &g_array_index (SWFEDIT_TOKEN (list)->tokens, SwfeditTokenEntry,
134 SWFEDIT_TOKEN (list)->tokens->len - 1);
135 if (GPOINTER_TO_UINT (entry->value) == 0)
136 break;
138 def++;
139 for (;def->name != NULL; def++) {
140 SwfeditTagDefinition def2 = *def;
141 if (def2.n_items)
142 def2.n_items += offset;
143 swfedit_tag_read_tag (SWFEDIT_TOKEN (list), bits, &def2);
145 offset += list->n_defs;
147 return list;