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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
! 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 favorite_handling
implicit none
private
public :: favorite, sort_added, sort_alpha, read_favorites, &
write_favorites, add_favorite, remove_favorite, &
is_favorite
type :: favorite
character(1024)::name
character(1024)::link
integer::added
end type favorite
contains
pure function string_compare(s1, s2) result(res)
implicit none
character(*), intent(in)::s1, s2
integer::res
integer::n1, n2
integer::i
n1 = len_trim(s1)
n2 = len_trim(s2)
res = 0
do i = 1, min(n1, n2)
if(s1(i:i) < s2(i:i)) then
res = -1
exit
else if(s1(i:i) > s2(i:i)) then
res = 1
exit
end if
end do
end function string_compare
pure function string_location(array, minsearch) result(res)
implicit none
character(*), dimension(:), intent(in)::array
logical, intent(in), optional::minsearch
integer::res
logical::mymin
integer::i
mymin = .false.
if(present(minsearch)) then
mymin = minsearch
end if
res = 1
do i = 2, size(array)
if(mymin .and. string_compare(array(res), array(i)) > 0) then
res = i
else if((.not. mymin) .and. string_compare(array(res), array(i)) < 0) then
res = i
end if
end do
end function string_location
subroutine sort_added(favorites, latest)
implicit none
type(favorite), dimension(:), intent(inout)::favorites
logical, intent(in)::latest
integer::i_loc
integer::i, n
type(favorite)::swapper
n = size(favorites)
do i=1, n-1
if(latest) then
i_loc = maxloc(favorites(i:n)%added, 1)
else
i_loc = minloc(favorites(i:n)%added, 1)
end if
i_loc = i_loc + i - 1
if(i /= i_loc) then
swapper = favorites(i)
favorites(i) = favorites(i_loc)
favorites(i_loc) = swapper
end if
end do
end subroutine sort_added
subroutine sort_alpha(favorites, descending)
implicit none
type(favorite), dimension(:), intent(inout)::favorites
logical, intent(in)::descending
integer::i_loc
integer::i, n
type(favorite)::swapper
n = size(favorites)
do i=1, n-1
if(descending) then
i_loc = string_location(favorites(i:n)%name)
else
i_loc = string_location(favorites(i:n)%name, minsearch=.true.)
end if
i_loc = i_loc + i - 1
if(i /= i_loc) then
swapper = favorites(i)
favorites(i) = favorites(i_loc)
favorites(i_loc) = swapper
end if
end do
end subroutine sort_alpha
function read_favorite(unit_number) result(res)
implicit none
type(favorite)::res
integer, intent(in)::unit_number
character::c
character(len=10)::text_date
integer::i, ios
! Leading spaces...
read(unit_number, '(A1)', advance='no') c
do while(c == ' ' .or. c == char(9))
read(unit_number, '(A1)', advance='no') c
end do
! Link
res%link = " "
i = 1
do while(c /= ' ' .and. c /= char(9))
res%link(i:i) = c
read(unit_number, '(A1)', advance='no') c
i = i + 1
end do
! Separating spaces...
do while(c == ' ' .or. c == char(9))
read(unit_number, '(A1)', advance='no') c
end do
! Name...
res%name = " "
ios = 0
i = 1
do while(ios == 0)
res%name(i:i) = c
read(unit_number, '(A1)', advance='no', iostat=ios) c
i = i + 1
end do
! The date should be at the end of the link
i = index(res%name, "(", .true.)
text_date = res%name(i+1:i+10)
do i = 5, 9
text_date(i:i) = text_date(i+1:i+1)
end do
do i = 7, 8
text_date(i:i) = text_date(i+1:i+1)
end do
read(text_date, '(I8)') res%added
end function read_favorite
function read_favorites(unit_number) result(faves)
implicit none
integer, intent(in)::unit_number
type(favorite), dimension(:), pointer::faves
character(80)::temp
integer::n, i, ios
faves => null()
! Title
read(unit_number, *, iostat=ios) temp
if(ios /= 0) then
return
end if
! Blank
!read(unit_number, *) temp
! Count
read(unit_number, *, iostat=ios) n
if(ios /= 0) then
return
end if
! Blank
!read(unit_number, *) temp
allocate(faves(n))
do i = 1, n
ios = 1
do while(ios /= 0)
read(unit_number, '(A2)', advance='no', iostat=ios) temp
end do
faves(i) = read_favorite(unit_number)
end do
end function read_favorites
subroutine write_favorite(unit_number, f)
implicit none
integer, intent(in)::unit_number
type(favorite), intent(in)::f
write(unit_number, '(A2)', advance='no') "=>"
write(unit_number, *) trim(f%link)//" "//trim(f%name)
end subroutine write_favorite
subroutine write_favorites(unit_number, faves, skip_heading)
implicit none
integer, intent(in)::unit_number
type(favorite), dimension(:), intent(in), pointer::faves
logical, intent(in), optional::skip_heading
integer::i
character(8)::n_text
if(present(skip_heading)) then
if(.not. skip_heading) then
write(unit_number, '(A11)') "# Favorites"
write(unit_number, *)
end if
else
write(unit_number, '(A11)') "# Favorites"
write(unit_number, *)
end if
if(.not.associated(faves)) then
return
end if
write(n_text, '(I8)') size(faves)
write(unit_number, *) trim(adjustl(n_text))//" Links Are Marked As Favorites"
write(unit_number, *)
do i = 1, size(faves)
call write_favorite(unit_number, faves(i))
end do
end subroutine write_favorites
pure function find_favorite(faves, link)
implicit none
integer::find_favorite
type(favorite), dimension(:), intent(in), pointer::faves
character(*), intent(in)::link
if(.not. associated(faves)) then
find_favorite = -1
else
do find_favorite = 1, size(faves)
if(trim(link) == trim(faves(find_favorite)%link)) then
exit
end if
end do
if(find_favorite > size(faves)) then
find_favorite = -1
end if
end if
end function find_favorite
subroutine add_favorite(faves, link, name)
implicit none
type(favorite), dimension(:), intent(inout), pointer::faves
character(*), intent(in)::link
character(*), intent(in)::name
character(8)::now
integer::n
type(favorite), dimension(:), pointer::holding
n = -1
if(.not. associated(faves)) then
n = 1
allocate(faves(1))
else if(find_favorite(faves, link) < 0) then
n = size(faves)
holding => faves
faves => null()
allocate(faves(n+1))
faves(1:n) = holding
n = n + 1
deallocate(holding)
end if
if(n > 0) then
faves(n)%link = trim(link)
call date_and_time(date=now)
faves(n)%name = trim(name)//" ("//now(1:4)//"-"//now(5:6)//"-"//now(7:8)//")"
read(now, '(I8)') faves(n)%added
end if
end subroutine add_favorite
subroutine remove_favorite(faves, link)
implicit none
type(favorite), dimension(:), intent(inout), pointer::faves
character(*), intent(in)::link
integer::i, n
type(favorite), dimension(:), allocatable::holding
i = find_favorite(faves, link)
if(i > 0) then
if(size(faves) == 1) then
deallocate(faves)
else
n = size(faves)
allocate(holding(n-1))
if(i > 1) then
holding(1:i-1) = faves(1:i-1)
end if
if(i < n) then
holding(i:n-1) = faves(i+1:n)
end if
deallocate(faves)
allocate(faves(n-1))
faves = holding
deallocate(holding)
end if
end if
end subroutine remove_favorite
function is_favorite(faves, link)
implicit none
type(favorite), dimension(:), intent(in), pointer::faves
character(*), intent(in)::link
logical::is_favorite
is_favorite = .false.
if(associated(faves)) then
is_favorite = (find_favorite(faves, link) > 0)
end if
end function is_favorite
end module favorite_handling
|