DAMASK_EICMD/src/system_routines.f90

163 lines
5.2 KiB
Fortran
Raw Normal View History

2016-05-05 16:30:46 +05:30
!--------------------------------------------------------------------------------------------------
!> @author Martin Diehl, Max-Planck-Institut für Eisenforschung GmbH
!> @brief provides wrappers to C routines
!--------------------------------------------------------------------------------------------------
2016-03-12 01:29:14 +05:30
module system_routines
implicit none
private
public :: &
isDirectory, &
getCWD, &
2018-05-26 02:52:32 +05:30
getHostName, &
setCWD
2016-03-12 01:29:14 +05:30
interface
2018-05-26 02:52:32 +05:30
function isDirectory_C(path) bind(C)
2016-03-12 01:29:14 +05:30
use, intrinsic :: ISO_C_Binding, only: &
C_INT, &
C_CHAR
integer(C_INT) :: isDirectory_C
2016-05-18 02:46:17 +05:30
character(kind=C_CHAR), dimension(1024), intent(in) :: path ! C string is an array
2016-03-12 01:29:14 +05:30
end function isDirectory_C
2016-05-05 19:17:15 +05:30
subroutine getCurrentWorkDir_C(str, stat) bind(C)
2016-03-12 01:29:14 +05:30
use, intrinsic :: ISO_C_Binding, only: &
C_INT, &
C_CHAR
2016-05-18 02:46:17 +05:30
character(kind=C_CHAR), dimension(1024), intent(out) :: str ! C string is an array
2016-03-12 01:29:14 +05:30
integer(C_INT),intent(out) :: stat
end subroutine getCurrentWorkDir_C
subroutine getHostName_C(str, stat) bind(C)
use, intrinsic :: ISO_C_Binding, only: &
C_INT, &
C_CHAR
character(kind=C_CHAR), dimension(1024), intent(out) :: str ! C string is an array
integer(C_INT),intent(out) :: stat
end subroutine getHostName_C
2018-05-26 02:52:32 +05:30
function chdir_C(path) bind(C)
use, intrinsic :: ISO_C_Binding, only: &
C_INT, &
C_CHAR
integer(C_INT) :: chdir_C
character(kind=C_CHAR), dimension(1024), intent(in) :: path ! C string is an array
end function chdir_C
2016-03-12 01:29:14 +05:30
end interface
contains
2016-05-05 16:30:46 +05:30
!--------------------------------------------------------------------------------------------------
!> @brief figures out if a given path is a directory (and not an ordinary file)
!--------------------------------------------------------------------------------------------------
logical function isDirectory(path)
use, intrinsic :: ISO_C_Binding, only: &
2016-05-18 02:46:17 +05:30
C_INT, &
C_CHAR, &
C_NULL_CHAR
2016-03-12 01:29:14 +05:30
2016-05-05 16:30:46 +05:30
implicit none
character(len=*), intent(in) :: path
2016-05-18 02:46:17 +05:30
character(kind=C_CHAR), dimension(1024) :: strFixedLength
integer :: i
2016-03-12 01:29:14 +05:30
2016-05-18 02:46:17 +05:30
strFixedLength = repeat(C_NULL_CHAR,len(strFixedLength))
do i=1,len(path) ! copy array components
strFixedLength(i)=path(i:i)
enddo
isDirectory=merge(.True.,.False.,isDirectory_C(strFixedLength) /= 0_C_INT)
2016-03-12 01:29:14 +05:30
2016-05-05 16:30:46 +05:30
end function isDirectory
2016-03-12 01:29:14 +05:30
2016-05-05 16:30:46 +05:30
!--------------------------------------------------------------------------------------------------
!> @brief gets the current working directory
!--------------------------------------------------------------------------------------------------
character(len=1024) function getCWD()
2016-05-05 16:30:46 +05:30
use, intrinsic :: ISO_C_Binding, only: &
C_INT, &
C_CHAR, &
C_NULL_CHAR
2016-05-18 02:46:17 +05:30
2016-05-05 16:30:46 +05:30
implicit none
2018-08-22 21:39:17 +05:30
character(kind=C_CHAR), dimension(1024) :: charArray ! C string is an array
2016-05-05 16:30:46 +05:30
integer(C_INT) :: stat
2016-05-05 19:46:21 +05:30
integer :: i
2016-05-05 16:30:46 +05:30
2018-08-22 21:39:17 +05:30
call getCurrentWorkDir_C(charArray,stat)
if (stat /= 0_C_INT) then
getCWD = 'Error occured when getting currend working directory'
else
getCWD = repeat('',len(getCWD))
2018-08-22 21:39:17 +05:30
arrayToString: do i=1,len(getCWD)
if (charArray(i) /= C_NULL_CHAR) then
getCWD(i:i)=charArray(i)
else
exit
endif
2018-08-22 21:39:17 +05:30
enddo arrayToString
endif
2016-03-12 01:29:14 +05:30
2016-05-05 16:30:46 +05:30
end function getCWD
2016-03-12 01:29:14 +05:30
!--------------------------------------------------------------------------------------------------
!> @brief gets the current host name
!--------------------------------------------------------------------------------------------------
character(len=1024) function getHostName()
use, intrinsic :: ISO_C_Binding, only: &
C_INT, &
C_CHAR, &
C_NULL_CHAR
implicit none
2018-08-22 21:39:17 +05:30
character(kind=C_CHAR), dimension(1024) :: charArray ! C string is an array
integer(C_INT) :: stat
integer :: i
2018-08-22 21:39:17 +05:30
call getHostName_C(charArray,stat)
if (stat /= 0_C_INT) then
getHostName = 'Error occured when getting host name'
else
getHostName = repeat('',len(getHostName))
2018-08-22 21:39:17 +05:30
arrayToString: do i=1,len(getHostName)
if (charArray(i) /= C_NULL_CHAR) then
getHostName(i:i)=charArray(i)
else
exit
endif
2018-08-22 21:39:17 +05:30
enddo arrayToString
endif
end function getHostName
2018-05-26 02:52:32 +05:30
!--------------------------------------------------------------------------------------------------
!> @brief changes the current working directory
!--------------------------------------------------------------------------------------------------
logical function setCWD(path)
use, intrinsic :: ISO_C_Binding, only: &
C_INT, &
C_CHAR, &
C_NULL_CHAR
implicit none
character(len=*), intent(in) :: path
character(kind=C_CHAR), dimension(1024) :: strFixedLength ! C string is an array
integer :: i
strFixedLength = repeat(C_NULL_CHAR,len(strFixedLength))
do i=1,len(path) ! copy array components
strFixedLength(i)=path(i:i)
enddo
setCWD=merge(.True.,.False.,chdir_C(strFixedLength) /= 0_C_INT)
end function setCWD
2016-03-12 01:29:14 +05:30
end module system_routines