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) bind(c)
46 use iso_c_binding
47 implicit none
48 character(kind=c_char), intent(in) :: filename(*)
49 end subroutine varinfo_init
50
51 subroutine varinfo_getvar(name, var) bind(c)
52 use iso_c_binding
53 implicit none
54 character(kind=c_char), intent(in) :: name(*)
55 type(c_ptr), intent(inout) :: var
56 end subroutine varinfo_getvar
57
58 subroutine varinfo_getinfo(var, name, type, default, section, desc)
59 use iso_c_binding
60 implicit none
61 type(c_ptr), intent(in) :: var
62 type(c_ptr), intent(out) :: name
63 type(c_ptr), intent(out) :: type
64 type(c_ptr), intent(out) :: default
65 type(c_ptr), intent(out) :: section
66 type(c_ptr), intent(out) :: desc
67 end subroutine varinfo_getinfo
68
69 subroutine varinfo_getopt(var, opt)
70 use iso_c_binding
71 implicit none
72 type(c_ptr), intent(in) :: var
73 type(c_ptr), intent(inout) :: opt
74 end subroutine varinfo_getopt
75
76 subroutine varinfo_opt_getinfo(opt, name, val, desc)
77 use iso_c_binding
78 use, intrinsic :: iso_fortran_env
79 implicit none
80 type(c_ptr), intent(in) :: opt
81 type(c_ptr), intent(out) :: name
82 integer(int64), intent(out) :: val
83 type(c_ptr), intent(out) :: desc
84 end subroutine varinfo_opt_getinfo
85
86 subroutine varinfo_search_var(name, var) bind(c)
87 use iso_c_binding
88 implicit none
89 character(kind=c_char), intent(in) :: name(*)
90 type(c_ptr), intent(inout) :: var
91 end subroutine varinfo_search_var
92
93 subroutine varinfo_search_option(var, name, val, ierr) bind(c)
94 use iso_c_binding
95 implicit none
96 type(c_ptr), intent(in) :: var
97 character(kind=c_char), intent(in) :: name(*)
98 integer, intent(out) :: val
99 integer, intent(out) :: ierr
100 end subroutine varinfo_search_option
101
102 subroutine varinfo_end()
103 implicit none
104 end subroutine varinfo_end
105 end interface
106
107
108contains
109
110 ! ---------------------------------------------------------
111 subroutine varinfo_print(iunit, var, ierr)
112 integer, intent(in) :: iunit
113 character(len=*), intent(in) :: var
114 integer,optional, intent(out):: ierr
115
116 type(c_ptr) :: handle, opt, name, type, default, section, desc
117 integer(int64) :: val
118 logical :: first
119
120 call varinfo_getvar(string_f_to_c(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, " ")
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 call varinfo_getvar(string_f_to_c(var), handle)
178 if (.not. c_associated(handle)) then
179 write(0, '(3a)') 'ERROR: Could not find a variable named ', trim(var), '.'
180 stop
181 end if
182
183 opt = c_null_ptr
184 do
185 call varinfo_getopt(handle, opt)
186 if (.not. c_associated(opt)) exit
187 call varinfo_opt_getinfo(opt, name, val, desc)
189 if (is_flag_) then
190 option_ = iand(option_, not(val))
191 else
192 if (val == option_) then
193 l = .true.
194 return
195 end if
196 end if
198 end do
199
200 if (is_flag_ .and. (option_ == 0)) l = .true.
201
202 end function varinfo_valid_option_8
203
204 ! ---------------------------------------------------------
205
206 logical function varinfo_valid_option_4(var, option, is_flag) result(l)
207 character(len=*), intent(in) :: var
208 integer, intent(in) :: option
209 logical, optional, intent(in) :: is_flag
210
211 l = varinfo_valid_option_8(var, int(option, int64), is_flag)
212
213 end function varinfo_valid_option_4
214
215 ! ---------------------------------------------------------
216 subroutine varinfo_print_option(iunit, var, option, pre)
217 integer, intent(in) :: iunit
218 character(len=*), intent(in) :: var
219 integer, intent(in) :: option
220 character(len=*), intent(in), optional :: pre
221
222 type(c_ptr) :: handle, opt, name, desc
223 integer(int64) :: val
224 logical :: option_found
225 character(kind=c_char) :: cvar(c_str_len(var))
226
227 cvar = string_f_to_c(var)
228 call varinfo_getvar(cvar, handle)
229 if (.not. c_associated(handle)) then
230 write(iunit, '(3a)') 'ERROR: Could not find a variable named ', trim(var), '.'
231 stop
232 end if
233
234 option_found = .false.
235 opt = c_null_ptr
236 do
237 call varinfo_getopt(handle, opt)
238 if (.not. c_associated(opt)) exit
239
240 call varinfo_opt_getinfo(opt, name, val, desc)
241
242 if (val == int(option, int64)) then
243 option_found = .true.
244 exit
245 end if
246 end do
247
248 write(iunit, '(4a)', advance='no') "Input:", ' [', var, ' = '
249
250 if (option_found) then
251 call print_c_string(iunit, name, advance='no')
252 else
253 write(iunit,'(i6,a)', advance='no') option, " (INVALID)"
254 end if
255 write(iunit, '(a)', advance='no') ']'
256 if (present(pre)) then
257 write(iunit, '(3a)') ' (', trim(pre), ')'
258 else
259 write(iunit, '(1x)')
260 end if
261 ! uncomment to print the description of the options
262 !call print_C_string(iunit, desc, pre=' > ')
263
264 if (.not. option_found) then
265 ! we cannot use messages here :-(
266 write(iunit,'(a,i6,2a)') "ERROR: invalid option ", option, " for variable ", trim(var)
267 stop
268 end if
269
270 end subroutine varinfo_print_option
271
272 ! ---------------------------------------------------------
273 subroutine varinfo_search(iunit, var, ierr)
274 integer, intent(in) :: iunit
275 character(len=*), intent(in) :: var
276 integer,optional, intent(out):: ierr
277
278 type(c_ptr) :: handle, name, type, default, section, desc
279
280 handle = c_null_ptr
281 if (present(ierr)) ierr = -1
282 do
283 call varinfo_search_var(string_f_to_c(var), handle)
284
285 if (c_associated(handle)) then
286 if (present(ierr)) ierr = 0
287 else
288 exit
289 end if
290
291 call varinfo_getinfo(handle, name, type, default, section, desc)
292 call print_c_string(iunit, name)
293
294 end do
295
296 end subroutine varinfo_search
297
298 ! ---------------------------------------------------------
299 integer function varinfo_option(var, option) result(val)
300 character(len=*), intent(in) :: var
301 character(len=*), intent(in) :: option
302
303 type(c_ptr) :: handle
304 integer :: ierr
305
306 call varinfo_getvar(string_f_to_c(var), handle)
307 call varinfo_search_option(handle, string_f_to_c(option), val, ierr)
308
309 if (ierr /= 0) then
310 ! we cannot use messages here :-(
311 write(0,'(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 character(kind=c_char) :: cvar(c_str_len(var))
323 type(c_ptr) :: handle
324
325 handle = c_null_ptr
326
327 cvar = string_f_to_c(var)
328 call varinfo_search_var(cvar, handle)
329
330 exists = c_associated(handle)
331
332 end function varinfo_exists
333
334
335end module varinfo_oct_m
336
337!! Local Variables:
338!! mode: f90
339!! coding: utf-8
340!! End:
character(kind=c_char, len=1) function, dimension(:), allocatable, public string_f_to_c(f_string)
convert a Fortran string to a C string
Definition: string.F90:273
subroutine, public print_c_string(iunit, str, pre, advance)
prints the C string given by the pointer str
Definition: string.F90:227
integer pure function, public c_str_len(fortran_char)
Convert fortran character length to C character length.
Definition: string.F90:333
subroutine, public varinfo_print_option(iunit, var, option, pre)
Definition: varinfo.F90:312
logical function varinfo_valid_option_8(var, option, is_flag)
Definition: varinfo.F90:258
subroutine, public varinfo_print(iunit, var, ierr)
Definition: varinfo.F90:207
logical function varinfo_valid_option_4(var, option, is_flag)
Definition: varinfo.F90:302
subroutine, public varinfo_search(iunit, var, ierr)
Definition: varinfo.F90:369
logical function, public varinfo_exists(var)
Definition: varinfo.F90:415
integer function, public varinfo_option(var, option)
Definition: varinfo.F90:395
int true(void)