6 #include "primitives.h"
12 //----------------------------------------------------------------------------
18 //----------------------------------------------------------------------------
19 PyObject * taglist_new()
21 TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
22 mylog("+%08x(%d) taglist_new", (int)taglist, taglist->ob_refcnt);
23 taglist->tagmap = tagmap_new();
24 taglist->taglist = PyList_New(0);
25 return (PyObject*)taglist;
27 //----------------------------------------------------------------------------
28 PyObject * taglist_new2(TAG*tag)
30 TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
31 mylog("+%08x(%d) taglist_new2 tag=%08x", (int)taglist, taglist->ob_refcnt, tag);
32 taglist->tagmap = tagmap_new();
36 while(t) {nr++;t=t->next;}
37 taglist->taglist = PyList_New(nr);
39 mylog("+%08x(%d) taglist_new2: %d items", (int)taglist, taglist->ob_refcnt, nr);
44 PyObject*newtag = tag_new2(tag, taglist->tagmap);
45 PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
46 if(swf_isDefiningTag(t)) {
47 tagmap_add(taglist->tagmap, newtag);
52 return (PyObject*)taglist;
54 //----------------------------------------------------------------------------
55 TAG* taglist_getTAGs(PyObject*self)
57 if(!PY_CHECK_TYPE(self,&TagListClass)) {
58 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
61 TagListObject*taglist = (TagListObject*)self;
63 /* TODO: the tags will be modified by this. We should set mutexes. */
65 int l = PyList_Size(taglist->taglist);
69 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
71 PyObject*item = PyList_GetItem(taglist->taglist, t);
72 tag = tag_getTAG(item, tag, taglist->tagmap);
78 //----------------------------------------------------------------------------
79 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
82 TagListObject*taglist = (TagListObject*)self;
83 if(!self || !PyArg_ParseTuple(args,""))
85 swf.firstTag = taglist->firstTag;
87 taglist->firstTag = swf.firstTag;
88 taglist->lastTag = 0; // FIXME
89 taglist->searchTag = 0;*/
92 //----------------------------------------------------------------------------
93 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
96 /* TagListObject*taglist = (TagListObject*)self;
97 if(!self || !PyArg_ParseTuple(args,""))
99 swf.firstTag = taglist->firstTag;
101 taglist->firstTag = swf.firstTag;
102 taglist->lastTag = 0; // FIXME
103 taglist->searchTag = 0;*/
106 //----------------------------------------------------------------------------
107 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
110 /* TagListObject*taglist = (TagListObject*)self;
111 if(!self || !PyArg_ParseTuple(args,""))
113 swf.firstTag = taglist->firstTag;
115 taglist->firstTag = swf.firstTag;
116 taglist->lastTag = 0; // FIXME
117 taglist->searchTag = 0;*/
120 //----------------------------------------------------------------------------
121 static void taglist_dealloc(PyObject* self)
123 TagListObject*taglist = (TagListObject*)self;
124 mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
125 Py_DECREF(taglist->taglist);
126 taglist->taglist = 0;
127 Py_DECREF(taglist->tagmap);
131 //----------------------------------------------------------------------------
132 static PyMethodDef taglist_functions[] =
133 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
134 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
135 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
136 {NULL, NULL, 0, NULL}
139 static PyObject* taglist_getattr(PyObject * self, char* a)
141 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
142 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
145 //----------------------------------------------------------------------------
146 static int taglist_length(PyObject * self)
148 TagListObject*tags = (TagListObject*)self;
149 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
150 return PyList_GET_SIZE(tags->taglist);
152 //----------------------------------------------------------------------------
153 static int taglist_contains(PyObject * self, PyObject * tag)
155 mylog(" %08x(%d) taglist_contains %08x", (int)self, self->ob_refcnt, tag);
156 TagListObject*taglist = (TagListObject*)self;
157 PyObject*list = taglist->taglist;
158 int l = PyList_Size(list);
161 PyObject*item = PyList_GetItem(list, t);
163 mylog(" %08x(%d) taglist_contains: yes", (int)self, self->ob_refcnt);
167 mylog(" %08x(%d) taglist_contains: no", (int)self, self->ob_refcnt);
170 //----------------------------------------------------------------------------
171 static PyObject * taglist_concat(PyObject * self, PyObject* list)
174 PY_ASSERT_TYPE(self, &TagListClass);
175 TagListObject*taglist = (TagListObject*)self;
176 mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
178 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
179 mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
180 list = tag_getDependencies(tag);
181 int l = PyList_Size(list);
183 mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
185 PyObject*item = PyList_GetItem(list, t);
186 PyObject*_self = taglist_concat(self, item);
190 if(!taglist_contains(self, tag)) {
191 mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
192 PyList_Append(taglist->taglist, tag);
194 mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
197 /* copy tag, so we don't have to do INCREF(tag) (and don't
198 get problems if the tag is appended to more than one
200 /* TODO: handle IDs */
202 TAG*t = tag_getTAG(tag);
204 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
206 nt = swf_InsertTag(0, t->id);
207 swf_SetBlock(nt,t->data,t->len);
208 PyObject*newtag = tag_new(taglist->swf, nt);
209 if(swf_isDefiningTag(t)) {
210 int id = swf_GetDefineID(t);
211 PyObject*id = PyLong_FromLong(id);
212 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
214 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
221 if (PyList_Check(list)) {
222 int l = PyList_Size(list);
224 mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
226 PyObject*item = PyList_GetItem(list, t);
227 if(!PY_CHECK_TYPE(item, &TagClass)) {
228 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
231 PyObject*_self = taglist_concat(self, item);
241 if (PY_CHECK_TYPE(list, &TagListClass)) {
242 mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
243 TagListObject*taglist2 = (TagListObject*)list;
244 return taglist_concat(self, taglist2->taglist);
246 /*TAG* tags = taglist_getTAGs(self);
247 TAG* tags2 = taglist_getTAGs(list);
249 tags3 = swf_Concatenate(tags,tags2);
250 PyObject* newtaglist = taglist_new(tags3);
252 Py_INCREF(newtaglist);*/
256 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
259 //----------------------------------------------------------------------------
260 static PyObject * taglist_item(PyObject * self, int index)
262 TagListObject*taglist = (TagListObject*)self;
264 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
265 tag = PyList_GetItem(taglist->taglist, index);
266 Py_INCREF(tag); //TODO-REF
269 static PySequenceMethods taglist_as_sequence =
271 sq_length: taglist_length, // len(obj)
272 sq_concat: taglist_concat, // obj += [...], obj1+obj2
273 sq_repeat: 0, // x*n, intargfunc
274 sq_item: taglist_item, // obj[3]
275 sq_slice: 0, // x[i:j] intintargfunc
276 sq_ass_item: 0, // x[i] = y intobjargproc
277 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
278 sq_contains: taglist_contains, //???
280 static PyTypeObject TagListClass =
282 PyObject_HEAD_INIT(NULL)
285 tp_basicsize: sizeof(TagListObject),
287 tp_dealloc: taglist_dealloc,
288 tp_print: 0, // print x
289 tp_getattr: taglist_getattr, // x.attr
290 tp_setattr: 0, // x.attr = v
291 tp_compare: 0, // x>y
292 tp_repr: 0, // `x`, print x
294 tp_as_sequence: &taglist_as_sequence,