File: taglist.c

package info (click to toggle)
swftools 0.9.2%2Bgit20130725-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,680 kB
  • ctags: 17,348
  • sloc: ansic: 108,712; sh: 8,494; cpp: 8,040; yacc: 2,260; lisp: 904; makefile: 601; python: 300
file content (332 lines) | stat: -rw-r--r-- 10,652 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <Python.h>
#undef HAVE_STAT
#include "../rfxswf.h"
#include "../log.h"
#include "./pyutils.h"
#include "primitives.h"
#include "action.h"
#include "tag.h"
#include "tagmap.h"
#include "taglist.h"

//----------------------------------------------------------------------------
typedef struct {
    PyObject_HEAD
    PyObject* taglist;
} TagListObject;
//----------------------------------------------------------------------------
static void taglist_showcontents(PyObject* self)
{
    TagListObject*taglist = (TagListObject*)self;
    int t, l = PyList_Size(taglist->taglist);
    for(t=0;t<l;t++) {
	PyObject*item = PyList_GetItem(taglist->taglist, t);
	mylog(" %08x(%d) taglist_showcontents   item=%08x(%d)\n", (int)self, self->ob_refcnt, item, item->ob_refcnt);
    }
}
//----------------------------------------------------------------------------
PyObject * taglist_new()
{
    TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
    mylog("+%08x(%d) taglist_new", (int)taglist, taglist->ob_refcnt);
    taglist->taglist = PyList_New(0);
    return (PyObject*)taglist;
}
//----------------------------------------------------------------------------
PyObject * taglist_new2(TAG*tag)
{
    TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
    mylog("+%08x(%d) taglist_new2 tag=%08x", (int)taglist, taglist->ob_refcnt, tag);
    PyObject* tagmap = tagmap_new();

    int nr=0, len=0;
    TAG*t = tag;
    TAG*last = t;
    while(t) {len++;last=t;t=t->next;}

    if(last && last->id==ST_END) {
	swf_DeleteTag(0, last); last = 0;
	len--;
	if(len==0) tag = 0;
    }

    taglist->taglist = PyList_New(len);
    
    nr = 0;
    t = tag;
    while(t) {
	PyObject*newtag = tag_new2(t, tagmap);
	if(newtag==NULL) {
	    // pass through exception
	    Py_DECREF(tagmap);
	    return NULL;
	}
	PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
	if(swf_isDefiningTag(t)) {
	    int id = swf_GetDefineID(t);
	    tagmap_addMapping(tagmap, id, newtag);
	}
	nr++;
	t=t->next;
	Py_DECREF(newtag);
    }
    Py_DECREF(tagmap);
    return (PyObject*)taglist;
}
//----------------------------------------------------------------------------
TAG* taglist_getTAGs(PyObject*self)
{
    PyObject* tagmap = tagmap_new();
    TAG* tag = taglist_getTAGs2(self, tagmap, 1);
    Py_DECREF(tagmap);
    return tag;
}
//----------------------------------------------------------------------------
TAG* taglist_getTAGs2(PyObject*self, PyObject*tagmap, int addDependencies)
{
    if(!PY_CHECK_TYPE(self,&TagListClass)) {
	PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
	return 0;
    }
    TagListObject*taglist = (TagListObject*)self;

    /* TODO: the tags will be modified by this. We should set mutexes. */
    
    int l = PyList_Size(taglist->taglist);
    int t;
    TAG* tag = 0;
    TAG* firstTag = 0;
    mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
    for(t=0;t<l;t++) {
	PyObject*item = PyList_GetItem(taglist->taglist, t);
	if(addDependencies) {
	    PyObject* deps = tag_getDependencies(item);
	    int l = PyList_Size(deps);
	    int t;
	    for(t=0;t<l;t++) {
		PyObject*item = PyList_GetItem(deps, t);
		if(tagmap_obj2id(tagmap, item)<0) {
		    /*PyObject*_self = taglist_concat(self, item);
		    Py_DECREF(self);
		    self = _self;*/
		    tag = tag_getTAG(item, tag, tagmap);
		    if(!tag) { return 0; }
		    if(!firstTag)
			firstTag = tag;
		}
	    }
	}

	tag = tag_getTAG(item, tag, tagmap);
	if(!tag) { /* pass through errors */ return 0; }

	if(!firstTag)
	    firstTag = tag;
    }
    return firstTag;
}
//----------------------------------------------------------------------------
static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
{
/*    SWF swf;
    TagListObject*taglist = (TagListObject*)self;
    if(!self || !PyArg_ParseTuple(args,"")) 
	return NULL;
    swf.firstTag = taglist->firstTag;
    swf_FoldAll(&swf);
    taglist->firstTag = swf.firstTag;
    taglist->lastTag = 0; // FIXME
    taglist->searchTag = 0;*/
    return PY_NONE;
}
//----------------------------------------------------------------------------
static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
{
    SWF swf;
/*    TagListObject*taglist = (TagListObject*)self;
    if(!self || !PyArg_ParseTuple(args,"")) 
	return NULL;
    swf.firstTag = taglist->firstTag;
    swf_UnFoldAll(&swf);
    taglist->firstTag = swf.firstTag;
    taglist->lastTag = 0; // FIXME
    taglist->searchTag = 0;*/
    return PY_NONE;
}
//----------------------------------------------------------------------------
static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
{
    SWF swf;
/*    TagListObject*taglist = (TagListObject*)self;
    if(!self || !PyArg_ParseTuple(args,"")) 
	return NULL;
    swf.firstTag = taglist->firstTag;
    swf_UnFoldAll(&swf);
    taglist->firstTag = swf.firstTag;
    taglist->lastTag = 0; // FIXME
    taglist->searchTag = 0;*/
    return PY_NONE;
}
//----------------------------------------------------------------------------
static PyMethodDef taglist_functions[] =
{{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
 {NULL, NULL, 0, NULL}
};

static PyObject* taglist_getattr(PyObject * self, char* a)
{
    PyObject* ret = Py_FindMethod(taglist_functions, self, a);
    mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
    return ret;
}
//----------------------------------------------------------------------------
static int taglist_length(PyObject * self)
{
    TagListObject*tags = (TagListObject*)self;
    mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
    return PyList_GET_SIZE(tags->taglist);
}
//----------------------------------------------------------------------------
static int taglist_contains(PyObject * self, PyObject * tag)
{
    /* TODO: optimize! */
    TagListObject*taglist = (TagListObject*)self;
    PyObject*list = taglist->taglist;
    int l = PyList_Size(list);
    int t;
    for(t=0;t<l;t++) {
	PyObject*item = PyList_GetItem(list, t);
	if(item == tag) {
	    return 1;
	}
    }
    return 0;
}
//----------------------------------------------------------------------------
static PyObject * taglist_concat(PyObject * self, PyObject* list)
{
    PyObject*tag = 0;
    PY_ASSERT_TYPE(self, &TagListClass);
    TagListObject*taglist = (TagListObject*)self;
    mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);

    if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
	if(!taglist_contains(self, tag)) {
	    mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
	    PyList_Append(taglist->taglist, tag);
	} else {
	    mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
	}
	Py_INCREF(self);
	return self;
	/* copy tag, so we don't have to do INCREF(tag) (and don't
	   get problems if the tag is appended to more than one
	   taglist) */
	/* TODO: handle IDs */
	/*	
	TAG*t = tag_getTAG(tag);
	TAG*nt = 0;
	mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
	// copy tag
	nt = swf_InsertTag(0, t->id);
	swf_SetBlock(nt,t->data,t->len);
	PyObject*newtag = tag_new(taglist->swf, nt);
	if(swf_isDefiningTag(t)) {
	    int id = swf_GetDefineID(t);
	    PyObject*id = PyLong_FromLong(id);
	    PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
	    Py_INCREF(id);
	    PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
	    Py_INCREF(id);
	}
	Py_INCREF(self);
	return self;*/
    }
    PyErr_Clear();
    if (PyList_Check(list)) {
	int l = PyList_Size(list);
	int t;
	for(t=0;t<l;t++) {
	    PyObject*item = PyList_GetItem(list, t);
	    if(!PY_CHECK_TYPE(item, &TagClass)) {
		PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
		return 0;
	    }
	    PyObject*_self = taglist_concat(self, item);
	    Py_DECREF(self);
	    self = _self;
	    if(!self)
		return 0;
	}
	Py_INCREF(self);
	return self;
    }
    PyErr_Clear();
    if (PY_CHECK_TYPE(list, &TagListClass)) {
	TagListObject*taglist2 = (TagListObject*)list;
	return taglist_concat(self, taglist2->taglist);

	/*TAG* tags = taglist_getTAGs(self);
	TAG* tags2 = taglist_getTAGs(list);
	TAG* tags3;
	tags3 = swf_Concatenate(tags,tags2);
	PyObject* newtaglist = taglist_new(tags3);
	swf_FreeTags(tags3);
	Py_INCREF(newtaglist);*/
    }
    PyErr_Clear();

    PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
    return 0;
}
//----------------------------------------------------------------------------
static PyObject * taglist_item(PyObject * self, int index)
{
    TagListObject*taglist = (TagListObject*)self;
    PyObject*tag;
    tag = PyList_GetItem(taglist->taglist, index);
    if(!tag)
	return 0;
    mylog(" %08x(%d) taglist_item(%d): %08x", (int)self, self->ob_refcnt, index, tag);
    Py_INCREF(tag);
    return tag;
}
//----------------------------------------------------------------------------
static void taglist_dealloc(PyObject* self)
{
    TagListObject*taglist = (TagListObject*)self;
    mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt);
    Py_DECREF(taglist->taglist);
    taglist->taglist = 0;
    PyObject_Del(self);
}
//----------------------------------------------------------------------------
static PySequenceMethods taglist_as_sequence =
{
    sq_length: taglist_length, // len(obj)
    sq_concat: taglist_concat, // obj += [...], obj1+obj2
    sq_repeat: 0,            // x*n, intargfunc
    sq_item: taglist_item,  // obj[3]
    sq_slice: 0,             // x[i:j] intintargfunc
    sq_ass_item: 0,          // x[i] = y intobjargproc
    sq_ass_slice: 0,         // x[i:j] = v intintobjargproc
    sq_contains: taglist_contains,   //???
};
PyTypeObject TagListClass = 
{
    PyObject_HEAD_INIT(NULL)
    0,
    tp_name: "TagList",
    tp_basicsize: sizeof(TagListObject),
    tp_itemsize: 0,
    tp_dealloc: taglist_dealloc,
    tp_print: 0,                 // print x
    tp_getattr: taglist_getattr, // x.attr
    tp_setattr: 0,               // x.attr = v
    tp_compare: 0,               // x>y
    tp_repr: 0,                  // `x`, print x
    tp_as_number: 0,
    tp_as_sequence: &taglist_as_sequence,
};