File: string1.f90

package info (click to toggle)
lfortran 0.58.0-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 54,508 kB
  • sloc: cpp: 162,179; f90: 68,251; python: 17,476; ansic: 6,278; yacc: 2,334; sh: 1,317; fortran: 892; makefile: 34; javascript: 15
file content (43 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module fpm_strings
use iso_c_binding, only: c_char, c_ptr, c_size_t
implicit none

public :: f_string, string_t

type string_t
    character(len=:), allocatable :: s
end type

interface f_string
    module procedure f_string, f_string_cptr, f_string_cptr_n
end interface f_string

contains

function f_string(c_string)
    character(len=1), intent(in) :: c_string(:)
    character(:), allocatable :: f_string
end function f_string

function f_string_cptr(cptr) result(s)
    type(c_ptr), intent(in), value :: cptr
    character(len=:, kind=c_char), allocatable :: s

    interface
        function c_strlen(s) result(r) bind(c, name="strlen")
            import c_size_t, c_ptr
            type(c_ptr), intent(in), value :: s
            integer(kind=c_size_t) :: r
        end function
    end interface

    s = f_string_cptr_n(cptr, c_strlen(cptr))
end function

function f_string_cptr_n(cptr, n) result(s)
    type(c_ptr), intent(in), value :: cptr
    integer(kind=c_size_t), intent(in) :: n
    character(len=n, kind=c_char) :: s
end function

end module fpm_strings