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

#### 7.6.3 Wisdom Generic Export/Import from Fortran

The most generic wisdom export/import functions allow you to provide an arbitrary callback function to read/write one character at a time in any way you want. However, your callback function must be written in a special way, using the `bind(C)` attribute to be passed to a C interface.

In particular, to call the generic wisdom export function `fftw_export_wisdom`, you would write a callback subroutine of the form:

      subroutine my_write_char(c, p) bind(C)
        use, intrinsic :: iso_c_binding
        character(C_CHAR), value :: c
        type(C_PTR), value :: p
        ...write c...
      end subroutine my_write_char

Given such a subroutine (along with the corresponding interface definition), you could then export wisdom using:

      call fftw_export_wisdom(c_funloc(my_write_char), p)

The standard `c_funloc` intrinsic converts a Fortran `bind(C)` subroutine into a C function pointer. The parameter `p` is a `type(C_PTR)` to any arbitrary data that you want to pass to `my_write_char` (or `C_NULL_PTR` if none). (Note that you can get a C pointer to Fortran data using the intrinsic `c_loc`, and convert it back to a Fortran pointer in `my_write_char` using `c_f_pointer`.)

Similarly, to use the generic `fftw_import_wisdom`, you would define a callback function of the form:

      integer(C_INT) function my_read_char(p) bind(C)
        use, intrinsic :: iso_c_binding
        type(C_PTR), value :: p
        character :: c
        ...read a character c...
        my_read_char = ichar(c, C_INT)
      end function my_read_char

      ....

      integer(C_INT) :: ret
      ret = fftw_import_wisdom(c_funloc(my_read_char), p)
      if (ret .eq. 0) stop 'error importing wisdom'

Your function can return `-1` if the end of the input is reached. Again, `p` is an arbitrary `type(C_PTR` that is passed through to your function. `fftw_import_wisdom` returns `0` if an error occurred and nonzero otherwise.

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