summaryrefslogtreecommitdiff
path: root/lib/iso_c_utilities.f90
blob: cdcc1a74981116b412f46dc2f124c661fd42205c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module iso_c_utilities

  use, intrinsic :: iso_c_binding, only: c_ptr, c_char, c_f_pointer, c_associated
  implicit none

  public :: c_to_f_string, c_f_dyn_string

  private

  character(c_char), dimension(1), save, target :: dummy_string = "?"

  interface        ! strlen is a standard C function from <string.h>
     ! int strlen(char *string)
     function strlen (string) result (len) bind (c, name="strlen")
       use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t
       implicit none
       type(c_ptr), value :: string
       integer(kind=c_size_t) :: len
     end function strlen

     ! void free(void * p)
     subroutine c_free (p) bind (c, name="free")
       use, intrinsic :: iso_c_binding, only: c_ptr
       implicit none
       type(c_ptr), value :: p
     end subroutine c_free
  end interface

contains

  !
  ! Cast C string pointer to Fortran string pointer
  !
  ! Warning! - C data must outlive result scope
  !
  function c_to_f_string (c_str) result (f_str)
    use, intrinsic :: iso_c_binding, only: c_ptr, c_f_pointer, c_char
    implicit none
    type(c_ptr), intent(in) :: c_str
    character(kind=c_char, len=:), pointer :: f_str
    character(kind=c_char), pointer :: arr(:)
    interface        ! strlen is a standard C function from <string.h>
       ! int strlen(char *string)
       function strlen (string) result (len) bind (c, name="strlen")
         use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t
         implicit none
         type(c_ptr), value :: string
         integer(kind=c_size_t) :: len
       end function strlen
    end interface
    call c_f_pointer (c_str, arr, [strlen (c_str)])
    call get_scalar_pointer (size (arr), arr, f_str)
  end function c_to_f_string

  subroutine get_scalar_pointer (scalar_len, scalar, fptr)
    ! Convert a null-terminated C string into a Fortran character pointer
    use, intrinsic :: iso_c_binding, only: c_char
    integer, intent(in) :: scalar_len
    character(kind=c_char, len=scalar_len), intent(in), target :: scalar(1)
    character(kind=c_char, len=:), pointer :: fptr
    fptr => scalar(1)
  end subroutine get_scalar_pointer

  function c_f_dyn_string (cptr) result (fstr)
    ! Convert a null-terminated malloc'ed C string into a Fortran character array
    type(c_ptr), intent(in) :: cptr ! The C address
    character(kind=c_char), allocatable :: fstr(:)
    character(kind=c_char), pointer :: fptr(:)
    interface        ! strlen is a standard C function from <string.h>
       ! void free(void * p)
       subroutine c_free (p) bind (c, name="free")
         use, intrinsic :: iso_c_binding, only: c_ptr
         implicit none
         type(c_ptr), value :: p
       end subroutine c_free
    end interface
    if (c_associated (cptr)) then
       call c_f_pointer (fptr=fptr, cptr=cptr, shape=[strlen(cptr)])
    else
       ! To avoid segfaults, associate FPTR with a dummy target:
       fptr => dummy_string
    end if
    fstr = fptr
    call c_free (cptr)
  end function c_f_dyn_string

end module