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
|
/* tagmap.c
Python wrapper for librfxswf.
Part of the swftools package.
Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
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 <Python.h>
#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);
TagMapObject*tagmap = (TagMapObject*)self;
tagmap->obj2id = PyDict_New();
tagmap->id2obj = PyDict_New();
tagmap->objlist = PyList_New(0);
tagmap->currentID = 0; //IDs start at 1
/* mylog("+%08x(%d) tagmap_new %08x(%d) %08x(%d), %08x(%d)", (int)self, self->ob_refcnt,
tagmap->obj2id, tagmap->obj2id->ob_refcnt ,
tagmap->id2obj, tagmap->id2obj->ob_refcnt ,
tagmap->objlist, tagmap->objlist->ob_refcnt);*/
return self;
}
//----------------------------------------------------------------------------
int tagmap_obj2id(PyObject* self, PyObject* obj)
{
TagMapObject*tagmap = (TagMapObject*)self;
PyObject*id = PyDict_GetItem(tagmap->obj2id, obj);
if(id == 0)
return -1;
int _id = PyLong_AsLong(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);
Py_DECREF(id);
return obj;
}
//----------------------------------------------------------------------------
int tagmap_getFreeID(PyObject*self)
{
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;
}
//----------------------------------------------------------------------------
static void tagmap_add_mapping(PyObject*self, int id, PyObject* obj)
{
TagMapObject*tagmap = (TagMapObject*)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);
}
//----------------------------------------------------------------------------
void tagmap_addMapping(PyObject*self, int id, PyObject* obj)
{
TagMapObject*tagmap = (TagMapObject*)self;
int id2 = tagmap_obj2id(self, obj);
if(id2>=0) {
assert(id==id2);
return;
}
tagmap_add_mapping(self, id, obj);
}
//----------------------------------------------------------------------------
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);
tagmap_add_mapping(self, id, obj);
mylog(" %08x(%d) tagmap_add %08x->%d", (int)self, self->ob_refcnt, (int)obj, id);
return id;
}
//----------------------------------------------------------------------------
void tagmap_dealloc(PyObject* self)
{
TagMapObject*tagmap = (TagMapObject*)self;
mylog("-%08x(%d) tagmap_dealloc %08x(%d) %08x(%d), %08x(%d)", (int)self, self->ob_refcnt,
tagmap->obj2id, tagmap->obj2id->ob_refcnt ,
tagmap->id2obj, tagmap->id2obj->ob_refcnt ,
tagmap->objlist, tagmap->objlist->ob_refcnt);
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;
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,
};
|