Module RedisInterface
    ! use iso_c_binding, only: c_char
    Interface
        SUBROUTINE addnums(a, b) BIND(C,name='addnums')
            USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT
            IMPLICIT NONE
            INTEGER(C_INT) :: a, b
        END SUBROUTINE addnums

        SUBROUTINE initConnection() BIND(C, name='initConnection')
        END SUBROUTINE initConnection
        
        SUBROUTINE setData_C(str) BIND(C,name='setData')
            use iso_c_binding, only: c_char
            character(kind=c_char) :: str(*)
        END SUBROUTINE setData_C

        FUNCTION getData_C(len) BIND(C,name='getData')
            use iso_c_binding, only: c_ptr,c_int
            integer(kind=c_int)::len
            type(c_ptr) :: getData_C
        END FUNCTION getData_C

        SUBROUTINE deallocateData() BIND(C, name='deallocData')
        END SUBROUTINE deallocateData

        End Interface

    contains

        SUBROUTINE setData(str)
            use iso_c_binding, only: c_null_char
            character(len=*):: str
            character(len=len_trim(str)+1)::c_str
            c_str = str//c_null_char
            call setData_C(c_str)
        END SUBROUTINE setData

        SUBROUTINE getData(string) !result(string)
            use iso_c_binding, only: c_char,c_ptr,c_f_pointer
            ! character(:),allocatable :: getData2
            character(:),allocatable :: string
            integer :: string_shape(1)
            type(c_ptr) :: c_string
            integer::l
            ! print *,"reading data (getData2)"
            c_string = getData_C(l)
            ! string = c_str
            ! len = int(c_len,kind=kind(len))
            string_shape(1) =   int(l,kind=kind(integer))
            if(.not. allocated(string)) allocate(character(l) :: string)
            call c_f_pointer(c_string, string, string_shape)
            ! print *,len_trim(string), "chars read."
            ! print *,string(1:l)
            ! getData2=string
        END SUBROUTINE getData

END Module RedisInterface