aboutsummaryrefslogtreecommitdiff
path: root/pptxzip.f90
blob: 1a11b1e19345d119353f6a418553a3af8089fff7 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
! Copyright (c) 2022 Approximatrix, LLC <support@approximatrix.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 fpoint_pptxzip
use fpoint_pptxml
implicit none

    private
    public::pptxtracted
    
    type :: pptxtracted
        
        character(len=2048)::directory
        character(len=2048), dimension(:), pointer::slide_paths
        character(len=2048), dimension(:), pointer::note_paths
    
        type(slide), dimension(:), pointer::slides
        
        type(slide), dimension(:), pointer::notes
        integer, dimension(:), pointer::slide_note_indices
    
        contains
        
        procedure :: open => pptx_open
        procedure :: close => pptx_close
        procedure :: parse => pptx_parse
        procedure :: to_text => pptx_to_text
        procedure :: slide_has_notes
        procedure :: slide_count
        procedure :: get_notes => slide_get_notes
        procedure :: has_notes => pptx_has_notes
    
    end type pptxtracted
    
contains

    function pptx_has_notes(self)
    implicit none
    
        class(pptxtracted), intent(in)::self
        logical::pptx_has_notes
        
        if(.not. associated(self%slide_note_indices)) then
            pptx_has_notes = .false.
        else
            pptx_has_notes = (count(self%slide_note_indices > 0) > 0)
        end if
        
    end function pptx_has_notes

    function slide_has_notes(self, i)
    implicit none
    
        class(pptxtracted), intent(in)::self
        integer, intent(in)::i
        logical::slide_has_notes
        
        if(i > size(self%note_paths) .or. i < 1) then
            slide_has_notes = .false.
        else
            slide_has_notes = (len_trim(self%note_paths(i)) > 0)
        end if

    end function slide_has_notes
    
    function slide_get_notes(self, i, success)
    implicit none
    
        class(pptxtracted), intent(in)::self
        integer, intent(in)::i
        logical, intent(out), optional::success
        type(slide)::slide_get_notes
        
        integer::j
        
        if(self%slide_has_notes(i)) then
            j = self%slide_note_indices(i)
            slide_get_notes = self%notes(j)
            if(present(success)) then
                success = .true.
            end if
        else if(present(success)) then
            success = .false.
        end if
    
    end function slide_get_notes
    
    function slide_count(self)
    implicit none
        
        class(pptxtracted), intent(in)::self
        integer::slide_count
        
        if(associated(self%slide_paths)) then
            slide_count = size(self%slide_paths)
        else
            slide_count = 0
        end if
        
    end function slide_count

    function pptx_open(self, filename) result(res)
    use fpoint_fsutil
    use m_unzip, only: unzip
    implicit none
    
        logical::res
        class(pptxtracted), intent(out)::self
        character(len=*), intent(in)::filename
        integer::i, slidecount, j
        character(len=2048)::slidedir, slidecheck, notesdir
        character(len=4)::numtext
        logical::existence
        
        call get_temporary_directory(self%directory)
        i = index(filename, pathsep, back=.true.) + 1
        call append_to_path(self%directory, filename(i:len_trim(filename))//".extracted")
        call make_directory(trim(self%directory))
    
        ! Extract the file into said directory
        res = unzip(filename, trim(self%directory))
    
        ! Count slides
        slidedir = self%directory
        call append_to_path(slidedir, 'ppt')
        call append_to_path(slidedir, 'slides')
        do i = 1, 512
            slidecheck = slidedir
            write(numtext, '(I4)') i
            call append_to_path(slidecheck, "slide"//trim(adjustl(numtext))//".xml")
            inquire(file=slidecheck, exist=existence)
            if(.not. existence) then
                exit
            end if
        end do
        slidecount = i - 1
        
        allocate(self%slide_paths(slidecount))
        self%slide_paths = slidedir
        do i = 1, slidecount
            write(numtext, '(I4)') i
            call append_to_path(self%slide_paths(i), "slide"//trim(adjustl(numtext))//".xml")
        end do
        
        ! Now check for notes
        allocate(self%slide_note_indices(slidecount))
        self%slide_note_indices = -1
        j = 1
        
        notesdir = self%directory
        call append_to_path(notesdir, 'ppt')
        call append_to_path(notesdir, 'notesSlides')
        allocate(self%note_paths(slidecount))
        self%note_paths = notesdir
        do i = 1, slidecount
            write(numtext, '(I4)') i
            call append_to_path(self%note_paths(i), "notesSlide"//trim(adjustl(numtext))//".xml")
            inquire(file=self%note_paths(i), exist=existence)
            if(.not. existence) then
                self%note_paths(i) = " "
            else
                self%slide_note_indices(i) = j
                j = j + 1
            end if
        end do
    
        ! These should only be explicitly parsed
        self%slides => null()
        self%notes => null()
    
    end function pptx_open

    subroutine pptx_close(self)
    implicit none
    
        class(pptxtracted), intent(inout)::self
        
        ! Delete directory tree at self%directory
        
        deallocate(self%slide_paths)
        self%slide_paths => null()
        
        deallocate(self%note_paths)
        self%note_paths => null()
        
        self%directory = " "
        
    end subroutine pptx_close
    
    subroutine pptx_parse(self)
    use fpoint_pptxml
    implicit none
    
        class(pptxtracted), intent(inout)::self
        integer::i, j
        allocate(self%slides(self%slide_count()))
        allocate(self%notes(count(self%slide_note_indices > 0)))
        
        do i = 1, self%slide_count()
            call self%slides(i)%load(self%slide_paths(i))
            if(self%slide_has_notes(i)) then
                j = self%slide_note_indices(i)
                call self%notes(j)%load(self%note_paths(i), title=self%slides(i)%title)
            end if
        end do
            
    end subroutine pptx_parse
    
    function pptx_to_text(self, notes_only) result(t)
    use fpoint_pptxml
    implicit none
    
        class(pptxtracted), intent(in)::self
        logical, intent(in), optional::notes_only
        integer::text_length
        character(len=:), pointer::t, tmp
        integer::i
        type(slide)::one_note
        
        logical::local_notes
        
        t => null()
        local_notes = .false.
        if(present(notes_only)) then
            local_notes = notes_only
        end if
        
        
        if(self%slide_count() > 0 .and. associated(self%slides)) then
            text_length = 0
            do i = 1, self%slide_count()
                if(local_notes .and. self%slide_has_notes(i)) then
                    one_note = self%get_notes(i)
                    text_length = text_length + one_note%text_length()
                else if(.not. local_notes) then
                    text_length = text_length + self%slides(i)%text_length()
                end if
            end do
            
            allocate(character(len=text_length) :: t)
            
            text_length = 1
            do i = 1, self%slide_count()
                tmp => null()
                if(local_notes .and. self%slide_has_notes(i)) then
                    one_note = self%get_notes(i)
                    tmp => one_note%to_text()
                else if(.not. local_notes) then
                    tmp => self%slides(i)%to_text()
                end if
                
                if(associated(tmp)) then
                    t(text_length:len(t)) = trim(tmp)
                    text_length = text_length + len_trim(tmp)
                    deallocate(tmp)
                end if
            end do
            
        end if
    
    end function pptx_to_text
    
end module fpoint_pptxzip