1 | module m_wxml_dictionary |
---|
2 | |
---|
3 | private |
---|
4 | ! |
---|
5 | ! A very rough implementation for now |
---|
6 | ! It uses fixed-length buffers for key/value pairs, |
---|
7 | ! and the maximum number of dictionary items is hardwired. |
---|
8 | |
---|
9 | integer, parameter, private :: MAX_ITEMS = 30 |
---|
10 | type, public :: wxml_dictionary_t |
---|
11 | private |
---|
12 | integer :: number_of_items ! = 0 |
---|
13 | character(len=100), dimension(MAX_ITEMS) :: key |
---|
14 | character(len=100), dimension(MAX_ITEMS) :: value |
---|
15 | end type wxml_dictionary_t |
---|
16 | |
---|
17 | ! |
---|
18 | ! Building procedures |
---|
19 | ! |
---|
20 | public :: add_key_to_dict, add_value_to_dict, reset_dict |
---|
21 | |
---|
22 | ! |
---|
23 | ! Query and extraction procedures |
---|
24 | ! |
---|
25 | public :: len |
---|
26 | interface len |
---|
27 | module procedure number_of_entries |
---|
28 | end interface |
---|
29 | public :: number_of_entries |
---|
30 | public :: get_key |
---|
31 | public :: get_value |
---|
32 | public :: has_key |
---|
33 | public :: print_dict |
---|
34 | ! |
---|
35 | interface get_value |
---|
36 | module procedure wxml_get_value |
---|
37 | end interface |
---|
38 | |
---|
39 | CONTAINS |
---|
40 | |
---|
41 | !------------------------------------------------------ |
---|
42 | function number_of_entries(dict) result(n) |
---|
43 | type(wxml_dictionary_t), intent(in) :: dict |
---|
44 | integer :: n |
---|
45 | |
---|
46 | n = dict%number_of_items |
---|
47 | |
---|
48 | end function number_of_entries |
---|
49 | |
---|
50 | !------------------------------------------------------ |
---|
51 | function has_key(dict,key) result(found) |
---|
52 | type(wxml_dictionary_t), intent(in) :: dict |
---|
53 | character(len=*), intent(in) :: key |
---|
54 | logical :: found |
---|
55 | |
---|
56 | integer :: n, i |
---|
57 | found = .false. |
---|
58 | n = dict%number_of_items |
---|
59 | do i = 1, n |
---|
60 | if (dict%key(i) == key) then |
---|
61 | found = .true. |
---|
62 | exit |
---|
63 | endif |
---|
64 | enddo |
---|
65 | end function has_key |
---|
66 | |
---|
67 | !------------------------------------------------------ |
---|
68 | subroutine wxml_get_value(dict,key,value,status) |
---|
69 | type(wxml_dictionary_t), intent(in) :: dict |
---|
70 | character(len=*), intent(in) :: key |
---|
71 | character(len=*), intent(out) :: value |
---|
72 | integer, intent(out) :: status |
---|
73 | ! |
---|
74 | integer :: n, i |
---|
75 | |
---|
76 | status = -1 |
---|
77 | n = dict%number_of_items |
---|
78 | do i = 1, n |
---|
79 | if (dict%key(i) == key) then |
---|
80 | value = dict%value(i) |
---|
81 | status = 0 |
---|
82 | RETURN |
---|
83 | endif |
---|
84 | enddo |
---|
85 | |
---|
86 | end subroutine wxml_get_value |
---|
87 | |
---|
88 | !------------------------------------------------------ |
---|
89 | subroutine get_key(dict,i,key,status) |
---|
90 | ! |
---|
91 | ! Get the i'th key |
---|
92 | ! |
---|
93 | type(wxml_dictionary_t), intent(in) :: dict |
---|
94 | integer, intent(in) :: i |
---|
95 | character(len=*), intent(out) :: key |
---|
96 | integer, intent(out) :: status |
---|
97 | |
---|
98 | if (i <= dict%number_of_items) then |
---|
99 | key = dict%key(i) |
---|
100 | status = 0 |
---|
101 | else |
---|
102 | key = "" |
---|
103 | status = -1 |
---|
104 | endif |
---|
105 | |
---|
106 | end subroutine get_key |
---|
107 | |
---|
108 | !------------------------------------------------------ |
---|
109 | subroutine add_key_to_dict(key,dict) |
---|
110 | character(len=*), intent(in) :: key |
---|
111 | type(wxml_dictionary_t), intent(inout) :: dict |
---|
112 | |
---|
113 | integer :: n |
---|
114 | |
---|
115 | n = dict%number_of_items |
---|
116 | if (n == MAX_ITEMS) then |
---|
117 | write(unit=0,fmt=*) "Dictionary capacity exceeded !" |
---|
118 | RETURN |
---|
119 | endif |
---|
120 | |
---|
121 | n = n + 1 |
---|
122 | dict%key(n) = key |
---|
123 | dict%number_of_items = n |
---|
124 | |
---|
125 | end subroutine add_key_to_dict |
---|
126 | |
---|
127 | !------------------------------------------------------ |
---|
128 | ! Assumes we build the dictionary in an orderly fashion, |
---|
129 | ! so one adds first the key and then immediately afterwards the value. |
---|
130 | ! |
---|
131 | subroutine add_value_to_dict(value,dict) |
---|
132 | character(len=*), intent(in) :: value |
---|
133 | type(wxml_dictionary_t), intent(inout) :: dict |
---|
134 | |
---|
135 | integer :: n |
---|
136 | |
---|
137 | n = dict%number_of_items |
---|
138 | dict%value(n) = value |
---|
139 | |
---|
140 | end subroutine add_value_to_dict |
---|
141 | |
---|
142 | !------------------------------------------------------ |
---|
143 | subroutine reset_dict(dict) |
---|
144 | type(wxml_dictionary_t), intent(inout) :: dict |
---|
145 | |
---|
146 | dict%number_of_items = 0 |
---|
147 | |
---|
148 | end subroutine reset_dict |
---|
149 | |
---|
150 | !------------------------------------------------------ |
---|
151 | subroutine print_dict(dict) |
---|
152 | type(wxml_dictionary_t), intent(in) :: dict |
---|
153 | |
---|
154 | integer :: i |
---|
155 | |
---|
156 | do i = 1, dict%number_of_items |
---|
157 | print *, trim(dict%key(i)), " = ", trim(dict%value(i)) |
---|
158 | enddo |
---|
159 | |
---|
160 | end subroutine print_dict |
---|
161 | |
---|
162 | end module m_wxml_dictionary |
---|