diff --git a/.hgignore b/.hgignore index 53c6f972..a7e41fd4 100644 --- a/.hgignore +++ b/.hgignore @@ -9,7 +9,6 @@ syntax: glob conf.yaml build core_pe/modules/block/block.c -core_pe/modules/cache/cache.c cocoa/*/Info.plist cocoa/*/build cocoa/*/dg_cocoa.plugin diff --git a/core_pe/modules/cache/cache.c b/core_pe/modules/cache/cache.c new file mode 100644 index 00000000..b5ea4a2f --- /dev/null +++ b/core_pe/modules/cache/cache.c @@ -0,0 +1,78 @@ +/* Created By: Virgil Dupras + * Created On: 2010-01-30 + * Copyright 2010 Hardcoded Software (http://www.hardcoded.net) + */ +#include "Python.h" +#include "structmember.h" + +static PyObject* +cache_string_to_colors(PyObject *self, PyObject *args) +{ + char *s; + PyObject *result; + + if (!PyArg_ParseTuple(args, "s", &s)) { + return NULL; + } + + result = PyList_New(0); + if (result == NULL) { + return NULL; + } + + while (*s) { + long r, g, b, whole_color; + char buffer[7]; + PyObject *color_tuple; + PyObject *pi; + + strncpy(buffer, s, 6); + if (strlen(buffer) < 6) { /* incomplete color, terminate loop */ + break; + } + s += 6; + whole_color = strtol(buffer, NULL, 16); + r = whole_color >> 16; + g = (whole_color >> 8) & 0xff; + b = whole_color & 0xff; + + color_tuple = PyTuple_New(3); + pi = PyInt_FromLong(r); + if (pi == NULL) { + Py_DECREF(result); + return NULL; + } + PyTuple_SET_ITEM(color_tuple, 0, pi); + pi = PyInt_FromLong(g); + if (pi == NULL) { + Py_DECREF(result); + return NULL; + } + PyTuple_SET_ITEM(color_tuple, 1, pi); + pi = PyInt_FromLong(b); + if (pi == NULL) { + Py_DECREF(result); + return NULL; + } + PyTuple_SET_ITEM(color_tuple, 2, pi); + + if (PyList_Append(result, color_tuple) < 0) { + Py_DECREF(result); + return NULL; + } + } + + return result; +} + +static PyMethodDef CacheMethods[] = { + {"string_to_colors", cache_string_to_colors, METH_VARARGS, + "Transform the string 's' in a list of 3 sized tuples."}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +PyMODINIT_FUNC +init_cache(void) +{ + (void)Py_InitModule("_cache", CacheMethods); +} diff --git a/core_pe/modules/cache/cache.pyx b/core_pe/modules/cache/cache.pyx deleted file mode 100644 index 095d33d1..00000000 --- a/core_pe/modules/cache/cache.pyx +++ /dev/null @@ -1,36 +0,0 @@ -# Created By: Virgil Dupras -# Created On: 2009-04-23 -# Copyright 2010 Hardcoded Software (http://www.hardcoded.net) -# -# This software is licensed under the "HS" License as described in the "LICENSE" file, -# which should be included with this package. The terms are also available at -# http://www.hardcoded.net/licenses/hs_license - -# ok, this is hacky and stuff, but I don't know C well enough to play with char buffers, copy -# them around and stuff -cdef int xchar_to_int(char c): - if 48 <= c <= 57: # 0-9 - return c - 48 - elif 65 <= c <= 70: # A-F - return c - 55 - elif 97 <= c <= 102: # a-f - return c - 87 - -def string_to_colors(s): - """Transform the string 's' in a list of 3 sized tuples. - """ - result = [] - cdef int i, char_count, r, g, b - cdef char* cs - char_count = len(s) - char_count = (char_count // 6) * 6 - cs = s - for i in range(0, char_count, 6): - r = xchar_to_int(cs[i]) << 4 - r += xchar_to_int(cs[i+1]) - g = xchar_to_int(cs[i+2]) << 4 - g += xchar_to_int(cs[i+3]) - b = xchar_to_int(cs[i+4]) << 4 - b += xchar_to_int(cs[i+5]) - result.append((r, g, b)) - return result diff --git a/core_pe/modules/cache/setup.py b/core_pe/modules/cache/setup.py index c58c1ee6..b81aba20 100644 --- a/core_pe/modules/cache/setup.py +++ b/core_pe/modules/cache/setup.py @@ -8,9 +8,7 @@ from distutils.core import setup from distutils.extension import Extension -from Cython.Distutils import build_ext setup( - cmdclass = {'build_ext': build_ext}, - ext_modules = [Extension("_cache", ["cache.pyx"])] + ext_modules = [Extension("_cache", ["cache.c"])] ) \ No newline at end of file