File: derived_types_07.f90

package info (click to toggle)
lfortran 0.58.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 54,512 kB
  • sloc: cpp: 162,179; f90: 68,251; python: 17,476; ansic: 6,278; yacc: 2,334; sh: 1,317; fortran: 892; makefile: 33; javascript: 15
file content (115 lines) | stat: -rw-r--r-- 2,398 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module tomlf_type_value
   implicit none

   type, abstract :: toml_value
      character(len=:), allocatable :: key
   contains
      procedure(destroy), deferred :: destroy
      procedure :: match_key
   end type toml_value

   abstract interface
      subroutine destroy(self)
         import toml_value
         class(toml_value), intent(inout) :: self
      end subroutine destroy
   end interface

contains

    pure function match_key(self, key) result(match)
        class(toml_value), intent(in) :: self
        character(len=*), intent(in) :: key
        logical :: match

        if (allocated(self%key)) then
            match = key == self%key
        else
            match = .false.
        end if
    end function match_key

end module

module tomlf_type_keyval
   use tomlf_type_value, only : toml_value
   implicit none

   type, extends(toml_value) :: toml_keyval
      character(len=:), allocatable :: raw
   contains

      procedure :: destroy

   end type toml_keyval


contains

subroutine destroy(self)

   class(toml_keyval), intent(inout) :: self

   if (allocated(self%key)) then
      deallocate(self%key)
   end if

   if (allocated(self%raw)) then
      deallocate(self%raw)
   end if

end subroutine destroy


end module tomlf_type_keyval

module tomlf_structure_vector
   use tomlf_type_value, only : toml_value
   implicit none

   type :: toml_node

      class(toml_value), allocatable :: val

   end type toml_node

   type :: toml_vector

      integer :: n = 0
      type(toml_node), allocatable :: lst(:)

   end type toml_vector

contains

    subroutine new_vector(self, n)
        type(toml_vector), intent(out) :: self
        integer, intent(in), optional :: n

        self%n = 0
        if (present(n)) then
            allocate(self%lst(min(1, n)))
        end if
    end subroutine new_vector

    subroutine find(self, key, ptr)

        class(toml_vector), intent(inout), target :: self
        character(len=*), intent(in) :: key
        class(toml_value), pointer, intent(out) :: ptr
        integer :: i

        nullify(ptr)

        do i = 1, self%n
            if (allocated(self%lst(i)%val)) then
                if (self%lst(i)%val%match_key(key)) then
                    ptr => self%lst(i)%val
                    exit
                end if
            end if
        end do

    end subroutine find

end module tomlf_structure_vector