summaryrefslogtreecommitdiffstats
path: root/python/enum.c
blob: b57a8802978ad946f8670f7cff9dbf630da7f041 (plain)
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
/*------------------------------------------------------------------------
 *  Copyright 2009-2010 (c) Jeff Brown <spadix@users.sourceforge.net>
 *
 *  This file is part of the ZBar Bar Code Reader.
 *
 *  The ZBar Bar Code Reader is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Lesser Public License as
 *  published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *
 *  The ZBar Bar Code Reader 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 Lesser Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with the ZBar Bar Code Reader; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 *
 *  http://sourceforge.net/projects/zbar
 *------------------------------------------------------------------------*/

#include "zbarmodule.h"

static char enumitem_doc[] =
    PyDoc_STR("simple enumeration item.\n"
	      "\n"
	      "associates an int value with a name for printing.");

static zbarEnumItem *enumitem_new(PyTypeObject *type, PyObject *args,
				  PyObject *kwds)
{
    int val		  = 0;
    PyObject *name	  = NULL;
    static char *kwlist[] = { "value", "name", NULL };
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "iS", kwlist, &val, &name))
	return (NULL);

    zbarEnumItem *self = (zbarEnumItem *)type->tp_alloc(type, 0);
    if (!self)
	return (NULL);

#if PY_MAJOR_VERSION >= 3
    PyLongObject *longval = (PyLongObject *)PyLong_FromLong(val);
    if (!longval) {
	Py_DECREF(self);
	return (NULL);
    }

    /* we assume the "fast path" for a single-digit ints (see longobject.c) */
    /* this also holds if we get a small_int preallocated long */
#if PY_VERSION_HEX >= 0x030900A4
    Py_SET_SIZE(&self->val, Py_SIZE(longval));
#else
    Py_SIZE(&self->val)	  = Py_SIZE(longval);
#endif
#if PY_VERSION_HEX >= 0x030c0000
    self->val.long_value.ob_digit[0] = longval->long_value.ob_digit[0];
#else
    self->val.ob_digit[0] = longval->ob_digit[0];
#endif
    Py_DECREF(longval);
#else
    self->val.ob_ival = val;
#endif
    self->name = name;
    return (self);
}

static void enumitem_dealloc(zbarEnumItem *self)
{
    Py_CLEAR(self->name);
    ((PyObject *)self)->ob_type->tp_free((PyObject *)self);
}

static PyObject *enumitem_str(zbarEnumItem *self)
{
    Py_INCREF(self->name);
    return (self->name);
}

#if PY_MAJOR_VERSION < 3
/* tp_print was dropped on Python 3.9 */
static int enumitem_print(zbarEnumItem *self, FILE *fp, int flags)
{
    return (self->name->ob_type->tp_print(self->name, fp, flags));
}
#endif

static PyObject *enumitem_repr(zbarEnumItem *self)
{
    PyObject *name = PyObject_Repr(self->name);
    if (!name)
	return (NULL);
#if PY_MAJOR_VERSION >= 3
    PyObject *repr = PyUnicode_FromFormat("%s(%ld, %U)",
					  ((PyObject *)self)->ob_type->tp_name,
					  PyLong_AsLong((PyObject *)self),
					  name);
#else
    char *namestr  = PyString_AsString(name);
    PyObject *repr = PyString_FromFormat("%s(%ld, %s)",
					 ((PyObject *)self)->ob_type->tp_name,
					 self->val.ob_ival, namestr);
#endif
    Py_DECREF(name);
    return ((PyObject *)repr);
}

PyTypeObject zbarEnumItem_Type = {
    PyVarObject_HEAD_INIT(NULL, 0).tp_name = "zbar.EnumItem",
    .tp_doc				   = enumitem_doc,
    .tp_basicsize			   = sizeof(zbarEnumItem),
    .tp_flags	= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    .tp_new	= (newfunc)enumitem_new,
    .tp_dealloc = (destructor)enumitem_dealloc,
    .tp_str	= (reprfunc)enumitem_str,
#if PY_MAJOR_VERSION < 3
    .tp_print = (printfunc)enumitem_print,
#endif
    .tp_repr = (reprfunc)enumitem_repr,
};

zbarEnumItem *zbarEnumItem_New(PyObject *byname, PyObject *byvalue, int val,
			       const char *name)
{
    zbarEnumItem *self = PyObject_New(zbarEnumItem, &zbarEnumItem_Type);
    if (!self)
	return (NULL);
#if PY_MAJOR_VERSION >= 3
    PyLongObject *longval = (PyLongObject *)PyLong_FromLong(val);
    if (!longval) {
	Py_DECREF(self);
	return (NULL);
    }

    /* we assume the "fast path" for a single-digit ints (see longobject.c) */
    /* this also holds if we get a small_int preallocated long */
#if PY_VERSION_HEX >= 0x030900A4
    Py_SET_SIZE(&self->val, Py_SIZE(longval));
#else
    Py_SIZE(&self->val)	  = Py_SIZE(longval);
#endif
#if PY_VERSION_HEX >= 0x030c0000
    self->val.long_value.ob_digit[0] = longval->long_value.ob_digit[0];
#else
    self->val.ob_digit[0] = longval->ob_digit[0];
#endif
    Py_DECREF(longval);

    self->name = PyUnicode_FromString(name);
#else
    self->val.ob_ival = val;
    self->name	      = PyString_FromString(name);
#endif
    if (!self->name ||
	(byname && PyDict_SetItem(byname, self->name, (PyObject *)self)) ||
	(byvalue &&
	 PyDict_SetItem(byvalue, (PyObject *)self, (PyObject *)self))) {
	Py_DECREF((PyObject *)self);
	return (NULL);
    }
    return (self);
}

static char enum_doc[] = PyDoc_STR("enumeration container for EnumItems.\n"
				   "\n"
				   "exposes items as read-only attributes");

/* FIXME add iteration */

static int enum_traverse(zbarEnum *self, visitproc visit, void *arg)
{
    Py_VISIT(self->byname);
    Py_VISIT(self->byvalue);
    return (0);
}

static int enum_clear(zbarEnum *self)
{
    Py_CLEAR(self->byname);
    Py_CLEAR(self->byvalue);
    return (0);
}

static void enum_dealloc(zbarEnum *self)
{
    enum_clear(self);
    ((PyObject *)self)->ob_type->tp_free((PyObject *)self);
}

PyTypeObject zbarEnum_Type = {
    PyVarObject_HEAD_INIT(NULL, 0).tp_name = "zbar.Enum",

    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,

    .tp_doc	   = enum_doc,
    .tp_basicsize  = sizeof(zbarEnum),
    .tp_dictoffset = offsetof(zbarEnum, byname),
    .tp_traverse   = (traverseproc)enum_traverse,
    .tp_clear	   = (inquiry)enum_clear,
    .tp_dealloc	   = (destructor)enum_dealloc,
};

zbarEnum *zbarEnum_New()
{
    zbarEnum *self = PyObject_GC_New(zbarEnum, &zbarEnum_Type);
    if (!self)
	return (NULL);
    self->byname  = PyDict_New();
    self->byvalue = PyDict_New();
    if (!self->byname || !self->byvalue) {
	Py_DECREF(self);
	return (NULL);
    }
    return (self);
}

int zbarEnum_Add(zbarEnum *self, int val, const char *name)
{
    zbarEnumItem *item;
    item = zbarEnumItem_New(self->byname, self->byvalue, val, name);
    if (!item)
	return (-1);
    return (0);
}

zbarEnumItem *zbarEnum_LookupValue(zbarEnum *self, int val)
{
#if PY_MAJOR_VERSION >= 3
    PyObject *key = PyLong_FromLong(val);
#else
    PyObject *key = PyInt_FromLong(val);
#endif
    zbarEnumItem *e = (zbarEnumItem *)PyDict_GetItem(self->byvalue, key);
    if (!e)
	return ((zbarEnumItem *)key);
    Py_INCREF((PyObject *)e);
    Py_DECREF(key);
    return (e);
}

PyObject *zbarEnum_SetFromMask(zbarEnum *self, unsigned int mask)
{
    PyObject *result = PySet_New(NULL);
    PyObject *key, *item;
    Py_ssize_t i = 0;
    while (PyDict_Next(self->byvalue, &i, &key, &item)) {
#if PY_MAJOR_VERSION >= 3
	unsigned long val = (unsigned long)PyLong_AsLong(item);
#else
	int val = PyInt_AsLong(item);
#endif
	if (val < sizeof(mask) * 8 && ((mask >> val) & 1))
	    PySet_Add(result, item);
    }
    return (result);
}