1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2025-03-10 05:34:36 +00:00

core_pe: Re-implemented the "cache" cython module in C. I like coding in C from time to time...

This commit is contained in:
Virgil Dupras 2010-01-30 16:29:18 +01:00
parent ff2461df9d
commit 7ec64e8a3d
4 changed files with 79 additions and 40 deletions

View File

@ -9,7 +9,6 @@ syntax: glob
conf.yaml conf.yaml
build build
core_pe/modules/block/block.c core_pe/modules/block/block.c
core_pe/modules/cache/cache.c
cocoa/*/Info.plist cocoa/*/Info.plist
cocoa/*/build cocoa/*/build
cocoa/*/dg_cocoa.plugin cocoa/*/dg_cocoa.plugin

78
core_pe/modules/cache/cache.c vendored Normal file
View File

@ -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);
}

View File

@ -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

View File

@ -8,9 +8,7 @@
from distutils.core import setup from distutils.core import setup
from distutils.extension import Extension from distutils.extension import Extension
from Cython.Distutils import build_ext
setup( setup(
cmdclass = {'build_ext': build_ext}, ext_modules = [Extension("_cache", ["cache.c"])]
ext_modules = [Extension("_cache", ["cache.pyx"])]
) )