Nahuel Defosse, 24.11.2010 13:28:
> I want to call a C function which accepts 2 char * pointers, like this:
>
> int my_function( char *start, char *end);
>
> These pointers hold the start and end position of a long string which
> has to be splited.
I assume your data is held in a Python byte string?
> Then, there's another function which will return slices of a initial
> call to my_funciton, ala strtok.
>
> int my_iterate( char **start, char **end);
>
> How do I define a Python class which can hold this strucutre, and
> iterate over it? I can't figure out how to handle this kind of pointers
> in Cython.
Maybe something like this would work:
cdef class MyIterator:
cdef bytes data
cdef char* start
cdef char* end
def __cinit__(self, bytes data):
self.data = data # keep the object alive
self.start = data
self.end = self.start + len(data) # potential off-by-1
if not my_function(self.start, self.end):
raise ValueError("foutu") # or whatever
def __iter__(self):
return self
def __next__(self):
if not my_iterate(&self.start, &self.end):
raise StopIteration
return self.start[:self.end - self.start] # potential off-by-1
I likely misinterpreted the return values of the two functions, but I hope
you get the idea.
Stefan