Debian Codegraph @

src/atlas_f/util/atlas_JSON_module.F90

raw
1 ! (C) Copyright 2013 ECMWF.
2 !
3 ! This software is licensed under the terms of the Apache Licence Version 2.0
4 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5 ! In applying this licence, ECMWF does not waive the privileges and immunities
6 ! granted to it by virtue of its status as an intergovernmental organisation nor
7 ! does it submit to any jurisdiction.
8
9 #include "atlas/atlas_f.h"
10
11 module atlas_JSON_module
12
13 implicit none
14
15 public :: atlas_PathName
16 public :: atlas_JSON
17
18 private
19
20
21 TYPE :: atlas_PathName
22 character(len=1), allocatable, private :: string(:)
23 contains
24 procedure :: str => atlas_PathName__str
25 END TYPE atlas_PathName
26
27 interface atlas_PathName
28 module procedure atlas_PathName__ctor_str
29 end interface
30
31
32
33 TYPE :: atlas_JSON
34 character(len=1), allocatable, private :: string(:)
35 contains
36 procedure :: str => atlas_JSON__str
37
38 procedure, public :: delete => atlas_JSON__delete
39
40 END TYPE atlas_JSON
41
42 interface atlas_JSON
43 module procedure atlas_JSON__ctor_str
44 module procedure atlas_JSON__ctor_path
45 end interface
46
47 !------------------------------------------------------------------------------
48 !========================================================
49 contains
50 !========================================================
51
52
53 function atlas_PathName__ctor_str(str) result(PathName)
54 type(atlas_PathName) :: PathName
55 character(len=*), intent(in) :: str
56 integer i, nchars
57 nchars = len(str)
58 allocate( PathName%string(nchars) )
59 do i=1,nchars
60 PathName%string(i) = str(i:i)
61 enddo
62 end function
63
64 function atlas_PathName__str(this) result(str)
65 character(len=:), allocatable :: str
66 class(atlas_PathName) :: this
67 integer i, nchars
68 nchars = size(this%string)
69 allocate(character(len=nchars) :: str)
70 do i=1,nchars
71 str(i:i) = this%string(i)
72 enddo
73 end function
74
75
76 function atlas_JSON__ctor_str(str) result(JSON)
77 type(atlas_JSON) :: JSON
78 character(len=*), intent(in) :: str
79 integer i, nchars
80 nchars = len(str)
81 allocate( JSON%string(nchars) )
82 do i=1,nchars
83 JSON%string(i) = str(i:i)
84 enddo
85 end function
86
87 function atlas_JSON__ctor_path(path) result(JSON)
88 use, intrinsic :: iso_c_binding, only : c_char, c_int, c_ptr
89 use atlas_atlas_read_file_c_binding
90 use fckit_c_interop_module, only : c_str, c_ptr_to_string, c_ptr_free
91 type(atlas_JSON) :: JSON
92 type(atlas_PathName), intent(in) :: path
93 character(len=:), allocatable :: str
94 integer (c_int) :: iret
95 type(c_ptr) :: str_cptr
96 integer(c_int) :: str_size
97 iret = atlas__read_file(c_str(path%str()), str_cptr, str_size)
98 allocate(character(len=str_size) :: str )
99 str = c_ptr_to_string(str_cptr)
100 call c_ptr_free(str_cptr)
101 JSON = atlas_JSON__ctor_str(str)
102 end function
103
104 function atlas_JSON__str(this) result(str)
105 character(len=:), allocatable :: str
106 class(atlas_JSON) :: this
107 integer i, nchars
108 nchars = size(this%string)
109 allocate(character(len=nchars) :: str)
110 do i=1,nchars
111 str(i:i) = this%string(i)
112 enddo
113 end function
114
115 subroutine atlas_JSON__delete(this)
116 class(atlas_JSON), intent(inout) :: this
117 if( allocated(this%string) ) deallocate(this%string)
118 end subroutine
119
120 end module atlas_JSON_module
121