@@ -922,6 +922,25 @@ end function stdlib_is_directory
922
922
923
923
end function is_directory
924
924
925
+ ! A Helper function to convert C character arrays to Fortran character strings
926
+ function to_f_char (c_str_ptr , len ) result(f_str)
927
+ type (c_ptr), intent (in ) :: c_str_ptr
928
+ ! length of the string excluding the null character
929
+ integer (kind= c_size_t), intent (in ) :: len
930
+ character (:), allocatable :: f_str
931
+
932
+ integer :: i
933
+ character (kind= c_char), pointer :: c_str(:)
934
+
935
+ call c_f_pointer(c_str_ptr, c_str, [len])
936
+
937
+ allocate (character (len= len) :: f_str)
938
+
939
+ do concurrent (i= 1 :len)
940
+ f_str(i:i) = c_str(i)
941
+ end do
942
+ end function to_f_char
943
+
925
944
! A helper function to get the result of the C function `strerror`.
926
945
! `strerror` is a function provided by `<string.h>`.
927
946
! It returns a string describing the meaning of `errno` in the C header `<errno.h>`
@@ -937,18 +956,11 @@ end function strerror
937
956
end interface
938
957
939
958
type (c_ptr) :: c_str_ptr
940
- integer (c_size_t) :: len, i
941
- character (kind= c_char), pointer :: c_str(:)
959
+ integer (c_size_t) :: len
942
960
943
961
c_str_ptr = strerror(len)
944
962
945
- call c_f_pointer(c_str_ptr, c_str, [len])
946
-
947
- allocate (character (len= len) :: str)
948
-
949
- do concurrent (i= 1 :len)
950
- str(i:i) = c_str(i)
951
- end do
963
+ str = to_f_char(c_str_ptr, len)
952
964
end function c_get_strerror
953
965
954
966
! ! makes an empty directory
@@ -1064,9 +1076,8 @@ end function stdlib_get_cwd
1064
1076
end interface
1065
1077
1066
1078
type (c_ptr) :: c_str_ptr
1067
- integer (c_size_t) :: len, i
1079
+ integer (c_size_t) :: len
1068
1080
integer :: stat
1069
- character (kind= c_char), pointer :: c_str(:)
1070
1081
1071
1082
c_str_ptr = stdlib_get_cwd(len, stat)
1072
1083
@@ -1075,13 +1086,8 @@ end function stdlib_get_cwd
1075
1086
call err0% handle(err)
1076
1087
end if
1077
1088
1078
- call c_f_pointer(c_str_ptr, c_str, [len])
1079
-
1080
- allocate (character (len= len) :: cwd)
1089
+ cwd = to_f_char(c_str_ptr, len)
1081
1090
1082
- do concurrent (i= 1 :len)
1083
- cwd(i:i) = c_str(i)
1084
- end do
1085
1091
end subroutine get_cwd
1086
1092
1087
1093
subroutine set_cwd (path , err )
@@ -1124,21 +1130,13 @@ end function process_null_device
1124
1130
1125
1131
end interface
1126
1132
1127
- integer (c_size_t) :: i, len
1133
+ integer (c_size_t) :: len
1128
1134
type (c_ptr) :: c_path_ptr
1129
- character (kind= c_char), pointer :: c_path(:)
1130
1135
1131
1136
! Call the C function to get the null device path and its length
1132
1137
c_path_ptr = process_null_device(len)
1133
- call c_f_pointer(c_path_ptr,c_path,[len])
1134
1138
1135
- ! Allocate the Fortran string with the length returned from C
1136
- allocate (character (len= len) :: path)
1137
-
1138
- do concurrent (i= 1 :len)
1139
- path(i:i) = c_path(i)
1140
- end do
1141
-
1139
+ path = to_f_char(c_path_ptr, len)
1142
1140
end function null_device
1143
1141
1144
1142
! > Delete a file at the given path.
0 commit comments