aboutsummaryrefslogtreecommitdiff
path: root/files.f90
blob: b30c43616bf1783a576810e1fd11e0f6e83e8cfb (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
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, '(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 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)
        !print '(A10, I8)', "allocated", length
        
        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 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(line_length > 2 .AND. single_line%text(1:2) == "=>") then
                
                single_line%line_type = line_type_link
                single_line%text(1:2) = "  "
                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)

        call read_line_text(unit_number, first_line%text, iostatus)
        first_line%next => 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()
            
            walker%next => next_line
            next_line => null()
            walker => walker%next
            
           call read_line_text(unit_number, walker%text, iostatus)
            
        end do
    
    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