1c     Copyright (c) 2003, 2007-14 Matteo Frigo
2c     Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
3c
4c     This program is free software; you can redistribute it and/or modify
5c     it under the terms of the GNU General Public License as published by
6c     the Free Software Foundation; either version 2 of the License, or
7c     (at your option) any later version.
8c
9c     This program is distributed in the hope that it will be useful,
10c     but WITHOUT ANY WARRANTY; without even the implied warranty of
11c     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12c     GNU General Public License for more details.
13c
14c     You should have received a copy of the GNU General Public License
15c     along with this program; if not, write to the Free Software
16c     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17c
18cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
19c
20c     This is an example implementation of Fortran wisdom export/import
21c     to/from a Fortran unit (file), exploiting the generic
22c     dfftw_export_wisdom/dfftw_import_wisdom functions.
23c
24c     We cannot compile this file into the FFTW library itself, lest all
25c     FFTW-calling programs be required to link to the Fortran I/O
26c     libraries.
27c
28cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
29
30c     Strictly speaking, the '$' format specifier, which allows us to
31c     write a character without a trailing newline, is not standard F77.
32c     However, it seems to be a nearly universal extension.
33      subroutine write_char(c, iunit)
34      character c
35      integer iunit
36      write(iunit,321) c
37 321  format(a,$)
38      end
39
40      subroutine export_wisdom_to_file(iunit)
41      integer iunit
42      external write_char
43      call dfftw_export_wisdom(write_char, iunit)
44      end
45
46c     Fortran 77 does not have any portable way to read an arbitrary
47c     file one character at a time.  The best alternative seems to be to
48c     read a whole line into a buffer, since for fftw-exported wisdom we
49c     can bound the line length.  (If the file contains longer lines,
50c     then the lines will be truncated and the wisdom import should
51c     simply fail.)  Ugh.
52      subroutine read_char(ic, iunit)
53      integer ic
54      integer iunit
55      character*256 buf
56      save buf
57      integer ibuf
58      data ibuf/257/
59      save ibuf
60      if (ibuf .lt. 257) then
61         ic = ichar(buf(ibuf:ibuf))
62         ibuf = ibuf + 1
63         return
64      endif
65      read(iunit,123,end=666) buf
66      ic = ichar(buf(1:1))
67      ibuf = 2
68      return
69 666  ic = -1
70      ibuf = 257
71 123  format(a256)
72      end
73
74      subroutine import_wisdom_from_file(isuccess, iunit)
75      integer isuccess
76      integer iunit
77      external read_char
78      call dfftw_import_wisdom(isuccess, read_char, iunit)
79      end
80