dfhack/library/python/pydfhack/util.py

77 lines
2.0 KiB
Python

2010-05-11 15:34:59 -06:00
from ctypes import *
2010-05-27 11:35:40 -06:00
int_ptr = POINTER(c_int)
uint_ptr = POINTER(c_uint)
short_ptr = POINTER(c_short)
ushort_ptr = POINTER(c_ushort)
byte_ptr = POINTER(c_byte)
ubyte_ptr = POINTER(c_ubyte)
pointer_dict = {}
2010-05-27 11:35:40 -06:00
2010-05-11 15:34:59 -06:00
def _uintify(x, y, z):
return (c_uint(x), c_uint(y), c_uint(z))
def _allocate_array(ptr, t_type, count):
arr = (t_type * count)()
p = cast(arr, POINTER(t_type))
ptr[0] = p
pointer_dict[addressof(arr)] = (ptr, arr, p)
2010-05-27 11:35:40 -06:00
return 1
2010-05-25 14:37:40 -06:00
def _alloc_int_buffer(ptr, count):
return _allocate_array(ptr, c_int, count)
_int_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_int)), c_uint)
2010-05-27 11:35:40 -06:00
alloc_int_buffer = _int_functype(_alloc_int_buffer)
2010-05-25 14:37:40 -06:00
2010-05-27 11:35:40 -06:00
def _alloc_uint_buffer(ptr, count):
return _allocate_array(ptr, c_uint, count)
2010-05-25 14:37:40 -06:00
_uint_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_uint)), c_uint)
2010-05-27 11:35:40 -06:00
alloc_uint_buffer = _uint_functype(_alloc_uint_buffer)
2010-05-25 14:37:40 -06:00
2010-05-27 11:35:40 -06:00
def _alloc_short_buffer(ptr, count):
return _allocate_array(ptr, c_short, count)
2010-05-25 14:37:40 -06:00
_short_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_short)), c_uint)
2010-05-27 11:35:40 -06:00
alloc_short_buffer = _short_functype(_alloc_short_buffer)
def _alloc_ushort_buffer(ptr, count):
return _allocate_array(ptr, c_ushort, count)
2010-05-25 14:37:40 -06:00
_ushort_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_ushort)), c_uint)
2010-05-27 11:35:40 -06:00
alloc_ushort_buffer = _ushort_functype(_alloc_ushort_buffer)
2010-05-25 14:37:40 -06:00
2010-05-27 11:35:40 -06:00
def _alloc_byte_buffer(ptr, count):
return _allocate_array(ptr, c_byte, count)
2010-05-25 14:37:40 -06:00
_byte_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_byte)), c_uint)
2010-05-27 11:35:40 -06:00
alloc_byte_buffer = _byte_functype(_alloc_byte_buffer)
def _alloc_ubyte_buffer(ptr, count):
return _allocate_array(ptr, c_ubyte, count)
2010-05-25 14:37:40 -06:00
_ubyte_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_ubyte)), c_uint)
2010-05-27 11:35:40 -06:00
alloc_ubyte_buffer = _ubyte_functype(_alloc_ubyte_buffer)
2010-05-25 14:37:40 -06:00
2010-05-27 11:35:40 -06:00
def _alloc_char_buffer(ptr, count):
2010-05-25 14:37:40 -06:00
c = create_string_buffer(count)
p = cast(c, POINTER(c_char))
2010-05-25 14:37:40 -06:00
ptr[0] = p
pointer_dict[id(ptr[0])] = (ptr, c, p)
2010-05-25 14:37:40 -06:00
2010-05-27 11:35:40 -06:00
return 1
2010-05-25 14:37:40 -06:00
_char_functype = CFUNCTYPE(c_int, POINTER(POINTER(c_char)), c_uint)
2010-05-27 11:35:40 -06:00
alloc_char_buffer = _char_functype(_alloc_char_buffer)