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) bind(c)
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) bind(c)
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) bind(c)
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(c_int64_t), 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() bind(c)
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 handle = c_null_ptr
121 call varinfo_getvar(string_f_to_c(var), handle)
122 if (.not. c_associated(handle)) then
123 if (present(ierr)) then
124 ierr = -1
125 return
126 else
127 write(iunit, '(3a)') 'ERROR: Could not find a variable named ', trim(var), '.'
128 stop
129 end if
130 end if
131
132 if (present(ierr)) ierr = 0
133 call varinfo_getinfo(handle, name, type, default, section, desc)
135 call print_c_string(iunit, name, "Variable: ")
136 call print_c_string(iunit, type, "Type: ")
137 call print_c_string(iunit, default, "Default: ")
138 call print_c_string(iunit, section, "Section: ")
139 write(iunit, '(a)') "Description:"
140 call print_c_string(iunit, desc, " ")
141
142 opt = c_null_ptr
143 first = .true.
144 do
145 call varinfo_getopt(handle, opt)
146 if (.not. c_associated(opt)) then
147 exit
148 else
149 if (first) then
150 write(iunit, '(a)') "Available options:"
151 first = .false.
152 end if
153 call varinfo_opt_getinfo(opt, name, val, desc)
154 call print_c_string(iunit, name, " ")
155 call print_c_string(iunit, desc, " ")
156 end if
157 end do
158
159 end subroutine varinfo_print
160
161
162 ! ---------------------------------------------------------
163 logical function varinfo_valid_option_8(var, option, is_flag) result(l)
164 character(len=*), intent(in) :: var
165 integer(int64), intent(in) :: option
166 logical, optional, intent(in) :: is_flag
167
168 type(c_ptr) :: handle, opt, name, desc
169 integer(int64) :: val, option_
170 logical :: is_flag_
172 is_flag_ = .false.
173 if (present(is_flag)) is_flag_ = is_flag
174 option_ = option ! copy that we can change
175
176 l = .false.
177
178 handle = c_null_ptr
179 call varinfo_getvar(string_f_to_c(var), handle)
180 if (.not. c_associated(handle)) then
181 write(error_unit, '(3a)') 'ERROR: Could not find a variable named ', trim(var), '.'
182 stop
183 end if
184
185 opt = c_null_ptr
186 do
187 call varinfo_getopt(handle, opt)
188 if (.not. c_associated(opt)) exit
189 call varinfo_opt_getinfo(opt, name, val, desc)
190
191 if (is_flag_) then
192 option_ = iand(option_, not(val))
193 else
194 if (val == option_) then
195 l = .true.
196 return
197 end if
198 end if
199
200 end do
201
202 if (is_flag_ .and. (option_ == 0)) l = .true.
203
204 end function varinfo_valid_option_8
205
206 ! ---------------------------------------------------------
207
208 logical function varinfo_valid_option_4(var, option, is_flag) result(l)
209 character(len=*), intent(in) :: var
210 integer, intent(in) :: option
211 logical, optional, intent(in) :: is_flag
212
213 l = varinfo_valid_option_8(var, int(option, int64), is_flag)
214
215 end function varinfo_valid_option_4
216
217 ! ---------------------------------------------------------
218 subroutine varinfo_print_option(iunit, var, option, pre)
219 integer, intent(in) :: iunit
220 character(len=*), intent(in) :: var
221 integer, intent(in) :: option
222 character(len=*), intent(in), optional :: pre
223
224 type(c_ptr) :: handle, opt, name, desc
225 integer(int64) :: val
226 logical :: option_found
227 character(kind=c_char) :: cvar(c_str_len(var))
228
229 cvar = string_f_to_c(var)
230 call varinfo_getvar(cvar, handle)
231 if (.not. c_associated(handle)) then
232 write(iunit, '(3a)') 'ERROR: Could not find a variable named ', trim(var), '.'
233 stop
234 end if
235
236 option_found = .false.
237 opt = c_null_ptr
238 do
239 call varinfo_getopt(handle, opt)
240 if (.not. c_associated(opt)) exit
241
242 call varinfo_opt_getinfo(opt, name, val, desc)
243
244 if (val == int(option, int64)) then
245 option_found = .true.
246 exit
247 end if
248 end do
249
250 write(iunit, '(4a)', advance='no') "Input:", ' [', var, ' = '
251
252 if (option_found) then
253 call print_c_string(iunit, name, advance='no')
254 else
255 write(iunit,'(i6,a)', advance='no') option, " (INVALID)"
256 end if
257 write(iunit, '(a)', advance='no') ']'
258 if (present(pre)) then
259 write(iunit, '(3a)') ' (', trim(pre), ')'
260 else
261 write(iunit, '(1x)')
262 end if
263 ! uncomment to print the description of the options
264 !call print_C_string(iunit, desc, pre=' > ')
265
266 if (.not. option_found) then
267 ! we cannot use messages here :-(
268 write(iunit,'(a,i6,2a)') "ERROR: invalid option ", option, " for variable ", trim(var)
269 stop
270 end if
271
272 end subroutine varinfo_print_option
273
274 ! ---------------------------------------------------------
275 subroutine varinfo_search(iunit, var, ierr)
276 integer, intent(in) :: iunit
277 character(len=*), intent(in) :: var
278 integer,optional, intent(out):: ierr
279
280 type(c_ptr) :: handle, name, type, default, section, desc
281
282 handle = c_null_ptr
283 if (present(ierr)) ierr = -1
284 do
285 call varinfo_search_var(string_f_to_c(var), handle)
286
287 if (c_associated(handle)) then
288 if (present(ierr)) ierr = 0
289 else
290 exit
291 end if
292
293 call varinfo_getinfo(handle, name, type, default, section, desc)
294 call print_c_string(iunit, name)
295
296 end do
297
298 end subroutine varinfo_search
299
300 ! ---------------------------------------------------------
301 integer function varinfo_option(var, option) result(val)
302 character(len=*), intent(in) :: var
303 character(len=*), intent(in) :: option
304
305 type(c_ptr) :: handle
306 integer :: ierr
307
308 handle = c_null_ptr
309 call varinfo_getvar(string_f_to_c(var), handle)
310 call varinfo_search_option(handle, string_f_to_c(option), val, ierr)
311
312 if (ierr /= 0) then
313 ! we cannot use messages here :-(
314 write(error_unit,'(4a)') "ERROR: invalid option ", trim(option), " for variable ", trim(var)
315 stop
316 end if
317
318 end function varinfo_option
319
320 ! ----------------------------------------------------------
321
322 logical function varinfo_exists(var) result(exists)
323 character(len=*), intent(in) :: var
324
325 character(kind=c_char) :: cvar(c_str_len(var))
326 type(c_ptr) :: handle
327
328 handle = c_null_ptr
329
330 cvar = string_f_to_c(var)
331 call varinfo_search_var(cvar, handle)
332
333 exists = c_associated(handle)
334
335 end function varinfo_exists
336
337
338end module varinfo_oct_m
339
340!! Local Variables:
341!! mode: f90
342!! coding: utf-8
343!! 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:257
subroutine, public print_c_string(iunit, str, pre, advance)
prints the C string given by the pointer str
Definition: string.F90:204
integer pure function, public c_str_len(fortran_char)
Convert fortran character length to C character length.
Definition: string.F90:319
subroutine, public varinfo_print_option(iunit, var, option, pre)
Definition: varinfo.F90:314
logical function varinfo_valid_option_8(var, option, is_flag)
Definition: varinfo.F90:259
subroutine, public varinfo_print(iunit, var, ierr)
Definition: varinfo.F90:207
logical function varinfo_valid_option_4(var, option, is_flag)
Definition: varinfo.F90:304
subroutine, public varinfo_search(iunit, var, ierr)
Definition: varinfo.F90:371
logical function, public varinfo_exists(var)
Definition: varinfo.F90:418
integer function, public varinfo_option(var, option)
Definition: varinfo.F90:397
int true(void)