3 Python wrapper for librfxswf.
5 Part of the swftools package.
7 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
36 //----------------------------------------------------------------------------
37 PyObject* tagmap_new()
39 PyObject* self = (PyObject*)PyObject_New(TagMapObject, &TagMapClass);
40 mylog("+%08x(%d) tagmap_new", (int)self, self->ob_refcnt);
41 TagMapObject*tagmap = (TagMapObject*)self;
42 tagmap->obj2id = PyDict_New();
43 tagmap->id2obj = PyDict_New();
44 tagmap->objlist = PyList_New(0);
45 tagmap->currentID = 0; //IDs start at 1
49 //----------------------------------------------------------------------------
50 int tagmap_obj2id(PyObject* self, PyObject* obj)
52 mylog(" %08x(%d) tagmap_obj2id %08x", (int)self, self->ob_refcnt, obj);
53 TagMapObject*tagmap = (TagMapObject*)self;
54 PyObject*id = PyDict_GetItem(tagmap->obj2id, obj);
57 int _id = PyLong_AsLong(id);
62 //----------------------------------------------------------------------------
63 PyObject* tagmap_id2obj(PyObject* self, int _id)
65 TagMapObject*tagmap = (TagMapObject*)self;
66 PyObject*id = PyLong_FromLong(_id);
67 PyObject*obj = PyDict_GetItem(tagmap->id2obj, id);
68 mylog(" %08x(%d) tagmap_id2obj %d->%08x", (int)self, self->ob_refcnt, _id, obj);
72 //----------------------------------------------------------------------------
73 int tagmap_getFreeID(PyObject*self)
75 mylog(" %08x(%d) tagmap_getFreeID", (int)self, self->ob_refcnt);
76 TagMapObject*tagmap = (TagMapObject*)self;
77 int last = tagmap->currentID;
80 PyObject*id = PyLong_FromLong(tagmap->currentID);
81 PyObject*test = PyDict_GetItem(tagmap->id2obj,id);
85 mylog(" %08x(%d) tagmap_getFreeID -> %d", (int)self, self->ob_refcnt, tagmap->currentID);
86 return tagmap->currentID;
88 } while(last != tagmap->currentID);
89 mylog(" %08x(%d) tagmap_getFreeID -> -1", (int)self, self->ob_refcnt);
92 //----------------------------------------------------------------------------
93 static void tagmap_add_mapping(PyObject*self, int id, PyObject* obj)
95 TagMapObject*tagmap = (TagMapObject*)self;
96 PyList_Append(tagmap->objlist, obj);//Py_INCREF(obj); done by PyList_Append
97 PyObject*id_obj = PyLong_FromLong(id);
98 PyDict_SetItem(tagmap->obj2id, obj, id_obj);//Py_INCREF(id_obj);Py_INCREF(obj); done by PyDict_SetItem
99 PyDict_SetItem(tagmap->id2obj, id_obj, obj);//Py_INCREF(id_obj);Py_INCREF(obj); done by PyDict_SetItem
102 //----------------------------------------------------------------------------
103 void tagmap_addMapping(PyObject*self, int id, PyObject* obj)
105 TagMapObject*tagmap = (TagMapObject*)self;
106 int id2 = tagmap_obj2id(self, obj);
111 tagmap_add_mapping(self, id, obj);
113 //----------------------------------------------------------------------------
114 int tagmap_add(PyObject* self, PyObject* obj)
116 TagMapObject*tagmap = (TagMapObject*)self;
117 int id = tagmap_obj2id(self, obj);
119 mylog(" %08x(%d) tagmap_add %08x->%d (again)", (int)self, self->ob_refcnt, (int)obj, id);
122 id = tagmap_getFreeID(self);
124 tagmap_add_mapping(self, id, obj);
126 mylog(" %08x(%d) tagmap_add %08x->%d", (int)self, self->ob_refcnt, (int)obj, id);
130 //----------------------------------------------------------------------------
131 void tagmap_dealloc(PyObject* self)
133 mylog("-%08x(%d) tagmap_dealloc", (int)self, self->ob_refcnt);
134 TagMapObject*tagmap = (TagMapObject*)self;
135 Py_DECREF(tagmap->obj2id);
137 Py_DECREF(tagmap->id2obj);
139 Py_DECREF(tagmap->objlist);
143 //----------------------------------------------------------------------------
144 PyObject* tagmap_getObjectList(PyObject* self)
146 mylog(" %08x(%d) tagmap_getObjectList", (int)self, self->ob_refcnt);
147 TagMapObject*tagmap = (TagMapObject*)self;
148 Py_INCREF(tagmap->objlist);
149 return tagmap->objlist;
151 //----------------------------------------------------------------------------
152 PyTypeObject TagMapClass =
154 PyObject_HEAD_INIT(NULL)
157 tp_basicsize: sizeof(TagMapObject),
159 tp_dealloc: tagmap_dealloc,