From cd211a400d1f4735a5af6835e38fb6422c6fc275 Mon Sep 17 00:00:00 2001 From: Milan Skocic Date: Mon, 1 Jan 2024 07:33:46 +0100 Subject: [PATCH] ENH: C API in the same file as the Fortran module. --- src/codata.f90 | 3 +-- src/codata_version.f90 | 23 ++++++++++++++++++++++- src/codata_version_capi.f90 | 33 --------------------------------- 3 files changed, 23 insertions(+), 36 deletions(-) delete mode 100644 src/codata_version_capi.f90 diff --git a/src/codata.f90 b/src/codata.f90 index 4d71b1a..d589b58 100644 --- a/src/codata.f90 +++ b/src/codata.f90 @@ -1,8 +1,7 @@ module codata !! Main module for CODATA library. use codata__version -use codata__version_capi use codata__constants implicit none -end module \ No newline at end of file +end module diff --git a/src/codata_version.f90 b/src/codata_version.f90 index f0698d7..d01214a 100644 --- a/src/codata_version.f90 +++ b/src/codata_version.f90 @@ -1,12 +1,13 @@ module codata__version !! Version use iso_fortran_env +use iso_c_binding implicit none private character(len=*), parameter :: version = "0.9.0" character(len=:), allocatable, target :: version_f - +character(len=:), allocatable, target :: version_c public :: get_version @@ -28,4 +29,24 @@ function get_version()result(fptr) fptr => version_f end function +function capi_get_version()bind(c,name="codata_get_version")result(cptr) + !! Get the version. + implicit none + + ! Returns + type(c_ptr) :: cptr + !! Pointer to version string. + + character(len=:), pointer :: fptr + fptr => get_version() + + if(allocated(version_c))then + deallocate(version_c) + endif + allocate(character(len=len(fptr)+1) :: version_c) + + version_c = fptr // c_null_char + cptr = c_loc(fptr) +end function + end module codata__version diff --git a/src/codata_version_capi.f90 b/src/codata_version_capi.f90 deleted file mode 100644 index 2151212..0000000 --- a/src/codata_version_capi.f90 +++ /dev/null @@ -1,33 +0,0 @@ -module codata__version_capi -!! Version C API -use iso_c_binding -use codata__version -implicit none -private - -character(len=:), allocatable, target :: version_c - - -contains - -function capi_get_version()bind(c,name="codata_get_version")result(cptr) - !! Get the version. - implicit none - - ! Returns - type(c_ptr) :: cptr - !! Pointer to version string. - - character(len=:), pointer :: fptr - fptr => get_version() - - if(allocated(version_c))then - deallocate(version_c) - endif - allocate(character(len=len(fptr)+1) :: version_c) - - version_c = fptr // c_null_char - cptr = c_loc(fptr) -end function - -end module codata__version_capi