aboutsummaryrefslogtreecommitdiff
path: root/files.f90
blob: 1c76311d98182aaf4510a762ad33ddf8c62b5d84 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
! 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 file_handling

    integer, parameter::file_type_plain_text    = 1
    integer, parameter::file_type_gemini        = 2

    character(17), parameter::end_indicator = "** END OF FILE **"

contains

    subroutine mark_file_end(unit_number)
    implicit none
    
        integer, intent(in)::unit_number
        Write(unit_number, '(A1)', advance='no') new_line('a')
        Write(unit_number, '(A17)') end_indicator
        
    end subroutine mark_file_end

    function is_file_end_marker(text)
    implicit none
    
        character(*), intent(in)::text
        logical::is_file_end_marker
        
        is_file_end_marker = (index(text, end_indicator) > 0)
        
    end function is_file_end_marker
    
    subroutine skip_line(unit_number)
    implicit none
    
        integer, intent(in)::unit_number
        
        character::c
        integer::iostatus
        
        read(unit_number, '(A1)', advance='no', iostat=iostatus) c
        do while(c /= CHAR(10) .AND. iostatus == 0)
            read(unit_number, '(A1)', advance='no', iostat=iostatus) c
        end do
        
    end subroutine skip_line
    
    subroutine read_line_text(unit_number, res, iostatus)
    implicit none
    
        character(len=:), allocatable, intent(out)::res
        integer, intent(in)::unit_number
        integer, intent(out)::iostatus
        integer::startpos, endpos, length, i
        character::c
        
        inquire(unit=unit_number, pos=startpos)
        
        endpos = startpos
        read(unit_number, '(A1)', advance='no', iostat=iostatus) c
        do while(c /= CHAR(10) .AND. iostatus == 0)
            endpos = endpos + 1
            read(unit_number, '(A1)', advance='no', iostat=iostatus) c
        end do
        
        length = (endpos - startpos + 1)
        
        allocate(character(len=length) :: res)
        res = repeat(' ', length)
        
        ! Rewind seems necessary, especially on non-Windows...
        rewind(unit_number)
        
        read(unit_number, '(A1)', pos=startpos, advance='no', iostat=iostatus) c
        if(iostatus == 0) then
            res(1:1) = c
            do i=2, length
                read(unit_number, '(A1)', advance='no', iostat=iostatus) c
                
                if(iostatus /= 0) then
                    exit
                end if
                
                res(i:i) = c
            end do
        end if
        
    end subroutine read_line_text
    
    subroutine replace_tabs(text)
    implicit none
    
        character(*), intent(inout)::text
        integer::i
        
        i = index(text, char(9))
        do while(i > 0)
            text(i:i) = ' '
            i = index(text, char(9))
        end do
    
    end subroutine replace_tabs
    
    subroutine process_line(single_line, file_type, preformatted_on)
    use layout
    implicit none
    
        type(line), intent(inout)::single_line
        integer, intent(in)::file_type
        logical, intent(inout)::preformatted_on
        
        integer::line_length
    
        if(file_type == file_type_plain_text) then
        
            single_line%line_type = line_type_preformatted
            preformatted_on = .TRUE.
        
        else if(file_type == file_type_gemini) then
            
            line_length = len_trim(single_line%text)
            
            if((.not. preformatted_on) .AND. line_length > 2 .AND. &
               single_line%text(1:2) == "=>") then
                
                single_line%line_type = line_type_link
                single_line%text(1:2) = "  "
                call replace_tabs(single_line%text)
                single_line%text = adjustl(single_line%text)
                
            else if(line_length >= 3 .AND. single_line%text(1:3) == "```") then
            
                preformatted_on = .not. preformatted_on
                single_line%line_type = line_type_indicator
                
            else if(preformatted_on) then
                
                single_line%line_type = line_type_preformatted
                
            else
            
                single_line%line_type = line_type_text
                
            end if
            
        end if
        
    end subroutine process_line
    
    function load_unit(unit_number, file_type) result(first_line)
    use layout
    implicit none

        integer, intent(in)::unit_number, file_type
        type(line), pointer::first_line

        type(line), pointer::walker, next_line
        integer::iostatus
        logical::preformatted_on
        
        preformatted_on = .FALSE.
        
        allocate(first_line)

        ! Skip the first line - it has the status code
        rewind(unit_number)
        call skip_line(unit_number)

        call read_line_text(unit_number, first_line%text, iostatus)
        first_line%next => null()
        first_line%breaks => null()
        
        walker=>first_line
        
        do while(iostatus /= -1 .and. .not. is_file_end_marker(walker%text))  ! -1 should be end of file
            
            call process_line(walker, file_type, preformatted_on)
            
            allocate(next_line)
            next_line%next => null()
            next_line%breaks => null()
            
            walker%next => next_line
            next_line => null()
            walker => walker%next
            
            call read_line_text(unit_number, walker%text, iostatus)
            
        end do

        if(is_file_end_marker(walker%text)) then
            walker%text = " "
        end if
    
    end function load_unit

    function load_filename(filename, iostatus, mimetype) result(first_line)
    use layout, only: line
    implicit none
    
        character(*), intent(in)::filename
        integer, intent(out)::iostatus
        character(*), intent(in), optional::mimetype
        
        type(line), pointer::first_line
        
        integer::filetype
        
        open(unit=1097, file=filename, access='stream', iostat=iostatus, form='FORMATTED')
        if(iostatus == 0) then
        
            if(present(mimetype)) then
                if(trim(mimetype) == 'text/gemini') then
                    filetype = file_type_gemini
                else
                    filetype = file_type_plain_text
                end if
            else
                filetype = file_type_plain_text
            end if
            
            first_line => load_unit(1097, filetype)
        end if
    
    end function load_filename
    
end module file_handling