我能够在 Python 中成功调用带有 ctypes 的函数。我现在有一个缓冲区,里面装满了我要提取的数据结构。最好的策略是什么?还有什么我应该发布的吗?
功能:
class list():
def __init__(self):
#[...]
def getdirentries(self, path):
self.load_c()
self.fd = os.open(path, os.O_RDONLY)
self.statinfo = os.fstat(self.fd)
self.buffer = ctypes.create_string_buffer(self.statinfo.st_size)
nbytes = self.statinfo.st_size
transferred_bytes = self.libc.getdirentries(
self.fd,
ctypes.byref(self.buffer),
nbytes,
ctypes.byref(self.basep) )
#[...]
结构:
class dirent(ctypes.Structure):
_fields_ = [ ("d_fileno", ctypes.c_uint32), # /* file number of entry */
("d_reclen", ctypes.c_uint16), # /* length of this record */
("d_type", ctypes.c_uint8), # /* file type */
("d_namlen", ctypes.c_uint8), # /* length of string in d_name */
("d_name", ctypes.c_char * (MAXNAMELEN + 1) ) ]
一些输出:
传输字节:156
缓冲区大小:272
缓冲区:<ctypes.c_char_Array_272 object at 0x8c3f0>