28 module fms_string_utils_mod
29 use,
intrinsic :: iso_c_binding
30 use platform_mod,
only: r4_kind, r8_kind, i4_kind, i8_kind
55 type(c_ptr),
intent(inout) :: my_pointer(*)
57 integer(kind=c_int),
intent(in) :: p_size
58 integer(kind=c_int),
intent(inout) :: indices(*)
68 type(c_ptr),
intent(in) :: my_pointer(*)
69 integer(kind=c_int),
intent(in) :: p_size
70 character(kind=c_char),
intent(in) :: string_to_find(*)
71 integer(kind=c_int),
intent(inout) :: nfound
73 type(c_ptr) :: indices
82 type(c_ptr),
intent(in) :: my_pointer(*)
83 integer(kind=c_int),
intent(in) :: p_size
84 integer(kind=c_int) :: ntimes
91 character(kind=c_char),
intent(in) :: cs(*)
96 integer(c_size_t) pure function
c_strlen(s)
bind(c,name="strlen")
97 import c_size_t, c_ptr
98 type(c_ptr),
intent(in),
value :: s
102 subroutine c_free(ptr)
bind(c,name="free")
104 type(c_ptr),
value :: ptr
119 module procedure stringify_1d_r4, stringify_1d_r8
120 module procedure stringify_2d_r4, stringify_2d_r8
121 module procedure stringify_3d_r4, stringify_3d_r8
132 character(len=*),
target :: my_array(:)
133 type(c_ptr),
allocatable :: my_pointer(:)
137 if (
allocated(my_pointer))
call mpp_error(fatal,
"The c pointer array is &
138 already allocated. Deallocated before calling fms_array_to_pointer")
139 allocate(my_pointer(
size(my_array)))
141 do i = 1,
size(my_array)
142 my_pointer(i) = c_loc(my_array(i))
150 type(c_ptr),
intent(in) :: my_pointer(*)
151 integer,
intent(in) :: narray
152 character(len=:),
allocatable :: my_array(:)
154 character(len=:),
allocatable :: buffer
157 allocate(
character(len=255) :: my_array(narray))
174 type(c_ptr),
intent(in) :: my_pointer(*)
175 integer,
intent(in) :: narray
176 character(len=*),
intent(in) :: string_to_find
177 integer,
allocatable :: ifind(:)
180 character(len=:),
allocatable :: buffer
185 if (
allocated(ifind))
call mpp_error(fatal,
"The indices array is already allocated. &
186 Deallocate it before calling fms_find_my_string")
188 if (nfind .gt. 0)
then
189 allocate(ifind(nfind))
200 character (kind=c_char),
intent(in) :: cstring (*)
201 character(len=:),
allocatable :: fstring
208 type (c_ptr),
intent(in) :: cstring
209 character(len=:),
allocatable :: fstring
210 character(len=:,kind=c_char),
pointer :: string_buffer
211 integer(c_size_t) :: length
214 allocate (
character(len=length, kind=c_char) :: string_buffer)
216 character(len=length,kind=c_char),
pointer :: s
217 call c_f_pointer(cstring,s)
221 allocate(
character(len=length) :: fstring)
222 fstring = string_buffer
223 deallocate(string_buffer)
228 character (c_char),
intent (out) :: dest (:)
229 character (len=*),
intent (in) :: str_in
232 if (len(trim(str_in)) .ge.
size(dest))
call mpp_error(fatal, &
233 "The string "//trim(str_in)//
" is larger than the destination C string")
237 do i = 1, len(trim(str_in))
238 dest(i) = str_in(i:i)
245 class(*),
intent(in) :: v
246 character(*),
intent(in),
optional :: fmt
247 character(:),
allocatable ::
string
251 if (
present(fmt))
then
252 call mpp_error(warning,
"string(): Ignoring `fmt` argument for type `logical`")
260 type is (
integer(i4_kind))
261 allocate(
character(32) ::
string)
262 if (
present(fmt))
then
263 write(
string,
"(" // fmt //
")") v
269 type is (
integer(i8_kind))
270 allocate(
character(32) ::
string)
271 if (
present(fmt))
then
272 write(
string,
"(" // fmt //
")") v
278 type is (real(r4_kind))
279 allocate(
character(32) ::
string)
280 if (
present(fmt))
then
281 write(
string,
"(" // fmt //
")") v
287 type is (real(r8_kind))
288 allocate(
character(32) ::
string)
289 if (
present(fmt))
then
290 write(
string,
"(" // fmt //
")") v
297 call mpp_error(fatal,
"string(): Called with incompatible argument type. Possible types &
298 &include integer(4), integer(8), real(4), real(8), or logical.")
304 character(len=*),
intent(inout) :: dest
305 character(len=*),
intent(in) :: source
306 logical,
intent(in),
optional :: check_for_null
309 logical :: check_null
312 if (
present(check_for_null)) check_null = check_for_null
316 i = index(source, char(0)) - 1
319 if (i < 1 ) i = len_trim(source)
321 if (len_trim(source(1:i)) .gt. len(dest))
then
322 call mpp_error(fatal,
"The input destination string is not big enough to" &
323 //
" to hold the input source string.")
326 dest = adjustl(trim(source(1:i)))
329 #include "fms_string_utils_r4.fh"
330 #include "fms_string_utils_r8.fh"
332 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.