Line data Source code
1 : !> \file read_write.f90
2 : !! \brief Using dictionary_t to store a configuration file
3 :
4 : !> Read a "equal sign" separated input file
5 : !!
6 : !! Usage:
7 : !! ./read_write name_of_input_file [dict_size]
8 : !!
9 : !! dict_size is an optional integer argument that sets the size of the hash table. The
10 : !! default value is 1024.
11 : !!
12 : !! Output: the program returns one line per key, with the key and the value separated by an
13 : !! equal sign.
14 :
15 1 : program use_ssdm
16 1 : use dictionary_m
17 : implicit none
18 :
19 1 : type(dictionary_t) :: b
20 : character(len=128) :: line
21 2 : character(len=:), allocatable :: k, v, fname
22 : integer :: iostat, eq_location, funit, dict_size
23 :
24 1 : if (command_argument_count() < 1) then
25 0 : stop 'missing argument for parameter file'
26 : end if
27 :
28 1 : call get_command_argument(1, line)
29 1 : fname = trim(adjustl(line))
30 :
31 1 : if (command_argument_count() < 2) then
32 1 : dict_size = 1024
33 : else
34 0 : call get_command_argument(2, line)
35 0 : read(line, *) dict_size
36 : end if
37 :
38 1 : call b%init(dict_size)
39 :
40 1 : open(file=fname, newunit=funit)
41 9 : read_loop: do
42 10 : read(funit, '(a)', iostat=iostat) line
43 10 : if (iostat < 0) exit read_loop
44 9 : if (iostat > 0) cycle read_loop
45 9 : eq_location = index(line, '=')
46 9 : if (eq_location == 0) cycle read_loop
47 :
48 4 : k = trim(adjustl(line(1:eq_location-1)))
49 4 : v = trim(adjustl(line(eq_location+1:)))
50 :
51 4 : if (len(k) ==0) then
52 0 : write(*,*) 'empty key while reading data'
53 0 : stop
54 : end if
55 4 : if (len(v) ==0) then
56 0 : write(*,*) 'empty value while reading data'
57 0 : stop
58 : end if
59 :
60 4 : call b%set(k, v)
61 :
62 : end do read_loop
63 :
64 1 : close(funit)
65 :
66 2 : call fileshow(b)
67 :
68 : contains
69 :
70 1 : subroutine fileshow(d)
71 : class(dictionary_t), intent(in) :: d
72 :
73 : integer :: i, j, s
74 :
75 1025 : do i = 1, d%dict_size
76 1024 : s = d%buckets(i)%current_idx
77 1024 : if (s > 0) then
78 8 : do j = 1, s
79 4 : write(*,'(a)', advance='no') d%buckets(i)%entries(j)%key
80 4 : write(*,'(a)', advance='no') ' = '
81 4 : write(*,'(a)', advance='no') d%buckets(i)%entries(j)%value
82 4 : write(*,*)
83 : end do
84 : end if
85 : end do
86 :
87 1 : end subroutine fileshow
88 :
89 : end program use_ssdm
|