29 module fms_string_utils_mod
30 use,
intrinsic :: iso_c_binding
31 use platform_mod,
only: r4_kind, r8_kind, i4_kind, i8_kind
56 type(c_ptr),
intent(inout) :: my_pointer(*)
58 integer(kind=c_int),
intent(in) :: p_size
59 integer(kind=c_int),
intent(inout) :: indices(*)
69 type(c_ptr),
intent(in) :: my_pointer(*)
70 integer(kind=c_int),
intent(in) :: p_size
71 character(kind=c_char),
intent(in) :: string_to_find(*)
72 integer(kind=c_int),
intent(inout) :: nfound
74 type(c_ptr) :: indices
83 type(c_ptr),
intent(in) :: my_pointer(*)
84 integer(kind=c_int),
intent(in) :: p_size
85 integer(kind=c_int) :: ntimes
92 character(kind=c_char),
intent(in) :: cs(*)
97 integer(c_size_t) pure function
c_strlen(s)
bind(c,name="strlen")
98 import c_size_t, c_ptr
99 type(c_ptr),
intent(in),
value :: s
103 subroutine c_free(ptr)
bind(c,name="free")
105 type(c_ptr),
value :: ptr
120 module procedure stringify_1d_r4, stringify_1d_r8
121 module procedure stringify_2d_r4, stringify_2d_r8
122 module procedure stringify_3d_r4, stringify_3d_r8
133 character(len=*),
target :: my_array(:)
134 type(c_ptr),
allocatable :: my_pointer(:)
138 if (
allocated(my_pointer))
call mpp_error(fatal,
"The c pointer array is &
139 already allocated. Deallocated before calling fms_array_to_pointer")
140 allocate(my_pointer(
size(my_array)))
142 do i = 1,
size(my_array)
143 my_pointer(i) = c_loc(my_array(i))
151 type(c_ptr),
intent(in) :: my_pointer(*)
152 integer,
intent(in) :: narray
153 character(len=:),
allocatable :: my_array(:)
155 character(len=:),
allocatable :: buffer
158 allocate(
character(len=255) :: my_array(narray))
175 type(c_ptr),
intent(in) :: my_pointer(*)
176 integer,
intent(in) :: narray
177 character(len=*),
intent(in) :: string_to_find
178 integer,
allocatable :: ifind(:)
181 character(len=:),
allocatable :: buffer
186 if (
allocated(ifind))
call mpp_error(fatal,
"The indices array is already allocated. &
187 Deallocate it before calling fms_find_my_string")
189 if (nfind .gt. 0)
then
190 allocate(ifind(nfind))
201 character (kind=c_char),
intent(in) :: cstring (*)
202 character(len=:),
allocatable :: fstring
209 type (c_ptr),
intent(in) :: cstring
210 character(len=:),
allocatable :: fstring
211 character(len=:,kind=c_char),
pointer :: string_buffer
212 integer(c_size_t) :: length
215 allocate (
character(len=length, kind=c_char) :: string_buffer)
217 character(len=length,kind=c_char),
pointer :: s
218 call c_f_pointer(cstring,s)
222 allocate(
character(len=length) :: fstring)
223 fstring = string_buffer
224 deallocate(string_buffer)
229 character (c_char),
intent (out) :: dest (:)
230 character (len=*),
intent (in) :: str_in
233 if (len(trim(str_in)) .ge.
size(dest))
call mpp_error(fatal, &
234 "The string "//trim(str_in)//
" is larger than the destination C string")
238 do i = 1, len(trim(str_in))
239 dest(i) = str_in(i:i)
246 class(*),
intent(in) :: v
247 character(*),
intent(in),
optional :: fmt
248 character(:),
allocatable ::
string
252 if (
present(fmt))
then
253 call mpp_error(warning,
"string(): Ignoring `fmt` argument for type `logical`")
261 type is (
integer(i4_kind))
262 allocate(
character(32) ::
string)
263 if (
present(fmt))
then
264 write(
string,
"(" // fmt //
")") v
270 type is (
integer(i8_kind))
271 allocate(
character(32) ::
string)
272 if (
present(fmt))
then
273 write(
string,
"(" // fmt //
")") v
279 type is (real(r4_kind))
280 allocate(
character(32) ::
string)
281 if (
present(fmt))
then
282 write(
string,
"(" // fmt //
")") v
288 type is (real(r8_kind))
289 allocate(
character(32) ::
string)
290 if (
present(fmt))
then
291 write(
string,
"(" // fmt //
")") v
298 call mpp_error(fatal,
"string(): Called with incompatible argument type. Possible types &
299 &include integer(4), integer(8), real(4), real(8), or logical.")
305 character(len=*),
intent(inout) :: dest
306 character(len=*),
intent(in) :: source
307 logical,
intent(in),
optional :: check_for_null
310 logical :: check_null
313 if (
present(check_for_null)) check_null = check_for_null
317 i = index(source, char(0)) - 1
320 if (i < 1 ) i = len_trim(source)
322 if (len_trim(source(1:i)) .gt. len(dest))
then
323 call mpp_error(fatal,
"The input destination string is not big enough to" &
324 //
" to hold the input source string.")
327 dest = adjustl(trim(source(1:i)))
330 #include "fms_string_utils_r4.fh"
331 #include "fms_string_utils_r8.fh"
333 end module fms_string_utils_mod
Converts a C string to a Fortran string.
Converts an array of real numbers to a string.
character(len=:) function, allocatable cstring_fortran_conversion(cstring)
Converts a C-string to a pointer and then to a Fortran string.
character(len=:) function, dimension(:), allocatable, public fms_pointer_to_array(my_pointer, narray)
Convert an array of c pointers back to a character array.
subroutine, public string_copy(dest, source, check_for_null)
Safely copy a string from one buffer to another.
character(:) function, allocatable, public string(v, fmt)
Converts a number or a Boolean value to a string.
type(c_ptr) function, dimension(:), allocatable, public fms_array_to_pointer(my_array)
Converts a character array to an array of c pointers!
integer function, dimension(:), allocatable, public fms_find_my_string(my_pointer, narray, string_to_find)
Searches through a SORTED array of pointers for a string.
character(len=:) function, allocatable cpointer_fortran_conversion(cstring)
Converts a C-string returned from a TYPE(C_PTR) function to a fortran string with type character.
subroutine, public fms_f2c_string(dest, str_in)
Copies a Fortran string into a C string and puts c_null_char in any trailing spaces.
Finds the length of a C-string.
converts a kind=c_char to type c_ptr
Private c function that finds a string in a SORTED array of c pointers.
c function that finds the number of unique strings in an array of c pointers
Sorts an array of pointers (my pointer) of size (p_size) in alphabetical order.