FFTW 3.3.4: Wisdom String Export/Import from Fortran

Next: Wisdom Generic Export/Import from Fortran, Previous: Wisdom File Export/Import from Fortran, Up: Accessing the wisdom API from Fortran [Contents][Index]

#### 7.6.2 Wisdom String Export/Import from Fortran

Dealing with FFTW’s C string export/import is a bit more painful. In particular, the `fftw_export_wisdom_to_string` function requires you to deal with a dynamically allocated C string. To get its length, you must define an interface to the C `strlen` function, and to deallocate it you must define an interface to C `free`:

      use, intrinsic :: iso_c_binding
      interface
        integer(C_INT) function strlen(s) bind(C, name='strlen')
          import
          type(C_PTR), value :: s
        end function strlen
        subroutine free(p) bind(C, name='free')
          import
          type(C_PTR), value :: p
        end subroutine free
      end interface

Given these definitions, you can then export wisdom to a Fortran character array:

      character(C_CHAR), pointer :: s(:)
      integer(C_SIZE_T) :: slen
      type(C_PTR) :: p
      p = fftw_export_wisdom_to_string()
      if (.not. c_associated(p)) stop 'error exporting wisdom'
      slen = strlen(p)
      call c_f_pointer(p, s, [slen+1])
      ...
      call free(p)

Note that `slen` is the length of the C string, but the length of the array is `slen+1` because it includes the terminating null character. (You can omit the ‘`+1`’ if you don’t want Fortran to know about the null character.) The standard `c_associated` function checks whether `p` is a null pointer, which is returned by `fftw_export_wisdom_to_string` if there was an error.

To import wisdom from a string, use `fftw_import_wisdom_from_string` as usual; note that the argument of this function must be a `character(C_CHAR)` that is terminated by the `C_NULL_CHAR` character, like the `s` array above.