From 726d172d02228a2e9b084efba8d3173994343c2b Mon Sep 17 00:00:00 2001 From: kramm Date: Sat, 27 Mar 2004 19:50:03 +0000 Subject: [PATCH] fixed logging and tag id handling --- lib/python/Makefile | 11 ++- lib/python/SWF.c | 51 ++++++----- lib/python/primitives.c | 30 +++++-- lib/python/pyutils.c | 5 ++ lib/python/pyutils.h | 5 ++ lib/python/tag.c | 112 +++++++++++++++++++---- lib/python/tag.h | 3 +- lib/python/taglist.c | 229 +++++++++++++++++++++++++++++------------------ lib/python/tagmap.c | 144 +++++++++++++++++++++++++++++ lib/python/tagmap.h | 38 ++++++++ 10 files changed, 483 insertions(+), 145 deletions(-) create mode 100644 lib/python/tagmap.c create mode 100644 lib/python/tagmap.h diff --git a/lib/python/Makefile b/lib/python/Makefile index 847132a..0ab8c4b 100644 --- a/lib/python/Makefile +++ b/lib/python/Makefile @@ -4,9 +4,11 @@ LIB=-L/usr/lib/python2.2/config/ -lpython2.2 primitives.o: primitives.c primitives.h gcc -g -O2 -Wall -Wno-unused -fPIC -I/usr/include/python2.2 -c primitives.c -o primitives.o -taglist.o: taglist.c taglist.h +tagmap.o: tagmap.c tagmap.h + gcc -g -O2 -Wall -Wno-unused -fPIC -I/usr/include/python2.2 -c tagmap.c -o tagmap.o +taglist.o: taglist.c taglist.h tagmap.h gcc -g -O2 -Wall -Wno-unused -fPIC -I/usr/include/python2.2 -c taglist.c -o taglist.o -tag.o: tag.c tag.h +tag.o: tag.c tag.h tagmap.h gcc -g -O2 -Wall -Wno-unused -fPIC -I/usr/include/python2.2 -c tag.c -o tag.o action.o: action.c action.h gcc -g -O2 -Wall -Wno-unused -fPIC -I/usr/include/python2.2 -c action.c -o action.o @@ -16,8 +18,8 @@ pyutils.o: pyutils.c pyutils.h SWF.o: SWF.c pyutils.h primitives.h gcc -g -O2 -Wall -Wno-unused -fPIC -I/usr/include/python2.2 -c SWF.c -o SWF.o -SWF.so: SWF.o taglist.o tag.o action.o primitives.o pyutils.o ../librfxswf.a - gcc -shared SWF.o taglist.o tag.o action.o primitives.o pyutils.o -o SWF.so $(LIB) ../librfxswf.a -ljpeg -lz -lt1 -lfreetype +SWF.so: SWF.o taglist.o tag.o tagmap.o action.o primitives.o pyutils.o ../librfxswf.a + gcc -shared SWF.o tagmap.o taglist.o tag.o action.o primitives.o pyutils.o -o SWF.so $(LIB) ../librfxswf.a -ljpeg -lz -lt1 -lfreetype cp SWF.so test mypython: mypython.c @@ -26,6 +28,7 @@ mypython: mypython.c test: SWF.so cd test;./test_create.py #python fonts.py + swfdump -p test/test.swf clean: rm *.o *.so mypython diff --git a/lib/python/SWF.c b/lib/python/SWF.c index 8700e81..e011841 100644 --- a/lib/python/SWF.c +++ b/lib/python/SWF.c @@ -63,6 +63,9 @@ static PyObject* f_create(PyObject* self, PyObject* args, PyObject* kwargs) PyObject * obbox = 0; SRECT bbox = {0,0,0,0}; char* filename = 0; + + swf = PyObject_New(SWFObject, &SWFClass); + mylog("+%08x(%d) create\n", (int)swf, swf->ob_refcnt); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|idOs", kwlist, &version, &framerate, @@ -72,7 +75,6 @@ static PyObject* f_create(PyObject* self, PyObject* args, PyObject* kwargs) if (!PyArg_Parse(obbox, "(iiii)", &bbox.xmin, &bbox.ymin, &bbox.xmax, &bbox.ymax)) return NULL; - swf = PyObject_New(SWFObject, &SWFClass); memset(&swf->swf, 0, sizeof(SWF)); if(filename) swf->filename = strdup(filename); @@ -87,7 +89,7 @@ static PyObject* f_create(PyObject* self, PyObject* args, PyObject* kwargs) if(swf->swf.fileVersion>=6) swf->swf.compressed = 1; - mylog("create %08x -> %08x\n", (int)self, (int)swf); + mylog(" %08x(%d) create: done\n", (int)swf, swf->ob_refcnt); return (PyObject*)swf; } //---------------------------------------------------------------------------- @@ -123,7 +125,7 @@ static PyObject* f_load(PyObject* self, PyObject* args) swf->taglist = taglist_new2(swf->swf.firstTag); - mylog("load %08x -> %08x\n", (int)self, (int)swf); + mylog("+%08x(%d) load\n", (int)self, self->ob_refcnt); return (PyObject*)swf; } //---------------------------------------------------------------------------- @@ -213,11 +215,11 @@ static PyMethodDef swf_functions[] = //---------------------------------------------------------------------------- static void swf_dealloc(PyObject* self) { + mylog("-%08x(%d) swf_dealloc\n", (int)self, self->ob_refcnt); SWFObject*swfo; SWF*swf; swfo = (SWFObject*)self; swf = &swfo->swf; - mylog("swf_dealloc %08x(%d)\n", (int)self, self->ob_refcnt); if(swfo->filename) { free(swfo->filename); swfo->filename = 0; @@ -229,10 +231,10 @@ static void swf_dealloc(PyObject* self) //---------------------------------------------------------------------------- static int swf_print(PyObject * self, FILE *fi, int flags) //flags&Py_PRINT_RAW { + mylog(" %08x(%d) print \n", (int)self, self->ob_refcnt); SWFObject*swf = (SWFObject*)self; swf_DumpHeader(fi, &swf->swf); //void swf_DumpSWF(FILE * f,SWF*swf); - mylog("print %08x(%d)\n", (int)self, self->ob_refcnt); return 0; } //---------------------------------------------------------------------------- @@ -243,15 +245,15 @@ static PyObject* swf_getattr(PyObject * self, char* a) if(!strcmp(a, "fps")) { double fps = swf->swf.frameRate/256.0; - mylog("swf_getattr %08x(%d) %s = %f\n", (int)self, self->ob_refcnt, a, fps); + mylog(" %08x(%d) swf_getattr %s = %f\n", (int)self, self->ob_refcnt, a, fps); return Py_BuildValue("d", fps); } else if(!strcmp(a, "version")) { int version = swf->swf.fileVersion;; - mylog("swf_getattr %08x(%d) %s = %d\n", (int)self, self->ob_refcnt, a, version); + mylog(" %08x(%d) swf_getattr %s = %d\n", (int)self, self->ob_refcnt, a, version); return Py_BuildValue("i", version); } else if(!strcmp(a, "name")) { char*filename = swf->filename; - mylog("swf_getattr %08x(%d) %s = %s\n", (int)self, self->ob_refcnt, a, filename); + mylog(" %08x(%d) swf_getattr %s = %s\n", (int)self, self->ob_refcnt, a, filename); return Py_BuildValue("s", filename); } else if(!strcmp(a, "bbox")) { int xmin,ymin,xmax,ymax; @@ -259,17 +261,17 @@ static PyObject* swf_getattr(PyObject * self, char* a) ymin = swf->swf.movieSize.ymin; xmax = swf->swf.movieSize.xmax; ymax = swf->swf.movieSize.ymax; - mylog("swf_getattr %08x(%d) %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, xmin,ymin,xmax,ymax); + mylog(" %08x(%d) swf_getattr %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, xmin,ymin,xmax,ymax); return Py_BuildValue("(iiii)", xmin, ymin, xmax, ymax); } else if(!strcmp(a, "tags")) { PyObject*ret = (PyObject*)(swf->taglist); Py_INCREF(ret); - mylog("swf_getattr %08x(%d) %s = %08x(%d)\n", (int)self, self->ob_refcnt, a, ret, ret->ob_refcnt); + mylog(" %08x(%d) swf_getattr %s = %08x(%d)\n", (int)self, self->ob_refcnt, a, ret, ret->ob_refcnt); return ret; } ret = Py_FindMethod(swf_functions, self, a); - mylog("swf_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret); + mylog(" %08x(%d) swf_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret); return ret; } //---------------------------------------------------------------------------- @@ -281,14 +283,14 @@ static int swf_setattr(PyObject * self, char* a, PyObject * o) if (!PyArg_Parse(o, "d", &fps)) goto err; swf->swf.frameRate = (int)(fps*0x100); - mylog("swf_setattr %08x(%d) %s = %f\n", (int)self, self->ob_refcnt, a, fps); + mylog(" %08x(%d) swf_setattr %s = %f\n", (int)self, self->ob_refcnt, a, fps); return 0; } else if(!strcmp(a, "version")) { int version; if (!PyArg_Parse(o, "i", &version)) goto err; swf->swf.fileVersion = version; - mylog("swf_setattr %08x(%d) %s = %d\n", (int)self, self->ob_refcnt, a, version); + mylog(" %08x(%d) swf_setattr %s = %d\n", (int)self, self->ob_refcnt, a, version); return 0; } else if(!strcmp(a, "name")) { char*filename; @@ -298,7 +300,7 @@ static int swf_setattr(PyObject * self, char* a, PyObject * o) free(swf->filename);swf->filename=0; } swf->filename = strdup(filename); - mylog("swf_setattr %08x(%d) %s = %s\n", (int)self, self->ob_refcnt, a, filename); + mylog(" %08x(%d) swf_setattr %s = %s\n", (int)self, self->ob_refcnt, a, filename); return 0; } else if(!strcmp(a, "bbox")) { int xmin=0,ymin=0,xmax=0,ymax=0; @@ -309,7 +311,7 @@ static int swf_setattr(PyObject * self, char* a, PyObject * o) swf->swf.movieSize.ymin = ymin; swf->swf.movieSize.xmax = xmax; swf->swf.movieSize.ymax = ymax; - mylog("swf_setattr %08x(%d) %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, xmin,ymin,xmax,ymax); + mylog(" %08x(%d) swf_setattr %s = (%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, a, xmin,ymin,xmax,ymax); return 0; } else if(!strcmp(a, "tags")) { PyObject* taglist; @@ -320,11 +322,11 @@ static int swf_setattr(PyObject * self, char* a, PyObject * o) Py_DECREF(swf->taglist); swf->taglist = taglist; Py_INCREF(swf->taglist); - mylog("swf_setattr %08x(%d) %s = %08x\n", (int)self, self->ob_refcnt, a, swf->taglist); + mylog(" %08x(%d) swf_setattr %s = %08x\n", (int)self, self->ob_refcnt, a, swf->taglist); return 0; } err: - mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, a, o); + mylog(" %08x(%d) swf_setattr %s = ? (%08x)\n", (int)self, self->ob_refcnt, a, o); return 1; } @@ -364,29 +366,32 @@ PyMethodDef* swf_getMethods() #include "tag.h" #include "taglist.h" -static PyObject* module_verbose(PyObject* self, PyObject* args) +static PyObject* module_verbose(PyObject* self, PyObject* args, PyObject* kwargs) { - if (!PyArg_ParseTuple(args,"i", &verbose)) + int _verbose = 0; + static char *kwlist[] = {"verbosity", NULL}; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &verbose)) return NULL; + setVerbosity(_verbose); + return Py_BuildValue("s", 0); } static PyMethodDef LoggingMethods[] = { /* Module functions */ - {"verbose", module_verbose, METH_VARARGS, "Set the module verbosity"}, + {"verbose", (PyCFunction)module_verbose, METH_KEYWORDS, "Set the module verbosity"}, {0,0,0,0} }; void initSWF(void) { + PyObject*module; PyMethodDef* primitive_methods = primitive_getMethods(); PyMethodDef* tag_methods = tag_getMethods(); PyMethodDef* action_methods = action_getMethods(); PyMethodDef* swf_methods = swf_getMethods(); - initLog("test.log",8,0,0,0,0); - PyMethodDef* all_methods = 0; all_methods = addMethods(all_methods, primitive_methods); all_methods = addMethods(all_methods, tag_methods); @@ -395,5 +400,5 @@ void initSWF(void) all_methods = addMethods(all_methods, LoggingMethods); - (void)Py_InitModule("SWF", all_methods); + module = Py_InitModule("SWF", all_methods); } diff --git a/lib/python/primitives.c b/lib/python/primitives.c index aba757e..b1cb0c6 100644 --- a/lib/python/primitives.c +++ b/lib/python/primitives.c @@ -189,27 +189,39 @@ typedef struct { MATRIX matrix; } MatrixObject; -PyObject* f_Matrix(PyObject* self, PyObject* args, PyObject* kwargs) +PyObject* f_Matrix(PyObject* _self, PyObject* args, PyObject* kwargs) { - return NULL; + PyObject*self = (PyObject*)PyObject_New(MatrixObject, &MatrixClass); + MatrixObject*matrix = (MatrixObject*)self; + mylog("+%08x(%d) f_Matrix", self, self->ob_refcnt); + static char *kwlist[] = {"x", "y", "scale", "rotate", NULL}; + float x=0,y=0,scale=1.0,rotate=0; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ffff", kwlist, &x,&y,&scale,&rotate)) + return NULL; + mylog(" %08x(%d) f_Matrix: x=%f y=%f scale=%f rotate=%f", self, self->ob_refcnt, x,y,scale,rotate); + swf_GetMatrix(0, &matrix->matrix); + matrix->matrix.tx = (int)(x*20); + matrix->matrix.ty = (int)(y*20); + matrix->matrix.sx = (int)(scale*65536); + matrix->matrix.sy = (int)(scale*65536); + /* TODO: rotate */ + return self; } static PyObject* matrix_getattr(PyObject * self, char* a) { + PY_ASSERT_TYPE(self,&MatrixClass); return NULL; } static int matrix_setattr(PyObject * self, char* a, PyObject* o) { + PY_ASSERT_TYPE(self,&MatrixClass); return 0; } MATRIX matrix_getMatrix(PyObject*self) { - MatrixObject*matrix= 0; - if (!PyArg_Parse(self, "O!", &MatrixClass, &matrix)) { - MATRIX dummy; - memset(&dummy, 0, sizeof(dummy)); - mylog("Error: wrong type for function color_getRGBA"); - return dummy; - } + mylog(" %08x(%d) matrix_getMatrix", self, self->ob_refcnt); + PY_ASSERT_TYPE(self,&MatrixClass); + MatrixObject*matrix = (MatrixObject*)self; return matrix->matrix; } PyTypeObject MatrixClass = diff --git a/lib/python/pyutils.c b/lib/python/pyutils.c index 389193e..453b5c4 100644 --- a/lib/python/pyutils.c +++ b/lib/python/pyutils.c @@ -75,3 +75,8 @@ PyMethodDef* addMethods(PyMethodDef*obj1, PyMethodDef*obj2) //free(obj1)? return result; } +void setVerbosity(int _verbose) +{ + verbose = _verbose; + mylog("setting verbosity to %d", verbose); +} diff --git a/lib/python/pyutils.h b/lib/python/pyutils.h index 6f56a13..571c0b9 100644 --- a/lib/python/pyutils.h +++ b/lib/python/pyutils.h @@ -6,9 +6,14 @@ extern int verbose; +void setVerbosity(int verbose); + char* setError(char*format, ...); void mylog(char*format, ...); PyObject* FindMethodMore(PyObject*ret, PyMethodDef f[], PyObject*self, char* a); void dummy_dealloc(PyObject* self); PyMethodDef* addMethods(PyMethodDef*obj1, PyMethodDef*obj2); + +#define PY_CHECK_TYPE(o,c) ((o)->ob_type == (c)) +#define PY_ASSERT_TYPE(o,c) {if((o)->ob_type != (c)) {printf("Invalid type: %08x(%d)", (int)(o), (o)->ob_refcnt);exit(1);}} #endif diff --git a/lib/python/tag.c b/lib/python/tag.c index ed08931..a0139f6 100644 --- a/lib/python/tag.c +++ b/lib/python/tag.c @@ -6,10 +6,12 @@ #include "primitives.h" #include "action.h" #include "tag.h" +#include "tagmap.h" //---------------------------------------------------------------------------- typedef struct { PyObject_HEAD + PyObject* tagmap; TAG*tag; /* ST_DEFINEFONT*/ SWFFONT* font; @@ -18,10 +20,11 @@ typedef struct { PyObject* character; } TagObject; +//---------------------------------------------------------------------------- static void tag_dealloc(PyObject * self) { TagObject*tag = (TagObject*)self; - mylog("tag_dealoc %08x(%d)\n", (int)self, self->ob_refcnt); + mylog(" %08x(%d) tag_dealoc\n", (int)self, self->ob_refcnt); if(tag->placeobject) { swf_PlaceObjectFree(tag->placeobject); tag->placeobject = 0; @@ -34,6 +37,12 @@ static void tag_dealloc(PyObject * self) Py_DECREF(tag->character); tag->character = 0; } + if(tag->tag) { + swf_DeleteTag(tag->tag); + tag->tag = 0; + } + Py_DECREF(tag->tagmap); + tag->tagmap = 0; PyObject_Del(self); } //---------------------------------------------------------------------------- @@ -85,24 +94,32 @@ static PyObject* tag_getattr(PyObject * self, char* a) char* name = swf_TagGetName(tag->tag); return Py_BuildValue("s", name); } +/* if(!strcmp(a, "swf")) { + if(tag->swf == 0) + return Py_BuildValue("s", 0); + else { + Py_INCREF(tag->swf); + return tag->swf; + } + }*/ /* ------------ */ /* search for a tag specific function */ for(t=0;tob_refcnt, id); ret = Py_FindMethod(tagfunctions[t].f, self, a); if(!ret) return ret; ret = FindMethodMore(ret, common_tagfunctions, self, a); - mylog("tag_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret); + mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret); return ret; } } ret = Py_FindMethod(common_tagfunctions, self, a); - mylog("tag_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret); + mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret); return ret; } //---------------------------------------------------------------------------- @@ -111,19 +128,23 @@ static PyObject* tag_getattr(PyObject * self, char* a) PyObject* tag_new() { TagObject*tag = PyObject_New(TagObject, &TagClass); + mylog("+%08x(%d) tag_new\n", (int)tag, tag->ob_refcnt); tag->font = 0; tag->character = 0; tag->placeobject = 0; tag->tag = 0; + tag->tagmap = tagmap_new(); return (PyObject*)tag; } PyObject* tag_new2(TAG*_tag) { TagObject*tag = PyObject_New(TagObject, &TagClass); + mylog("+%08x(%d) tag_new\n", (int)tag, tag->ob_refcnt); tag->font = 0; tag->character = 0; tag->placeobject = 0; tag->tag = _tag; + tag->tagmap = tagmap_new(); return (PyObject*)tag; } @@ -133,6 +154,8 @@ static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* int r=0,g=0,b=0; TagObject*tag; PyObject*color; + + tag = (TagObject*)tag_new(); /* 1st try- copy constructor */ if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) { @@ -142,13 +165,12 @@ static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* if(!color) return NULL; - tag = (TagObject*)tag_new(); tag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR); RGBA rgba = color_getRGBA(color); swf_SetU8(tag->tag, rgba.r); swf_SetU8(tag->tag, rgba.g); swf_SetU8(tag->tag, rgba.b); - mylog("SetBackgroundColor(%02x,%02x,%02x) %08x -> %08x\n", rgba.r, rgba.g, rgba.b, (int)self, tag); + mylog(" %08x(%d) SetBackgroundColor(%02x,%02x,%02x)\n", (int)tag, tag->ob_refcnt, rgba.r, rgba.g, rgba.b); return (PyObject*)tag; } //---------------------------------------------------------------------------- @@ -172,9 +194,9 @@ static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs) tag = (TagObject*)tag_new(); tag->font = font; tag->tag = swf_InsertTag(0, ST_DEFINEFONT2); - tag->font->id = 0xabcd; // swf_SetU16(id); - swf_FontSetDefine2(tag->tag, tag->font); // TODO: should this be done later, in taglist? - mylog("DefineFont %08x -> %08x\n", (int)self, (int)tag); + tag->font->id = 0; + swf_FontSetDefine2(tag->tag, tag->font); + mylog("+%08x(%d) DefineFont\n", (int)tag, tag->ob_refcnt); return (PyObject*)tag; } //---------------------------------------------------------------------------- @@ -192,24 +214,24 @@ static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs) if(password) { swf_SetPassword(tag->tag, password); } - mylog("f_Protect %08x -> %08x\n", (int)self, (int)tag); + mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt); return (PyObject*)tag; } //---------------------------------------------------------------------------- static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs) { static char *kwlist[] = {"font", "text", "size", "color", NULL}; - TagObject*tag; + TagObject*tag = 0; char*text = 0; int textlen = 0; PyObject*unicode16; PyObject*unicode8; int size = 0; - RGBA rgba = {0,0,0,255}; + RGBA rgba = {255,0,0,0}; PyObject*color = 0; TagObject*font = 0; SRECT r; - + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color)) return NULL; @@ -223,10 +245,16 @@ static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs) mylog("DefineText: text = %s", text); tag = (TagObject*)tag_new(); + + /* notice: we do modify the (passed) font object here, + for the swf_SetDefineText call, who will write out the font id. */ + font->font->id = tagmap_add(tag->tagmap,(PyObject*)font); // add dependency on font + tag ->tag= swf_InsertTag(0, ST_DEFINETEXT2); swf_SetU16(tag->tag, /*ID*/0); r = swf_SetDefineText(tag->tag, font->font, &rgba, text, size); - mylog("DefineText %08x -> %08x\n", (int)self, (int)tag); + mylog("+%08x(%d) DefineText %08x -> %08x\n", (int)tag, tag->ob_refcnt); + return (PyObject*)tag; } //---------------------------------------------------------------------------- @@ -265,7 +293,8 @@ static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs) po->clipdepth = clipdepth; po->ratio = ratio; po->name = name; - if(clipdepth) po->matrix = matrix_getMatrix(matrix); + if(clipdepth) po->clipdepth = clipdepth; + if(matrix) po->matrix = matrix_getMatrix(matrix); if(cxform) po->cxform = colortransform_getCXForm(cxform); if(action) po->actions = action_getAction(action); @@ -274,15 +303,60 @@ static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs) Py_INCREF(character); tag->character = (PyObject*)character; tag->tag= swf_InsertTag(0, ST_PLACEOBJECT2); + + po->id = tagmap_add(tag->tagmap,(PyObject*)character); + swf_SetPlaceObject(tag->tag, po); - mylog("PlaceObject %08x -> %08x\n", (int)self, (int)tag); + mylog("+%08x(%d) PlaceObject %08x\n", (int)tag, tag->ob_refcnt, character); return (PyObject*)tag; } -TAG* tag_getTAG(PyObject*self) +TAG* tag_getRAWTAG(PyObject*self) { - // TODO: checking! - return ((TagObject*)self)->tag; + TagObject*tag = (TagObject*)self; + return tag->tag; +} + +/* serialize */ +TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap) +{ + mylog(" %08x(%d) tag_getTAG tagmap=%08x \n", (int)self, self->ob_refcnt, tagmap); + TagObject*tag = (TagObject*)self; + TAG* t = tag_getRAWTAG(self); + t->next = 0; + t->prev = prevTag; + if(prevTag) + prevTag->next = t; + + int num = swf_GetNumUsedIDs(t); + int * positions = malloc(num*sizeof(int)); + swf_GetUsedIDs(t, positions); + int i; + for(i=0;idata[positions[i]]); + PyObject* obj = tagmap_id2obj(tag->tagmap, id); + mylog(" %08x(%d) tag_getTAG: id %d is %08x\n", (int)tag, tag->ob_refcnt, id, obj); + assert(obj!=NULL); + TAG*othertag = tag_getRAWTAG(obj); + int newid = tagmap_add(tagmap, obj); + mylog(" %08x(%d) tag_getTAG: othertag->tagid=%d, new ID: %d\n", (int)tag, tag->ob_refcnt, othertag->id, newid); + + /* here comes the big hack- we define the *other* tags define ID. + This assumes that the other tag is not yet written or processed, + and we are, apart from the calling taglist, the only ones who know + about it. */ + swf_SetDefineID(othertag, newid); + + PUT16(&t->data[positions[i]], newid); + } + return t; +} + +PyObject* tag_getDependencies(PyObject*self) +{ + mylog("+%08x(%d) tag_getDependencies\n", (int)self, self->ob_refcnt); + TagObject*tag = (TagObject*)self; + return tagmap_getObjectList(tag->tagmap); } PyTypeObject TagClass = diff --git a/lib/python/tag.h b/lib/python/tag.h index 84318d6..48a303e 100644 --- a/lib/python/tag.h +++ b/lib/python/tag.h @@ -31,7 +31,8 @@ extern PyTypeObject TagClass; PyObject* tag_new(); PyObject* tag_new2(TAG*tag); -TAG* tag_getTAG(PyObject*self); +TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap); +PyObject* tag_getDependencies(PyObject*self); PyMethodDef* tag_getMethods(); #endif diff --git a/lib/python/taglist.c b/lib/python/taglist.c index 7814733..27b3a66 100644 --- a/lib/python/taglist.c +++ b/lib/python/taglist.c @@ -6,52 +6,77 @@ #include "primitives.h" #include "action.h" #include "tag.h" +#include "tagmap.h" #include "taglist.h" //---------------------------------------------------------------------------- typedef struct { PyObject_HEAD - TAG*firstTag; - TAG*searchTag; - TAG*lastTag; - PyDictObject* char2id; - PyDictObject* id2char; - U16 currentID; + PyObject* taglist; + PyObject* tagmap; } TagListObject; //---------------------------------------------------------------------------- PyObject * taglist_new() { TagListObject* taglist = PyObject_New(TagListObject, &TagListClass); - taglist->firstTag = 0; - taglist->searchTag = 0; - taglist->lastTag = 0; - taglist->currentID = 1; - taglist->char2id = (PyDictObject*)PyDict_New(); - taglist->id2char = (PyDictObject*)PyDict_New(); + mylog("+%08x(%d) taglist_new2", (int)taglist, taglist->ob_refcnt); + taglist->tagmap = tagmap_new(); + taglist->taglist = PyList_New(0); return (PyObject*)taglist; } +//---------------------------------------------------------------------------- PyObject * taglist_new2(TAG*tag) { TagListObject* taglist = PyObject_New(TagListObject, &TagListClass); - taglist->firstTag = tag; - taglist->searchTag = tag; - taglist->lastTag = tag; - while(taglist->lastTag->next) - taglist->lastTag = taglist->lastTag->next; - taglist->currentID = 1; - taglist->char2id = (PyDictObject*)PyDict_New(); - taglist->id2char = (PyDictObject*)PyDict_New(); + mylog("+%08x(%d) taglist_new2", (int)taglist, taglist->ob_refcnt); + taglist->tagmap = tagmap_new(); + + int nr=0; + TAG*t = tag; + while(t) {nr++;t=t->next;} + taglist->taglist = PyList_New(nr); + + nr = 0; + t = tag; + while(t) { + PyObject*newtag = tag_new(tag); + PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag); + if(swf_isDefiningTag(t)) { + tagmap_add(taglist->tagmap, newtag); + } + nr++; + t=t->next; + } return (PyObject*)taglist; } -TAG* taglist_getTAGs(PyObject*taglist) +//---------------------------------------------------------------------------- +TAG* taglist_getTAGs(PyObject*self) { - // TODO: checking! - return ((TagListObject*)taglist)->firstTag; + 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;ttaglist, t); + tag = tag_getTAG(item, tag, taglist->tagmap); + if(!firstTag) + firstTag = tag; + } + return firstTag; } //---------------------------------------------------------------------------- static PyObject * taglist_foldAll(PyObject* self, PyObject* args) { - SWF swf; +/* SWF swf; TagListObject*taglist = (TagListObject*)self; if(!self || !PyArg_ParseTuple(args,"")) return NULL; @@ -59,48 +84,46 @@ static PyObject * taglist_foldAll(PyObject* self, PyObject* args) swf_FoldAll(&swf); taglist->firstTag = swf.firstTag; taglist->lastTag = 0; // FIXME - taglist->searchTag = 0; + taglist->searchTag = 0;*/ return PY_NONE; } //---------------------------------------------------------------------------- static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args) { SWF swf; - TagListObject*taglist = (TagListObject*)self; +/* 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; + taglist->searchTag = 0;*/ return PY_NONE; } //---------------------------------------------------------------------------- static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args) { SWF swf; - TagListObject*taglist = (TagListObject*)self; +/* 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; + taglist->searchTag = 0;*/ return PY_NONE; } //---------------------------------------------------------------------------- static void taglist_dealloc(PyObject* self) { TagListObject*taglist = (TagListObject*)self; - SWF swf; - mylog("taglist_dealloc %08x(%d)\n", (int)self, self->ob_refcnt); - swf.firstTag = taglist->firstTag; - swf_FreeTags(&swf); - taglist->firstTag = 0; - taglist->lastTag = 0; - taglist->searchTag = 0; + 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); } //---------------------------------------------------------------------------- @@ -114,67 +137,120 @@ static PyMethodDef taglist_functions[] = static PyObject* taglist_getattr(PyObject * self, char* a) { PyObject* ret = Py_FindMethod(taglist_functions, self, a); - mylog("taglist_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret); + 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; - TAG*tag; - int l = 0; - mylog("taglist_length %08x(%d)", (int)self, self->ob_refcnt); - tag = tags->firstTag; - while(tag) { - l++; - tag = tag->next; + mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt); + return PyList_GET_SIZE(tags->taglist); +} +//---------------------------------------------------------------------------- +static int taglist_contains(PyObject * self, PyObject * tag) +{ + mylog(" %08x(%d) taglist_contains %08x", (int)self, self->ob_refcnt, tag); + TagListObject*taglist = (TagListObject*)self; + PyObject*list = taglist->taglist; + int l = PyList_Size(list); + int t; + for(t=0;tob_refcnt); + return 1; + } } - return l; + mylog(" %08x(%d) taglist_contains: no", (int)self, self->ob_refcnt); + return 0; } //---------------------------------------------------------------------------- static PyObject * taglist_concat(PyObject * self, PyObject* list) { PyObject*tag = 0; + PY_ASSERT_TYPE(self, &TagListClass); TagListObject*taglist = (TagListObject*)self; - mylog("taglist_concat %08x(%d) %08x", (int)self, self->ob_refcnt, list); + mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list); - /* TODO: rewrite */ - 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;tob_refcnt, tag); + PyList_Append(taglist->taglist, tag); + } + 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 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); - taglist->lastTag = swf_InsertTag(taglist->lastTag, tag_getTAG(tag)->id); - swf_SetBlock(taglist->lastTag, tag_getTAG(tag)->data, tag_getTAG(tag)->len); - if(!taglist->firstTag) { - taglist->firstTag = taglist->searchTag = taglist->lastTag; - } - if(swf_isDefiningTag(tag_getTAG(tag))) { - PyObject*id = PyLong_FromLong(taglist->currentID); + // 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; + return self;*/ } PyErr_Clear(); if (PyList_Check(list)) { int l = PyList_Size(list); int t; - mylog("taglist_concat: PythonList", (int)self, self->ob_refcnt); + mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt); for(t=0;tob_refcnt); + 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; } @@ -182,36 +258,11 @@ static PyObject * taglist_concat(PyObject * self, PyObject* list) static PyObject * taglist_item(PyObject * self, int index) { TagListObject*taglist = (TagListObject*)self; - TAG*tag; - PyObject*tagobject; - int i = 0; - mylog("taglist_item %08x(%d) [%d]", (int)self, self->ob_refcnt, index); - - if(index<0) { - PyErr_SetString(PyExc_Exception, setError("Negative Indices not supported.")); - return NULL; - } - - tag = taglist->firstTag; - while(tag && inext; - i++; - } - if(!tag || i != index) { - if(index> i+10) { - PyErr_SetString(PyExc_Exception, setError("No Tag at position %d", index)); - return NULL; - } - - mylog("taglist_item %08x(%d)->IndexError (%d)", (int)self, self->ob_refcnt, index); - - Py_INCREF(PyExc_IndexError); - PyErr_SetObject(PyExc_IndexError, Py_None); - return NULL; - } - - tagobject = tag_new2(tag); - return (PyObject*)tagobject; + 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 + return tag; } static PySequenceMethods taglist_as_sequence = { @@ -222,7 +273,7 @@ static PySequenceMethods taglist_as_sequence = 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: 0, //??? + sq_contains: taglist_contains, //??? }; static PyTypeObject TagListClass = { diff --git a/lib/python/tagmap.c b/lib/python/tagmap.c new file mode 100644 index 0000000..055d035 --- /dev/null +++ b/lib/python/tagmap.c @@ -0,0 +1,144 @@ +/* tagmap.c + + Python wrapper for librfxswf. + + Part of the swftools package. + + Copyright (c) 2003 Matthias Kramm + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include +#undef HAVE_STAT +#include "pyutils.h" +#include "tagmap.h" + +typedef struct { + PyObject_HEAD + PyObject* obj2id; + PyObject* id2obj; + PyObject* objlist; + int currentID; +} TagMapObject; + +//---------------------------------------------------------------------------- +PyObject* tagmap_new() +{ + PyObject* self = (PyObject*)PyObject_New(TagMapObject, &TagMapClass); + mylog("+%08x(%d) tagmap_new", (int)self, self->ob_refcnt); + TagMapObject*tagmap = (TagMapObject*)self; + tagmap->obj2id = PyDict_New(); + tagmap->id2obj = PyDict_New(); + tagmap->objlist = PyList_New(0); + tagmap->currentID = 0; //IDs start at 1 + return self; +} + +//---------------------------------------------------------------------------- +int tagmap_obj2id(PyObject* self, PyObject* obj) +{ + mylog(" %08x(%d) tagmap_obj2id %08x", (int)self, self->ob_refcnt, obj); + TagMapObject*tagmap = (TagMapObject*)self; + PyObject*id = PyDict_GetItem(tagmap->obj2id, obj); + if(id == 0) + return -1; + int _id = PyLong_AsLong(id); + Py_DECREF(id); + return _id; +} + +//---------------------------------------------------------------------------- +PyObject* tagmap_id2obj(PyObject* self, int _id) +{ + TagMapObject*tagmap = (TagMapObject*)self; + PyObject*id = PyLong_FromLong(_id); + PyObject*obj = PyDict_GetItem(tagmap->id2obj, id); + mylog(" %08x(%d) tagmap_id2obj %d->%08x", (int)self, self->ob_refcnt, _id, obj); + Py_DECREF(id); + return obj; +} +//---------------------------------------------------------------------------- +int tagmap_getFreeID(PyObject*self) +{ + mylog(" %08x(%d) tagmap_getFreeID", (int)self, self->ob_refcnt); + TagMapObject*tagmap = (TagMapObject*)self; + int last = tagmap->currentID; + do { + tagmap->currentID++; + PyObject*id = PyLong_FromLong(tagmap->currentID); + PyObject*test = PyDict_GetItem(tagmap->id2obj,id); + Py_DECREF(id); + if(test == 0) { + PyErr_Clear(); + mylog(" %08x(%d) tagmap_getFreeID -> %d", (int)self, self->ob_refcnt, tagmap->currentID); + return tagmap->currentID; + } + } while(last != tagmap->currentID); + mylog(" %08x(%d) tagmap_getFreeID -> -1", (int)self, self->ob_refcnt); + return -1; +} +//---------------------------------------------------------------------------- +int tagmap_add(PyObject* self, PyObject* obj) +{ + TagMapObject*tagmap = (TagMapObject*)self; + int id = tagmap_obj2id(self, obj); + if(id>=0) { + mylog(" %08x(%d) tagmap_add %08x->%d (again)", (int)self, self->ob_refcnt, (int)obj, id); + return id; + } + id = tagmap_getFreeID(self); + PyList_Append(tagmap->objlist, obj);//Py_INCREF(obj); done by PyList_Append + PyObject*id_obj = PyLong_FromLong(id); + PyDict_SetItem(tagmap->obj2id, obj, id_obj);//Py_INCREF(id_obj);Py_INCREF(obj); done by PyDict_SetItem + PyDict_SetItem(tagmap->id2obj, id_obj, obj);//Py_INCREF(id_obj);Py_INCREF(obj); done by PyDict_SetItem + Py_DECREF(id_obj); + mylog(" %08x(%d) tagmap_add %08x->%d", (int)self, self->ob_refcnt, (int)obj, id); + return id; +} + +//---------------------------------------------------------------------------- +void tagmap_dealloc(PyObject* self) +{ + mylog("-%08x(%d) tagmap_dealloc", (int)self, self->ob_refcnt); + TagMapObject*tagmap = (TagMapObject*)self; + Py_DECREF(tagmap->obj2id); + tagmap->obj2id = 0; + Py_DECREF(tagmap->id2obj); + tagmap->id2obj = 0; + Py_DECREF(tagmap->objlist); + tagmap->objlist = 0; + PyObject_Del(self); +} +//---------------------------------------------------------------------------- +PyObject* tagmap_getObjectList(PyObject* self) +{ + mylog(" %08x(%d) tagmap_getObjectList", (int)self, self->ob_refcnt); + TagMapObject*tagmap = (TagMapObject*)self; + Py_INCREF(tagmap->objlist); + return tagmap->objlist; +} +//---------------------------------------------------------------------------- +PyTypeObject TagMapClass = +{ + PyObject_HEAD_INIT(NULL) + 0, + tp_name: "TagMap", + tp_basicsize: sizeof(TagMapObject), + tp_itemsize: 0, + tp_dealloc: tagmap_dealloc, + tp_print: 0, + tp_getattr: 0, + tp_setattr: 0, +}; diff --git a/lib/python/tagmap.h b/lib/python/tagmap.h new file mode 100644 index 0000000..ccaaf88 --- /dev/null +++ b/lib/python/tagmap.h @@ -0,0 +1,38 @@ +/* action.h + + Python wrapper for librfxswf- actionscript stuff (header) + + Part of the swftools package. + + Copyright (c) 2003 Matthias Kramm + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#ifndef __tagmap_h__ +#define __tagmap_h__ + +#include "../rfxswf.h" +#undef HAVE_STAT +#include + +extern PyTypeObject TagMapClass; +int tagmap_obj2id(PyObject* self, PyObject* obj); +PyObject* tagmap_id2obj(PyObject* self, int _id); +int tagmap_add(PyObject* self, PyObject* obj); +PyObject* tagmap_getObjectList(PyObject* self); + +PyObject* tagmap_new(); + +#endif -- 1.7.10.4