6 #include "primitives.h"
11 //----------------------------------------------------------------------------
17 PyDictObject* char2id;
18 PyDictObject* id2char;
21 //----------------------------------------------------------------------------
22 PyObject * taglist_new()
24 TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
25 taglist->firstTag = 0;
26 taglist->searchTag = 0;
28 taglist->currentID = 1;
29 taglist->char2id = (PyDictObject*)PyDict_New();
30 taglist->id2char = (PyDictObject*)PyDict_New();
31 return (PyObject*)taglist;
33 PyObject * taglist_new2(TAG*tag)
35 TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
36 taglist->firstTag = tag;
37 taglist->searchTag = tag;
38 taglist->lastTag = tag;
39 while(taglist->lastTag->next)
40 taglist->lastTag = taglist->lastTag->next;
41 taglist->currentID = 1;
42 taglist->char2id = (PyDictObject*)PyDict_New();
43 taglist->id2char = (PyDictObject*)PyDict_New();
44 return (PyObject*)taglist;
46 TAG* taglist_getTAGs(PyObject*taglist)
49 return ((TagListObject*)taglist)->firstTag;
51 //----------------------------------------------------------------------------
52 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
55 TagListObject*taglist = (TagListObject*)self;
56 if(!self || !PyArg_ParseTuple(args,""))
58 swf.firstTag = taglist->firstTag;
60 taglist->firstTag = swf.firstTag;
61 taglist->lastTag = 0; // FIXME
62 taglist->searchTag = 0;
65 //----------------------------------------------------------------------------
66 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
69 TagListObject*taglist = (TagListObject*)self;
70 if(!self || !PyArg_ParseTuple(args,""))
72 swf.firstTag = taglist->firstTag;
74 taglist->firstTag = swf.firstTag;
75 taglist->lastTag = 0; // FIXME
76 taglist->searchTag = 0;
79 //----------------------------------------------------------------------------
80 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
83 TagListObject*taglist = (TagListObject*)self;
84 if(!self || !PyArg_ParseTuple(args,""))
86 swf.firstTag = taglist->firstTag;
88 taglist->firstTag = swf.firstTag;
89 taglist->lastTag = 0; // FIXME
90 taglist->searchTag = 0;
93 //----------------------------------------------------------------------------
94 static void taglist_dealloc(PyObject* self)
96 TagListObject*taglist = (TagListObject*)self;
98 mylog("taglist_dealloc %08x(%d)\n", (int)self, self->ob_refcnt);
99 swf.firstTag = taglist->firstTag;
101 taglist->firstTag = 0;
102 taglist->lastTag = 0;
103 taglist->searchTag = 0;
106 //----------------------------------------------------------------------------
107 static PyMethodDef taglist_functions[] =
108 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
109 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
110 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
111 {NULL, NULL, 0, NULL}
114 static PyObject* taglist_getattr(PyObject * self, char* a)
116 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
117 mylog("taglist_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
120 //----------------------------------------------------------------------------
121 static int taglist_length(PyObject * self)
123 TagListObject*tags = (TagListObject*)self;
126 mylog("taglist_length %08x(%d)", (int)self, self->ob_refcnt);
127 tag = tags->firstTag;
134 //----------------------------------------------------------------------------
135 static PyObject * taglist_concat(PyObject * self, PyObject* list)
138 TagListObject*taglist = (TagListObject*)self;
139 mylog("taglist_concat %08x(%d) %08x", (int)self, self->ob_refcnt, list);
143 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
144 /* copy tag, so we don't have to do INCREF(tag) (and don't
145 get problems if the tag is appended to more than one
147 /* TODO: handle IDs */
148 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
149 taglist->lastTag = swf_InsertTag(taglist->lastTag, tag_getTAG(tag)->id);
150 swf_SetBlock(taglist->lastTag, tag_getTAG(tag)->data, tag_getTAG(tag)->len);
151 if(!taglist->firstTag) {
152 taglist->firstTag = taglist->searchTag = taglist->lastTag;
154 if(swf_isDefiningTag(tag_getTAG(tag))) {
155 PyObject*id = PyLong_FromLong(taglist->currentID);
156 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
158 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
165 if (PyList_Check(list)) {
166 int l = PyList_Size(list);
168 mylog("taglist_concat: PythonList", (int)self, self->ob_refcnt);
170 PyObject*item = PyList_GetItem(list, t);
171 self = taglist_concat(self, item);
178 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
181 //----------------------------------------------------------------------------
182 static PyObject * taglist_item(PyObject * self, int index)
184 TagListObject*taglist = (TagListObject*)self;
188 mylog("taglist_item %08x(%d) [%d]", (int)self, self->ob_refcnt, index);
191 PyErr_SetString(PyExc_Exception, setError("Negative Indices not supported."));
195 tag = taglist->firstTag;
196 while(tag && i<index) {
200 if(!tag || i != index) {
202 PyErr_SetString(PyExc_Exception, setError("No Tag at position %d", index));
206 mylog("taglist_item %08x(%d)->IndexError (%d)", (int)self, self->ob_refcnt, index);
208 Py_INCREF(PyExc_IndexError);
209 PyErr_SetObject(PyExc_IndexError, Py_None);
213 tagobject = tag_new2(tag);
214 return (PyObject*)tagobject;
216 static PySequenceMethods taglist_as_sequence =
218 sq_length: taglist_length, // len(obj)
219 sq_concat: taglist_concat, // obj += [...], obj1+obj2
220 sq_repeat: 0, // x*n, intargfunc
221 sq_item: taglist_item, // obj[3]
222 sq_slice: 0, // x[i:j] intintargfunc
223 sq_ass_item: 0, // x[i] = y intobjargproc
224 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
225 sq_contains: 0, //???
227 static PyTypeObject TagListClass =
229 PyObject_HEAD_INIT(NULL)
232 tp_basicsize: sizeof(TagListObject),
234 tp_dealloc: taglist_dealloc,
235 tp_print: 0, // print x
236 tp_getattr: taglist_getattr, // x.attr
237 tp_setattr: 0, // x.attr = v
238 tp_compare: 0, // x>y
239 tp_repr: 0, // `x`, print x
241 tp_as_sequence: &taglist_as_sequence,