XBeach
C:/repositories/XBeach/trunk/src/xbeachlibrary/iso_c_utils.f90
Go to the documentation of this file.
00001 
00002 module iso_c_utils
00003    use iso_c_binding
00004    implicit none
00005 
00006    ! always use max stringlen for c arrays otherwise you have to specify lengths by hand
00007    integer(c_int), parameter :: MAXSTRINGLEN = 1024
00008 contains
00009    ! Utility functions, move these to interop module
00010    ! Make functions pure so they can be used as input arguments.
00011    integer(c_int) pure function strlen(char_array)
00012       character(c_char), intent(in) :: char_array(MAXSTRINGLEN)
00013       integer :: i
00014       strlen = 0
00015       do i = 1, size(char_array)
00016          if (char_array(i) .eq. C_NULL_CHAR) then
00017             strlen = i-1
00018             exit
00019          end if
00020       end do
00021    end function strlen
00022 
00023    integer(c_int) pure function strcmp(char_array1, char_array2)
00024       character(c_char), intent(in) :: char_array1(MAXSTRINGLEN)
00025       character(c_char), intent(in) :: char_array2(MAXSTRINGLEN)
00026 
00027       character(len=strlen(char_array1)) :: string1
00028       character(len=strlen(char_array2)) :: string2
00029 
00030       string1 = trim(char_array_to_string(char_array1))
00031       string2 = trim(char_array_to_string(char_array1))
00032       strcmp = 0
00033       if (llt(string1,string2)) strcmp = -1
00034       if (lgt(string1,string2)) strcmp = 1
00035 
00036    end function strcmp
00037 
00038 
00039 
00040    subroutine strcpy(str1, str2)
00041       character(c_char), intent(in) :: str1(MAXSTRINGLEN)
00042       character(c_char), intent(out) :: str2(MAXSTRINGLEN)
00043 
00044       integer :: i
00045       str2 = ' '
00046       do i=1,(strlen(str1) + 1)
00047          str2(i) = str1(i)
00048       end do
00049    end subroutine strcpy
00050 
00051    pure function char_array_to_string(char_array) result(string)
00052       character(c_char),intent(in) :: char_array(MAXSTRINGLEN)
00053       character(len=strlen(char_array)) :: string
00054       integer :: i
00055       do i = 1, strlen(char_array)
00056          string(i:i) = char_array(i)
00057       enddo
00058    end function char_array_to_string
00059 
00060    pure function string_to_char_array(string) result(char_array)
00061       character(len=*), intent(in) :: string
00062       character(kind=c_char,len=1) :: char_array(MAXSTRINGLEN)
00063       integer :: i
00064       do i = 1, len_trim(string)
00065          char_array(i) = string(i:i)
00066       enddo
00067       char_array(len_trim(string)+1) = C_NULL_CHAR
00068    end function string_to_char_array
00069 
00070 end module iso_c_utils
 All Classes Files Functions Variables Defines