keyvalue fortran routine

version 4.6 : Feb 24, 2006
version 2 : Sep. 15, 2004.

'Keyvalue' can read

logical, integer, real, character values vector of integer and real block structures with minimum programing efforts. You can set default values. case insensitive.

a sample input file

nsite 1 < - integer
Coulomb_energy 2.0 < - real
calc_physical_properties true < - logical
lattice_set 1 2 3 4 5 6 7 < - integer vector
temperature_set 1.0 2.0 3.0 4.0 < - real vector

<block_foo> < - block structure
test
test22
</block_foo>

## scf_method simple #< - comment

filename1 test1.dat all text < - read only the first element
title beta'-ET2I3 12kbar parameter set #1 < - read all the element

filename2 Kondo.dat
mixing_parameter 0.001

a sample program (read from stdin)

      open(fid,status='scratch')

c     read from stdin and write to the scratch file
      do while(.true.)
         read(5,'(a200)',end=501) ckey
         write(fid,'(a200)') ckey
      enddo
 501  continue

      call getkeyvalue(fid,"nsite",ikey)
      write(*,*) 'nsite=',ikey

      call getkeyvalue(fid,"title",ckey,mode='all')
      write(*,*) 'buf=',ckey(:len_trim(ckey))

      call getkeyblock(fid,"<block_foo>", unit=unit, n=ikey, status=ret)
      write(*,*) "<block_foo>",ret, ikey, unit
      do i=1,ikey
         read(unit,'(a80)') ckey
         write(*,*)ckey(:len_trim(ckey))
      enddo
      close(unit)   ! <= do not forget closing it.

      call  getkeyvalue_printunknown(fid)  !<= print unknown keywords in fid

      close(fid)    ! <= close the scratch file

a sample program (read from file(s))

      filename='IN'
      call getkeyvalue(filename,"nsite",ikey,status=ret)  !<= integer
      write(*,*) "nsite",ret,ikey

      call getkeyvalue(filename,"coulomb_energy",rkey,status=ret) !<= real
      write(*,*) "coulomb_energy",ret,rkey

      call getkeyvalue(filename,"calc_physical_properties",lkey,status=ret ) !<= logical
      write(*,*) "calc_physical_properties",ret,lkey

      call getkeyvalue(filename,"filename1",ckey,status=ret) !<= a word
      write(*,*) "filename1",ret,ckey(:len_trim(ckey))

      call getkeyvalue(filename,"title",ckey,status=ret,mode='all') !<= all the words
      write(*,*) "title",ret,ckey(:len_trim(ckey))
      call getkeyvalue(filename,"real2",rkey,status=ret)
      write(*,*) "real2",ret,rkey

      call  getkeyvalue_printunknown(filename)  !<= print unknown keywords in filename


result of the sample program

open fid=          10
 nsite=          -1 val=           0
 val=beta'
 getkeyvalue_block_direct: can not find key <block_foo>
 <block_foo>          -1           0          -1
 closing fid=          -1
 fid=          10
fileID=  10  unknown keyword=(int)
fileID=  10  unknown keyword=(real)
fileID=  10  unknown keyword=(intv)
fileID=  10  unknown keyword=(realv)
fileID=  10  unknown keyword=(<block>)
fileID=  10  unknown keyword=(str)
fileID=  10  unknown keyword=(logical)
fileID=  10  unknown keyword=(int2)
fileID=  10  unknown keyword=(real2)
 int           1 val=         112
 real           1 val=  -2.00110000000000    
 logical           1 val= T
 str           1 val=string
 title           1 val=beta' ET2I3  12kbar
 intv           5 val=           1           2           3           4
           5
 realv           3 val= -0.100000000000000      -0.200000000000000    
 -0.300000000000000    
 target_temperature_set           0 val=  -1.00000000000000    
  -2.00000000000000       -3.00000000000000    
 real3           0 val=   100.010000000000    
 <block>           1  n=           2  unit=          11
 block1st
 block2end
 close unit=          11
 final temperature           0 val=   500.000000000000    
 velocity           0 val=  -500.000000000000  
 file=IN, unknown keyword=(int2)
 file=IN, unknown keyword=(real2)

a program and sample files

keyvaluev46.f
keyvalue_containerv1.f
IN (an input file)

% a.out < IN

comments: I wrote it in the fixed format. Use cpp and long column options to complie. A main program is in #if 1 ... #endif section

Inputtool version 2 for C

Inputtools2.c