78147339c40a59d330462c760de8c9511e89aaae
[swftools.git] / lib / python / taglist.c
1 #include <Python.h>
2 #undef HAVE_STAT
3 #include "../rfxswf.h"
4 #include "../log.h"
5 #include "./pyutils.h"
6 #include "primitives.h"
7 #include "action.h"
8 #include "tag.h"
9 #include "taglist.h"
10
11 //----------------------------------------------------------------------------
12 typedef struct {
13     PyObject_HEAD
14     TAG*firstTag;
15     TAG*searchTag;
16     TAG*lastTag;
17     PyDictObject* char2id;
18     PyDictObject* id2char;
19     U16 currentID;
20 } TagListObject;
21 //----------------------------------------------------------------------------
22 PyObject * taglist_new()
23 {
24     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
25     taglist->firstTag = 0;
26     taglist->searchTag = 0;
27     taglist->lastTag = 0;
28     taglist->currentID = 1;
29     taglist->char2id = (PyDictObject*)PyDict_New();
30     taglist->id2char = (PyDictObject*)PyDict_New();
31     return (PyObject*)taglist;
32 }
33 PyObject * taglist_new2(TAG*tag)
34 {
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;
45 }
46 TAG* taglist_getTAGs(PyObject*taglist)
47 {
48     // TODO: checking!
49     return ((TagListObject*)taglist)->firstTag;
50 }
51 //----------------------------------------------------------------------------
52 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
53 {
54     SWF swf;
55     TagListObject*taglist = (TagListObject*)self;
56     if(!self || !PyArg_ParseTuple(args,"")) 
57         return NULL;
58     swf.firstTag = taglist->firstTag;
59     swf_FoldAll(&swf);
60     taglist->firstTag = swf.firstTag;
61     taglist->lastTag = 0; // FIXME
62     taglist->searchTag = 0;
63     return PY_NONE;
64 }
65 //----------------------------------------------------------------------------
66 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
67 {
68     SWF swf;
69     TagListObject*taglist = (TagListObject*)self;
70     if(!self || !PyArg_ParseTuple(args,"")) 
71         return NULL;
72     swf.firstTag = taglist->firstTag;
73     swf_UnFoldAll(&swf);
74     taglist->firstTag = swf.firstTag;
75     taglist->lastTag = 0; // FIXME
76     taglist->searchTag = 0;
77     return PY_NONE;
78 }
79 //----------------------------------------------------------------------------
80 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
81 {
82     SWF swf;
83     TagListObject*taglist = (TagListObject*)self;
84     if(!self || !PyArg_ParseTuple(args,"")) 
85         return NULL;
86     swf.firstTag = taglist->firstTag;
87     swf_UnFoldAll(&swf);
88     taglist->firstTag = swf.firstTag;
89     taglist->lastTag = 0; // FIXME
90     taglist->searchTag = 0;
91     return PY_NONE;
92 }
93 //----------------------------------------------------------------------------
94 static void taglist_dealloc(PyObject* self)
95 {
96     TagListObject*taglist = (TagListObject*)self;
97     SWF swf;
98     mylog("taglist_dealloc %08x(%d)\n", (int)self, self->ob_refcnt);
99     swf.firstTag = taglist->firstTag;
100     swf_FreeTags(&swf);
101     taglist->firstTag = 0;
102     taglist->lastTag = 0;
103     taglist->searchTag = 0;
104     PyObject_Del(self);
105 }
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}
112 };
113
114 static PyObject* taglist_getattr(PyObject * self, char* a)
115 {
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);
118     return ret;
119 }
120 //----------------------------------------------------------------------------
121 static int taglist_length(PyObject * self)
122 {
123     TagListObject*tags = (TagListObject*)self;
124     TAG*tag;
125     int l = 0;
126     mylog("taglist_length %08x(%d)", (int)self, self->ob_refcnt);
127     tag = tags->firstTag;
128     while(tag) {
129         l++;
130         tag = tag->next;
131     }
132     return l;
133 }
134 //----------------------------------------------------------------------------
135 static PyObject * taglist_concat(PyObject * self, PyObject* list)
136 {
137     PyObject*tag = 0;
138     TagListObject*taglist = (TagListObject*)self;
139     mylog("taglist_concat %08x(%d) %08x", (int)self, self->ob_refcnt, list);
140
141     /* TODO: rewrite */
142         
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
146            taglist) */
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;
153         }
154         if(swf_isDefiningTag(tag_getTAG(tag))) {
155             PyObject*id = PyLong_FromLong(taglist->currentID);
156             PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
157             Py_INCREF(id);
158             PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
159             Py_INCREF(id);
160         }
161         Py_INCREF(self);
162         return self;
163     }
164     PyErr_Clear();
165     if (PyList_Check(list)) {
166         int l = PyList_Size(list);
167         int t;
168         mylog("taglist_concat: PythonList", (int)self, self->ob_refcnt);
169         for(t=0;t<l;t++) {
170             PyObject*item = PyList_GetItem(list, t);
171             self = taglist_concat(self, item);
172             if(!self)
173                 return 0;
174         }
175         Py_INCREF(self);
176         return self;
177     }
178     PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
179     return 0;
180 }
181 //----------------------------------------------------------------------------
182 static PyObject * taglist_item(PyObject * self, int index)
183 {
184     TagListObject*taglist = (TagListObject*)self;
185     TAG*tag;
186     PyObject*tagobject;
187     int i = 0;
188     mylog("taglist_item %08x(%d) [%d]", (int)self, self->ob_refcnt, index);
189
190     if(index<0) {
191         PyErr_SetString(PyExc_Exception, setError("Negative Indices not supported."));
192         return NULL;
193     }
194
195     tag = taglist->firstTag;
196     while(tag && i<index) {
197         tag = tag->next;
198         i++;
199     }
200     if(!tag || i != index) {
201         if(index> i+10) {
202             PyErr_SetString(PyExc_Exception, setError("No Tag at position %d", index));
203             return NULL;
204         }
205
206         mylog("taglist_item %08x(%d)->IndexError (%d)", (int)self, self->ob_refcnt, index);
207
208         Py_INCREF(PyExc_IndexError);
209         PyErr_SetObject(PyExc_IndexError, Py_None);
210         return NULL;
211     }
212
213     tagobject = tag_new2(tag);
214     return (PyObject*)tagobject;
215 }
216 static PySequenceMethods taglist_as_sequence =
217 {
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,          //???
226 };
227 static PyTypeObject TagListClass = 
228 {
229     PyObject_HEAD_INIT(NULL)
230     0,
231     tp_name: "TagList",
232     tp_basicsize: sizeof(TagListObject),
233     tp_itemsize: 0,
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
240     tp_as_number: 0,
241     tp_as_sequence: &taglist_as_sequence,
242 };