33 use,
INTRINSIC :: iso_c_binding
34 #ifdef WITH_VARYING_STRING 35 USE iso_varying_string
70 TYPE(c_ptr),
POINTER :: elem(:) => null()
71 CHARACTER(len=1),
POINTER :: buffer(:) => null()
78 MODULE PROCEDURE strlen_char, strlen_chararr, strlen_intarr, &
80 #ifdef WITH_VARYING_STRING 81 MODULE PROCEDURE strlen_var_str
104 MODULE PROCEDURE strtofchar_char, strtofchar_chararr, strtofchar_intarr, &
115 MODULE PROCEDURE c_ptr_ptr_new_from_c, c_ptr_ptr_new_from_fchar
118 INTERFACE assignment(=)
119 MODULE PROCEDURE strtofchararr_assign
120 END INTERFACE ASSIGNMENT(=)
129 PURE FUNCTION strlen_char(string) RESULT(strlen)
133 CHARACTER(kind=c_char,len=*),
INTENT(in) :: string
138 DO i = 1, len(string)
139 IF (string(i:i) == char(0))
EXIT 143 END FUNCTION strlen_char
146 PURE FUNCTION strlen_chararr(string) RESULT(strlen)
150 CHARACTER(kind=c_char,len=1),
INTENT(in) :: string(:)
155 DO i = 1,
SIZE(string)
156 IF (string(i) == char(0))
EXIT 160 END FUNCTION strlen_chararr
163 PURE FUNCTION strlen_intarr(string) RESULT(strlen)
167 INTEGER(kind=c_signed_char),
INTENT(in) :: string(:)
172 DO i = 1,
SIZE(string)
173 IF (string(i) == 0)
EXIT 177 END FUNCTION strlen_intarr
180 FUNCTION strlen_ptr(string) RESULT(strlen)
184 TYPE(c_ptr),
INTENT(in) :: string
187 INTEGER(kind=c_signed_char),
POINTER :: pstring(:)
190 IF (c_associated(string))
THEN 192 CALL c_f_pointer(string, pstring, (/huge(i)/))
194 DO i = 1,
SIZE(pstring)
195 IF (pstring(i) == 0)
EXIT 202 END FUNCTION strlen_ptr
205 #ifdef WITH_VARYING_STRING 206 PURE FUNCTION strlen_var_str(string) RESULT(strlen)
210 TYPE(varying_string),
INTENT(in) :: string
215 END FUNCTION strlen_var_str
219 FUNCTION strtofchar_char(string) RESULT(fchar)
223 CHARACTER(kind=c_char,len=*),
INTENT(in) :: string
224 CHARACTER(len=strlen(string)) :: fchar
226 fchar(:) = string(1:len(fchar))
228 END FUNCTION strtofchar_char
231 FUNCTION strtofchar_chararr(string) RESULT(fchar)
235 CHARACTER(kind=c_char,len=1),
INTENT(in) :: string(:)
236 CHARACTER(len=strlen(string)) :: fchar
241 fchar(i:i) = string(i)
244 END FUNCTION strtofchar_chararr
247 FUNCTION strtofchar_intarr(string) RESULT(fchar)
251 INTEGER(kind=c_signed_char),
INTENT(in) :: string(:)
252 CHARACTER(len=strlen(string)) :: fchar
254 fchar(:) = transfer(string(1:len(fchar)), fchar)
256 END FUNCTION strtofchar_intarr
280 FUNCTION strtofchar_ptr_2(string, fixlen) RESULT(fchar)
284 TYPE(c_ptr),
INTENT(in) :: string
285 INTEGER,
INTENT(in) :: fixlen
286 CHARACTER(len=fixlen) :: fchar
288 CHARACTER(len=fixlen),
POINTER :: pfchar
291 safelen = min(
strlen(string), fixlen)
294 IF (c_associated(string))
THEN 295 CALL c_f_pointer(string, pfchar)
296 fchar(1:safelen) = pfchar(1:safelen)
299 END FUNCTION strtofchar_ptr_2
306 FUNCTION fchartostr(fchar) RESULT(string)
310 CHARACTER(len=*),
INTENT(in) :: fchar
311 CHARACTER(kind=c_char,len=LEN(fchar)+1) :: string
313 string = fchar//char(0)
315 END FUNCTION fchartostr
323 FUNCTION fchartrimtostr(fchar) RESULT(string)
327 CHARACTER(len=*),
INTENT(in) :: fchar
328 CHARACTER(kind=c_char,len=LEN_TRIM(fchar)+1) :: string
330 string = trim(fchar)//char(0)
332 END FUNCTION fchartrimtostr
335 SUBROUTINE strtofchararr_assign(fchar, string)
339 CHARACTER(kind=c_char,len=1),
ALLOCATABLE,
INTENT(out) :: fchar(:)
340 TYPE(c_ptr),
INTENT(in) :: string
342 CHARACTER(kind=c_char),
POINTER :: pstring(:)
346 CALL c_f_pointer(string, pstring, (/l/))
348 fchar(:) = pstring(:)
350 END SUBROUTINE strtofchararr_assign
359 FUNCTION c_ptr_ptr_new_from_c(c_ptr_ptr_c) RESULT(this)
363 TYPE(c_ptr),
VALUE :: c_ptr_ptr_c
367 TYPE(c_ptr),
POINTER :: charp(:)
369 IF (c_associated(c_ptr_ptr_c))
THEN 371 CALL c_f_pointer(c_ptr_ptr_c, charp, (/huge(1)/))
372 DO i = 1,
SIZE(charp)
373 IF (.NOT.c_associated(charp(i)))
THEN 374 CALL c_f_pointer(c_ptr_ptr_c, this%elem, (/i/))
379 END FUNCTION c_ptr_ptr_new_from_c
388 FUNCTION c_ptr_ptr_new_from_fchar(fchar) RESULT(this)
389 CHARACTER(len=*) :: fchar(:)
392 INTEGER :: i, j, totlen
395 DO i = 1,
SIZE(fchar)
396 totlen = totlen + len_trim(fchar(i)) + 1
398 ALLOCATE(this%buffer(totlen), this%elem(
SIZE(fchar) + 1))
400 DO i = 1,
SIZE(fchar)
401 this%elem(i) = c_loc(this%buffer(totlen))
402 DO j = 1, len_trim(fchar(i))
403 this%buffer(totlen) = fchar(i)(j:j)
406 this%buffer(totlen) = char(0)
409 this%elem(i) = c_null_ptr
411 END FUNCTION c_ptr_ptr_new_from_fchar
417 FUNCTION c_ptr_ptr_getsize(this)
422 INTEGER :: c_ptr_ptr_getsize
424 IF (
ASSOCIATED(this%elem))
THEN 425 c_ptr_ptr_getsize =
SIZE(this%elem) - 1
427 c_ptr_ptr_getsize = 0
430 END FUNCTION c_ptr_ptr_getsize
440 FUNCTION c_ptr_ptr_getptr(this, n)
445 INTEGER,
INTENT(in) :: n
446 TYPE(c_ptr) :: c_ptr_ptr_getptr
448 c_ptr_ptr_getptr = c_null_ptr
449 IF (
ASSOCIATED(this%elem))
THEN 450 IF (n > 0 .AND. n <=
SIZE(this%elem))
THEN 451 c_ptr_ptr_getptr = this%elem(n)
455 END FUNCTION c_ptr_ptr_getptr
461 FUNCTION c_ptr_ptr_getobject(this)
463 TYPE(c_ptr) :: c_ptr_ptr_getobject
465 c_ptr_ptr_getobject = c_null_ptr
466 IF (
ASSOCIATED(this%elem))
THEN 467 c_ptr_ptr_getobject = c_loc(this%elem(1))
470 END FUNCTION c_ptr_ptr_getobject
Fortran derived type for handling void**, char**, etc C objects (pointer to pointer or array of point...
Constructor for a c_ptr_ptr object.
Utility module for supporting Fortran 2003 C language interface module.
Equivalent of the strlen C function.
Convert a null-terminated C string into a Fortran CHARACTER variable of the proper length...