Fortran 2003-2008中是否有替代GETCWD()的方法

时间:2015-05-16 18:24:28

标签: fortran gfortran fortran2003 fortran2008

GNU Fortran编译器的GNU扩展提供了子例程GETCWD(),它获取当前工作目录。但是,我的代码也必须可移植到ifortnagfor编译器,并使用F2003功能。

那么,对于F2003及更高版本,是否有GETCWD()的替代方案?

我在这里有标准,但它相当大,而且我已经经历了一段时间了,并没有发现任何有用的东西......

3 个答案:

答案 0 :(得分:6)

如评论中所述,您可以使用标准Fortran的get_environment_variable(例如F2008 13.7.67)。此示例程序查询$PWD的值,该值应包含调用可执行文件时shell所在的目录。

program test
 implicit none
 character(len=128) :: pwd
 call get_environment_variable('PWD',pwd)
 print *, "The current working directory is: ",trim(pwd)
end program

及其输出:

casey@convect code % pwd
/home/casey/code
casey@convect code % so/getpwd 
 The current working directory is: /home/casey/code

这是标准的Fortran,但它的可移植性仅限于设置此变量的Unix和类Unix shell。

标准但丑陋(在我看来)的另一种选择是使用execute_command_line来运行可以将工作目录输出到临时文件(例如pwd > /tmp/mypwd)的命令,然后读取该文件。

答案 1 :(得分:5)

您还可以使用ISO_C_Binding并调用相应的C函数:

cwd.c:

#ifdef _WIN32
/* Windows */
#include <direct.h>
#define GETCWD _getcwd

#else
/* Unix */
#include <unistd.h>
#define GETCWD getcwd

#endif

void getCurrentWorkDir( char *str, int *stat )
{
  if ( GETCWD(str, sizeof(str)) == str ) {
    *stat = 0;
  } else {
    *stat = 1;
  }
}

test.F90:

program test
 use ISO_C_Binding, only: C_CHAR, C_INT
 interface
   subroutine getCurrentWorkDir(str, stat) bind(C, name="getCurrentWorkDir")
     use ISO_C_Binding, only: C_CHAR, C_INT
     character(kind=C_CHAR),intent(out) :: str(*)
     integer(C_INT),intent(out)         :: stat
    end subroutine
  end interface
  character(len=30)   :: str
  integer(C_INT)      :: stat

  str=''
  call getCurrentWorkDir(str, stat)
  print *, stat, trim(str)

end program

此代码适用于Windows和Unix派生(Linux,OSX,BSD等)

答案 2 :(得分:0)

接受的答案包含两个错误(它将错误的值作为字符串的长度传递给GETCWD,并留在C_NULL_CHAR中)。该答案纠正了这些错误,并使界面在Fortran中更加可用。

基本思想是相同的:使用C调用getcwd_getcwd,然后使用Fortran的C互操作性功能调用C包装器。在Fortran方面,包装子例程用于处理字符串长度,因此不必显式传递它。

此外,C_INTC_CHAR不必与Fortran上需要的默认整数和默认字符相同(尽管实际上我不知道{{1 }}和默认字符不同)。包装器也将其转换。另外,从C返回的字符串包含终止符C_CHAR,必须删除该终止符,才能在Fortran端使用该字符串。

C代码:

C_NULL_CHAR

Fortran代码:

#ifdef _WIN32
#include <direct.h>
#define GETCWD _getcwd
#else
#include <unistd.h>
#define GETCWD getcwd
#endif

/* Return 0 on success, 1 on error. */
int getCWDHelper(char *str, int len)
{
    return GETCWD(str, len) != str;
}

测试代码:

module cwd
    use iso_c_binding, only: C_INT, C_CHAR, C_NULL_CHAR
    implicit none
    private
    public :: getCWD

    interface
        function getCWDHelper(str, len) bind(C, name="getCWDHelper")
            use iso_c_binding, only: C_INT, C_CHAR
            integer(kind=C_INT) :: getCWDHelper
            character(kind=C_CHAR), intent(out) :: str(*)
            integer(kind=C_INT), value :: len
        end function getCWDHelper
    end interface

contains

    ! Writes the current working directory path into str.
    ! Returns 0 on success, or 1 on error.
    function getCWD(str)
        integer :: getCWD
        character(*), intent(out) :: str

        integer :: i, length
        character(len=len(str), kind=C_CHAR) :: str_copy

        ! Call the C helper, passing the length as the correct int kind
        getCWD = getCWDHelper(str_copy, len(str_copy, kind=C_INT))

        if (getCWD /= 0) then
            str = '' ! Error, clear the string
            return
        end if

        ! Copy the C_CHAR string to the output string,
        ! removing the C_NULL_CHAR and clearing the rest.
        length = index(str_copy, C_NULL_CHAR) - 1
        do i = 1, length
            str(i:i) = char(ichar(str_copy(i:i)))
        end do
        str(length+1:) = ''
    end function getCWD

end module

让返回值可分配并循环以获得足够的大小留给读者练习。