aboutsummaryrefslogtreecommitdiff
path: root/platform.F90
blob: 73200d29f30a120091549458ccb87702d353163d (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
! Copyright (c) 2020 Jeffrey Armstrong <jeff@rainbow-100.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

module platform
implicit none

#ifdef WINDOWS
    character, parameter::dir_sep = '\'
#else
    character, parameter::dir_sep = '/'
#endif

    character(*), parameter::favorites_file = "favorites.gmi"

contains

    subroutine make_directory(dir)
    use iso_c_binding
    implicit none

        character(*), intent(in)::dir
        
#ifdef WINDOWS
        character(kind=c_char, len=:), allocatable, target::passdir
        interface
            function CreateDirectory(dir, ignored) bind(c, name="CreateDirectoryA")
            use iso_c_binding
            type(c_ptr), value::dir
            type(c_ptr), value::ignored
            integer(kind=c_int)::CreateDirectory
            end function CreateDirectory
        end interface
        
        allocate(character(len=(len_trim(dir)+1)) :: passdir)
        passdir = trim(dir)//c_null_char
        if(CreateDirectory(c_loc(passdir), c_null_ptr) == 0) then
            print *, "Could not create directory, maybe exists?"
        end if
        deallocate(passdir)
            
        !call execute_command_line('mkdir "'//trim(dir)//'"')
#else
        call execute_command_line('mkdir -p "'//trim(dir)//'"')
#endif
    end subroutine make_directory          

    subroutine get_settings_directory(dir)
    use iso_c_binding
    implicit none
        
        character(*), intent(out)::dir
#ifdef WINDOWS
        
        interface
            function SHGetFolderPath(hwnd, csidl, htoken, dwflags, path) bind(c, name="SHGetFolderPathA")
            use iso_c_binding
            type(c_ptr), value::hwnd
            integer(kind=c_int), value::csidl
            type(c_ptr), value::htoken
            integer(kind=c_int32_t), value::dwflags
            character(kind=c_char), dimension(260)::path
            integer(kind=c_intptr_t)::SHGetFolderPath
            end function SHGetFolderPath
        end interface
        
        integer(kind=c_intptr_t), parameter::S_OK = 0
        integer(kind=c_int), parameter::CSIDL_APPDATA = 26
        
        character(kind=c_char), dimension(260)::path
        integer::i
        
        if(SHGetFolderPath(c_null_ptr, CSIDL_APPDATA, c_null_ptr, 0, path) == S_OK) then
            dir = " "
            i = 1
            do while(path(i) /= c_null_char .and. i < 260)
                dir(i:i) = path(i)
                i = i + 1
            end do
        else
            Print *, "Warning: Could not access CSIDL_APPDATA"
            call get_environment_variable("HOME", value=dir)
        end if
        
        if(dir(len_trim(dir):len_trim(dir)) /= dir_sep) then
            dir = trim(dir)//dir_sep
        end if
        dir = trim(dir)//"LR-87"
#else
        call get_environment_variable("HOME", value=dir)
        if(dir(len_trim(dir):len_trim(dir)) /= dir_sep) then
            dir = trim(dir)//dir_sep
        end if
        dir = trim(dir)//".lr87"
#endif        
    
        ! Harmless
        call make_directory(dir)

    end subroutine get_settings_directory
    
    subroutine get_favorites_file(filename)
    implicit none
    
        character(*), intent(out)::filename
        
        call get_settings_directory(filename)
        filename = trim(filename)//dir_sep//favorites_file
        
    end subroutine get_favorites_file
    
end module platform