1/*
2 * Manual wrappers for CFBitVector
3 */
4static PyObject*
5mod_CFBitVectorCreate(PyObject* self __attribute__((__unused__)),
6	PyObject* args)
7{
8	PyObject* py_allocator;
9	PyObject* py_bytes;
10	Py_ssize_t count;
11	CFAllocatorRef allocator;
12	CFBitVectorRef vector;
13
14
15	if (!PyArg_ParseTuple(args, "OO" Py_ARG_SIZE_T, &py_allocator, &py_bytes, &count)) {
16		return NULL;
17	}
18
19	if (PyObjC_PythonToObjC(@encode(CFAllocatorRef), py_allocator, &allocator) < 0) {
20		return NULL;
21	}
22
23	PyObject* buf;
24	void* bytes;
25	int r;
26	Py_ssize_t byteCount;
27
28	if (count == -1) {
29		byteCount = -1;
30	} else {
31		byteCount = count / 8;
32	}
33
34        r = PyObjC_PythonToCArray(NO, NO, "z", py_bytes, &bytes, &byteCount, &buf);
35	if (r == -1) {
36		return NULL;
37	}
38
39	if (count == -1) {
40		count = byteCount * 8;
41	}
42
43	vector = CFBitVectorCreate(allocator, bytes, count);
44
45	PyObjC_FreeCArray(r, bytes);
46	Py_XDECREF(buf);
47
48	PyObject* result = PyObjC_ObjCToPython(@encode(CFBitVectorRef), &vector);
49	if (vector) {
50		CFRelease(vector);
51	}
52	return result;
53}
54
55static PyObject*
56mod_CFBitVectorGetBits(PyObject* self __attribute__((__unused__)),
57		PyObject* args)
58{
59	PyObject* py_vector;
60	PyObject* py_range;
61	PyObject* py_bytes;
62	CFBitVectorRef vector;
63	CFRange range;
64
65	if (!PyArg_ParseTuple(args, "OOO", &py_vector, &py_range, &py_bytes)) {
66		return NULL;
67	}
68
69
70	if (PyObjC_PythonToObjC(@encode(CFBitVectorRef), py_vector, &vector) < 0) {
71		return NULL;
72	}
73	if (PyObjC_PythonToObjC(@encode(CFRange), py_range, &range) < 0) {
74		return NULL;
75	}
76	if (py_bytes != Py_None) {
77		PyErr_Format(PyExc_ValueError, "argument 3: expecting None, got instance of %s",
78			Py_TYPE(py_bytes)->tp_name);
79		return NULL;
80	}
81
82	PyObject* buffer = PyBytes_FromStringAndSize(NULL, (range.length+7)/8);
83	if (buffer == NULL) {
84		return NULL;
85	}
86	memset(PyBytes_AsString(buffer), 0, (range.length+7)/8);
87
88	CFBitVectorGetBits(vector, range, (unsigned char*)PyBytes_AsString(buffer));
89	return buffer;
90}
91
92
93
94
95
96#define COREFOUNDATION_BITVECTOR_METHODS \
97        {	\
98		"CFBitVectorCreate",	\
99		(PyCFunction)mod_CFBitVectorCreate,	\
100		METH_VARARGS,	\
101		NULL	\
102	},	\
103        {	\
104		"CFBitVectorGetBits",	\
105		(PyCFunction)mod_CFBitVectorGetBits,	\
106		METH_VARARGS,	\
107		NULL	\
108	},
109