| 1 |
! |
| 2 |
! KIM-API: An API for interatomic models |
| 3 |
! Copyright (c) 2013--2022, Regents of the University of Minnesota. |
| 4 |
! All rights reserved. |
| 5 |
! |
| 6 |
! Contributors: |
| 7 |
! Ryan S. Elliott |
| 8 |
! |
| 9 |
! SPDX-License-Identifier: LGPL-2.1-or-later |
| 10 |
! |
| 11 |
! This library is free software; you can redistribute it and/or |
| 12 |
! modify it under the terms of the GNU Lesser General Public |
| 13 |
! License as published by the Free Software Foundation; either |
| 14 |
! version 2.1 of the License, or (at your option) any later version. |
| 15 |
! |
| 16 |
! This library is distributed in the hope that it will be useful, |
| 17 |
! but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
! Lesser General Public License for more details. |
| 20 |
! |
| 21 |
! You should have received a copy of the GNU Lesser General Public License |
| 22 |
! along with this library; if not, write to the Free Software Foundation, |
| 23 |
! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 |
! |
| 25 |
|
| 26 |
! |
| 27 |
! Release: This file is part of the kim-api.git repository. |
| 28 |
! |
| 29 |
|
| 30 |
module kim_convert_string_module |
| 31 |
implicit none |
| 32 |
private |
| 33 |
|
| 34 |
public & |
| 35 |
kim_convert_c_char_array_to_string, & |
| 36 |
kim_convert_c_char_ptr_to_string, & |
| 37 |
kim_convert_string_to_c_char_array |
| 38 |
|
| 39 |
contains |
| 40 |
recursive subroutine kim_convert_c_char_array_to_string(c_char_array, string) |
| 41 |
use, intrinsic :: iso_c_binding |
| 42 |
implicit none |
| 43 |
character(len=1, kind=c_char), intent(in) :: c_char_array(:) |
| 44 |
character(len=*, kind=c_char), intent(out) :: string |
| 45 |
|
| 46 |
integer(c_int) :: i |
| 47 |
integer(c_int) :: null_index |
| 48 |
integer(c_int) :: length |
| 49 |
|
| 50 |
length = len(string) + 1 |
| 51 |
do null_index = 1, length |
| 52 |
if (c_char_array(null_index) == c_null_char) exit |
| 53 |
end do |
| 54 |
if (null_index == length) then |
| 55 |
null_index = len(string) |
| 56 |
else |
| 57 |
null_index = null_index - 1 |
| 58 |
end if |
| 59 |
string = "" |
| 60 |
do i = 1, null_index |
| 61 |
string(i:i) = c_char_array(i) |
| 62 |
end do |
| 63 |
end subroutine kim_convert_c_char_array_to_string |
| 64 |
|
| 65 |
recursive subroutine kim_convert_c_char_ptr_to_string(c_char_ptr, string) |
| 66 |
use, intrinsic :: iso_c_binding |
| 67 |
implicit none |
| 68 |
type(c_ptr), intent(in) :: c_char_ptr |
| 69 |
character(len=*, kind=c_char), intent(out) :: string |
| 70 |
|
| 71 |
character(len=1, kind=c_char), pointer :: fp(:) |
| 72 |
integer(c_int) :: length |
| 73 |
|
| 74 |
if (c_associated(c_char_ptr)) then |
| 75 |
length = len(string) + 1 |
| 76 |
call c_f_pointer(c_char_ptr, fp, [length]) |
| 77 |
call kim_convert_c_char_array_to_string(fp, string) |
| 78 |
else |
| 79 |
string = "" |
| 80 |
end if |
| 81 |
end subroutine kim_convert_c_char_ptr_to_string |
| 82 |
|
| 83 |
recursive subroutine kim_convert_string_to_c_char_array(string, c_char_array) |
| 84 |
use, intrinsic :: iso_c_binding |
| 85 |
implicit none |
| 86 |
character(len=*, kind=c_char), intent(in) :: string |
| 87 |
character(len=1, kind=c_char), intent(out) :: c_char_array(:) |
| 88 |
|
| 89 |
c_char_array(:) = trim(string)//c_null_char |
| 90 |
end subroutine kim_convert_string_to_c_char_array |
| 91 |
end module kim_convert_string_module |