2 * Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; version 2
7 * of the License or (at your option) a later version of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 #if (PY_VERSION_HEX < 0x02050000)
25 typedef int Py_ssize_t;
28 #ifdef __MINGW32_VERSION
29 size_t strnlen(char *text, size_t maxlen)
31 const char *last = memchr(text, '\0', maxlen);
32 return last ? (size_t) (last - text) : maxlen;
36 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
38 static PyObject *sha_to_pyhex(const unsigned char *sha)
42 for (i = 0; i < 20; i++) {
43 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
44 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
47 return PyString_FromStringAndSize(hexsha, 40);
50 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
52 char *text, *start, *end;
54 PyObject *ret, *item, *name;
56 if (!PyArg_ParseTuple(args, "s#", &text, &len))
59 /* TODO: currently this returns a list; if memory usage is a concern,
60 * consider rewriting as a custom iterator object */
72 mode = strtol(text, &text, 8);
75 PyErr_SetString(PyExc_ValueError, "Expected space");
82 namelen = strnlen(text, len - (text - start));
84 name = PyString_FromStringAndSize(text, namelen);
90 if (text + namelen + 20 >= end) {
91 PyErr_SetString(PyExc_ValueError, "SHA truncated");
97 item = Py_BuildValue("(NlN)", name, mode,
98 sha_to_pyhex((unsigned char *)text+namelen+1));
104 if (PyList_Append(ret, item) == -1) {
123 int cmp_tree_item(const void *_a, const void *_b)
125 const struct tree_item *a = _a, *b = _b;
126 const char *remain_a, *remain_b;
128 if (strlen(a->name) > strlen(b->name)) {
129 common = strlen(b->name);
130 remain_a = a->name + common;
131 remain_b = (S_ISDIR(b->mode)?"/":"");
132 } else if (strlen(b->name) > strlen(a->name)) {
133 common = strlen(a->name);
134 remain_a = (S_ISDIR(a->mode)?"/":"");
135 remain_b = b->name + common;
136 } else { /* strlen(a->name) == strlen(b->name) */
141 ret = strncmp(a->name, b->name, common);
144 return strcmp(remain_a, remain_b);
147 static void free_tree_items(struct tree_item *items, int num) {
149 for (i = 0; i < num; i++) {
150 Py_DECREF(items[i].tuple);
155 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
157 struct tree_item *qsort_entries;
161 PyObject *key, *value;
163 if (!PyDict_Check(entries)) {
164 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
168 num = PyDict_Size(entries);
169 qsort_entries = malloc(num * sizeof(struct tree_item));
170 if (qsort_entries == NULL) {
176 while (PyDict_Next(entries, &pos, &key, &value)) {
177 PyObject *py_mode, *py_int_mode, *py_sha;
179 if (!PyString_Check(key)) {
180 PyErr_SetString(PyExc_TypeError, "Name is not a string");
181 free_tree_items(qsort_entries, i);
185 if (PyTuple_Size(value) != 2) {
186 PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
187 free_tree_items(qsort_entries, i);
191 py_mode = PyTuple_GET_ITEM(value, 0);
192 py_int_mode = PyNumber_Int(py_mode);
194 PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
195 free_tree_items(qsort_entries, i);
199 py_sha = PyTuple_GET_ITEM(value, 1);
200 if (!PyString_Check(py_sha)) {
201 PyErr_SetString(PyExc_TypeError, "SHA is not a string");
202 Py_DECREF(py_int_mode);
203 free_tree_items(qsort_entries, i);
206 qsort_entries[i].name = PyString_AS_STRING(key);
207 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
208 qsort_entries[i].tuple = PyTuple_Pack(3, key, py_int_mode, py_sha);
209 Py_DECREF(py_int_mode);
213 qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
215 ret = PyList_New(num);
217 free_tree_items(qsort_entries, i);
222 for (i = 0; i < num; i++) {
223 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
231 static PyMethodDef py_objects_methods[] = {
232 { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
233 { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
234 { NULL, NULL, 0, NULL }
237 void init_objects(void)
241 m = Py_InitModule3("_objects", py_objects_methods, NULL);