56 integer,
parameter,
public :: iunit_out = 8
57 integer,
parameter,
public :: iunit_err = 9
58 character(len=MAX_PATH_LEN) :: work_dir
59 character(len=MAX_PATH_LEN) :: current_working_dir
60 integer(int64),
save :: io_open_count
61 integer(int64),
save :: io_close_count
70 logical,
optional,
intent(in) :: defaults
72 character(len=MAX_PATH_LEN) :: filename
78 call get_environment_variable(
"PWD", current_working_dir, status=status)
80 write(*,
'("ERROR: could not determine current working directory!")')
87 if (
present(defaults))
then
92 work_dir = current_working_dir
110 if (trim(filename) /=
'-')
then
112 open(stdout, file=filename, status=
'unknown')
126 if (trim(filename) /=
'-')
then
128 open(stderr, file=filename, status=
'unknown')
160 if (work_dir /=
'.')
call loct_mkdir(trim(work_dir))
162 if (
debug%info .or.
debug%interaction_graph .or.
debug%propagation_graph)
then
166 if (
debug%trace_file)
then
178 if (stderr /= 0)
call io_close(stderr)
179 if (stdin /= 5)
call io_close(stdin)
180 if (stdout /= 6)
call io_close(stdout)
189 character(len=MAX_PATH_LEN) function io_workdir()
result (wpath)
191 integer :: len_work_dir
192 character :: first_char
194 len_work_dir = len_trim(work_dir)
195 first_char = work_dir(1:1)
197 select case (first_char)
202 if (len_work_dir>1)
then
204 wpath = trim(current_working_dir) // trim(work_dir(2:len_work_dir))
206 wpath = current_working_dir
210 wpath = trim(current_working_dir) //
"/" // trim(work_dir)
221 character(len=MAX_PATH_LEN) function io_workpath(path, namespace)
result(wpath)
222 character(len=*),
intent(in) :: path
223 type(
namespace_t),
optional,
intent(in) :: namespace
225 logical :: absolute_path
229 absolute_path = .false.
230 if (len_trim(path) > 0)
then
231 absolute_path = path(1:1) ==
'/'
235 total_len = len_trim(path)
236 if (.not. absolute_path)
then
237 total_len = total_len + len_trim(work_dir) + 1
238 if (
present(namespace))
then
239 if (namespace%len() > 0) total_len = total_len + namespace%len() + 1
243 write(stderr,
"(A,I5)")
"Path is longer than the maximum path length of ",
max_path_len
244 write(stderr,
"(A)") path
248 if (absolute_path)
then
252 wpath = trim(work_dir)
253 if (
present(namespace))
then
255 if (namespace%len() > 0) wpath = trim(wpath) +
"/" + trim(namespace%get(
'/'))
257 wpath = trim(wpath) +
"/" + trim(path)
264 subroutine io_mkdir(fname, namespace, parents)
265 character(len=*),
intent(in) :: fname
266 type(
namespace_t),
optional,
intent(in) :: namespace
267 logical,
optional,
intent(in) :: parents
270 integer :: last_slash, pos, length
273 if (
present(parents)) parents_ = parents
275 if (.not. parents_)
then
279 last_slash = max(index(fname,
"/", .
true.), len_trim(fname))
281 length = index(fname,
'/') - 1
282 do while (pos < last_slash)
284 pos = pos + length + 1
285 length = index(fname(pos:),
"/") - 1
286 if (length < 1) length = len_trim(fname(pos:))
295 subroutine io_rm(fname, namespace)
296 character(len=*),
intent(in) :: fname
297 type(
namespace_t),
optional,
intent(in) :: namespace
305 integer function io_open(file, namespace, action, status, form, position, die, recl, grp)
result(iunit)
306 character(len=*),
intent(in) :: file, action
307 type(
namespace_t),
intent(in),
optional :: namespace
308 character(len=*),
intent(in),
optional :: status, form, position
309 logical,
intent(in),
optional :: die
310 integer,
intent(in),
optional :: recl
311 type(
mpi_grp_t),
intent(in),
optional :: grp
313 character(len=20) :: status_, form_, position_
314 character(len=MAX_PATH_LEN) :: file_
317 character(len=100) :: io_emsg
320 if (
present(grp))
then
329 if (grp_%is_root())
then
332 if (
present(status)) status_ = status
334 if (
present(form)) form_ = form
336 if (
present(position)) position_ = position
342 if (
present(recl))
then
343 open(newunit=iunit, file=trim(file_), status=trim(status_), form=trim(form_), &
344 recl=recl, action=trim(action), position=trim(position_), iostat=iostat, iomsg=io_emsg)
346 open(newunit=iunit, file=trim(file_), status=trim(status_), form=trim(form_), &
347 action=trim(action), position=trim(position_), iostat=iostat, iomsg=io_emsg)
350 io_open_count = io_open_count + 1
352 if (iostat /= 0)
then
355 write(stderr,
'(a,a)')
'*** IO Error: ', trim(io_emsg)
362 if (grp_%size > 1)
then
363 call grp_%bcast(iunit, 1, mpi_integer, 0)
371 integer,
intent(inout) :: iunit
372 type(
mpi_grp_t),
intent(in),
optional :: grp
376 if (
present(grp))
then
384 if (grp_%is_root())
then
386 io_close_count = io_close_count + 1
395 integer,
intent(in) :: ounit
396 character(len=*),
intent(in) :: filename
398 integer :: iunit, err
399 character(len=80) :: line
403 open(newunit=iunit, file=filename, iostat=err, action=
'read', status=
'old')
406 read(iunit, fmt=
'(a80)', iostat=err) line
408 write(ounit,
'(a)') trim(line)
422 character(len = *),
intent(in) :: path
425 i = index(path,
".", back = .
true.)
426 j = index(path(i+1:),
"/")
427 if (i == 0 .or. j /= 0)
then
443 if (
io_file_exists(
'enable_debug_mode', msg=
'Enabling DebugMode'))
then
449 call loct_rm(
'enable_debug_mode')
454 if (
io_file_exists(
'disable_debug_mode', msg=
'Disabling DebugMode'))
then
457 call loct_rm(
'disable_debug_mode')
468 logical function io_file_exists(filename, msg)
result(file_exists)
469 character(len=*),
intent(in) :: filename
470 character(len=*),
optional,
intent(in) :: msg
472 file_exists = .false.
473 inquire(file=trim(filename), exist=file_exists)
474 if (file_exists .and.
present(msg))
then
475 write(stderr,
'(a)') trim(msg)
483 character(len=*),
intent(in) :: dir
484 type(
namespace_t),
optional,
intent(in) :: namespace
491 subroutine iopar_read(grp, iunit, lines, n_lines, ierr)
493 integer,
intent(in) :: iunit
494 character(len=*),
intent(out) :: lines(:)
495 integer,
intent(in) :: n_lines
496 integer,
intent(out) :: ierr
500 assert(n_lines <=
size(lines))
502 if (grp%is_root())
then
504 read(iunit,
'(a)', iostat=ierr) lines(il)
509 call grp%bcast(ierr, 1, mpi_integer, 0)
510 call grp%bcast(lines(1), len(lines(1))*n_lines, mpi_character, 0)
517 integer,
intent(in) :: iunit
519 if (grp%is_root())
then
529 integer,
intent(in) :: iunit
530 character(len=*),
intent(in) :: line
531 integer,
intent(out) :: ierr
533 character(len=80) :: read_line
535 if (grp%is_root())
then
538 read(iunit,
'(a)', iostat=ierr) read_line
539 if (ierr /= 0 .or. trim(line) == trim(read_line))
exit
543 call grp%bcast(ierr, 1, mpi_integer, 0)
550 integer,
intent(in) :: iunit
552 character(len=1) :: a
566 count = io_open_count
593 integer,
intent(in) :: iio
595 integer :: open_count
597 open_count = int(iio/100)
subroutine, public debug_enable(this)
type(debug_t), save, public debug
subroutine, public debug_disable(this)
subroutine, public debug_delete_trace()
integer, public no_sub_stack
integer, parameter, public max_path_len
Public types, variables and procedures.
character(len=8) function, public io_get_extension(path)
Given a path, it returns the extension (if it exists) of the file (that is, the part of the name that...
integer(int64), save io_open_count
subroutine, public io_init(defaults)
If the argument defaults is present and set to true, then the routine will not try to read anything f...
subroutine, public io_close(iunit, grp)
subroutine, public io_skip_header(iunit)
subroutine, public io_incr_counters(iio)
subroutine, public io_incr_close_count()
subroutine, public io_end()
subroutine, public iopar_read(grp, iunit, lines, n_lines, ierr)
integer(int64) pure function, public io_get_close_count()
logical function, public io_file_exists(filename, msg)
Returns true if a file with name 'filename' exists and issues a reminder.
character(len=max_path_len) function, public io_workpath(path, namespace)
construct path name from given name and namespace
subroutine, public io_incr_open_count()
subroutine, public iopar_backspace(grp, iunit)
integer(int64), save io_close_count
subroutine, public io_debug_on_the_fly(namespace)
check if debug mode should be enabled or disabled on the fly
subroutine, public io_rm(fname, namespace)
character(len=max_path_len) function, public io_workdir()
construct working directory
subroutine, public io_mkdir(fname, namespace, parents)
logical function, public io_dir_exists(dir, namespace)
Returns true if a dir with name 'dir' exists.
subroutine, public iopar_find_line(grp, iunit, line, ierr)
integer(int64) pure function, public io_get_open_count()
subroutine, public io_dump_file(ounit, filename)
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
logical function, public loct_dir_exists(dirname)
type(mpi_comm), parameter, public mpi_comm_undefined
used to indicate a communicator has not been initialized
type(mpi_grp_t), public mpi_world
subroutine mpi_grp_init(grp, comm)
Initialize MPI group instance.
type(namespace_t), public global_namespace
This is defined even when running serial.