-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathutils.f90
More file actions
491 lines (374 loc) · 12.3 KB
/
Copy pathutils.f90
File metadata and controls
491 lines (374 loc) · 12.3 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
! This file is part of mctc-lib.
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
module mctc_io_utils
use mctc_env_accuracy, only : wp
use mctc_env_error, only : error_type, fatal_error
implicit none
private
public :: getline, next_line
public :: token_type, next_token, read_token, read_next_token
public :: io_error, io2_error
public :: filename, to_string
public :: to_lower
!> Text token
type :: token_type
!> Begin of sequence
integer :: first
!> End of sequence
integer :: last
end type token_type
interface read_token
module procedure :: read_token_int
module procedure :: read_token_real
end interface read_token
interface read_next_token
module procedure :: read_next_token_int
module procedure :: read_next_token_real
end interface read_next_token
contains
subroutine getline(unit, line, iostat, iomsg)
!> Formatted IO unit
integer, intent(in) :: unit
!> Line to read
character(len=:), allocatable, intent(out) :: line
!> Status of operation
integer, intent(out) :: iostat
!> Error message
character(len=:), allocatable, intent(out), optional :: iomsg
integer, parameter :: bufsize = 512
character(len=bufsize) :: buffer
character(len=bufsize) :: msg
integer :: size
integer :: stat
allocate(character(len=0) :: line)
do
read(unit, "(a)", advance="no", iostat=stat, iomsg=msg, size=size) &
& buffer
if (stat > 0) exit
line = line // buffer(:size)
if (stat < 0) then
if (is_iostat_eor(stat)) then
stat = 0
end if
exit
end if
end do
if (stat /= 0) then
if (present(iomsg)) iomsg = trim(msg)
end if
iostat = stat
end subroutine getline
!> Convenience function to read a line and update associated descriptors
subroutine next_line(unit, line, pos, lnum, iostat, iomsg)
!> Formatted IO unit
integer, intent(in) :: unit
!> Line to read
character(len=:), allocatable, intent(out) :: line
!> Current position in line
integer, intent(out) :: pos
!> Current line number
integer, intent(inout) :: lnum
!> Status of operation
integer, intent(out) :: iostat
!> Error message
character(len=:), allocatable, intent(out), optional :: iomsg
pos = 0
call getline(unit, line, iostat, iomsg)
if (iostat == 0) lnum = lnum + 1
end subroutine next_line
!> Advance pointer to next text token
subroutine next_token(string, pos, token)
!> String to check
character(len=*), intent(in) :: string
!> Current position in string
integer, intent(inout) :: pos
!> Token found
type(token_type), intent(out) :: token
integer :: start
if (pos >= len(string)) then
token = token_type(len(string)+1, len(string)+1)
return
end if
do while(pos < len(string))
pos = pos + 1
select case(string(pos:pos))
case(" ", achar(9), achar(10), achar(13))
continue
case default
exit
end select
end do
start = pos
do while(pos < len(string))
pos = pos + 1
select case(string(pos:pos))
case(" ", achar(9), achar(10), achar(13))
pos = pos - 1
exit
case default
continue
end select
end do
token = token_type(start, pos)
end subroutine next_token
function filename(unit)
integer, intent(in) :: unit
character(len=:), allocatable :: filename
character(len=512) :: buffer
logical :: opened
filename = "(input)"
if (unit /= -1) then
buffer = ""
inquire(unit=unit, opened=opened, name=buffer)
if (opened .and. len_trim(buffer) > 0) then
filename = trim(buffer)
end if
end if
end function filename
!> Create new IO error
subroutine io_error(error, message, source, token, filename, line, label)
!> Error handler
type(error_type), allocatable, intent(out) :: error
!> Main error message
character(len=*), intent(in) :: message
!> String representing the offending input
character(len=*), intent(in) :: source
!> Last processed token
type(token_type), intent(in) :: token
!> Name of the input file
character(len=*), intent(in), optional :: filename
!> Line number
integer, intent(in), optional :: line
!> Label of the offending statement
character(len=*), intent(in), optional :: label
character(len=*), parameter :: nl = new_line("a")
integer :: offset, lnum, width
character(len=:), allocatable :: string
lnum = 1
if (present(line)) lnum = line
offset = integer_width(lnum)
width = token%last - token%first + 1
! Fix print alignment for empty files
if (offset == 0) offset = 1
string = "Error: " // message
if (present(filename)) then
string = string // nl // &
repeat(" ", offset)//"--> "//filename
string = string // ":" // to_string(lnum)
if (token%first > 0 .and. token%last >= token%first) then
string = string // &
":"//to_string(token%first)
if (token%last > token%first) string = string//"-"//to_string(token%last)
end if
end if
string = string // nl //&
repeat(" ", offset+1)//"|"//nl//&
to_string(lnum)//" | "//source//nl//&
repeat(" ", offset+1)//"|"//repeat(" ", token%first)//repeat("^", width)
if (present(label)) then
string = string // " " // label
end if
string = string // nl //&
repeat(" ", offset+1)//"|"
call fatal_error(error, string)
end subroutine io_error
!> Create new IO error
subroutine io2_error(error, message, source1, source2, token1, token2, filename, &
& line1, line2, label1, label2)
!> Error handler
type(error_type), allocatable, intent(out) :: error
!> Main error message
character(len=*), intent(in) :: message
!> String representing the offending input
character(len=*), intent(in) :: source1, source2
!> Last processed token
type(token_type), intent(in) :: token1, token2
!> Name of the input file
character(len=*), intent(in), optional :: filename
!> Line number
integer, intent(in), optional :: line1, line2
!> Label of the offending statement
character(len=*), intent(in), optional :: label1, label2
character(len=*), parameter :: nl = new_line("a")
integer :: offset, lnum1, lnum2, width1, width2
character(len=:), allocatable :: string
lnum1 = 1
lnum2 = 1
if (present(line1)) lnum1 = line1
if (present(line2)) lnum2 = line2
offset = integer_width(max(lnum1, lnum2))
width1 = token1%last - token1%first + 1
width2 = token2%last - token2%first + 1
string = "Error: " // message
if (present(filename)) then
string = string // nl // &
repeat(" ", offset)//"--> "//filename
string = string // ":" // to_string(lnum2)
if (token2%first > 0 .and. token2%last >= token2%first) then
string = string // &
":"//to_string(token2%first)
if (token2%last > token2%first) string = string//"-"//to_string(token2%last)
end if
end if
string = string // nl //&
repeat(" ", offset+1)//"|"//nl//&
to_string(lnum1, offset)//" | "//source1//nl//&
repeat(" ", offset+1)//"|"//repeat(" ", token1%first)//repeat("-", width1)
if (present(label1)) then
string = string // " " // label1
end if
string = string // nl //&
repeat(" ", offset+1)//":"//nl//&
to_string(lnum2)//" | "//source2//nl//&
repeat(" ", offset+1)//"|"//repeat(" ", token2%first)//repeat("^", width2)
if (present(label2)) then
string = string // " " // label2
end if
string = string // nl //&
repeat(" ", offset+1)//"|"
call fatal_error(error, string)
end subroutine io2_error
pure function integer_width(input) result(width)
integer, intent(in) :: input
integer :: width
integer :: val
val = input
width = 0
do while (val /= 0)
val = val / 10
width = width + 1
end do
end function integer_width
!> Represent an integer as character sequence.
pure function to_string(val, width) result(string)
integer, intent(in) :: val
integer, intent(in), optional :: width
character(len=:), allocatable :: string
integer, parameter :: buffer_len = range(val)+2
character(len=buffer_len) :: buffer
integer :: pos
integer :: n
character(len=1), parameter :: numbers(0:9) = &
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
if (val == 0) then
if (present(width)) then
string = repeat(" ", width-1) // numbers(0)
else
string = numbers(0)
end if
return
end if
n = abs(val)
buffer = ""
pos = buffer_len + 1
do while (n > 0)
pos = pos - 1
buffer(pos:pos) = numbers(mod(n, 10))
n = n/10
end do
if (val < 0) then
pos = pos - 1
buffer(pos:pos) = "-"
end if
if (present(width)) then
string = repeat(" ", max(width-(buffer_len+1-pos), 0)) // buffer(pos:)
else
string = buffer(pos:)
end if
end function to_string
subroutine read_next_token_int(line, pos, token, val, iostat, iomsg)
character(len=*), intent(in) :: line
integer, intent(inout) :: pos
type(token_type), intent(inout) :: token
integer, intent(out) :: val
integer, intent(out) :: iostat
character(len=:), allocatable, intent(out), optional :: iomsg
call next_token(line, pos, token)
call read_token(line, token, val, iostat, iomsg)
end subroutine read_next_token_int
subroutine read_token_int(line, token, val, iostat, iomsg)
character(len=*), intent(in) :: line
type(token_type), intent(in) :: token
integer, intent(out) :: val
integer, intent(out) :: iostat
character(len=:), allocatable, intent(out), optional :: iomsg
character(len=512) :: msg
if (token%first > 0 .and. token%last <= len(line)) then
val = 0
read(line(token%first:token%last), *, iostat=iostat, iomsg=msg) val
if (iostat /= 0) val = 0
else
iostat = 1
msg = "No input found"
end if
if (present(iomsg)) iomsg = trim(msg)
end subroutine read_token_int
subroutine read_next_token_real(line, pos, token, val, iostat, iomsg)
character(len=*), intent(in) :: line
integer, intent(inout) :: pos
type(token_type), intent(inout) :: token
real(wp), intent(out) :: val
integer, intent(out) :: iostat
character(len=:), allocatable, intent(out), optional :: iomsg
call next_token(line, pos, token)
call read_token(line, token, val, iostat, iomsg)
end subroutine read_next_token_real
subroutine read_token_real(line, token, val, iostat, iomsg)
character(len=*), intent(in) :: line
type(token_type), intent(in) :: token
real(wp), intent(out) :: val
integer, intent(out) :: iostat
character(len=:), allocatable, intent(out), optional :: iomsg
character(len=512) :: msg
if (token%first > 0 .and. token%last <= len(line)) then
val = 0.0_wp
read(line(token%first:token%last), *, iostat=iostat, iomsg=msg) val
if (iostat /= 0) val = 0.0_wp
else
iostat = 1
msg = "No input found"
end if
if (present(iomsg)) iomsg = trim(msg)
end subroutine read_token_real
!> Convert input string to lowercase
elemental function to_lower(str) result(lcstr)
!> Input string
character(len=*), intent(in) :: str
!> Lowercase version of string
character(len=len(str)):: lcstr
integer :: ilen, iquote, i, iav, iqc
integer, parameter :: offset = iachar("A") - iachar("a")
ilen = len(str)
iquote = 0
lcstr = str
do i = 1, ilen
iav = iachar(str(i:i))
if (iquote == 0 .and. (iav == 34 .or.iav == 39)) then
iquote = 1
iqc = iav
cycle
end if
if (iquote == 1 .and. iav==iqc) then
iquote=0
cycle
end if
if (iquote == 1) cycle
if (iav >= iachar("A") .and. iav <= iachar("Z")) then
lcstr(i:i) = achar(iav - offset)
else
lcstr(i:i) = str(i:i)
end if
end do
end function to_lower
end module mctc_io_utils