error checking functions for geos ctypes prototype functions getting the free routine used to free the memory allocated for string pointers returned by geos checking the status code double passed in by reference return its value error checking for routines that return explicitly sized strings this frees the memory allocated by geos at the result pointer a csizet object is passed in by reference for the second argument on these routines and its needed to determine the correct size freeing the memory allocated within geos error checking for routines that return strings this frees the memory allocated by geos at the result pointer getting the string value at the pointer address freeing the memory allocated within geos getting the free routine used to free the memory allocated for string pointers returned by geos checking the status code double passed in by reference return its value error checking for routines that return explicitly sized strings this frees the memory allocated by geos at the result pointer a c_size_t object is passed in by reference for the second argument on these routines and its needed to determine the correct size freeing the memory allocated within geos error checking for routines that return strings this frees the memory allocated by geos at the result pointer getting the string value at the pointer address freeing the memory allocated within geos
from ctypes import c_void_p, string_at

from django.contrib.gis.geos.error import GEOSException
from django.contrib.gis.geos.libgeos import GEOSFuncFactory



free = GEOSFuncFactory("GEOSFree")
free.argtypes = [c_void_p]


def last_arg_byref(args):
    "Return the last C argument's value by reference."
    return args[-1]._obj.value


def check_dbl(result, func, cargs):
    "Check the status code and returns the double value passed in by reference."
    
    if result != 1:
        return None
    
    return last_arg_byref(cargs)


def check_geom(result, func, cargs):
    "Error checking on routines that return Geometries."
    if not result:
        raise GEOSException(
            'Error encountered checking Geometry returned from GEOS C function "%s".'
            % func.__name__
        )
    return result


def check_minus_one(result, func, cargs):
    "Error checking on routines that should not return -1."
    if result == -1:
        raise GEOSException(
            'Error encountered in GEOS C function "%s".' % func.__name__
        )
    else:
        return result


def check_predicate(result, func, cargs):
    "Error checking for unary/binary predicate functions."
    if result == 1:
        return True
    elif result == 0:
        return False
    else:
        raise GEOSException(
            'Error encountered on GEOS C predicate function "%s".' % func.__name__
        )


def check_sized_string(result, func, cargs):
    
    if not result:
        raise GEOSException(
            'Invalid string pointer returned by GEOS C function "%s"' % func.__name__
        )
    
    
    
    s = string_at(result, last_arg_byref(cargs))
    
    free(result)
    return s


def check_string(result, func, cargs):
    
    if not result:
        raise GEOSException(
            'Error encountered checking string return value in GEOS C function "%s".'
            % func.__name__
        )
    
    s = string_at(result)
    
    free(result)
    return s