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