Here's the code that take a pointer to a 'C' style string and returns the characters in a FORTRAN CHARACTER data type.
Note: You have to declare the function in the calling program with a specific length. That said, you could use a similar function to return the length of the string BEFORE calling the routine that declares the function and use that, if it something that really, truly needs to be variable.
!FTN95 application...
PROGRAM main
INTEGER k
character*(255),external:: C_TO_F_STRING ! has to be assigned a specific length here
character*255:: my_string='This is me'//char(0) ! simulated 'C' string, length s/b 10
character*32:: result
! loc(mystring might be the return value of a 'C' funtion of the form
! char* c_function(variables)
result = c_to_f_string(loc(my_string))
print *,'Actual non blank data returned=',len_trim(result),'=',trim(result),' full result=[',result,']'
k= len(c_to_f_string(loc(my_string)))
print *,'Function length returned=',k
pause
END PROGRAM main
character*(*) function C_TO_F_STRING(c_string_pointer)
! no error checking has been performed here (like for a NULL result from the allocate()
integer(7):: c_string_pointer ! this is a pointer to somewhere in memory
character(:) , allocatable:: f_string
integer:: array_index ! this will be used to determine how long the string is before allocation
array_index = 0
do while(ichar(ccore1(c_string_pointer+array_index)) .ne.0)
array_index = array_index+1
end do
if(array_index.le.0)array_index = 1 ! always return something
allocate(character*(array_index):: f_string)
f_string = ' ' ! make the entire string blank, regardless
print *,'Length of result string to be allocated=',len(f_string) ! comment out for production
do i=1,array_index
f_string(i:i) = ccore1(c_string_pointer+i-1)
end do
c_to_f_string = f_string
deallocate(f_string)
return
end
Here's the run-time. Note: The 'full result' here is shown truncated because multiple spaces are removed when posting.
'
k= len(c_to_f_string(loc(my_string)))
print ,'Function length returned=',k
pause
END PROGRAM main
character(*) function C_TO_F_STRING(c_string_pointer)
! no error checking has been performed here (like for a NULL result from the allocate()
integer(7):: c_string_pointer ! this is a pointer to somewhere in memory
character(:) , allocatable:: f_string
integer:: array_index ! this will be used to determine how long the string is before allocation
array_index = 0
do while(ichar(ccore1(c_string_pointer+array_index)) .ne.0)
array_index = array_index+1
end do
if(array_index.le.0)array_index = 1 ! always return something
allocate(character*(array_index):: f_string)
f_string = ' ' ! make the entire string blank, regardless
print *,'Length of result string to be allocated=',len(f_string) ! comment out for production
do i=1,array_index
f_string(i:i) = ccore1(c_string_pointer+i-1)
end do
c_to_f_string = f_string
deallocate(f_string)
return
end
Here's the run-time. Note: The 'full result' here is shown truncated because multiple spaces are removed when posting.
[quote:28a3d39e25] Length of result string to be allocated= 10
Actual non blank data returned= 10=This is me full result=[This is me ]
Length of result string to be allocated= 10
Function length returned= 255