Skip to content

Commit 481cadf

Browse files
authored
fix: clean up string routines for fms_string_utils_mod (NOAA-GFDL#953)
1 parent 6d3c464 commit 481cadf

10 files changed

+137
-180
lines changed

CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ list(APPEND fms_fortran_src_files
183183
# Collect FMS C source files
184184
list(APPEND fms_c_src_files
185185
affinity/affinity.c
186-
fms/fms_c.c
187186
mosaic/create_xgrid.c
188187
mosaic/gradient_c2l.c
189188
mosaic/interp.c
@@ -198,7 +197,6 @@ list(APPEND fms_c_src_files
198197
list(APPEND fms_header_files
199198
include/file_version.h
200199
include/fms_platform.h
201-
fms/fms_c.h
202200
)
203201

204202
# Standard FMS compiler definitions

Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ SUBDIRS = \
3939
mpp \
4040
constants \
4141
memutils \
42+
string_utils \
4243
fms2_io \
4344
mosaic2 \
4445
fms \
4546
parser \
46-
string_utils \
4747
affinity \
4848
mosaic \
4949
time_manager \

fms/Makefile.am

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ noinst_LTLIBRARIES = libfms.la
3131

3232
# Each convenience library depends on its source.
3333
libfms_la_SOURCES = \
34-
fms_c.c \
35-
fms_c.h \
3634
fms.F90 \
3735
fms_io.F90 \
3836
fms_io_unstructured_field_exist.inc \

fms/fms.F90

+1-89
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ module fms_mod
164164
use fms2_io_mod, only: fms2_io_init
165165
use memutils_mod, only: print_memuse_stats, memutils_init
166166
use grid2_mod, only: grid_init, grid_end
167+
use fms_string_utils_mod, only: fms_c2f_string, fms_cstring2cpointer, string
167168

168169
use, intrinsic :: iso_c_binding
169170

@@ -294,40 +295,6 @@ module fms_mod
294295

295296
!> @}
296297

297-
!> Converts a number to a string
298-
!> @ingroup fms_mod
299-
interface string
300-
module procedure string_from_integer
301-
module procedure string_from_real
302-
end interface
303-
!> Converts a C string to a Fortran string
304-
!> @ingroup fms_mod
305-
interface fms_c2f_string
306-
module procedure cstring_fortran_conversion
307-
module procedure cpointer_fortran_conversion
308-
end interface
309-
!> C functions
310-
interface
311-
!> @brief converts a kind=c_char to type c_ptr
312-
pure function fms_cstring2cpointer (cs) result (cp) bind(c, name="cstring2cpointer")
313-
import c_char, c_ptr
314-
character(kind=c_char), intent(in) :: cs(*) !< C string input
315-
type (c_ptr) :: cp !< C pointer
316-
end function fms_cstring2cpointer
317-
318-
!> @brief Finds the length of a C-string
319-
integer(c_size_t) pure function c_strlen(s) bind(c,name="strlen")
320-
import c_size_t, c_ptr
321-
type(c_ptr), intent(in), value :: s !< A C-string whose size is desired
322-
end function
323-
324-
!> @brief Frees a C pointer
325-
subroutine c_free(ptr) bind(c,name="free")
326-
import c_ptr
327-
type(c_ptr), value :: ptr !< A C-pointer to free
328-
end subroutine
329-
end interface
330-
331298
!> @addtogroup fms_mod
332299
!> @{
333300
contains
@@ -801,61 +768,6 @@ function monotonic_array ( array, direction )
801768

802769
end function monotonic_array
803770

804-
!! Functions from the old fms_io
805-
!> @brief Converts an integer to a string
806-
!!
807-
!> This has been updated from the fms_io function.
808-
function string_from_integer(i) result (res)
809-
integer, intent(in) :: i !< Integer to be converted to a string
810-
character(:),allocatable :: res !< String converted frominteger
811-
character(range(i)+2) :: tmp !< Temp string that is set to correct size
812-
write(tmp,'(i0)') i
813-
res = trim(tmp)
814-
return
815-
816-
end function string_from_integer
817-
818-
!#######################################################################
819-
!> @brief Converts a real to a string
820-
function string_from_real(a)
821-
real, intent(in) :: a
822-
character(len=32) :: string_from_real
823-
824-
write(string_from_real,*) a
825-
826-
return
827-
828-
end function string_from_real
829-
830-
!> \brief Converts a C-string to a pointer and then to a Fortran string
831-
function cstring_fortran_conversion (cstring) result(fstring)
832-
character (kind=c_char), intent(in) :: cstring (*) !< Input C-string
833-
character(len=:), allocatable :: fstring !< The fortran string returned
834-
fstring = cpointer_fortran_conversion(fms_cstring2cpointer(cstring))
835-
end function cstring_fortran_conversion
836-
837-
!> \brief Converts a C-string returned from a TYPE(C_PTR) function to
838-
!! a fortran string with type character.
839-
function cpointer_fortran_conversion (cstring) result(fstring)
840-
type (c_ptr), intent(in) :: cstring !< Input C-pointer
841-
character(len=:), allocatable :: fstring !< The fortran string returned
842-
character(len=:,kind=c_char), pointer :: string_buffer !< A temporary pointer to between C and Fortran
843-
integer(c_size_t) :: length !< The string length
844-
845-
length = c_strlen(cstring)
846-
allocate (character(len=length, kind=c_char) :: string_buffer)
847-
block
848-
character(len=length,kind=c_char), pointer :: s
849-
call c_f_pointer(cstring,s) ! Recovers a view of the C string
850-
string_buffer = s ! Copies the string contents
851-
end block
852-
853-
allocate(character(len=length) :: fstring) !> Set the length of fstring
854-
fstring = string_buffer
855-
deallocate(string_buffer)
856-
857-
end function cpointer_fortran_conversion
858-
859771
!#######################################################################
860772
!> @brief Prints to the log file (or a specified unit) the version id string and
861773
!! tag name.

fms2_io/fms2_io.F90

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ module fms2_io_mod
103103
public :: set_filename_appendix
104104
public :: get_instance_filename
105105
public :: nullify_filename_appendix
106-
public :: string2
107106
public :: flush_file
108107
!> @}
109108

fms2_io/fms_io_utils.F90

+1-78
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module fms_io_utils_mod
3636
mpp_get_current_ntile, mpp_get_tile_id, &
3737
mpp_get_UG_domain_ntiles, mpp_get_UG_domain_tile_id
3838
use platform_mod
39+
use fms_string_utils_mod, only: string_copy
3940
implicit none
4041
private
4142

@@ -55,7 +56,6 @@ module fms_io_utils_mod
5556
public :: put_array_section
5657
public :: get_array_section
5758
public :: get_data_type_string
58-
public :: string2
5959
public :: open_check
6060
public :: string_compare
6161
public :: restart_filepath_mangle
@@ -76,14 +76,6 @@ module fms_io_utils_mod
7676
type(char_linked_list), pointer :: head => null()
7777
endtype char_linked_list
7878

79-
!> @brief Converts a given integer or real into a character string
80-
!> @ingroup fms_io_utils_mod
81-
interface string2
82-
module procedure string_from_integer2
83-
module procedure string_from_real2
84-
end interface string2
85-
86-
8779
!> @ingroup fms_io_utils_mod
8880
interface parse_mask_table
8981
module procedure parse_mask_table_2d
@@ -227,36 +219,6 @@ subroutine openmp_thread_trap()
227219
#endif
228220
end subroutine openmp_thread_trap
229221

230-
231-
!> @brief Safely copy a string from one buffer to another.
232-
subroutine string_copy(dest, source, check_for_null)
233-
character(len=*), intent(inout) :: dest !< Destination string.
234-
character(len=*), intent(in) :: source !< Source string.
235-
logical, intent(in), optional :: check_for_null !<Flag indicating to test for null character
236-
237-
integer :: i
238-
logical :: check_null
239-
240-
check_null = .false.
241-
if (present(check_for_null)) check_null = check_for_null
242-
243-
i = 0
244-
if (check_null) then
245-
i = index(source, char(0)) - 1
246-
endif
247-
248-
if (i < 1 ) i = len_trim(source)
249-
250-
if (len_trim(source(1:i)) .gt. len(dest)) then
251-
call error("The input destination string is not big enough to" &
252-
//" to hold the input source string.")
253-
endif
254-
dest = ""
255-
dest = adjustl(trim(source(1:i)))
256-
257-
end subroutine string_copy
258-
259-
260222
!> @brief Compare strings.
261223
!! @return Flag telling if the strings are the same.
262224
function string_compare(string1, string2, ignore_case) &
@@ -870,45 +832,6 @@ subroutine get_instance_filename(name_in,name_out)
870832

871833
end subroutine get_instance_filename
872834

873-
function string_from_integer2(n)
874-
integer, intent(in) :: n
875-
character(len=16) :: string_from_integer2
876-
if(n<0) then
877-
call mpp_error(FATAL, 'fms2_io_mod: n should be non-negative integer, contact developer')
878-
else if( n<10 ) then
879-
write(string_from_integer2,'(i1)') n
880-
else if( n<100 ) then
881-
write(string_from_integer2,'(i2)') n
882-
else if( n<1000 ) then
883-
write(string_from_integer2,'(i3)') n
884-
else if( n<10000 ) then
885-
write(string_from_integer2,'(i4)') n
886-
else if( n<100000 ) then
887-
write(string_from_integer2,'(i5)') n
888-
else if( n<1000000 ) then
889-
write(string_from_integer2,'(i6)') n
890-
else if( n<10000000 ) then
891-
write(string_from_integer2,'(i7)') n
892-
else if( n<100000000 ) then
893-
write(string_from_integer2,'(i8)') n
894-
else
895-
call mpp_error(FATAL, 'fms2_io_mod: n is greater than 1e8, contact developer')
896-
end if
897-
898-
return
899-
900-
end function string_from_integer2
901-
902-
function string_from_real2(a)
903-
real, intent(in) :: a
904-
character(len=32) :: string_from_real2
905-
906-
write(string_from_real2,*) a
907-
908-
return
909-
910-
end function string_from_real2
911-
912835
include "array_utils.inc"
913836
include "array_utils_char.inc"
914837
include "get_data_type_string.inc"

mosaic2/grid2.F90

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module grid2_mod
2929
use constants_mod, only : PI, radius
3030
use fms2_io_mod, only : get_global_attribute, read_data, global_att_exists, &
3131
variable_exists, file_exists, open_file, close_file, get_variable_size, &
32-
FmsNetcdfFile_t, string => string2
32+
FmsNetcdfFile_t
33+
use fms_string_utils_mod, only: string
3334
use mosaic2_mod, only : get_mosaic_ntiles, get_mosaic_xgrid_size, get_mosaic_grid_sizes, &
3435
get_mosaic_xgrid, calc_mosaic_grid_area, calc_mosaic_grid_great_circle_area
3536

parser/yaml_parser.F90

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module yaml_parser_mod
3030

3131
#ifdef use_yaml
3232
use fms_mod, only: fms_c2f_string
33-
use fms_io_utils_mod, only: string_copy
33+
use fms_string_utils_mod, only: string_copy
3434
use platform_mod
3535
use mpp_mod
3636
use iso_c_binding

0 commit comments

Comments
 (0)