more horizontal refactoring
[swftools.git] / lib / python / taglist.c
index 9caf3b2..0a30c08 100644 (file)
 typedef struct {
     PyObject_HEAD
     PyObject* taglist;
-    PyObject* tagmap;
 } 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->tagmap = tagmap_new();
     taglist->taglist = PyList_New(0);
     return (PyObject*)taglist;
 }
@@ -29,31 +37,53 @@ PyObject * taglist_new2(TAG*tag)
 {
     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
     mylog("+%08x(%d) taglist_new2 tag=%08x", (int)taglist, taglist->ob_refcnt, tag);
-    taglist->tagmap = tagmap_new();
+    PyObject* tagmap = tagmap_new();
 
-    int nr=0;
+    int nr=0, len=0;
     TAG*t = tag;
-    while(t) {nr++;t=t->next;}
-    taglist->taglist = PyList_New(nr);
-    
-    mylog("+%08x(%d) taglist_new2: %d items", (int)taglist, taglist->ob_refcnt, nr);
+    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(tag, taglist->tagmap);
+       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)) {
-           tagmap_add(taglist->tagmap, newtag);
+           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;
@@ -69,7 +99,27 @@ TAG* taglist_getTAGs(PyObject*self)
     mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
     for(t=0;t<l;t++) {
        PyObject*item = PyList_GetItem(taglist->taglist, t);
-       tag = tag_getTAG(item, tag, taglist->tagmap);
+       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;
     }
@@ -118,17 +168,6 @@ static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
     return PY_NONE;
 }
 //----------------------------------------------------------------------------
-static void taglist_dealloc(PyObject* self)
-{
-    TagListObject*taglist = (TagListObject*)self;
-    mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
-    Py_DECREF(taglist->taglist);
-    taglist->taglist = 0;
-    Py_DECREF(taglist->tagmap);
-    taglist->tagmap= 0;
-    PyObject_Del(self);
-}
-//----------------------------------------------------------------------------
 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"},
@@ -152,7 +191,7 @@ static int taglist_length(PyObject * self)
 //----------------------------------------------------------------------------
 static int taglist_contains(PyObject * self, PyObject * tag)
 {
-    mylog(" %08x(%d) taglist_contains %08x", (int)self, self->ob_refcnt, tag);
+    /* TODO: optimize! */
     TagListObject*taglist = (TagListObject*)self;
     PyObject*list = taglist->taglist;
     int l = PyList_Size(list);
@@ -160,11 +199,9 @@ static int taglist_contains(PyObject * self, PyObject * tag)
     for(t=0;t<l;t++) {
        PyObject*item = PyList_GetItem(list, t);
        if(item == tag) {
-           mylog(" %08x(%d) taglist_contains: yes", (int)self, self->ob_refcnt);
            return 1;
        }
     }
-    mylog(" %08x(%d) taglist_contains: no", (int)self, self->ob_refcnt);
     return 0;
 }
 //----------------------------------------------------------------------------
@@ -173,25 +210,15 @@ 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", (int)self, self->ob_refcnt, list);
+    mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);
 
     if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
-       mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
-       list = tag_getDependencies(tag);
-       int l = PyList_Size(list);
-       int t;
-       mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
-       for(t=0;t<l;t++) {
-           PyObject*item = PyList_GetItem(list, t);
-           PyObject*_self = taglist_concat(self, item);
-           Py_DECREF(self);
-           self = _self;
-       }
        if(!taglist_contains(self, tag)) {
-           mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, 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);
        }
-       mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
        Py_INCREF(self);
        return self;
        /* copy tag, so we don't have to do INCREF(tag) (and don't
@@ -221,7 +248,6 @@ static PyObject * taglist_concat(PyObject * self, PyObject* list)
     if (PyList_Check(list)) {
        int l = PyList_Size(list);
        int t;
-       mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
        for(t=0;t<l;t++) {
            PyObject*item = PyList_GetItem(list, t);
            if(!PY_CHECK_TYPE(item, &TagClass)) {
@@ -239,7 +265,6 @@ static PyObject * taglist_concat(PyObject * self, PyObject* list)
     }
     PyErr_Clear();
     if (PY_CHECK_TYPE(list, &TagListClass)) {
-       mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
        TagListObject*taglist2 = (TagListObject*)list;
        return taglist_concat(self, taglist2->taglist);
 
@@ -261,11 +286,23 @@ static PyObject * taglist_item(PyObject * self, int index)
 {
     TagListObject*taglist = (TagListObject*)self;
     PyObject*tag;
-    mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
     tag = PyList_GetItem(taglist->taglist, index);
-    Py_INCREF(tag); //TODO-REF
+    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)
@@ -277,7 +314,7 @@ static PySequenceMethods taglist_as_sequence =
     sq_ass_slice: 0,         // x[i:j] = v intintobjargproc
     sq_contains: taglist_contains,   //???
 };
-static PyTypeObject TagListClass = 
+PyTypeObject TagListClass = 
 {
     PyObject_HEAD_INIT(NULL)
     0,