File: optional_08.f90

package info (click to toggle)
lfortran 0.60.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,412 kB
  • sloc: cpp: 173,406; f90: 80,491; python: 17,586; ansic: 9,610; yacc: 2,356; sh: 1,401; fortran: 895; makefile: 37; javascript: 15
file content (78 lines) | stat: -rw-r--r-- 2,296 bytes parent folder | download
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
program optional_08
    implicit none

    type :: base
        character(len=:), allocatable :: working_dir
    end type base

    class(base), allocatable :: obj
    character(len=:), allocatable :: working_dir

    ! ---------------------------
    ! Initial setup
    ! ---------------------------
    working_dir = "Hello"

    if (.not. allocated(working_dir)) then
        error stop "ERROR: working_dir not allocated after assignment"
    end if

    ! ---------------------------
    ! Get command-line settings
    ! ---------------------------
    call get_command_line_settings(obj)

    if (.not. allocated(obj)) then
        error stop "ERROR: obj not allocated by get_command_line_settings"
    end if

    ! ---------------------------
    ! Extract working directory
    ! ---------------------------
    call get_working_dir(obj, working_dir)

    if (.not. allocated(working_dir)) then
        error stop "ERROR: working_dir not allocated after get_working_dir"
    end if

    print *, "SUCCESS"
    print *, "working_dir =", trim(working_dir)

contains

    subroutine get_working_dir(settings, working_dir_)
        class(base), optional, intent(in) :: settings
        character(len=:), allocatable, intent(out) :: working_dir_

        ! Validate presence
        if (.not. present(settings)) then
            error stop "ERROR: settings not present in get_working_dir"
        end if

        ! Validate allocation of component
        if (.not. allocated(settings%working_dir)) then
            error stop "ERROR: settings%working_dir not allocated"
        end if

        ! Safe assignment (auto-allocates working_dir_)
        working_dir_ = settings%working_dir
    end subroutine get_working_dir

    subroutine get_command_line_settings(cmd_settings)
        class(base), allocatable, intent(out) :: cmd_settings

        allocate(cmd_settings)

        if (.not. allocated(cmd_settings)) then
            error stop "ERROR: failed to allocate cmd_settings"
        end if

        ! Initialize component explicitly
        cmd_settings%working_dir = "Hello"

        if (.not. allocated(cmd_settings%working_dir)) then
            error stop "ERROR: cmd_settings%working_dir not allocated"
        end if
    end subroutine get_command_line_settings

end program