6 #include "primitives.h"
12 //----------------------------------------------------------------------------
17 //----------------------------------------------------------------------------
18 PyObject * taglist_new()
20 TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
21 mylog("+%08x(%d) taglist_new", (int)taglist, taglist->ob_refcnt);
22 taglist->taglist = PyList_New(0);
23 return (PyObject*)taglist;
25 //----------------------------------------------------------------------------
26 PyObject * taglist_new2(TAG*tag)
28 TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
29 mylog("+%08x(%d) taglist_new2 tag=%08x", (int)taglist, taglist->ob_refcnt, tag);
30 PyObject* tagmap = tagmap_new();
35 while(t) {len++;last=t;t=t->next;}
37 if(last && last->id==ST_END) {
38 swf_DeleteTag(last); last = 0;
43 taglist->taglist = PyList_New(len);
48 PyObject*newtag = tag_new2(t, tagmap);
50 // pass through exception
54 PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
55 if(swf_isDefiningTag(t)) {
56 int id = swf_GetDefineID(t);
57 tagmap_addMapping(tagmap, id, newtag);
63 return (PyObject*)taglist;
65 //----------------------------------------------------------------------------
66 TAG* taglist_getTAGs(PyObject*self)
68 PyObject* tagmap = tagmap_new();
69 if(!PY_CHECK_TYPE(self,&TagListClass)) {
70 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
73 TagListObject*taglist = (TagListObject*)self;
75 /* TODO: the tags will be modified by this. We should set mutexes. */
77 int l = PyList_Size(taglist->taglist);
81 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
83 PyObject*item = PyList_GetItem(taglist->taglist, t);
84 tag = tag_getTAG(item, tag, tagmap);
96 //----------------------------------------------------------------------------
97 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
100 TagListObject*taglist = (TagListObject*)self;
101 if(!self || !PyArg_ParseTuple(args,""))
103 swf.firstTag = taglist->firstTag;
105 taglist->firstTag = swf.firstTag;
106 taglist->lastTag = 0; // FIXME
107 taglist->searchTag = 0;*/
110 //----------------------------------------------------------------------------
111 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
114 /* TagListObject*taglist = (TagListObject*)self;
115 if(!self || !PyArg_ParseTuple(args,""))
117 swf.firstTag = taglist->firstTag;
119 taglist->firstTag = swf.firstTag;
120 taglist->lastTag = 0; // FIXME
121 taglist->searchTag = 0;*/
124 //----------------------------------------------------------------------------
125 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
128 /* TagListObject*taglist = (TagListObject*)self;
129 if(!self || !PyArg_ParseTuple(args,""))
131 swf.firstTag = taglist->firstTag;
133 taglist->firstTag = swf.firstTag;
134 taglist->lastTag = 0; // FIXME
135 taglist->searchTag = 0;*/
138 //----------------------------------------------------------------------------
139 static void taglist_dealloc(PyObject* self)
141 TagListObject*taglist = (TagListObject*)self;
142 mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt);
143 Py_DECREF(taglist->taglist);
144 taglist->taglist = 0;
147 //----------------------------------------------------------------------------
148 static PyMethodDef taglist_functions[] =
149 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
150 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
151 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
152 {NULL, NULL, 0, NULL}
155 static PyObject* taglist_getattr(PyObject * self, char* a)
157 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
158 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
161 //----------------------------------------------------------------------------
162 static int taglist_length(PyObject * self)
164 TagListObject*tags = (TagListObject*)self;
165 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
166 return PyList_GET_SIZE(tags->taglist);
168 //----------------------------------------------------------------------------
169 static int taglist_contains(PyObject * self, PyObject * tag)
171 /* TODO: optimize! */
172 TagListObject*taglist = (TagListObject*)self;
173 PyObject*list = taglist->taglist;
174 int l = PyList_Size(list);
177 PyObject*item = PyList_GetItem(list, t);
184 //----------------------------------------------------------------------------
185 static PyObject * taglist_concat(PyObject * self, PyObject* list)
188 PY_ASSERT_TYPE(self, &TagListClass);
189 TagListObject*taglist = (TagListObject*)self;
190 mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);
192 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
193 list = tag_getDependencies(tag);
194 int l = PyList_Size(list);
197 PyObject*item = PyList_GetItem(list, t);
198 PyObject*_self = taglist_concat(self, item);
202 if(!taglist_contains(self, tag)) {
203 mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
204 PyList_Append(taglist->taglist, tag);
206 mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
210 /* copy tag, so we don't have to do INCREF(tag) (and don't
211 get problems if the tag is appended to more than one
213 /* TODO: handle IDs */
215 TAG*t = tag_getTAG(tag);
217 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
219 nt = swf_InsertTag(0, t->id);
220 swf_SetBlock(nt,t->data,t->len);
221 PyObject*newtag = tag_new(taglist->swf, nt);
222 if(swf_isDefiningTag(t)) {
223 int id = swf_GetDefineID(t);
224 PyObject*id = PyLong_FromLong(id);
225 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
227 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
234 if (PyList_Check(list)) {
235 int l = PyList_Size(list);
238 PyObject*item = PyList_GetItem(list, t);
239 if(!PY_CHECK_TYPE(item, &TagClass)) {
240 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
243 PyObject*_self = taglist_concat(self, item);
253 if (PY_CHECK_TYPE(list, &TagListClass)) {
254 TagListObject*taglist2 = (TagListObject*)list;
255 return taglist_concat(self, taglist2->taglist);
257 /*TAG* tags = taglist_getTAGs(self);
258 TAG* tags2 = taglist_getTAGs(list);
260 tags3 = swf_Concatenate(tags,tags2);
261 PyObject* newtaglist = taglist_new(tags3);
263 Py_INCREF(newtaglist);*/
267 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
270 //----------------------------------------------------------------------------
271 static PyObject * taglist_item(PyObject * self, int index)
273 TagListObject*taglist = (TagListObject*)self;
275 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
276 tag = PyList_GetItem(taglist->taglist, index);
280 static PySequenceMethods taglist_as_sequence =
282 sq_length: taglist_length, // len(obj)
283 sq_concat: taglist_concat, // obj += [...], obj1+obj2
284 sq_repeat: 0, // x*n, intargfunc
285 sq_item: taglist_item, // obj[3]
286 sq_slice: 0, // x[i:j] intintargfunc
287 sq_ass_item: 0, // x[i] = y intobjargproc
288 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
289 sq_contains: taglist_contains, //???
291 PyTypeObject TagListClass =
293 PyObject_HEAD_INIT(NULL)
296 tp_basicsize: sizeof(TagListObject),
298 tp_dealloc: taglist_dealloc,
299 tp_print: 0, // print x
300 tp_getattr: taglist_getattr, // x.attr
301 tp_setattr: 0, // x.attr = v
302 tp_compare: 0, // x>y
303 tp_repr: 0, // `x`, print x
305 tp_as_sequence: &taglist_as_sequence,