Octopus
loct.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch, M. Oliveira
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 loct_oct_m
22 use iso_c_binding
23 use, intrinsic :: iso_fortran_env
24
25 implicit none
26
28 private
29 public :: &
30 loct_clock, &
35 loct_mkdir, &
36 loct_rm, &
48
49 private :: string_f_to_c
50
51 ! ---------------------------------------------------------
53
54 interface loct_clock
55 function oct_clock()
56 use, intrinsic :: iso_fortran_env
57 implicit none
58 real(real64) :: oct_clock
59 end function oct_clock
60 end interface loct_clock
61
62 interface loct_gettimeofday
63 subroutine oct_gettimeofday(sec, usec)
64 implicit none
65 integer, intent(out) :: sec, usec
66 end subroutine oct_gettimeofday
67 end interface loct_gettimeofday
68
69 interface loct_nanosleep
70 subroutine oct_nanosleep(sec, nsec)
71 implicit none
72 integer, intent(in) :: sec
73 integer, intent(in) :: nsec
74 end subroutine oct_nanosleep
75 end interface loct_nanosleep
76
77 ! ---------------------------------------------------------
79 interface loct_number_of_lines
80 integer function oct_number_of_lines(filename) bind(c)
81 use iso_c_binding
82 implicit none
83 character(kind=c_char), intent(in) :: filename(*)
84 end function oct_number_of_lines
85 end interface loct_number_of_lines
86
87 interface loct_break_c_string
88 subroutine oct_break_c_string(str, s, line)
89 use iso_c_binding
90 implicit none
91 type(c_ptr), intent(in) :: str
92 type(c_ptr), intent(inout) :: s
93 character(kind=c_char), intent(out) :: line(*)
94 end subroutine oct_break_c_string
95 end interface loct_break_c_string
96
97 ! ---------------------------------------------------------
99 interface loct_progress_bar
100 subroutine oct_progress_bar(a, maxcount)
101 implicit none
102 integer, intent(in) :: a, maxcount
103 end subroutine oct_progress_bar
104 end interface loct_progress_bar
105
106 interface loct_wfs_list
107 subroutine oct_wfs_list(str, l) bind(c)
108 use iso_c_binding
109 implicit none
110 character(kind=c_char), intent(in) :: str(*)
111 integer, intent(out) :: l
112 end subroutine oct_wfs_list
113 end interface loct_wfs_list
114
115 interface loct_get_memory_usage
116 integer(c_intptr_t) function oct_get_memory_usage()
117 use iso_c_binding, only: c_intptr_t
118 implicit none
119 end function oct_get_memory_usage
120 end interface loct_get_memory_usage
121
122contains
123
124 subroutine loct_print_recipe(dir, filename)
125 character(len=*), intent(in) :: dir
126 character(len=*), intent(inout) :: filename
127
128 character(kind=c_char) :: c_name(len(filename)+1)
129
130 interface
131 subroutine oct_print_recipe(dir, filename) bind(c)
132 use iso_c_binding
133 implicit none
134 character(kind=c_char), intent(in) :: dir(*)
135 character(kind=c_char), intent(out) :: filename(*)
136 end subroutine oct_print_recipe
137 end interface
138
139 call oct_print_recipe(string_f_to_c(dir), c_name)
140 call string_c_to_f(c_name, filename)
141 end subroutine loct_print_recipe
142
143
144 ! ---------------------------------------------------------
147 !
148 function string_f_to_c(f_string) result(c_string)
149 character(len=*), intent(in) :: f_string
150 character(kind=c_char,len=1) :: c_string(len_trim(f_string) + 1)
151
152 integer :: i, strlen
153
154 strlen = len_trim(f_string)
155
156 do i = 1, strlen
157 c_string(i) = f_string(i:i)
158 end do
159 c_string(strlen+1) = c_null_char
160
161 end function string_f_to_c
162
163 ! ---------------------------------------------------------
166 subroutine string_c_to_f(c_string, f_string)
167 character(kind=c_char,len=1), intent(in) :: c_string(*)
168 character(len=*), intent(out) :: f_string
169
170 integer :: i
171
172 i = 1
173 do while(c_string(i) /= c_null_char .and. i <= len(f_string))
174 f_string(i:i) = c_string(i)
175 i = i + 1
176 end do
177 if (i < len(f_string)) f_string(i:) = ' '
178
179 end subroutine string_c_to_f
180
181 logical function loct_isinstringlist(a, s) result(inlist)
182 integer, intent(in) :: a
183 character(len=*), intent(in) :: s
184
185 integer, allocatable :: list(:)
186
187 allocate(list(2**14))
188
189 call loct_wfs_list(string_f_to_c(s), list(1))
190 inlist = .false.
191 if (list(a) == 1) inlist = .true.
192
193 deallocate(list)
196
197 subroutine loct_mkdir(name)
198 character(len=*), intent(in) :: name
199
200 interface oct_mkdir
201 subroutine oct_mkdir(name) bind(c)
202 use iso_c_binding
203 implicit none
204 character(kind=c_char), intent(in) :: name(*)
205 end subroutine oct_mkdir
206 end interface oct_mkdir
207
208 call oct_mkdir(string_f_to_c(name))
209 end subroutine loct_mkdir
211 subroutine loct_rm(name)
212 character(len=*), intent(in) :: name
213
214 interface oct_rm
215 subroutine oct_rm(name) bind(c)
216 use iso_c_binding
217 implicit none
218 character(kind=c_char), intent(in) :: name(*)
219 end subroutine oct_rm
220 end interface oct_rm
221
222 call oct_rm(string_f_to_c(name))
223 end subroutine loct_rm
224
225 subroutine loct_sysname(name)
226 character(len=*), intent(inout) :: name
227 character(kind=c_char) :: c_name(len(name)+1)
228
229 interface oct_sysname
230 subroutine oct_sysname(name) bind(c)
231 use iso_c_binding
232 implicit none
233 character(kind=c_char), intent(out) :: name(*)
234 end subroutine oct_sysname
235 end interface oct_sysname
236
237 c_name = c_null_char
238 call oct_sysname(c_name)
239 call string_c_to_f(c_name, name)
240 end subroutine loct_sysname
241
242 logical function loct_dir_exists(dirname) result(exists)
243 character(len=*), intent(in) :: dirname
244
245 interface oct_dir_exists
246 integer function oct_dir_exists(dirname) bind(c)
247 use iso_c_binding
248 implicit none
249 character(kind=c_char), intent(in) :: dirname(*)
250 end function oct_dir_exists
251 end interface oct_dir_exists
252
253 exists = oct_dir_exists(string_f_to_c(dirname)) /= 0
254
255 end function loct_dir_exists
256
257 subroutine loct_getenv(var, val)
258 character(len=*), intent(in) :: var
259 character(len=*), intent(inout) :: val
260
261 character(kind=c_char) :: c_val(len(val)+1)
262
263 interface oct_getenv
264 subroutine oct_getenv(var, val) bind(c)
265 use iso_c_binding
266 implicit none
267 character(kind=c_char), intent(in) :: var(*)
268 character(kind=c_char), intent(inout) :: val(*)
269 end subroutine oct_getenv
270 end interface oct_getenv
271
272 c_val = c_null_char
273 call oct_getenv(string_f_to_c(var), c_val)
274 call string_c_to_f(c_val, val)
275 end subroutine loct_getenv
277 subroutine loct_strerror(errno, res)
278 integer, intent(in) :: errno
279 character(len=*), intent(inout) :: res
280
281 character(kind=c_char) :: cres(len(res)+1)
282
283 interface oct_strerror
284 subroutine oct_strerror(errno, res) bind(c)
285 use iso_c_binding
286 implicit none
287 integer, intent(in) :: errno
288 character(kind=c_char), intent(inout) :: res(*)
289 end subroutine oct_strerror
290 end interface oct_strerror
291
292 cres = c_null_char
293 call oct_strerror(errno, cres)
294 call string_c_to_f(cres, res)
295 end subroutine loct_strerror
296
297 subroutine loct_search_file_lr(freq, tag, ierr, dirname)
298 real(real64), intent(inout) :: freq
299 integer, intent(in) :: tag
300 integer, intent(out) :: ierr
301 character(len=*), intent(in) :: dirname
302
303 interface
304 subroutine oct_search_file_lr(freq, tag, ierr, dirname) bind(c)
305 use iso_c_binding
306 use, intrinsic :: iso_fortran_env
307 implicit none
308 real(real64), intent(inout) :: freq
309 integer, intent(in) :: tag
310 integer, intent(out) :: ierr
311 character(kind=c_char), intent(in) :: dirname(*)
312 end subroutine oct_search_file_lr
313 end interface
314
315 call oct_search_file_lr(freq, tag, ierr, string_f_to_c(dirname))
316 end subroutine loct_search_file_lr
317
318 subroutine loct_executable_path(fpath)
319 character(len=*), intent(inout) :: fpath
321 character(kind=c_char) :: path(len(fpath)+1)
322 interface
323 subroutine oct_executable_path(path) bind(c)
324 use iso_c_binding
325 character(kind=c_char), intent(out) :: path(*)
326 end subroutine oct_executable_path
327 end interface
328
329 path = c_null_char
330 call oct_executable_path(path)
331 call string_c_to_f(path, fpath)
332 end subroutine loct_executable_path
333
334 subroutine loct_dirname(path, dir)
335 character(len=*), intent(in) :: path
336 character(len=*), intent(inout) :: dir
338 character(kind=c_char) :: cdir(len(dir)+1)
339 interface
340 subroutine oct_dirname(path, dir) bind(c)
341 use iso_c_binding
342 character(kind=c_char), intent(in) :: path(*)
343 character(kind=c_char), intent(inout) :: dir(*)
344 end subroutine oct_dirname
345 end interface
346
347 call oct_dirname(string_f_to_c(path), cdir)
348 call string_c_to_f(cdir, dir)
349 end subroutine loct_dirname
350
351end module loct_oct_m
353!! Local Variables:
354!! mode: f90
355!! coding: utf-8
356!! End:
Define which routines can be seen from the outside.
Definition: loct.F90:149
subroutine, public loct_rm(name)
Definition: loct.F90:307
logical function, public loct_isinstringlist(a, s)
Definition: loct.F90:277
subroutine, public loct_search_file_lr(freq, tag, ierr, dirname)
Definition: loct.F90:393
subroutine, public loct_mkdir(name)
Definition: loct.F90:293
subroutine, public loct_print_recipe(dir, filename)
Definition: loct.F90:220
subroutine, public loct_strerror(errno, res)
Definition: loct.F90:373
subroutine, public loct_getenv(var, val)
Definition: loct.F90:353
subroutine, public loct_dirname(path, dir)
Definition: loct.F90:430
logical function, public loct_dir_exists(dirname)
Definition: loct.F90:338
character(kind=c_char, len=1) function, dimension(len_trim(f_string)+1), private string_f_to_c(f_string)
convert a Fortran string to a C string
Definition: loct.F90:244
subroutine, public loct_executable_path(fpath)
Definition: loct.F90:414
subroutine string_c_to_f(c_string, f_string)
convert a C string to a Fortran string
Definition: loct.F90:262
subroutine, public loct_sysname(name)
Definition: loct.F90:321
void oct_wfs_list(char *str_c, fint l[16384])
Definition: oct_f.c:5137
void oct_executable_path(char *path)
Definition: oct_f.c:5483
void oct_rm(char *name_c)
Definition: oct_f.c:5081
void oct_sysname(char *name_c)
Definition: oct_f.c:5282
int oct_number_of_lines(char *name_c)
Definition: oct_f.c:5294
void oct_dirname(char *fn, char *dn)
Definition: oct_f.c:5104
void oct_getenv(char *name_c, char *var_c)
Definition: oct_f.c:5116
int oct_dir_exists(char *name_c)
Definition: oct_f.c:5060
void oct_search_file_lr(double *freq, const fint *tag, fint *ierr, char *name_c)
Definition: oct_f.c:5355
void oct_mkdir(char *name_c)
Definition: oct_f.c:5013
void oct_strerror(const fint *err, char *res)
Definition: oct_gsl_f.c:12402
void oct_print_recipe(char *dir_, char *filename)
Definition: recipes.c:3962
int true(void)