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();
37 while(t) {nr++;last=t;t=t->next;}
39 if(last && last->id==ST_END) {
44 taglist->taglist = PyList_New(nr);
46 mylog("+%08x(%d) taglist_new2: %d items", (int)taglist, taglist->ob_refcnt, nr);
51 PyObject*newtag = tag_new2(t, tagmap);
53 // pass through exception
57 PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
58 if(swf_isDefiningTag(t)) {
59 int id = swf_GetDefineID(t);
60 tagmap_addMapping(tagmap, id, newtag);
66 return (PyObject*)taglist;
68 //----------------------------------------------------------------------------
69 TAG* taglist_getTAGs(PyObject*self)
71 PyObject* tagmap = tagmap_new();
72 if(!PY_CHECK_TYPE(self,&TagListClass)) {
73 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
76 TagListObject*taglist = (TagListObject*)self;
78 /* TODO: the tags will be modified by this. We should set mutexes. */
80 int l = PyList_Size(taglist->taglist);
84 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
86 PyObject*item = PyList_GetItem(taglist->taglist, t);
87 tag = tag_getTAG(item, tag, tagmap);
99 //----------------------------------------------------------------------------
100 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
103 TagListObject*taglist = (TagListObject*)self;
104 if(!self || !PyArg_ParseTuple(args,""))
106 swf.firstTag = taglist->firstTag;
108 taglist->firstTag = swf.firstTag;
109 taglist->lastTag = 0; // FIXME
110 taglist->searchTag = 0;*/
113 //----------------------------------------------------------------------------
114 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
117 /* TagListObject*taglist = (TagListObject*)self;
118 if(!self || !PyArg_ParseTuple(args,""))
120 swf.firstTag = taglist->firstTag;
122 taglist->firstTag = swf.firstTag;
123 taglist->lastTag = 0; // FIXME
124 taglist->searchTag = 0;*/
127 //----------------------------------------------------------------------------
128 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
131 /* TagListObject*taglist = (TagListObject*)self;
132 if(!self || !PyArg_ParseTuple(args,""))
134 swf.firstTag = taglist->firstTag;
136 taglist->firstTag = swf.firstTag;
137 taglist->lastTag = 0; // FIXME
138 taglist->searchTag = 0;*/
141 //----------------------------------------------------------------------------
142 static void taglist_dealloc(PyObject* self)
144 TagListObject*taglist = (TagListObject*)self;
145 mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt);
146 Py_DECREF(taglist->taglist);
147 taglist->taglist = 0;
150 //----------------------------------------------------------------------------
151 static PyMethodDef taglist_functions[] =
152 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
153 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
154 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
155 {NULL, NULL, 0, NULL}
158 static PyObject* taglist_getattr(PyObject * self, char* a)
160 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
161 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
164 //----------------------------------------------------------------------------
165 static int taglist_length(PyObject * self)
167 TagListObject*tags = (TagListObject*)self;
168 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
169 return PyList_GET_SIZE(tags->taglist);
171 //----------------------------------------------------------------------------
172 static int taglist_contains(PyObject * self, PyObject * tag)
174 /* TODO: optimize! */
175 TagListObject*taglist = (TagListObject*)self;
176 PyObject*list = taglist->taglist;
177 int l = PyList_Size(list);
180 PyObject*item = PyList_GetItem(list, t);
187 //----------------------------------------------------------------------------
188 static PyObject * taglist_concat(PyObject * self, PyObject* list)
191 PY_ASSERT_TYPE(self, &TagListClass);
192 TagListObject*taglist = (TagListObject*)self;
193 mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);
195 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
196 list = tag_getDependencies(tag);
197 int l = PyList_Size(list);
200 PyObject*item = PyList_GetItem(list, t);
201 PyObject*_self = taglist_concat(self, item);
205 if(!taglist_contains(self, tag)) {
206 mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
207 PyList_Append(taglist->taglist, tag);
209 mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
213 /* copy tag, so we don't have to do INCREF(tag) (and don't
214 get problems if the tag is appended to more than one
216 /* TODO: handle IDs */
218 TAG*t = tag_getTAG(tag);
220 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
222 nt = swf_InsertTag(0, t->id);
223 swf_SetBlock(nt,t->data,t->len);
224 PyObject*newtag = tag_new(taglist->swf, nt);
225 if(swf_isDefiningTag(t)) {
226 int id = swf_GetDefineID(t);
227 PyObject*id = PyLong_FromLong(id);
228 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
230 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
237 if (PyList_Check(list)) {
238 int l = PyList_Size(list);
241 PyObject*item = PyList_GetItem(list, t);
242 if(!PY_CHECK_TYPE(item, &TagClass)) {
243 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
246 PyObject*_self = taglist_concat(self, item);
256 if (PY_CHECK_TYPE(list, &TagListClass)) {
257 TagListObject*taglist2 = (TagListObject*)list;
258 return taglist_concat(self, taglist2->taglist);
260 /*TAG* tags = taglist_getTAGs(self);
261 TAG* tags2 = taglist_getTAGs(list);
263 tags3 = swf_Concatenate(tags,tags2);
264 PyObject* newtaglist = taglist_new(tags3);
266 Py_INCREF(newtaglist);*/
270 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
273 //----------------------------------------------------------------------------
274 static PyObject * taglist_item(PyObject * self, int index)
276 TagListObject*taglist = (TagListObject*)self;
278 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
279 tag = PyList_GetItem(taglist->taglist, index);
283 static PySequenceMethods taglist_as_sequence =
285 sq_length: taglist_length, // len(obj)
286 sq_concat: taglist_concat, // obj += [...], obj1+obj2
287 sq_repeat: 0, // x*n, intargfunc
288 sq_item: taglist_item, // obj[3]
289 sq_slice: 0, // x[i:j] intintargfunc
290 sq_ass_item: 0, // x[i] = y intobjargproc
291 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
292 sq_contains: taglist_contains, //???
294 PyTypeObject TagListClass =
296 PyObject_HEAD_INIT(NULL)
299 tp_basicsize: sizeof(TagListObject),
301 tp_dealloc: taglist_dealloc,
302 tp_print: 0, // print x
303 tp_getattr: taglist_getattr, // x.attr
304 tp_setattr: 0, // x.attr = v
305 tp_compare: 0, // x>y
306 tp_repr: 0, // `x`, print x
308 tp_as_sequence: &taglist_as_sequence,