Line data Source code
1 : !> HSD backend — thin wrapper around hsd-fortran's parser and formatter.
2 : module hsd_data_hsd
3 : use hsd, only: hsd_table, hsd_error_t, hsd_load, hsd_load_string, &
4 : & hsd_dump, hsd_dump_to_string, HSD_STAT_IO_ERROR
5 : implicit none(type, external)
6 : private
7 :
8 : public :: hsd_backend_load, hsd_backend_load_string
9 : public :: hsd_backend_dump, hsd_backend_dump_to_string
10 :
11 : contains
12 :
13 : !> Load an HSD file into an hsd_table tree.
14 114 : subroutine hsd_backend_load(filename, root, error)
15 : character(len=*), intent(in) :: filename
16 : type(hsd_table), intent(out) :: root
17 : type(hsd_error_t), allocatable, intent(out), optional :: error
18 :
19 57 : call hsd_load(filename, root, error)
20 :
21 114 : end subroutine hsd_backend_load
22 :
23 : !> Load an HSD string into an hsd_table tree.
24 70 : subroutine hsd_backend_load_string(source, root, error, filename)
25 : character(len=*), intent(in) :: source
26 : type(hsd_table), intent(out) :: root
27 : type(hsd_error_t), allocatable, intent(out), optional :: error
28 : character(len=*), intent(in), optional :: filename
29 :
30 35 : call hsd_load_string(source, root, error, filename)
31 :
32 57 : end subroutine hsd_backend_load_string
33 :
34 : !> Dump an hsd_table tree to an HSD file.
35 16 : subroutine hsd_backend_dump(root, filename, error, pretty)
36 : type(hsd_table), intent(in) :: root
37 : character(len=*), intent(in) :: filename
38 : type(hsd_error_t), allocatable, intent(out), optional :: error
39 : logical, intent(in), optional :: pretty
40 :
41 16 : character(len=:), allocatable :: output
42 16 : integer :: unit_num, ios
43 :
44 : ! pretty is accepted for interface compatibility but not used
45 : ! (HSD output is always human-readable)
46 : if (.false. .and. present(pretty)) continue
47 :
48 16 : call hsd_dump_to_string(root, output)
49 :
50 : open(newunit=unit_num, file=filename, status="replace", action="write", &
51 16 : & iostat=ios)
52 16 : if (ios /= 0) then
53 0 : if (present(error)) then
54 0 : allocate(error)
55 0 : error%code = HSD_STAT_IO_ERROR
56 0 : error%message = "Failed to open file for writing: " // trim(filename)
57 : end if
58 0 : return
59 : end if
60 16 : write(unit_num, "(a)", iostat=ios) output
61 16 : close(unit_num)
62 :
63 16 : if (ios /= 0 .and. present(error)) then
64 0 : allocate(error)
65 0 : error%code = HSD_STAT_IO_ERROR
66 0 : error%message = "Failed to write to file: " // trim(filename)
67 : end if
68 :
69 51 : end subroutine hsd_backend_dump
70 :
71 : !> Dump an hsd_table tree to an HSD string.
72 52 : subroutine hsd_backend_dump_to_string(root, output, pretty)
73 : type(hsd_table), intent(in) :: root
74 : character(len=:), allocatable, intent(out) :: output
75 : logical, intent(in), optional :: pretty
76 :
77 : ! pretty is accepted for interface compatibility but not used
78 : if (.false. .and. present(pretty)) continue
79 :
80 52 : call hsd_dump_to_string(root, output)
81 :
82 16 : end subroutine hsd_backend_dump_to_string
83 :
84 : end module hsd_data_hsd
|