Octopus
varinfo.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
21module varinfo_oct_m
22 use iso_c_binding
23 use, intrinsic :: iso_fortran_env
24 use string_oct_m
25
26 implicit none
27
28 private
29 public :: &
38
39 interface varinfo_valid_option
40 module procedure varinfo_valid_option_8
41 module procedure varinfo_valid_option_4
42 end interface varinfo_valid_option
43
44 interface
45 subroutine varinfo_init(filename)
46 implicit none
47 character(len=*), intent(in) :: filename
48 end subroutine varinfo_init
49
50 subroutine varinfo_getvar(name, var)
51 use iso_c_binding
52 implicit none
53 character(len=*), intent(in) :: name
54 type(c_ptr), intent(inout) :: var
55 end subroutine varinfo_getvar
56
57 subroutine varinfo_getinfo(var, name, type, default, section, desc)
58 use iso_c_binding
59 implicit none
60 type(c_ptr), intent(in) :: var
61 type(c_ptr), intent(out) :: name
62 type(c_ptr), intent(out) :: type
63 type(c_ptr), intent(out) :: default
64 type(c_ptr), intent(out) :: section
65 type(c_ptr), intent(out) :: desc
66 end subroutine varinfo_getinfo
67
68 subroutine varinfo_getopt(var, opt)
69 use iso_c_binding
70 implicit none
71 type(c_ptr), intent(in) :: var
72 type(c_ptr), intent(inout) :: opt
73 end subroutine varinfo_getopt
74
75 subroutine varinfo_opt_getinfo(opt, name, val, desc)
76 use iso_c_binding
77 use, intrinsic :: iso_fortran_env
78 implicit none
79 type(c_ptr), intent(in) :: opt
80 type(c_ptr), intent(out) :: name
81 integer(int64), intent(out) :: val
82 type(c_ptr), intent(out) :: desc
83 end subroutine varinfo_opt_getinfo
84
85 subroutine varinfo_search_var(name, var)
86 use iso_c_binding
87 implicit none
88 character(len=*), intent(in) :: name
89 type(c_ptr), intent(inout) :: var
90 end subroutine varinfo_search_var
91
92 subroutine varinfo_search_option(var, name, val, ierr)
93 use iso_c_binding
94 implicit none
95 type(c_ptr), intent(in) :: var
96 character(len=*), intent(in) :: name
97 integer(c_int), intent(out) :: val
98 integer(c_int), intent(out) :: ierr
99 end subroutine varinfo_search_option
100
101 subroutine varinfo_end()
102 implicit none
103 end subroutine varinfo_end
104 end interface
105
106
107contains
108
109 ! ---------------------------------------------------------
110 subroutine varinfo_print(iunit, var, ierr)
111 integer, intent(in) :: iunit
112 character(len=*), intent(in) :: var
113 integer,optional, intent(out):: ierr
115 type(c_ptr) :: handle, opt, name, type, default, section, desc
116 integer(int64) :: val
117 logical :: first
118
119 handle = c_null_ptr
120 call varinfo_getvar(var, handle)
121 if (.not. c_associated(handle)) then
122 if (present(ierr)) then
123 ierr = -1
124 return
125 else
126 write(iunit, '(3a)') 'ERROR: Could not find a variable named ', trim(var), '.'
127 stop
128 end if
129 end if
130
131 if (present(ierr)) ierr = 0
132 call varinfo_getinfo(handle, name, type, default, section, desc)
133
134 call print_c_string(iunit, name, "Variable: ")
135 call print_c_string(iunit, type, "Type: ")
136 call print_c_string(iunit, default, "Default: ")
137 call print_c_string(iunit, section, "Section: ")
138 write(iunit, '(a)') "Description:"
139 call print_c_string(iunit, desc, " ")
140
141 opt = c_null_ptr
142 first = .true.
143 do
144 call varinfo_getopt(handle, opt)
145 if (.not. c_associated(opt)) then
146 exit
147 else
148 if (first) then
149 write(iunit, '(a)') "Available options:"
150 first = .false.
151 end if
152 call varinfo_opt_getinfo(opt, name, val, desc)
153 call print_c_string(iunit, name, " ")
154 call print_c_string(iunit, desc, " ")
155 end if
156 end do
157
158 end subroutine varinfo_print
159
160
161 ! ---------------------------------------------------------
162 logical function varinfo_valid_option_8(var, option, is_flag) result(l)
163 character(len=*), intent(in) :: var
164 integer(int64), intent(in) :: option
165 logical, optional, intent(in) :: is_flag
166
167 type(c_ptr) :: handle, opt, name, desc
168 integer(int64) :: val, option_
169 logical :: is_flag_
170
171 is_flag_ = .false.
172 if (present(is_flag)) is_flag_ = is_flag
173 option_ = option ! copy that we can change
174
175 l = .false.
176
177 handle = c_null_ptr
178 call varinfo_getvar(var, handle)
179 if (.not. c_associated(handle)) then
180 write(error_unit, '(3a)') 'ERROR: Could not find a variable named ', trim(var), '.'
181 stop
182 end if
183
184 opt = c_null_ptr
185 do
186 call varinfo_getopt(handle, opt)
187 if (.not. c_associated(opt)) exit
188 call varinfo_opt_getinfo(opt, name, val, desc)
189
190 if (is_flag_) then
191 option_ = iand(option_, not(val))
192 else
193 if (val == option_) then
194 l = .true.
195 return
196 end if
197 end if
198
199 end do
200
201 if (is_flag_ .and. (option_ == 0)) l = .true.
202
204
205 ! ---------------------------------------------------------
206
207 logical function varinfo_valid_option_4(var, option, is_flag) result(l)
208 character(len=*), intent(in) :: var
209 integer, intent(in) :: option
210 logical, optional, intent(in) :: is_flag
211
212 l = varinfo_valid_option_8(var, int(option, int64), is_flag)
213
214 end function varinfo_valid_option_4
215
216 ! ---------------------------------------------------------
217 subroutine varinfo_print_option(iunit, var, option, pre)
218 integer, intent(in) :: iunit
219 character(len=*), intent(in) :: var
220 integer, intent(in) :: option
221 character(len=*), intent(in), optional :: pre
222
223 type(c_ptr) :: handle, opt, name, desc
224 integer(int64) :: val
225 logical :: option_found
226
227 call varinfo_getvar(var, handle)
228 if (.not. c_associated(handle)) then
229 write(iunit, '(3a)') 'ERROR: Could not find a variable named ', trim(var), '.'
230 stop
231 end if
232
233 option_found = .false.
234 opt = c_null_ptr
235 do
236 call varinfo_getopt(handle, opt)
237 if (.not. c_associated(opt)) exit
238
239 call varinfo_opt_getinfo(opt, name, val, desc)
240
241 if (val == int(option, int64)) then
242 option_found = .true.
243 exit
244 end if
245 end do
246
247 write(iunit, '(4a)', advance='no') "Input:", ' [', var, ' = '
248
249 if (option_found) then
250 call print_c_string(iunit, name, advance='no')
251 else
252 write(iunit,'(i6,a)', advance='no') option, " (INVALID)"
253 end if
254 write(iunit, '(a)', advance='no') ']'
255 if (present(pre)) then
256 write(iunit, '(3a)') ' (', trim(pre), ')'
257 else
258 write(iunit, '(1x)')
259 end if
260 ! uncomment to print the description of the options
261 !call print_C_string(iunit, desc, pre=' > ')
262
263 if (.not. option_found) then
264 ! we cannot use messages here :-(
265 write(iunit,'(a,i6,2a)') "ERROR: invalid option ", option, " for variable ", trim(var)
266 stop
267 end if
268
269 end subroutine varinfo_print_option
270
271 ! ---------------------------------------------------------
272 subroutine varinfo_search(iunit, var, ierr)
273 integer, intent(in) :: iunit
274 character(len=*), intent(in) :: var
275 integer,optional, intent(out):: ierr
276
277 type(c_ptr) :: handle, name, type, default, section, desc
278
279 handle = c_null_ptr
280 if (present(ierr)) ierr = -1
281 do
282 call varinfo_search_var(var, handle)
283
284 if (c_associated(handle)) then
285 if (present(ierr)) ierr = 0
286 else
287 exit
288 end if
289
290 call varinfo_getinfo(handle, name, type, default, section, desc)
291 call print_c_string(iunit, name)
292
293 end do
294
295 end subroutine varinfo_search
296
297 ! ---------------------------------------------------------
298 integer function varinfo_option(var, option) result(val)
299 character(len=*), intent(in) :: var
300 character(len=*), intent(in) :: option
301
302 type(c_ptr) :: handle
303 integer :: ierr
304
305 handle = c_null_ptr
306 call varinfo_getvar(var, handle)
307 call varinfo_search_option(handle, option, val, ierr)
308
309 if (ierr /= 0) then
310 ! we cannot use messages here :-(
311 write(error_unit,'(4a)') "ERROR: invalid option ", trim(option), " for variable ", trim(var)
312 stop
313 end if
314
315 end function varinfo_option
316
317 ! ----------------------------------------------------------
318
319 logical function varinfo_exists(var) result(exists)
320 character(len=*), intent(in) :: var
321
322 type(c_ptr) :: handle
323
324 handle = c_null_ptr
325
326 call varinfo_search_var(var, handle)
327
328 exists = c_associated(handle)
329
330 end function varinfo_exists
331
332
333end module varinfo_oct_m
334
335!! Local Variables:
336!! mode: f90
337!! coding: utf-8
338!! End:
subroutine, public print_c_string(iunit, str, pre, advance)
prints the C string given by the pointer str
Definition: string.F90:202
subroutine, public varinfo_print_option(iunit, var, option, pre)
Definition: varinfo.F90:311
logical function varinfo_valid_option_8(var, option, is_flag)
Definition: varinfo.F90:256
subroutine, public varinfo_print(iunit, var, ierr)
Definition: varinfo.F90:204
logical function varinfo_valid_option_4(var, option, is_flag)
Definition: varinfo.F90:301
subroutine, public varinfo_search(iunit, var, ierr)
Definition: varinfo.F90:366
logical function, public varinfo_exists(var)
Definition: varinfo.F90:413
integer function, public varinfo_option(var, option)
Definition: varinfo.F90:392
int true(void)